Skip to content

Commit

Permalink
Fleshing out the classes for documents, nodes, and tokens.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Leung committed Feb 28, 2012
1 parent 0418884 commit cefe1e2
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 3 deletions.
27 changes: 27 additions & 0 deletions src/document.cpp
@@ -0,0 +1,27 @@
#include <cstdio>
#include "document.hpp"

namespace Sass {
Document::Document(char* _path, char* _source) {
path = _path;
if (!source) {
std::FILE *f;
f = std::fopen(path, "rb");
// if (!f) {
// printf("ERROR: could not open file %s", path);
// abort();
// }
std::fseek(f, 0, SEEK_END);
int len = std::ftell(f);
std::rewind(f);
// char *buf = (char *)malloc(len * sizeof(char) + 1);
source = new char[len + 1];
std::fread(source, sizeof(char), len, f);
source[len] = '\0';
std::fclose(f);
}
else {
source = _source;
}
}
}
8 changes: 6 additions & 2 deletions src/document.hpp
Expand Up @@ -2,11 +2,15 @@
#include "node.hpp"

namespace Sass {
using std::vector;

struct Document {
using std::vector;

char* path;
char* source;
unsigned int position;
unsigned int line_number;
vector<Node> statements;

Document(char* _path, char* _source = 0);
};
}
15 changes: 15 additions & 0 deletions src/node.cpp
@@ -0,0 +1,15 @@
#include "node.hpp"

namespace Sass {
Node::Node() { }
Node::Node(Node_Type _type, Token& _token) {
type = _type;
token = _token;
}
void Node::push_child(Node& node) {
children.push_back(node);
}
void Node::push_opt_child(Node& node) {
opt_children.push_back(node);
}
}
26 changes: 25 additions & 1 deletion src/node.hpp
Expand Up @@ -2,5 +2,29 @@
#include "token.hpp"

namespace Sass {
using std::vector;
struct Node {

enum Node_Type {
null,
comment,
rule_set,
declaration,
selector_group,
selector,
simple_selector_sequence,
simple_selector,
property,
value
};

Node_Type type;
Token token;
vector<Node> children;
vector<Node> opt_children;

Node();
Node(Node_Type _type, Token& _token);
void push_child(Node& node);
void push_opt_child(Node& node);
};
}
16 changes: 16 additions & 0 deletions src/token.cpp
@@ -0,0 +1,16 @@
#include "token.hpp"

namespace Sass {
Token::Token() {
begin = 0;
end = 0;
line_number = 1;
}
Token::Token(const char* _begin,
const char* _end,
unsigned int _line_number) {
begin = _begin;
end = _end;
line_number = _line_number;
}
}
12 changes: 12 additions & 0 deletions src/token.hpp
@@ -0,0 +1,12 @@
namespace Sass {
struct Token {
const char* begin;
const char* end;
unsigned int line_number;
Token();
Token(const char* _begin,
const char* _end,
unsigned int _line_number);
inline bool is_null() { return begin == 0 || end == 0; }
};
}
33 changes: 33 additions & 0 deletions src/vectest.cpp
@@ -0,0 +1,33 @@
#include <vector>
#include <iostream>

using namespace std;

struct box {
vector<int> vec;
vector<box> vec2;
};

int main() {
box b;

b.vec.push_back(1);
b.vec.push_back(2);
b.vec.push_back(3);

box c = b;
c.vec.push_back(4);

cout << b.vec.size() << endl;
cout << c.vec.size() << endl;

c.vec[0] = 42;
cout << b.vec[0] << endl;
cout << c.vec[0] << endl;

b.vec2.push_back(c);
cout << b.vec2[0].vec[0] << endl;

return 0;
}

0 comments on commit cefe1e2

Please sign in to comment.