Commit 193110d6 by Aaron Leung

Changing the way nodes are stored at the root level. Makes for easier tree splicing.

parent 81ac5275
......@@ -3,17 +3,19 @@
namespace Sass {
Document::Document(char* path, char* source)
Document::Document(string path, char* source)
: path(path), source(source),
line_number(1), own_source(false),
context(*(new Context())),
statements(vector<Node>()),
root(Node(1, Node::root)),
// statements(vector<Node>()),
lexed(Token())
{
// if (!source) read_file();
if (!source) {
std::FILE *f;
// TO DO: CHECK f AGAINST NULL/0
f = std::fopen(path, "rb");
f = std::fopen(path.c_str(), "rb");
std::fseek(f, 0, SEEK_END);
int len = std::ftell(f);
std::rewind(f);
......@@ -25,7 +27,29 @@ namespace Sass {
own_source = true;
}
position = source;
// printf("INPUT FILE:\n%s", source);
}
Document::Document(string path, Context& context)
: path(path), source(source),
line_number(1), own_source(false),
context(context),
root(Node(1, Node::root)),
// statements(vector<Node>()),
lexed(Token())
{
std::FILE *f;
// TO DO: CHECK f AGAINST NULL/0
f = std::fopen(path.c_str(), "rb");
std::fseek(f, 0, SEEK_END);
int len = std::ftell(f);
std::rewind(f);
// TO DO: WRAP THE new[] IN A TRY/CATCH BLOCK
source = new char[len + 1];
std::fread(source, sizeof(char), len, f);
source[len] = '\0';
std::fclose(f);
own_source = true;
position = source;
}
Document::~Document() {
......
......@@ -3,6 +3,7 @@
#include "context.hpp"
namespace Sass {
using std::string;
using std::vector;
using std::map;
using namespace Prelexer;
......@@ -10,7 +11,7 @@ namespace Sass {
struct Document {
enum CSS_Style { nested, expanded, compact, compressed, echo };
char* path;
string path;
char* source;
char* position;
size_t line_number;
......@@ -21,11 +22,12 @@ namespace Sass {
Context& context;
vector<Node> statements;
Node root;
// vector<Node> statements;
Token lexed;
Document(char* path, char* source = 0);
// Document(char* path, Context& context);
Document(string path, char* source = 0);
Document(string path, Context& context);
~Document();
template <prelexer mx>
......@@ -120,5 +122,6 @@ namespace Sass {
char* look_for_values(char* start = 0);
string emit_css(CSS_Style style);
};
}
\ No newline at end of file
......@@ -7,19 +7,30 @@ namespace Sass {
string Document::emit_css(CSS_Style style) {
stringstream output;
for (int i = 0; i < statements.size(); ++i) {
// for (int i = 0; i < statements.size(); ++i) {
// switch (style) {
// case echo:
// statements[i].echo(output);
// break;
// case nested:
// statements[i].emit_nested_css(output, 0, vector<string>());
// break;
// case expanded:
// statements[i].emit_expanded_css(output, "");
// break;
// }
// }
switch (style) {
case echo:
statements[i].echo(output);
root.echo(output);
break;
case nested:
statements[i].emit_nested_css(output, 0, vector<string>());
root.emit_nested_css(output, 0, vector<string>());
break;
case expanded:
statements[i].emit_expanded_css(output, "");
root.emit_expanded_css(output, "");
break;
}
}
string retval(output.str());
if (!retval.empty()) retval.resize(retval.size()-1);
return retval;
......
......@@ -7,16 +7,30 @@ namespace Sass {
void Document::parse_scss()
{
lex<optional_spaces>();
// while(*position) {
// if (lex< block_comment >()) {
// statements.push_back(Node(line_number, Node::comment, lexed));
// }
// else if (peek< variable >(position)) {
// parse_var_def();
// lex< exactly<';'> >();
// }
// else {
// statements.push_back(parse_ruleset());
// }
// lex<optional_spaces>();
// }
while(*position) {
if (lex< block_comment >()) {
statements.push_back(Node(line_number, Node::comment, lexed));
root << Node(line_number, Node::comment, lexed);
}
else if (peek< variable >(position)) {
parse_var_def();
lex< exactly<';'> >();
}
else {
statements.push_back(parse_ruleset());
root << parse_ruleset();
}
lex<optional_spaces>();
}
......
......@@ -77,6 +77,11 @@ namespace Sass {
const vector<string>& prefixes)
{
switch (type) {
case root:
for (int i = 0; i < children->size(); ++i) {
children->at(i).emit_nested_css(buf, depth, prefixes);
}
break;
case ruleset: {
Node sel_group(children->at(0));
Node block(children->at(1));
......@@ -93,9 +98,6 @@ namespace Sass {
for (int i = 0; i < prefixes.size(); ++i) {
for (int j = 0; j < sel_group.children->size(); ++j) {
new_prefixes.push_back(sel_group.children->at(j).to_string(prefixes[i]));
// new_prefixes.push_back(prefixes[i] +
// ' ' +
// string(sel_group.children->at(j)));
}
}
}
......
......@@ -10,6 +10,7 @@ namespace Sass {
struct Node {
enum Type {
nil,
root,
comment,
ruleset,
propset,
......
......@@ -2,8 +2,8 @@
namespace Sass {
Token::Token() : begin(0), end(0) { }
Token::Token(const char* _begin, const char* _end)
: begin(_begin), end(_end) { }
Token::Token(const char* begin, const char* end)
: begin(begin), end(end) { }
void Token::stream_unquoted(std::stringstream& buf) const {
const char* p = begin;
......
......@@ -10,7 +10,7 @@ namespace Sass {
const char* end;
Token();
Token(const char* _begin, const char* _end);
Token(const char* begin, const char* end);
inline operator string() const
{ return string(begin, end - begin); }
......
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