Commit 43ae0e6b by Aaron Leung

Starting to add error handling.

parent 5c604545
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <cstring> #include <cstring>
#include "document.hpp" #include "document.hpp"
#include "eval_apply.hpp" #include "eval_apply.hpp"
#include "error.hpp"
#include <iostream> #include <iostream>
namespace Sass { namespace Sass {
...@@ -112,12 +113,8 @@ namespace Sass { ...@@ -112,12 +113,8 @@ namespace Sass {
// if (context.ref_count == 0) delete &context; // if (context.ref_count == 0) delete &context;
} }
// void Document::eval_pending() void Document::syntax_error(string message, size_t ln)
// { { throw Error(Error::syntax, ln ? ln : line_number, path, message); }
// for (int i = 0; i < context.pending.size(); ++i) {
// eval(context.pending[i], context.global_env);
// }
// }
using std::string; using std::string;
using std::stringstream; using std::stringstream;
......
...@@ -153,7 +153,8 @@ namespace Sass { ...@@ -153,7 +153,8 @@ namespace Sass {
const char* look_for_pseudo(const char* start = 0); const char* look_for_pseudo(const char* start = 0);
const char* look_for_attrib(const char* start = 0); const char* look_for_attrib(const char* start = 0);
void eval_pending(); void syntax_error(string message, size_t ln = 0);
string emit_css(CSS_Style style); string emit_css(CSS_Style style);
}; };
......
#include "document.hpp" #include "document.hpp"
#include "error.hpp"
#include <iostream> #include <iostream>
namespace Sass { namespace Sass {
...@@ -15,7 +16,7 @@ namespace Sass { ...@@ -15,7 +16,7 @@ namespace Sass {
else if (peek< import >(position)) { else if (peek< import >(position)) {
// TO DO: don't splice in place at parse-time -- use an expansion node // TO DO: don't splice in place at parse-time -- use an expansion node
root += parse_import(); root += parse_import();
lex< exactly<';'> >(); if (!lex< exactly<';'> >()) syntax_error("top-level @import directive must be terminated by a semicolon");
} }
else if (peek< mixin >(position)) { else if (peek< mixin >(position)) {
root << parse_mixin_definition(); root << parse_mixin_definition();
...@@ -23,11 +24,11 @@ namespace Sass { ...@@ -23,11 +24,11 @@ namespace Sass {
else if (peek< include >(position)) { else if (peek< include >(position)) {
root << parse_mixin_call(); root << parse_mixin_call();
root[0].has_expansions = true; root[0].has_expansions = true;
lex< exactly<';'> >(); if (!lex< exactly<';'> >()) syntax_error("top-level @include directive must be terminated by a semicolon");
} }
else if (peek< variable >(position)) { else if (peek< variable >(position)) {
root << parse_assignment(); root << parse_assignment();
lex< exactly<';'> >(); if (!lex< exactly<';'> >()) syntax_error("top-level variable binding must be terminated by a semicolon");
} }
else { else {
root << parse_ruleset(); root << parse_ruleset();
......
...@@ -3,12 +3,13 @@ namespace Sass { ...@@ -3,12 +3,13 @@ namespace Sass {
struct Error { struct Error {
enum Type { read, write, syntax, evaluation }; enum Type { read, write, syntax, evaluation };
Type type;
size_t line_number; size_t line_number;
string file_name; string file_name;
string message; string message;
Error(size_t line_number, string file_name, string message) Error(Type type, size_t line_number, string file_name, string message)
: line_number(line_number), file_name(file_name), message(message) : type(type), line_number(line_number), file_name(file_name), message(message)
{ } { }
}; };
......
...@@ -2,8 +2,10 @@ ...@@ -2,8 +2,10 @@
#include <string> #include <string>
#include <cstdlib> #include <cstdlib>
#include <unistd.h> #include <unistd.h>
#include <iostream>
#include "document.hpp" #include "document.hpp"
#include "eval_apply.hpp" #include "eval_apply.hpp"
#include "error.hpp"
#include "sass_interface.h" #include "sass_interface.h"
extern "C" { extern "C" {
...@@ -47,22 +49,32 @@ extern "C" { ...@@ -47,22 +49,32 @@ extern "C" {
int sass_compile(sass_context* c_ctx) int sass_compile(sass_context* c_ctx)
{ {
using namespace Sass; using namespace Sass;
// TO DO: CATCH ALL EXCEPTIONS try {
Context cpp_ctx(c_ctx->options.include_paths); Context cpp_ctx(c_ctx->options.include_paths);
Document doc(0, c_ctx->input_string, cpp_ctx); Document doc(0, c_ctx->input_string, cpp_ctx);
c_ctx->output_string = process_document(doc, c_ctx->options.output_style); c_ctx->output_string = process_document(doc, c_ctx->options.output_style);
}
catch (Error e) {
cerr << "ERROR -- " << e.file_name << ", line " << e.line_number << ": " << e.message << endl;
c_ctx->output_string = 0;
}
// TO DO: CATCH EVERYTHING ELSE
return 0; return 0;
} }
int sass_compile_file(sass_file_context* c_ctx) int sass_compile_file(sass_file_context* c_ctx)
{ {
using namespace Sass; using namespace Sass;
// TO DO: CATCH ALL EXCEPTIONS try {
Context cpp_ctx(c_ctx->options.include_paths); Context cpp_ctx(c_ctx->options.include_paths);
Document doc(c_ctx->input_path, 0, cpp_ctx); Document doc(c_ctx->input_path, 0, cpp_ctx);
c_ctx->output_string = process_document(doc, c_ctx->options.output_style); c_ctx->output_string = process_document(doc, c_ctx->options.output_style);
}
catch (Error e) {
cerr << "ERROR -- " << e.file_name << ", line " << e.line_number << ": " << e.message << endl;
c_ctx->output_string = 0;
}
// TO DO: CATCH EVERYTHING ELSE
return 0; return 0;
} }
......
...@@ -34,7 +34,7 @@ int main(int argc, char** argv) ...@@ -34,7 +34,7 @@ int main(int argc, char** argv)
sass_compile_file(ctx); sass_compile_file(ctx);
printf("%s", ctx->output_string); if (ctx->output_string) printf("%s", ctx->output_string);
sass_free_file_context(ctx); sass_free_file_context(ctx);
return 0; 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