Commit 3945804b by Aaron Leung

Better whitespace handling.

parent 80e62ee2
...@@ -17,22 +17,39 @@ namespace Sass { ...@@ -17,22 +17,39 @@ namespace Sass {
~Document(); ~Document();
inline Token& peek() { return top; } inline Token& peek() { return top; }
template <prelexer mx> template <prelexer mx>
bool try_munching() { bool try_munching() {
char* after_whitespace = spaces_and_comments(position); char* after_whitespace;
if (mx == block_comment) {
after_whitespace = optional_spaces(position);
}
else if (mx == spaces || mx == ancestor_of) {
after_whitespace = spaces(position);
if (after_whitespace) {
top = Token(mx, position, after_whitespace, line_number);
line_number += count_interval<'\n'>(position, after_whitespace);
position = after_whitespace;
return last_munch_succeeded = true;
}
else {
return last_munch_succeeded = false;
}
}
else {
after_whitespace = spaces_and_comments(position);
}
line_number += count_interval<'\n'>(position, after_whitespace); line_number += count_interval<'\n'>(position, after_whitespace);
char* after_token = mx(after_whitespace); char* after_token = mx(after_whitespace);
if (after_token) { if (after_token) {
top = Token(mx, after_whitespace, after_token, line_number); top = Token(mx, after_whitespace, after_token, line_number);
position = after_token; position = after_token;
last_munch_succeeded = true; return last_munch_succeeded = true;
return true;
} }
else { else {
last_munch_succeeded = false; return last_munch_succeeded = false;
return false;
} }
} }
}; };
} }
\ No newline at end of file
div { div {
/* a comment that should be preserved */
color: red; color: red;
background: blue; background: blue;
span { span {
......
...@@ -11,8 +11,9 @@ namespace Sass { ...@@ -11,8 +11,9 @@ namespace Sass {
const char* _end, const char* _end,
unsigned int _line_number) { unsigned int _line_number) {
type = _type; type = _type;
begin = _begin; if (_begin > _end) begin = end = 0;
end = _end; else begin = _begin, end = _end;
line_number = _line_number; line_number = _line_number;
} }
} }
\ No newline at end of file
#include <string>
#include "prelexer.hpp" #include "prelexer.hpp"
namespace Sass { namespace Sass {
using std::string;
struct Token { struct Token {
Prelexer::prelexer type; Prelexer::prelexer type;
const char* begin; const char* begin;
...@@ -12,5 +15,6 @@ namespace Sass { ...@@ -12,5 +15,6 @@ namespace Sass {
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; }
inline operator string() { return string(begin, end - begin); }
}; };
} }
\ No newline at end of file
...@@ -26,10 +26,18 @@ int main(int argc, char* argv[]) { ...@@ -26,10 +26,18 @@ int main(int argc, char* argv[]) {
print_slice(doc.top.begin, doc.top.end); print_slice(doc.top.begin, doc.top.end);
doc.try_munching<Prelexer::exactly<'{'> >(); doc.try_munching<Prelexer::exactly<'{'> >();
print_slice(doc.top.begin, doc.top.end); print_slice(doc.top.begin, doc.top.end);
doc.try_munching<Prelexer::block_comment>();
print_slice(doc.top.begin, doc.top.end);
doc.try_munching<Prelexer::identifier>(); doc.try_munching<Prelexer::identifier>();
print_slice(doc.top.begin, doc.top.end); print_slice(doc.top.begin, doc.top.end);
doc.try_munching<Prelexer::dash_match>(); doc.try_munching<Prelexer::dash_match>();
print_slice(doc.top.begin, doc.top.end); print_slice(doc.top.begin, doc.top.end);
printf("sizeof char is %ld\n", sizeof(char));
printf("sizeof document object is %ld\n", sizeof(doc));
printf("sizeof node object is %ld\n", sizeof(Node));
printf("sizeof Node vector object is %ld\n", sizeof(std::vector<Node>));
printf("sizeof pointer to Node vector is %ld\n", sizeof(std::vector<Node>*));
} }
return 0; return 0;
......
...@@ -94,6 +94,8 @@ int main() { ...@@ -94,6 +94,8 @@ int main() {
cout << ptr << endl; cout << ptr << endl;
if (ptr1 == ptr2) cout << "This shouldn't be the case!" << endl; if (ptr1 == ptr2) cout << "This shouldn't be the case!" << endl;
else cout << "The prelexer pointers are different!" << endl; else cout << "The prelexer pointers are different!" << endl;
//ptr == prelexer(exactly<'x'>);
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