Commit 159728a3 by Aaron Leung

Refactoring the Document constructors. Don't try to compile them yet.

parent 87ab283f
...@@ -6,80 +6,26 @@ ...@@ -6,80 +6,26 @@
#include <iostream> #include <iostream>
namespace Sass { namespace Sass {
Document::Document(char* path_str, char* source_str, Context& ctx)
: path(string()),
source(source_str),
line_number(1),
context(ctx),
root(Node(Node::root, context.registry, 1)),
lexed(Token::make())
{
if (source_str) {
own_source = false;
position = source;
end = position + std::strlen(source);
}
else if (path_str) {
path = string(path_str);
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';
end = source + len;
std::fclose(f);
own_source = true;
position = source;
context.source_refs.push_back(source);
}
else {
// report an error
}
++context.ref_count;
}
Document::Document(string path, char* source) Document::Document(Context& ctx) : context(ctx)
: path(path), source(source), { ++context.ref_count; }
line_number(1), own_source(false),
context(*(new Context())), Document::Document(const Document& doc)
root(Node(Node::root, context.registry, 1)), : path(doc.path),
lexed(Token::make()) source(doc.source),
{ position(doc.position),
if (!source) { end(doc.end),
std::FILE *f; line_number(doc.line_number),
f = std::fopen(path.c_str(), "rb"); own_source(doc.own_source),
if (!f) throw path; context(doc.context),
if (std::fseek(f, 0, SEEK_END)) throw path; root(doc.root),
int len = std::ftell(f); lexed(doc.lexed)
if (len < 0) throw path; { ++doc.context.ref_count; }
std::rewind(f);
// TO DO: CATCH THE POTENTIAL badalloc EXCEPTION Document::~Document()
source = new char[len + 1]; { --context.ref_count; }
std::fread(source, sizeof(char), len, f);
if (std::ferror(f)) throw path; static Document Document::make_from_file(Context& ctx, string path)
source[len] = '\0';
end = source + len;
if (std::fclose(f)) throw path;
own_source = true;
}
position = source;
context.source_refs.push_back(source);
++context.ref_count;
}
Document::Document(string path, Context& context)
: path(path), source(0),
line_number(1), own_source(false),
context(context),
root(Node(Node::root, context.registry, 1)),
lexed(Token::make())
{ {
std::FILE *f; std::FILE *f;
f = std::fopen(path.c_str(), "rb"); f = std::fopen(path.c_str(), "rb");
...@@ -88,39 +34,62 @@ namespace Sass { ...@@ -88,39 +34,62 @@ namespace Sass {
int len = std::ftell(f); int len = std::ftell(f);
if (len < 0) throw path; if (len < 0) throw path;
std::rewind(f); std::rewind(f);
// TO DO: CATCH THE POTENTIAL badalloc EXCEPTION char* source = new char[len + 1];
source = new char[len + 1];
std::fread(source, sizeof(char), len, f); std::fread(source, sizeof(char), len, f);
if (std::ferror(f)) throw path; if (std::ferror(f)) throw path;
source[len] = '\0'; source[len] = '\0';
end = source + len; char* end = source + len;
if (std::fclose(f)) throw path; if (std::fclose(f)) throw path;
position = source;
context.source_refs.push_back(source); Document doc(ctx);
++context.ref_count; doc.path = path;
doc.line_number = 1;
doc.root = ctx.new_Node(Node::root, path, 1, 0);
doc.lexed = Token::make();
doc.own_source = true;
doc.source = source;
doc.end = end;
doc.position = source;
doc.context.source_refs.push_back(source);
return doc;
} }
Document::Document(const string& path, size_t line_number, Token t, Context& context)
: path(path),
source(const_cast<char*>(t.begin)),
position(t.begin),
end(t.end),
line_number(line_number),
own_source(false),
context(context),
root(Node(Node::root, context.registry, 1)),
lexed(Token::make())
{ }
Document::~Document() { static Document Document::make_from_source_chars(Context& ctx, char* src, string path)
--context.ref_count; {
// if (context.ref_count == 0) delete &context; Document doc(ctx);
doc.path = path;
doc.line_number = 1;
doc.root = ctx.new_Node(Node::root, path, 1, 0);
doc.lexed = Token::make();
doc.own_source = false;
doc.source = src;
doc.end = src + std::strlen(src);
doc.position = doc.end;
// doc.context.source_refs.push_back(src); // we don't own src
return doc;
}
static Document Document::make_from_token(Context& ctx, Token t, string path, size_t line_number)
{
Document doc(ctx);
doc.path = path;
doc.line_number = line_number;
doc.root = ctx.new_Node(Node::root, path, 1, 0);
doc.lexed = Token::make();
doc.own_source = false;
doc.source = const_cast<char*>(t.begin);
doc.end = t.end;
doc.position = doc.source;
return doc;
} }
void Document::syntax_error(string message, size_t ln) void Document::throw_syntax_error(string message, size_t ln)
{ throw Error(Error::syntax, ln ? ln : line_number, path, message); } { throw Error(Error::syntax, ln ? ln : line_number, path, message); }
void Document::read_error(string message, size_t ln) void Document::throw_read_error(string message, size_t ln)
{ throw Error(Error::read, ln ? ln : line_number, path, message); } { throw Error(Error::read, ln ? ln : line_number, path, message); }
using std::string; using std::string;
......
...@@ -26,13 +26,17 @@ namespace Sass { ...@@ -26,13 +26,17 @@ namespace Sass {
Node root; Node root;
Token lexed; Token lexed;
Document(char* path_str, char* source_str, Context& ctx); private:
Document(string path, char* source = 0); // force the use of the "make_from_..." factory funtions
Document(string path, Context& context); Document(Context& ctx);
Document(const string& path, size_t line_number, Token t, Context& context);
~Document(); ~Document();
public:
static Document make_from_file(Context& ctx, string path);
static Document make_from_source_chars(Context& ctx, char* src, string path = "");
static Document make_from_token(Context& ctx, Token t, string path = "", size_t line_number = 1);
template <prelexer mx> template <prelexer mx>
const char* peek(const char* start = 0) const char* peek(const char* start = 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