Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.

Commit ba11a13

Browse files
author
Aaron Leung
committed
Parsing @if directives.
1 parent f1f1d45 commit ba11a13

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

document.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ namespace Sass {
160160
Node parse_function_call();
161161
Node parse_string();
162162
Node parse_value_schema();
163+
Node parse_if_directive(Node surrounding_ruleset);
164+
163165

164166
Selector_Lookahead lookahead_for_selector(const char* start = 0);
165167

document_parser.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ namespace Sass {
3636
root << parse_mixin_call();
3737
if (!lex< exactly<';'> >()) throw_syntax_error("top-level @include directive must be terminated by ';'");
3838
}
39+
else if (peek< if_directive >()) {
40+
root << parse_if_directive(Node());
41+
}
3942
else {
4043
lex< spaces_and_comments >();
4144
throw_syntax_error("invalid top-level expression");
@@ -477,13 +480,16 @@ namespace Sass {
477480
semicolon = true;
478481
}
479482
else if (lex< extend >()) {
483+
if (surrounding_ruleset.is_null_ptr()) throw_syntax_error("@extend directive may only be used within rules");
480484
Node extendee(parse_simple_selector_sequence());
481-
// context.extensions[extendee] = surrounding_ruleset;
482485
context.extensions.insert(pair<Node, Node>(extendee, surrounding_ruleset));
483486
cerr << "PARSED EXTENSION REQUEST: " << surrounding_ruleset[0].to_string() << " EXTENDS " << extendee.to_string() << endl;
484487
context.has_extensions = true;
485488
semicolon = true;
486489
}
490+
else if (peek< if_directive >()) {
491+
block << parse_if_directive(surrounding_ruleset);
492+
}
487493
else if (!peek< exactly<';'> >()) {
488494
Node rule(parse_rule());
489495
// check for lbrace; if it's there, we have a namespace property with a value
@@ -871,8 +877,28 @@ namespace Sass {
871877
call.should_eval() = true;
872878
return call;
873879
}
880+
881+
Node Document::parse_if_directive(Node surrounding_ruleset)
882+
{
883+
lex< if_directive >();
884+
Node conditional(context.new_Node(Node::if_directive, path, line, 2));
885+
conditional << parse_list(); // the predicate
886+
if (!lex< exactly<'{'> >()) throw_syntax_error("expected '{' after the predicate for @if");
887+
conditional << parse_block(surrounding_ruleset); // the consequent
888+
// collect all "@else if"s
889+
while (lex< elseif_directive >()) {
890+
conditional << parse_list(); // the next predicate
891+
if (!lex< exactly<'{'> >()) throw_syntax_error("expected '{' after the predicate for @else if");
892+
conditional << parse_block(surrounding_ruleset); // the next consequent
893+
}
894+
// parse the "@else" if present
895+
if (lex< else_directive >()) {
896+
if (!lex< exactly<'{'> >()) throw_syntax_error("expected '{' after @else");
897+
conditional << parse_block(surrounding_ruleset); // the alternative
898+
}
899+
return conditional;
900+
}
874901

875-
// const char* Document::lookahead_for_selector(const char* start)
876902
Selector_Lookahead Document::lookahead_for_selector(const char* start)
877903
{
878904
const char* p = start ? start : position;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy