Commit cefe1e26 by Aaron Leung

Fleshing out the classes for documents, nodes, and tokens.

parent 04188848
#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;
}
}
}
\ No newline at end of file
...@@ -2,11 +2,15 @@ ...@@ -2,11 +2,15 @@
#include "node.hpp" #include "node.hpp"
namespace Sass { namespace Sass {
using std::vector;
struct Document { struct Document {
using std::vector; char* path;
char* source; char* source;
unsigned int position; unsigned int position;
unsigned int line_number;
vector<Node> statements; vector<Node> statements;
Document(char* _path, char* _source = 0);
}; };
} }
\ No newline at end of file
#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);
}
}
\ No newline at end of file
...@@ -2,5 +2,29 @@ ...@@ -2,5 +2,29 @@
#include "token.hpp" #include "token.hpp"
namespace Sass { namespace Sass {
using std::vector;
struct Node { struct Node {
enum Node_Type {
\ No newline at end of file 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);
};
}
\ No newline at end of file
#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;
}
}
\ No newline at end of file
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; }
};
}
\ No newline at end of file
#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;
}
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