Commit 159728a3 by Aaron Leung

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

parent 87ab283f
......@@ -6,80 +6,26 @@
#include <iostream>
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)
: path(path), source(source),
line_number(1), own_source(false),
context(*(new Context())),
root(Node(Node::root, context.registry, 1)),
lexed(Token::make())
{
if (!source) {
std::FILE *f;
f = std::fopen(path.c_str(), "rb");
if (!f) throw path;
if (std::fseek(f, 0, SEEK_END)) throw path;
int len = std::ftell(f);
if (len < 0) throw path;
std::rewind(f);
// TO DO: CATCH THE POTENTIAL badalloc EXCEPTION
source = new char[len + 1];
std::fread(source, sizeof(char), len, f);
if (std::ferror(f)) throw 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())
Document::Document(Context& ctx) : context(ctx)
{ ++context.ref_count; }
Document::Document(const Document& doc)
: path(doc.path),
source(doc.source),
position(doc.position),
end(doc.end),
line_number(doc.line_number),
own_source(doc.own_source),
context(doc.context),
root(doc.root),
lexed(doc.lexed)
{ ++doc.context.ref_count; }
Document::~Document()
{ --context.ref_count; }
static Document Document::make_from_file(Context& ctx, string path)
{
std::FILE *f;
f = std::fopen(path.c_str(), "rb");
......@@ -88,39 +34,62 @@ namespace Sass {
int len = std::ftell(f);
if (len < 0) throw path;
std::rewind(f);
// TO DO: CATCH THE POTENTIAL badalloc EXCEPTION
source = new char[len + 1];
char* source = new char[len + 1];
std::fread(source, sizeof(char), len, f);
if (std::ferror(f)) throw path;
source[len] = '\0';
end = source + len;
char* end = source + len;
if (std::fclose(f)) throw path;
position = source;
context.source_refs.push_back(source);
++context.ref_count;
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 = 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() {
--context.ref_count;
// if (context.ref_count == 0) delete &context;
static Document Document::make_from_source_chars(Context& ctx, char* src, string path)
{
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); }
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); }
using std::string;
......
......@@ -26,13 +26,17 @@ namespace Sass {
Node root;
Token lexed;
Document(char* path_str, char* source_str, Context& ctx);
Document(string path, char* source = 0);
Document(string path, Context& context);
Document(const string& path, size_t line_number, Token t, Context& context);
private:
// force the use of the "make_from_..." factory funtions
Document(Context& ctx);
~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>
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