Commit d235ff2d by Aaron Leung

Working on handling imports.

parent 0f2b1c70
namespace Sass {
using std::map;
struct Context {
map<Token, Node> environment;
Context() : environment(map<Token, Node>()) { }
};
}
\ No newline at end of file
...@@ -3,9 +3,14 @@ ...@@ -3,9 +3,14 @@
namespace Sass { namespace Sass {
Document::Document(char* _path, char* _source) { Document::Document(char* path, char* source)
path = _path; : path(path), source(source),
if (!_source) { line_number(1), own_source(false),
context(*(new Context())),
statements(vector<Node>()),
lexed(Token())
{
if (!source) {
std::FILE *f; std::FILE *f;
// TO DO: CHECK f AGAINST NULL/0 // TO DO: CHECK f AGAINST NULL/0
f = std::fopen(path, "rb"); f = std::fopen(path, "rb");
...@@ -17,16 +22,15 @@ namespace Sass { ...@@ -17,16 +22,15 @@ namespace Sass {
std::fread(source, sizeof(char), len, f); std::fread(source, sizeof(char), len, f);
source[len] = '\0'; source[len] = '\0';
std::fclose(f); std::fclose(f);
} own_source = true;
else {
source = _source;
} }
position = source; position = source;
line_number = 1; // printf("INPUT FILE:\n%s", source);
} }
Document::~Document() { Document::~Document() {
delete [] source; if (own_source) delete [] source;
delete &context;
} }
} }
\ No newline at end of file
#include <map> #include <map>
#include "node.hpp" #include "node.hpp"
#include "context.hpp"
namespace Sass { namespace Sass {
using std::vector; using std::vector;
...@@ -13,14 +14,18 @@ namespace Sass { ...@@ -13,14 +14,18 @@ namespace Sass {
char* source; char* source;
char* position; char* position;
size_t line_number; size_t line_number;
bool own_source;
// TO DO: move the environment up into the context class when it's ready // TO DO: move the environment up into the context class when it's ready
map<Token, Node> environment; // map<Token, Node> environment;
Context& context;
vector<Node> statements; vector<Node> statements;
Token lexed; Token lexed;
Document(char* _path, char* _source = 0); Document(char* path, char* source = 0);
// Document(char* path, Context& context);
~Document(); ~Document();
template <prelexer mx> template <prelexer mx>
......
...@@ -11,7 +11,7 @@ namespace Sass { ...@@ -11,7 +11,7 @@ namespace Sass {
if (lex< block_comment >()) { if (lex< block_comment >()) {
statements.push_back(Node(line_number, Node::comment, lexed)); statements.push_back(Node(line_number, Node::comment, lexed));
} }
else if (lex< variable >()) { else if (peek< variable >(position)) {
parse_var_def(); parse_var_def();
lex< exactly<';'> >(); lex< exactly<';'> >();
} }
...@@ -24,9 +24,10 @@ namespace Sass { ...@@ -24,9 +24,10 @@ namespace Sass {
void Document::parse_var_def() void Document::parse_var_def()
{ {
lex< variable >();
const Token key(lexed); const Token key(lexed);
lex< exactly<':'> >(); lex< exactly<':'> >();
environment[key] = parse_values(); context.environment[key] = parse_values();
} }
Node Document::parse_ruleset() Node Document::parse_ruleset()
...@@ -209,7 +210,7 @@ namespace Sass { ...@@ -209,7 +210,7 @@ namespace Sass {
lex< hex >() || lex < string_constant >() || lex< hex >() || lex < string_constant >() ||
lex< variable >()) { lex< variable >()) {
if (lexed.begin[0] == '$') { if (lexed.begin[0] == '$') {
Node fetched(environment[lexed]); Node fetched(context.environment[lexed]);
for (int i = 0; i < fetched.children->size(); ++i) { for (int i = 0; i < fetched.children->size(); ++i) {
values << fetched.children->at(i); values << fetched.children->at(i);
} }
......
...@@ -13,6 +13,9 @@ namespace Sass { ...@@ -13,6 +13,9 @@ namespace Sass {
return *src ? 0 : src; return *src ? 0 : src;
} }
// Match any single character.
char *any_char(char* src) { return *src ? src++ : src; }
// Match a single character satisfying the ctype predicates. // Match a single character satisfying the ctype predicates.
char* space(char* src) { return std::isspace(*src) ? src+1 : 0; } char* space(char* src) { return std::isspace(*src) ? src+1 : 0; }
char* alpha(char* src) { return std::isalpha(*src) ? src+1 : 0; } char* alpha(char* src) { return std::isalpha(*src) ? src+1 : 0; }
...@@ -196,6 +199,12 @@ namespace Sass { ...@@ -196,6 +199,12 @@ namespace Sass {
char* variable(char* src) { char* variable(char* src) {
return sequence<exactly<'$'>, name>(src); return sequence<exactly<'$'>, name>(src);
} }
// Path matching functions.
char* folder(char* src) {
return sequence< zero_plus< negate< exactly<'/'> > >,
exactly<'/'> >(src);
}
} }
} }
\ No newline at end of file
...@@ -84,6 +84,9 @@ namespace Sass { ...@@ -84,6 +84,9 @@ namespace Sass {
} }
} }
// Match any single character.
char *any_char(char* src);
// Matches zero characters (always succeeds without consuming input). // Matches zero characters (always succeeds without consuming input).
char* epsilon(char*); char* epsilon(char*);
...@@ -308,6 +311,9 @@ namespace Sass { ...@@ -308,6 +311,9 @@ namespace Sass {
// Match SCSS variable names. // Match SCSS variable names.
char* variable(char* src); char* variable(char* src);
// Path matching functions.
char* folder(char* src);
// Utility functions for finding and counting characters in a string. // Utility functions for finding and counting characters in a string.
template<char c> template<char c>
char* find_first(char* src) { char* find_first(char* src) {
......
...@@ -54,6 +54,9 @@ char nonanc1[] = " { blah"; ...@@ -54,6 +54,9 @@ char nonanc1[] = " { blah";
char bi1[] = "+2n + 42"; char bi1[] = "+2n + 42";
char bi2[] = "23n+1"; char bi2[] = "23n+1";
char nonbi1[] = "- n+2"; char nonbi1[] = "- n+2";
char fld1[] = "blah/bloo/foo.txt";
char fld2[] = "/bloo/blee";
char nonfld1[] = "blah.txt";
extern const char slash_star[] = "/*"; extern const char slash_star[] = "/*";
...@@ -101,6 +104,8 @@ int main() { ...@@ -101,6 +104,8 @@ int main() {
check_twice(ancestor_of, anc1, nonanc1); check_twice(ancestor_of, anc1, nonanc1);
check_twice(binomial, bi1, nonbi1); check_twice(binomial, bi1, nonbi1);
check_twice(binomial, bi2, nonbi1); check_twice(binomial, bi2, nonbi1);
check_twice(folder, fld1, nonfld1);
check_twice(folder, fld2, nonfld1);
cout << count_interval<'\n'>(ws1, spaces_and_comments(ws1)) << endl; cout << count_interval<'\n'>(ws1, spaces_and_comments(ws1)) << endl;
cout << count_interval<'*'>(ws1, spaces_and_comments(ws1)) << endl; cout << count_interval<'*'>(ws1, spaces_and_comments(ws1)) << endl;
cout << count_interval<exactly<slash_star> >(ws1, spaces_and_comments(ws1)) << endl; cout << count_interval<exactly<slash_star> >(ws1, spaces_and_comments(ws1)) << endl;
......
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