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