Commit a9c41491 by Aaron Leung

Bam! Robust tokenizing!

parent 9010aa33
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
#include "document.hpp" #include "document.hpp"
namespace Sass { namespace Sass {
using namespace Prelexer;
Document::Document(char* _path, char* _source) { Document::Document(char* _path, char* _source) {
path = _path; path = _path;
if (!_source) { if (!_source) {
...@@ -28,4 +30,6 @@ namespace Sass { ...@@ -28,4 +30,6 @@ namespace Sass {
Document::~Document() { Document::~Document() {
delete [] source; delete [] source;
} }
} }
\ No newline at end of file
...@@ -2,15 +2,32 @@ ...@@ -2,15 +2,32 @@
namespace Sass { namespace Sass {
using std::vector; using std::vector;
using namespace Prelexer;
struct Document { struct Document {
char* path; char* path;
char* source; char* source;
unsigned int position; char* position;
unsigned int line_number; unsigned int line_number;
vector<Node> statements; vector<Node> statements;
Token top;
Document(char* _path, char* _source = 0); Document(char* _path, char* _source = 0);
~Document(); ~Document();
inline Token& peek() { return top; }
template <prelexer mx>
bool try_munching() {
char* after_whitespace = spaces_and_comments(position);
line_number += count_interval<'\n'>(position, after_whitespace);
char* after_token = mx(after_whitespace);
if (after_token) {
top = Token(mx, after_whitespace, after_token, line_number);
position = after_token;
return true;
}
else return false;
}
}; };
} }
\ No newline at end of file
...@@ -14,7 +14,9 @@ namespace Sass { ...@@ -14,7 +14,9 @@ namespace Sass {
simple_selector_sequence, simple_selector_sequence,
simple_selector, simple_selector,
property, property,
value value,
lookahead_sequence,
lookahead_token
}; };
Node_Type type; Node_Type type;
......
div {
color: red;
background: blue;
span {
font-weight: bold;
display: inline-block;
}
margin: 10px 5px;
}
\ No newline at end of file
...@@ -6,9 +6,11 @@ namespace Sass { ...@@ -6,9 +6,11 @@ namespace Sass {
end = 0; end = 0;
line_number = 1; line_number = 1;
} }
Token::Token(const char* _begin, Token::Token(Prelexer::prelexer _type,
const char* _begin,
const char* _end, const char* _end,
unsigned int _line_number) { unsigned int _line_number) {
type = _type;
begin = _begin; begin = _begin;
end = _end; end = _end;
line_number = _line_number; line_number = _line_number;
......
#include "prelexer.hpp"
namespace Sass { namespace Sass {
struct Token { struct Token {
Prelexer::prelexer type;
const char* begin; const char* begin;
const char* end; const char* end;
unsigned int line_number; unsigned int line_number;
Token(); Token();
Token(const char* _begin, Token(Prelexer::prelexer _type,
const char* _begin,
const char* _end, const char* _end,
unsigned int _line_number); unsigned int _line_number);
inline bool is_null() { return begin == 0 || end == 0; } inline bool is_null() { return begin == 0 || end == 0; }
......
...@@ -3,11 +3,32 @@ ...@@ -3,11 +3,32 @@
using namespace Sass; using namespace Sass;
void print_slice(const char *s, const char *t) {
if (t) {
printf("succeeded with %ld characters:\t", t - s);
while (s < t) putchar(*s++);
putchar('\n');
}
else {
printf("failed\n");
}
}
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
Document doc(argv[1], 0); if (argc > 1) {
char* src = doc.source; Document doc(argv[1], 0);
printf("FILE BEGINS ON NEXT LINE\n"); char* src = doc.source;
while (*src) std::putchar(*(src++)); printf("FILE BEGINS ON NEXT LINE\n");
printf("<EOF>\n"); while (*src) std::putchar(*(src++));
printf("<EOF>\n");
doc.try_munching<Prelexer::identifier>();
print_slice(doc.top.begin, doc.top.end);
doc.try_munching<Prelexer::exactly<'{'> >();
print_slice(doc.top.begin, doc.top.end);
doc.try_munching<Prelexer::identifier>();
print_slice(doc.top.begin, doc.top.end);
}
return 0; return 0;
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment