Skip to content

Commit

Permalink
Implement number prefix parsing (/[\+\-]+/)
Browse files Browse the repository at this point in the history
Fixes #535
  • Loading branch information
mgreter committed Mar 9, 2015
1 parent c64f228 commit 6f360d2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,22 @@ namespace Sass {
}
}

// parse +/- and return false if negative
bool Parser::parse_number_prefix()
{
bool positive = true;
while(true) {
if (lex < block_comment >()) continue;
if (lex < number_prefix >()) continue;
if (lex < exactly < '-' > >()) {
positive = !positive;
continue;
}
break;
}
return positive;
}

Expression* Parser::parse_map()
{
To_String to_string(&ctx);
Expand Down Expand Up @@ -1203,6 +1219,10 @@ namespace Sass {
else if (lex< sequence< not_op, spaces_and_comments > >()) {
return new (ctx.mem) Unary_Expression(pstate, Unary_Expression::NOT, parse_factor());
}
else if (peek < sequence < one_plus < alternatives < spaces_and_comments, exactly<'-'>, exactly<'+'> > >, number > >()) {
if (parse_number_prefix()) return parse_value(); // prefix is positive
return new (ctx.mem) Unary_Expression(pstate, Unary_Expression::MINUS, parse_value());
}
else {
return parse_value();
}
Expand Down
1 change: 1 addition & 0 deletions parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ namespace Sass {
Simple_Selector* parse_pseudo_selector();
Attribute_Selector* parse_attribute_selector();
Block* parse_block();
bool parse_number_prefix();
Declaration* parse_declaration();
Expression* parse_map_value();
Expression* parse_map();
Expand Down
12 changes: 12 additions & 0 deletions prelexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ namespace Sass {
return sequence< exactly<'-'>, exactly<'-'>, identifier >(src);
}

// Match number prefix ([\+\-]+)
const char* number_prefix(const char* src) {
return alternatives <
exactly < '+' >,
sequence <
exactly < '-' >,
optional_spaces_and_comments,
exactly< '-' >
>
>(src);
}

// Match interpolant schemas
const char* identifier_schema(const char* src) {
// follows this pattern: (x*ix*)+ ... well, not quite
Expand Down
2 changes: 2 additions & 0 deletions prelexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ namespace Sass {
const char* quoted_string(const char* src);
// Match interpolants.
const char* interpolant(const char* src);
// Match number prefix ([\+\-]+)
const char* number_prefix(const char* src);

// Whitespace handling.
const char* optional_spaces(const char* src);
Expand Down

0 comments on commit 6f360d2

Please sign in to comment.