Commit 3945804b by Aaron Leung

Better whitespace handling.

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