Commit 4c086066 by Aaron Leung

Evaluating numeric constants as soon as possible (i.e., at parse time). Should…

Evaluating numeric constants as soon as possible (i.e., at parse time). Should prevent subtle eval bugs later on.
parent 4f2b3015
#include <cstdlib>
#include <iostream> #include <iostream>
#include "document.hpp" #include "document.hpp"
#include "constants.hpp" #include "constants.hpp"
...@@ -907,17 +908,45 @@ namespace Sass { ...@@ -907,17 +908,45 @@ namespace Sass {
if (lex< identifier >()) if (lex< identifier >())
{ return context.new_Node(Node::identifier, path, line, lexed); } { return context.new_Node(Node::identifier, path, line, lexed); }
// if (lex< percentage >())
// { return context.new_Node(Node::textual_percentage, path, line, lexed); }
// if (lex< dimension >())
// { return context.new_Node(Node::textual_dimension, path, line, lexed); }
// if (lex< number >())
// { return context.new_Node(Node::textual_number, path, line, lexed); }
// if (lex< hex >())
// { return context.new_Node(Node::textual_hex, path, line, lexed); }
if (lex< percentage >()) if (lex< percentage >())
{ return context.new_Node(Node::textual_percentage, path, line, lexed); } { return context.new_Node(path, line, atof(lexed.begin), Node::numeric_percentage); }
if (lex< dimension >()) if (lex< dimension >()) {
{ return context.new_Node(Node::textual_dimension, path, line, lexed); } return context.new_Node(path, line, atof(lexed.begin),
Token::make(Prelexer::number(lexed.begin), lexed.end));
}
if (lex< number >()) if (lex< number >())
{ return context.new_Node(Node::textual_number, path, line, lexed); } { return context.new_Node(path, line, atof(lexed.begin)); }
if (lex< hex >()) if (lex< hex >()) {
{ return context.new_Node(Node::textual_hex, path, line, lexed); } Node triple(context.new_Node(Node::numeric_color, path, line, 4));
Token hext(Token::make(lexed.begin+1, lexed.end));
if (hext.length() == 6) {
for (int i = 0; i < 6; i += 2) {
triple << context.new_Node(path, line, static_cast<double>(strtol(string(hext.begin+i, 2).c_str(), NULL, 16)));
}
}
else {
for (int i = 0; i < 3; ++i) {
triple << context.new_Node(path, line, static_cast<double>(strtol(string(2, hext.begin[i]).c_str(), NULL, 16)));
}
}
triple << context.new_Node(path, line, 1.0);
return triple;
}
if (peek< string_constant >()) if (peek< string_constant >())
{ return parse_string(); } { return parse_string(); }
...@@ -989,16 +1018,30 @@ namespace Sass { ...@@ -989,16 +1018,30 @@ namespace Sass {
schema << context.new_Node(Node::identifier, path, line, lexed); schema << context.new_Node(Node::identifier, path, line, lexed);
} }
else if (lex< percentage >()) { else if (lex< percentage >()) {
schema << context.new_Node(Node::textual_percentage, path, line, lexed); schema << context.new_Node(path, line, atof(lexed.begin), Node::numeric_percentage);
} }
else if (lex< dimension >()) { else if (lex< dimension >()) {
schema << context.new_Node(Node::textual_dimension, path, line, lexed); schema << context.new_Node(path, line, atof(lexed.begin),
Token::make(Prelexer::number(lexed.begin), lexed.end));
} }
else if (lex< number >()) { else if (lex< number >()) {
schema << context.new_Node(Node::textual_number, path, line, lexed); schema << context.new_Node(path, line, atof(lexed.begin));
} }
else if (lex< hex >()) { else if (lex< hex >()) {
schema << context.new_Node(Node::textual_hex, path, line, lexed); Node triple(context.new_Node(Node::numeric_color, path, line, 4));
Token hext(Token::make(lexed.begin+1, lexed.end));
if (hext.length() == 6) {
for (int i = 0; i < 6; i += 2) {
triple << context.new_Node(path, line, static_cast<double>(strtol(string(hext.begin+i, 2).c_str(), NULL, 16)));
}
}
else {
for (int i = 0; i < 3; ++i) {
triple << context.new_Node(path, line, static_cast<double>(strtol(string(2, hext.begin[i]).c_str(), NULL, 16)));
}
}
triple << context.new_Node(path, line, 1.0);
schema << triple;
} }
else if (lex< string_constant >()) { else if (lex< string_constant >()) {
schema << context.new_Node(Node::string_constant, path, line, lexed); schema << context.new_Node(Node::string_constant, path, line, lexed);
......
...@@ -230,15 +230,6 @@ namespace Sass { ...@@ -230,15 +230,6 @@ namespace Sass {
expr[i] = eval(expr[i], prefix, env, f_env, new_Node, ctx); expr[i] = eval(expr[i], prefix, env, f_env, new_Node, ctx);
} }
return reduce(expr, 1, expr[0], new_Node); return reduce(expr, 1, expr[0], new_Node);
// Node acc(new_Node(Node::expression, expr.path(), expr.line(), 1));
// acc << eval(expr[0], prefix, env, f_env, new_Node, ctx);
// Node rhs(eval(expr[2], prefix, env, f_env, new_Node, ctx));
// accumulate(expr[1].type(), acc, rhs, new_Node);
// for (size_t i = 3, S = expr.size(); i < S; i += 2) {
// Node rhs(eval(expr[i+1], prefix, env, f_env, new_Node, ctx));
// accumulate(expr[i].type(), acc, rhs, new_Node);
// }
// return acc.size() == 1 ? acc[0] : acc;
} break; } break;
case Node::term: { case Node::term: {
...@@ -247,15 +238,6 @@ namespace Sass { ...@@ -247,15 +238,6 @@ namespace Sass {
expr[i] = eval(expr[i], prefix, env, f_env, new_Node, ctx); expr[i] = eval(expr[i], prefix, env, f_env, new_Node, ctx);
} }
return reduce(expr, 1, expr[0], new_Node); return reduce(expr, 1, expr[0], new_Node);
// Node acc(new_Node(Node::expression, expr.path(), expr.line(), 1));
// acc << eval(expr[0], prefix, env, f_env, new_Node, ctx);
// Node rhs(eval(expr[2], prefix, env, f_env, new_Node, ctx));
// accumulate(expr[1].type(), acc, rhs, new_Node);
// for (size_t i = 3, S = expr.size(); i < S; i += 2) {
// Node rhs(eval(expr[i+1], prefix, env, f_env, new_Node, ctx));
// accumulate(expr[i].type(), acc, rhs, new_Node);
// }
// return acc.size() == 1 ? acc[0] : acc;
} }
else { else {
return expr; return expr;
......
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