Commit d3b2584d by Aaron Leung

Parsing and echoing works.

parent 2e424258
...@@ -59,15 +59,38 @@ namespace Sass { ...@@ -59,15 +59,38 @@ namespace Sass {
Node Document::parse_declarations() { Node Document::parse_declarations() {
try_munching<exactly<'{'> >(); try_munching<exactly<'{'> >();
Node decls(Node::declarations);
while(!try_munching<exactly<'}'> >()) { while(!try_munching<exactly<'}'> >()) {
if (try_munching<block_comment>()) {
decls.push_child(Node(Node::comment, top));
continue;
}
try_munching<identifier>(); try_munching<identifier>();
Token id = top; Token id = top;
if (try_munching<exactly<':'> >()) { if (try_munching<exactly<':'> >()) {
Node rule(Node::rule); Node rule(Node::rule);
rule.push_child(Node(Node::property, id)); rule.push_child(Node(Node::property, id));
rule.push_child(Node(Node::value, parse_value())); rule.push_child(parse_values());
return rule; decls.push_child(rule);
try_munching<exactly<';'> >();
}
else {
Node ruleset(Node::ruleset);
ruleset.push_child(Node(Node::selector, id));
ruleset.push_child(parse_declarations());
decls.push_opt_child(ruleset);
}
} }
return decls;
} }
Node Document::parse_values() {
Node values(Node::values);
while(try_munching<identifier>() || try_munching<dimension>() ||
try_munching<percentage>() || try_munching<number>()) {
values.push_child(Node(Node::value, top));
} }
return values;
}
} }
\ No newline at end of file
...@@ -59,6 +59,7 @@ namespace Sass { ...@@ -59,6 +59,7 @@ namespace Sass {
Node parse_ruleset(); Node parse_ruleset();
Node parse_selector(); Node parse_selector();
Node parse_declarations(); Node parse_declarations();
Node parse_values();
}; };
} }
\ No newline at end of file
#include <iostream>
#include "node.hpp" #include "node.hpp"
#include <string>
using std::string;
using std::cout;
using std::endl;
namespace Sass { namespace Sass {
Node::Node() { } Node::Node() { }
...@@ -15,4 +21,49 @@ namespace Sass { ...@@ -15,4 +21,49 @@ namespace Sass {
void Node::push_opt_child(const Node& node) { void Node::push_opt_child(const Node& node) {
opt_children.push_back(node); opt_children.push_back(node);
} }
void Node::dump(unsigned int depth) {
switch (type) {
case comment:
for (int i = depth; i > 0; --i) cout << " ";
cout << string(token) << endl;
break;
case selector:
cout << string(token);
break;
case value:
cout << string(token);
break;
case property:
cout << string(token) << ":";
break;
case values:
for (int i = 0; i < children.size(); ++i)
cout << " " << string(children[i].token);
break;
case rule:
for (int i = depth; i > 0; --i) cout << " ";
children[0].dump(depth);
children[1].dump(depth);
cout << ";" << endl;
break;
case declarations:
cout << " {" << endl;
for (int i = 0; i < children.size(); ++i) {
children[i].dump(depth + 1);
}
for (int i = 0; i < opt_children.size(); ++i) {
opt_children[i].dump(depth + 1);
}
for (int i = depth; i > 0; --i) cout << " ";
cout << "}" << endl;
break;
case ruleset:
for (int i = depth; i > 0; --i) cout << " ";
children[0].dump(depth);
children[1].dump(depth);
break;
default: cout << "HUH?"; break;
}
}
} }
\ No newline at end of file
...@@ -15,6 +15,7 @@ namespace Sass { ...@@ -15,6 +15,7 @@ namespace Sass {
simple_selector, simple_selector,
rule, rule,
property, property,
values,
value, value,
lookahead_sequence, lookahead_sequence,
lookahead_token lookahead_token
...@@ -30,5 +31,6 @@ namespace Sass { ...@@ -30,5 +31,6 @@ namespace Sass {
Node(Node_Type _type, Token& _token); Node(Node_Type _type, Token& _token);
void push_child(const Node& node); void push_child(const Node& node);
void push_opt_child(const Node& node); void push_opt_child(const Node& node);
void dump(unsigned int depth);
}; };
} }
\ No newline at end of file
...@@ -35,6 +35,7 @@ int main(int argc, char* argv[]) { ...@@ -35,6 +35,7 @@ int main(int argc, char* argv[]) {
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 char is %ld\n", sizeof(char));
printf("sizeof int is %ld\n", sizeof(int));
printf("sizeof document object is %ld\n", sizeof(doc)); printf("sizeof document object is %ld\n", sizeof(doc));
printf("sizeof node object is %ld\n", sizeof(Node)); printf("sizeof node object is %ld\n", sizeof(Node));
printf("sizeof Node vector object is %ld\n", sizeof(std::vector<Node>)); printf("sizeof Node vector object is %ld\n", sizeof(std::vector<Node>));
...@@ -51,7 +52,7 @@ int main(int argc, char* argv[]) { ...@@ -51,7 +52,7 @@ int main(int argc, char* argv[]) {
int j = doc.statements.size(); int j = doc.statements.size();
printf("%d\n", j); printf("%d\n", j);
for (i = 0; i < j; ++i) { for (i = 0; i < j; ++i) {
print_slice(doc.statements[i].token.begin, doc.statements[i].token.end); doc.statements[i].dump(0);
} }
} }
......
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