Commit 1e476642 by Aaron Leung

Moving the parsing functions into their own file. Also adding emitter drivers to…

Moving the parsing functions into their own file. Also adding emitter drivers to the Document class.
parent 25815f6b
build: sassc.cpp document.cpp node.cpp token.cpp prelexer.cpp
g++ -o bin/sassc sassc.cpp document.cpp node.cpp token.cpp prelexer.cpp
g++ -o bin/sassc sassc.cpp document.cpp document_parser.cpp document_emitter.cpp node.cpp token.cpp prelexer.cpp
test: build
ruby spec.rb spec/basic/
......
#include <cstdio>
#include "document.hpp"
#include <iostream>
namespace Sass {
using namespace Prelexer;
using std::cout; using std::endl;
Document::Document(char* _path, char* _source) {
path = _path;
......@@ -28,95 +25,9 @@ namespace Sass {
line_number = 1;
last_munch_succeeded = false;
}
Document::~Document() {
delete [] source;
}
void Document::parse_scss() {
try_munching<optional_spaces>();
while (*position) {
statements.push_back(parse_statement());
try_munching<optional_spaces>();
}
}
Node Document::parse_statement() {
if (try_munching<block_comment>()) {
return Node(Node::comment, top);
}
else if (try_munching<variable>()) {
return parse_var_def();
}
else return parse_ruleset();
}
Node Document::parse_var_def() {
const Token key = top;
try_munching<exactly<':'> >();
environment[string(key)] = parse_values();
try_munching<exactly<';'> >();
return Node(Node::null);
}
Node Document::parse_ruleset() {
Node ruleset(Node::ruleset);
ruleset.push_child(parse_selector());
ruleset.push_child(parse_clauses());
return ruleset;
}
Node Document::parse_selector() {
try_munching<identifier>();
return Node(Node::selector, top);
}
Node Document::parse_clauses() {
try_munching<exactly<'{'> >();
Node decls(Node::clauses);
while(!try_munching<exactly<'}'> >()) {
if (try_munching<block_comment>()) {
decls.push_child(Node(Node::comment, top));
continue;
}
else if (try_munching<variable>()) {
decls.push_child(parse_var_def());
continue;
}
try_munching<identifier>();
Token id = top;
if (try_munching<exactly<':'> >()) {
Node rule(Node::rule);
rule.push_child(Node(Node::property, id));
rule.push_child(parse_values());
decls.push_child(rule);
try_munching<exactly<';'> >();
}
else {
Node ruleset(Node::ruleset);
ruleset.push_child(Node(Node::selector, id));
ruleset.push_child(parse_clauses());
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>() ||
try_munching<hex>() || try_munching<string_constant>() ||
try_munching<variable>()) {
if (top.begin[0] == '$') {
Node stuff(environment[string(top)]);
for (int i = 0; i < stuff.children.size(); ++i) {
values.push_child(stuff.children[i]);
}
}
else
values.push_child(Node(Node::value, top));
}
return values;
}
}
\ No newline at end of file
......@@ -7,6 +7,8 @@ namespace Sass {
using namespace Prelexer;
struct Document {
enum CSS_Style { nested, expanded, compact, compressed };
char* path;
char* source;
char* position;
......@@ -68,6 +70,7 @@ namespace Sass {
Node parse_selector();
Node parse_clauses();
Node parse_values();
string emit_css(CSS_Style style);
};
}
\ No newline at end of file
#include "document.hpp"
namespace Sass {
using std::string;
using std::stringstream;
string Document::emit_css(CSS_Style style) {
stringstream output;
for (int i = 0; i < statements.size(); ++i) {
switch (style) {
// case nested:
// statements[i].emit_nested_css(output, "");
// break;
case expanded:
statements[i].emit_expanded_css(output, "");
break;
}
}
return output.str();
}
}
\ No newline at end of file
#include "document.hpp"
namespace Sass {
void Document::parse_scss() {
try_munching<optional_spaces>();
while (*position) {
statements.push_back(parse_statement());
try_munching<optional_spaces>();
}
}
Node Document::parse_statement() {
if (try_munching<block_comment>()) {
return Node(Node::comment, top);
}
else if (try_munching<variable>()) {
return parse_var_def();
}
else return parse_ruleset();
}
Node Document::parse_var_def() {
const Token key = top;
try_munching<exactly<':'> >();
environment[string(key)] = parse_values();
try_munching<exactly<';'> >();
return Node(Node::null);
}
Node Document::parse_ruleset() {
Node ruleset(Node::ruleset);
ruleset.push_child(parse_selector());
ruleset.push_child(parse_clauses());
return ruleset;
}
Node Document::parse_selector() {
try_munching<identifier>();
return Node(Node::selector, top);
}
Node Document::parse_clauses() {
try_munching<exactly<'{'> >();
Node decls(Node::clauses);
while(!try_munching<exactly<'}'> >()) {
if (try_munching<block_comment>()) {
decls.push_child(Node(Node::comment, top));
continue;
}
else if (try_munching<variable>()) {
decls.push_child(parse_var_def());
continue;
}
try_munching<identifier>();
Token id = top;
if (try_munching<exactly<':'> >()) {
Node rule(Node::rule);
rule.push_child(Node(Node::property, id));
rule.push_child(parse_values());
decls.push_child(rule);
try_munching<exactly<';'> >();
}
else {
Node ruleset(Node::ruleset);
ruleset.push_child(Node(Node::selector, id));
ruleset.push_child(parse_clauses());
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>() ||
try_munching<hex>() || try_munching<string_constant>() ||
try_munching<variable>()) {
if (top.begin[0] == '$') {
Node stuff(environment[string(top)]);
for (int i = 0; i < stuff.children.size(); ++i) {
values.push_child(stuff.children[i]);
}
}
else
values.push_child(Node(Node::value, top));
}
return values;
}
}
\ No newline at end of file
#include <iostream>
#include <sstream>
#include <string>
#include "document.hpp"
using namespace Sass;
......@@ -13,12 +13,8 @@ int main(int argc, char* argv[]) {
Document doc(argv[1], 0);
doc.parse_scss();
stringstream output;
for (int i = 0; i < doc.statements.size(); ++i) {
doc.statements[i].emit_expanded_css(output, "");
}
cout << output.str();
string output = doc.emit_css(doc.expanded);
cout << output;
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