From efc0331228d639505e41d18699bd133b6d82b4cf Mon Sep 17 00:00:00 2001 From: Marcel Greter Date: Sat, 17 Mar 2018 03:36:36 +0100 Subject: [PATCH] Rename ast2c and move c2ast to own compile unit --- Makefile.conf | 3 +- src/{to_c.cpp => ast2c.cpp} | 29 +++++++------- src/{to_c.hpp => ast2c.hpp} | 17 ++++----- src/c2ast.cpp | 64 +++++++++++++++++++++++++++++++ src/c2ast.hpp | 14 +++++++ src/eval.cpp | 76 ++++++------------------------------- src/eval.hpp | 2 - src/fn_colors.cpp | 3 +- src/fn_strings.cpp | 3 ++ win/libsass.targets | 6 ++- win/libsass.vcxproj.filters | 10 ++++- 11 files changed, 129 insertions(+), 98 deletions(-) rename src/{to_c.cpp => ast2c.cpp} (63%) rename src/{to_c.hpp => ast2c.hpp} (71%) create mode 100644 src/c2ast.cpp create mode 100644 src/c2ast.hpp diff --git a/Makefile.conf b/Makefile.conf index 6e42a8677..2725c6bf0 100644 --- a/Makefile.conf +++ b/Makefile.conf @@ -51,7 +51,8 @@ SOURCES = \ sass2scss.cpp \ backtrace.cpp \ operators.cpp \ - to_c.cpp \ + ast2c.cpp \ + c2ast.cpp \ to_value.cpp \ source_map.cpp \ subset_map.cpp \ diff --git a/src/to_c.cpp b/src/ast2c.cpp similarity index 63% rename from src/to_c.cpp rename to src/ast2c.cpp index 8a6ea8d51..be9ea800f 100644 --- a/src/to_c.cpp +++ b/src/ast2c.cpp @@ -1,28 +1,25 @@ #include "sass.hpp" -#include "to_c.hpp" +#include "ast2c.hpp" #include "ast.hpp" namespace Sass { - union Sass_Value* To_C::fallback_impl(AST_Node_Ptr n) - { return sass_make_error("unknown type for C-API"); } - - union Sass_Value* To_C::operator()(Boolean_Ptr b) + union Sass_Value* AST2C::operator()(Boolean_Ptr b) { return sass_make_boolean(b->value()); } - union Sass_Value* To_C::operator()(Number_Ptr n) + union Sass_Value* AST2C::operator()(Number_Ptr n) { return sass_make_number(n->value(), n->unit().c_str()); } - union Sass_Value* To_C::operator()(Custom_Warning_Ptr w) + union Sass_Value* AST2C::operator()(Custom_Warning_Ptr w) { return sass_make_warning(w->message().c_str()); } - union Sass_Value* To_C::operator()(Custom_Error_Ptr e) + union Sass_Value* AST2C::operator()(Custom_Error_Ptr e) { return sass_make_error(e->message().c_str()); } - union Sass_Value* To_C::operator()(Color_Ptr c) + union Sass_Value* AST2C::operator()(Color_Ptr c) { return sass_make_color(c->r(), c->g(), c->b(), c->a()); } - union Sass_Value* To_C::operator()(String_Constant_Ptr s) + union Sass_Value* AST2C::operator()(String_Constant_Ptr s) { if (s->quote_mark()) { return sass_make_qstring(s->value().c_str()); @@ -31,10 +28,10 @@ namespace Sass { } } - union Sass_Value* To_C::operator()(String_Quoted_Ptr s) + union Sass_Value* AST2C::operator()(String_Quoted_Ptr s) { return sass_make_qstring(s->value().c_str()); } - union Sass_Value* To_C::operator()(List_Ptr l) + union Sass_Value* AST2C::operator()(List_Ptr l) { union Sass_Value* v = sass_make_list(l->length(), l->separator(), l->is_bracketed()); for (size_t i = 0, L = l->length(); i < L; ++i) { @@ -43,7 +40,7 @@ namespace Sass { return v; } - union Sass_Value* To_C::operator()(Map_Ptr m) + union Sass_Value* AST2C::operator()(Map_Ptr m) { union Sass_Value* v = sass_make_map(m->length()); int i = 0; @@ -55,7 +52,7 @@ namespace Sass { return v; } - union Sass_Value* To_C::operator()(Arguments_Ptr a) + union Sass_Value* AST2C::operator()(Arguments_Ptr a) { union Sass_Value* v = sass_make_list(a->length(), SASS_COMMA, false); for (size_t i = 0, L = a->length(); i < L; ++i) { @@ -64,11 +61,11 @@ namespace Sass { return v; } - union Sass_Value* To_C::operator()(Argument_Ptr a) + union Sass_Value* AST2C::operator()(Argument_Ptr a) { return a->value()->perform(this); } // not strictly necessary because of the fallback - union Sass_Value* To_C::operator()(Null_Ptr n) + union Sass_Value* AST2C::operator()(Null_Ptr n) { return sass_make_null(); } }; diff --git a/src/to_c.hpp b/src/ast2c.hpp similarity index 71% rename from src/to_c.hpp rename to src/ast2c.hpp index a5331e3bf..428791619 100644 --- a/src/to_c.hpp +++ b/src/ast2c.hpp @@ -1,5 +1,5 @@ -#ifndef SASS_TO_C_H -#define SASS_TO_C_H +#ifndef SASS_AST2C_H +#define SASS_AST2C_H #include "ast_fwd_decl.hpp" #include "operation.hpp" @@ -7,14 +7,12 @@ namespace Sass { - class To_C : public Operation_CRTP { - // override this to define a catch-all - union Sass_Value* fallback_impl(AST_Node_Ptr n); + class AST2C : public Operation_CRTP { public: - To_C() { } - ~To_C() { } + AST2C() { } + ~AST2C() { } union Sass_Value* operator()(Boolean_Ptr); union Sass_Value* operator()(Number_Ptr); @@ -29,9 +27,10 @@ namespace Sass { union Sass_Value* operator()(Arguments_Ptr); union Sass_Value* operator()(Argument_Ptr); - // dispatch to fallback implementation + // return sass error if type is not supported union Sass_Value* fallback(AST_Node_Ptr x) - { return fallback_impl(x); } + { return sass_make_error("unknown type for C-API"); } + }; } diff --git a/src/c2ast.cpp b/src/c2ast.cpp new file mode 100644 index 000000000..c766fa184 --- /dev/null +++ b/src/c2ast.cpp @@ -0,0 +1,64 @@ +#include "ast.hpp" +#include "units.hpp" +#include "position.hpp" +#include "backtrace.hpp" +#include "sass/values.h" +#include "ast_fwd_decl.hpp" +#include "error_handling.hpp" + +namespace Sass { + + Value_Ptr c2ast(union Sass_Value* v, Backtraces traces, ParserState pstate) + { + using std::strlen; + using std::strcpy; + Value_Ptr e = NULL; + switch (sass_value_get_tag(v)) { + case SASS_BOOLEAN: { + e = SASS_MEMORY_NEW(Boolean, pstate, !!sass_boolean_get_value(v)); + } break; + case SASS_NUMBER: { + e = SASS_MEMORY_NEW(Number, pstate, sass_number_get_value(v), sass_number_get_unit(v)); + } break; + case SASS_COLOR: { + e = SASS_MEMORY_NEW(Color, pstate, sass_color_get_r(v), sass_color_get_g(v), sass_color_get_b(v), sass_color_get_a(v)); + } break; + case SASS_STRING: { + if (sass_string_is_quoted(v)) + e = SASS_MEMORY_NEW(String_Quoted, pstate, sass_string_get_value(v)); + else { + e = SASS_MEMORY_NEW(String_Constant, pstate, sass_string_get_value(v)); + } + } break; + case SASS_LIST: { + List_Ptr l = SASS_MEMORY_NEW(List, pstate, sass_list_get_length(v), sass_list_get_separator(v)); + for (size_t i = 0, L = sass_list_get_length(v); i < L; ++i) { + l->append(c2ast(sass_list_get_value(v, i), traces, pstate)); + } + l->is_bracketed(sass_list_get_is_bracketed(v)); + e = l; + } break; + case SASS_MAP: { + Map_Ptr m = SASS_MEMORY_NEW(Map, pstate); + for (size_t i = 0, L = sass_map_get_length(v); i < L; ++i) { + *m << std::make_pair( + c2ast(sass_map_get_key(v, i), traces, pstate), + c2ast(sass_map_get_value(v, i), traces, pstate)); + } + e = m; + } break; + case SASS_NULL: { + e = SASS_MEMORY_NEW(Null, pstate); + } break; + case SASS_ERROR: { + error("Error in C function: " + std::string(sass_error_get_message(v)), pstate, traces); + } break; + case SASS_WARNING: { + error("Warning in C function: " + std::string(sass_warning_get_message(v)), pstate, traces); + } break; + default: break; + } + return e; + } + +} diff --git a/src/c2ast.hpp b/src/c2ast.hpp new file mode 100644 index 000000000..d6c0a97c3 --- /dev/null +++ b/src/c2ast.hpp @@ -0,0 +1,14 @@ +#ifndef SASS_C2AST_H +#define SASS_C2AST_H + +#include "position.hpp" +#include "backtrace.hpp" +#include "ast_fwd_decl.hpp" + +namespace Sass { + + Value_Ptr c2ast(union Sass_Value* v, Backtraces traces, ParserState pstate); + +} + +#endif diff --git a/src/eval.cpp b/src/eval.cpp index 1a0c7a8bd..512c420e2 100644 --- a/src/eval.cpp +++ b/src/eval.cpp @@ -17,7 +17,8 @@ #include "position.hpp" #include "sass/values.h" #include "to_value.hpp" -#include "to_c.hpp" +#include "ast2c.hpp" +#include "c2ast.hpp" #include "context.hpp" #include "backtrace.hpp" #include "lexer.hpp" @@ -333,9 +334,9 @@ namespace Sass { Sass_Function_Entry c_function = def->c_function(); Sass_Function_Fn c_func = sass_function_get_function(c_function); - To_C to_c; + AST2C ast2c; union Sass_Value* c_args = sass_make_list(1, SASS_COMMA, false); - sass_list_set_value(c_args, 0, message->perform(&to_c)); + sass_list_set_value(c_args, 0, message->perform(&ast2c)); union Sass_Value* c_val = c_func(c_args, c_function, ctx.c_compiler); ctx.c_options.output_style = outstyle; ctx.callee_stack.pop_back(); @@ -381,9 +382,9 @@ namespace Sass { Sass_Function_Entry c_function = def->c_function(); Sass_Function_Fn c_func = sass_function_get_function(c_function); - To_C to_c; + AST2C ast2c; union Sass_Value* c_args = sass_make_list(1, SASS_COMMA, false); - sass_list_set_value(c_args, 0, message->perform(&to_c)); + sass_list_set_value(c_args, 0, message->perform(&ast2c)); union Sass_Value* c_val = c_func(c_args, c_function, ctx.c_compiler); ctx.c_options.output_style = outstyle; ctx.callee_stack.pop_back(); @@ -425,9 +426,9 @@ namespace Sass { Sass_Function_Entry c_function = def->c_function(); Sass_Function_Fn c_func = sass_function_get_function(c_function); - To_C to_c; + AST2C ast2c; union Sass_Value* c_args = sass_make_list(1, SASS_COMMA, false); - sass_list_set_value(c_args, 0, message->perform(&to_c)); + sass_list_set_value(c_args, 0, message->perform(&ast2c)); union Sass_Value* c_val = c_func(c_args, c_function, ctx.c_compiler); ctx.c_options.output_style = outstyle; ctx.callee_stack.pop_back(); @@ -1054,14 +1055,14 @@ namespace Sass { { env } }); - To_C to_c; + AST2C ast2c; union Sass_Value* c_args = sass_make_list(params->length(), SASS_COMMA, false); for(size_t i = 0; i < params->length(); i++) { Parameter_Obj param = params->at(i); std::string key = param->name(); AST_Node_Obj node = fn_env.get_local(key); Expression_Obj arg = Cast(node); - sass_list_set_value(c_args, i, arg->perform(&to_c)); + sass_list_set_value(c_args, i, arg->perform(&ast2c)); } union Sass_Value* c_val = c_func(c_args, c_function, ctx.c_compiler); if (sass_value_get_tag(c_val) == SASS_ERROR) { @@ -1069,7 +1070,7 @@ namespace Sass { } else if (sass_value_get_tag(c_val) == SASS_WARNING) { error("warning in C function " + c->name() + ": " + sass_warning_get_message(c_val), c->pstate(), traces); } - result = cval_to_astnode(c_val, traces, c->pstate()); + result = c2ast(c_val, traces, c->pstate()); ctx.callee_stack.pop_back(); traces.pop_back(); @@ -1467,61 +1468,6 @@ namespace Sass { return 0; } - // All the binary helpers. - - Expression_Ptr cval_to_astnode(union Sass_Value* v, Backtraces traces, ParserState pstate) - { - using std::strlen; - using std::strcpy; - Expression_Ptr e = NULL; - switch (sass_value_get_tag(v)) { - case SASS_BOOLEAN: { - e = SASS_MEMORY_NEW(Boolean, pstate, !!sass_boolean_get_value(v)); - } break; - case SASS_NUMBER: { - e = SASS_MEMORY_NEW(Number, pstate, sass_number_get_value(v), sass_number_get_unit(v)); - } break; - case SASS_COLOR: { - e = SASS_MEMORY_NEW(Color, pstate, sass_color_get_r(v), sass_color_get_g(v), sass_color_get_b(v), sass_color_get_a(v)); - } break; - case SASS_STRING: { - if (sass_string_is_quoted(v)) - e = SASS_MEMORY_NEW(String_Quoted, pstate, sass_string_get_value(v)); - else { - e = SASS_MEMORY_NEW(String_Constant, pstate, sass_string_get_value(v)); - } - } break; - case SASS_LIST: { - List_Ptr l = SASS_MEMORY_NEW(List, pstate, sass_list_get_length(v), sass_list_get_separator(v)); - for (size_t i = 0, L = sass_list_get_length(v); i < L; ++i) { - l->append(cval_to_astnode(sass_list_get_value(v, i), traces, pstate)); - } - l->is_bracketed(sass_list_get_is_bracketed(v)); - e = l; - } break; - case SASS_MAP: { - Map_Ptr m = SASS_MEMORY_NEW(Map, pstate); - for (size_t i = 0, L = sass_map_get_length(v); i < L; ++i) { - *m << std::make_pair( - cval_to_astnode(sass_map_get_key(v, i), traces, pstate), - cval_to_astnode(sass_map_get_value(v, i), traces, pstate)); - } - e = m; - } break; - case SASS_NULL: { - e = SASS_MEMORY_NEW(Null, pstate); - } break; - case SASS_ERROR: { - error("Error in C function: " + std::string(sass_error_get_message(v)), pstate, traces); - } break; - case SASS_WARNING: { - error("Warning in C function: " + std::string(sass_warning_get_message(v)), pstate, traces); - } break; - default: break; - } - return e; - } - Selector_List_Ptr Eval::operator()(Selector_List_Ptr s) { SelectorStack rv; diff --git a/src/eval.hpp b/src/eval.hpp index a5895fd89..a0cb97aae 100644 --- a/src/eval.hpp +++ b/src/eval.hpp @@ -98,8 +98,6 @@ namespace Sass { }; - Expression_Ptr cval_to_astnode(union Sass_Value* v, Backtraces traces, ParserState pstate = ParserState("[AST]")); - } #endif diff --git a/src/fn_colors.cpp b/src/fn_colors.cpp index cc4613b6a..642e9d4f4 100644 --- a/src/fn_colors.cpp +++ b/src/fn_colors.cpp @@ -1,3 +1,4 @@ +#include #include #include "ast.hpp" #include "fn_utils.hpp" @@ -738,4 +739,4 @@ namespace Sass { } -} \ No newline at end of file +} diff --git a/src/fn_strings.cpp b/src/fn_strings.cpp index 7949cc4f1..16ed74b34 100644 --- a/src/fn_strings.cpp +++ b/src/fn_strings.cpp @@ -1,4 +1,7 @@ +#include +#include "utf8.h" #include "ast.hpp" +#include "sass.hpp" #include "fn_utils.hpp" #include "fn_strings.hpp" diff --git a/win/libsass.targets b/win/libsass.targets index eb0aca770..62780aeac 100644 --- a/win/libsass.targets +++ b/win/libsass.targets @@ -63,7 +63,8 @@ - + + @@ -124,7 +125,8 @@ - + + diff --git a/win/libsass.vcxproj.filters b/win/libsass.vcxproj.filters index 836b4c298..407fac783 100644 --- a/win/libsass.vcxproj.filters +++ b/win/libsass.vcxproj.filters @@ -198,7 +198,10 @@ Headers - + + Headers + + Headers @@ -383,7 +386,10 @@ Sources - + + Sources + + Sources