Skip to content

Commit

Permalink
Bam! Robust tokenizing!
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Leung committed Feb 28, 2012
1 parent 9010aa3 commit a9c4149
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "document.hpp"

namespace Sass {
using namespace Prelexer;

Document::Document(char* _path, char* _source) {
path = _path;
if (!_source) {
Expand All @@ -28,4 +30,6 @@ namespace Sass {
Document::~Document() {
delete [] source;
}


}
19 changes: 18 additions & 1 deletion src/document.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,32 @@

namespace Sass {
using std::vector;
using namespace Prelexer;

struct Document {
char* path;
char* source;
unsigned int position;
char* position;
unsigned int line_number;
vector<Node> statements;
Token top;

Document(char* _path, char* _source = 0);
~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;
}

};
}
4 changes: 3 additions & 1 deletion src/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ namespace Sass {
simple_selector_sequence,
simple_selector,
property,
value
value,
lookahead_sequence,
lookahead_token
};

Node_Type type;
Expand Down
9 changes: 9 additions & 0 deletions src/test.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
div {
color: red;
background: blue;
span {
font-weight: bold;
display: inline-block;
}
margin: 10px 5px;
}
4 changes: 3 additions & 1 deletion src/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ namespace Sass {
end = 0;
line_number = 1;
}
Token::Token(const char* _begin,
Token::Token(Prelexer::prelexer _type,
const char* _begin,
const char* _end,
unsigned int _line_number) {
type = _type;
begin = _begin;
end = _end;
line_number = _line_number;
Expand Down
6 changes: 5 additions & 1 deletion src/token.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include "prelexer.hpp"

namespace Sass {
struct Token {
Prelexer::prelexer type;
const char* begin;
const char* end;
unsigned int line_number;
Token();
Token(const char* _begin,
Token(Prelexer::prelexer _type,
const char* _begin,
const char* _end,
unsigned int _line_number);
inline bool is_null() { return begin == 0 || end == 0; }
Expand Down
31 changes: 26 additions & 5 deletions src/unit-test-document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,32 @@

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[]) {
Document doc(argv[1], 0);
char* src = doc.source;
printf("FILE BEGINS ON NEXT LINE\n");
while (*src) std::putchar(*(src++));
printf("<EOF>\n");
if (argc > 1) {
Document doc(argv[1], 0);
char* src = doc.source;
printf("FILE BEGINS ON NEXT LINE\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;
}

0 comments on commit a9c4149

Please sign in to comment.