Skip to content

Commit b44b06a

Browse files
committed
Add parse_block_comments function to parser
Prepare some parser calls for further optimizations. Only skip over white-space once and not for every op!
1 parent 5e00318 commit b44b06a

File tree

2 files changed

+102
-102
lines changed

2 files changed

+102
-102
lines changed

parser.cpp

Lines changed: 100 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,8 @@ namespace Sass {
6666
lex< optional_spaces >();
6767
Selector_Lookahead lookahead_result;
6868
while (position < end) {
69-
if (lex< block_comment >()) {
70-
bool is_important = lexed.begin[2] == '!';
71-
String* contents = parse_interpolated_chunk(lexed);
72-
Comment* comment = new (ctx.mem) Comment(pstate, contents, is_important);
73-
(*root) << comment;
74-
}
75-
else if (peek< import >()) {
69+
parse_block_comments(root);
70+
if (peek< import >()) {
7671
Import* imp = parse_import();
7772
if (!imp->urls().empty()) (*root) << imp;
7873
if (!imp->files().empty()) {
@@ -483,10 +478,12 @@ namespace Sass {
483478
group->last_block(block_stack.back());
484479
do {
485480
reloop = false;
486-
if (peek< exactly<'{'> >() ||
487-
peek< exactly<'}'> >() ||
488-
peek< exactly<')'> >() ||
489-
peek< exactly<';'> >())
481+
if (peek< alternatives <
482+
exactly<'{'>,
483+
exactly<'}'>,
484+
exactly<')'>,
485+
exactly<';'>
486+
> >())
490487
break; // in case there are superfluous commas at the end
491488
Complex_Selector* comb = parse_selector_combination();
492489
if (!comb->has_reference() && !in_at_root) {
@@ -531,12 +528,13 @@ namespace Sass {
531528
{
532529
Position sel_source_position(-1);
533530
Compound_Selector* lhs;
534-
if (peek< exactly<'+'> >() ||
535-
peek< exactly<'~'> >() ||
536-
peek< exactly<'>'> >()) {
537-
// no selector before the combinator
538-
lhs = 0;
539-
}
531+
if (peek< alternatives <
532+
exactly<'+'>,
533+
exactly<'~'>,
534+
exactly<'>'>
535+
> >())
536+
// no selector before the combinator
537+
{ lhs = 0; }
540538
else {
541539
lhs = parse_simple_selector_sequence();
542540
sel_source_position = before_token;
@@ -551,15 +549,16 @@ namespace Sass {
551549
bool cpx_lf = peek_newline();
552550

553551
Complex_Selector* rhs;
554-
if (peek< exactly<','> >() ||
555-
peek< exactly<')'> >() ||
556-
peek< exactly<'{'> >() ||
557-
peek< exactly<'}'> >() ||
558-
peek< exactly<';'> >() ||
559-
peek< optional >()) {
560-
// no selector after the combinator
561-
rhs = 0;
562-
}
552+
if (peek< alternatives <
553+
exactly<','>,
554+
exactly<')'>,
555+
exactly<'{'>,
556+
exactly<'}'>,
557+
exactly<';'>,
558+
optional
559+
> >())
560+
// no selector after the combinator
561+
{ rhs = 0; }
563562
else {
564563
rhs = parse_selector_combination();
565564
sel_source_position = before_token;
@@ -604,14 +603,15 @@ namespace Sass {
604603
}
605604

606605
while (!peek< spaces >(position) &&
607-
!(peek < exactly<'+'> >(position) ||
608-
peek < exactly<'~'> >(position) ||
609-
peek < exactly<'>'> >(position) ||
610-
peek < exactly<','> >(position) ||
611-
peek < exactly<')'> >(position) ||
612-
peek < exactly<'{'> >(position) ||
613-
peek < exactly<'}'> >(position) ||
614-
peek < exactly<';'> >(position))) {
606+
!(peek < alternatives < exactly<'+'>,
607+
exactly<'~'>,
608+
exactly<'>'>,
609+
exactly<','>,
610+
exactly<')'>,
611+
exactly<'{'>,
612+
exactly<'}'>,
613+
exactly<';'>
614+
> >(position))) {
615615
(*seq) << parse_simple_selector();
616616
}
617617
return seq;
@@ -751,6 +751,16 @@ namespace Sass {
751751
return new (ctx.mem) Attribute_Selector(p, name, matcher, value);
752752
}
753753

754+
/* parse block comment and add to block */
755+
void Parser::parse_block_comments(Block* block)
756+
{
757+
while (lex< block_comment >()) {
758+
bool is_important = lexed.begin[2] == '!';
759+
String* contents = parse_interpolated_chunk(lexed);
760+
(*block) << new (ctx.mem) Comment(pstate, contents, is_important);
761+
}
762+
}
763+
754764
Block* Parser::parse_block()
755765
{
756766
lex< exactly<'{'> >();
@@ -760,33 +770,18 @@ namespace Sass {
760770
block_stack.push_back(block);
761771
lex< zero_plus < alternatives < space, line_comment > > >();
762772
// JMA - ensure that a block containing only block_comments is parsed
763-
while (lex< block_comment >()) {
764-
bool is_important = lexed.begin[2] == '!';
765-
String* contents = parse_interpolated_chunk(lexed);
766-
Comment* comment = new (ctx.mem) Comment(pstate, contents, is_important);
767-
(*block) << comment;
768-
}
773+
parse_block_comments(block);
769774

770775
while (!lex< exactly<'}'> >()) {
776+
parse_block_comments(block);
771777
if (semicolon) {
772778
if (!lex< one_plus< exactly<';'> > >()) {
773779
error("non-terminal statement or declaration must end with ';'", pstate);
774780
}
775781
semicolon = false;
776-
while (lex< block_comment >()) {
777-
bool is_important = lexed.begin[2] == '!';
778-
String* contents = parse_interpolated_chunk(lexed);
779-
Comment* comment = new (ctx.mem) Comment(pstate, contents, is_important);
780-
(*block) << comment;
781-
}
782+
parse_block_comments(block);
782783
if (lex< sequence< exactly<'}'>, zero_plus< exactly<';'> > > >()) break;
783784
}
784-
if (lex< block_comment >()) {
785-
bool is_important = lexed.begin[2] == '!';
786-
String* contents = parse_interpolated_chunk(lexed);
787-
Comment* comment = new (ctx.mem) Comment(pstate, contents, is_important);
788-
(*block) << comment;
789-
}
790785
else if (peek< import >(position)) {
791786
if (stack.back() == mixin_def || stack.back() == function_def) {
792787
lex< import >(); // to adjust the before_token number
@@ -922,12 +917,7 @@ namespace Sass {
922917
}
923918
}
924919
else lex< one_plus< exactly<';'> > >();
925-
while (lex< block_comment >()) {
926-
bool is_important = lexed.begin[2] == '!';
927-
String* contents = parse_interpolated_chunk(lexed);
928-
Comment* comment = new (ctx.mem) Comment(pstate, contents, is_important);
929-
(*block) << comment;
930-
}
920+
parse_block_comments(block);
931921
}
932922
block_stack.pop_back();
933923
return block;
@@ -1018,13 +1008,15 @@ namespace Sass {
10181008

10191009
Expression* Parser::parse_comma_list()
10201010
{
1021-
if (//peek< exactly<'!'> >(position) ||
1022-
peek< exactly<';'> >(position) ||
1023-
peek< exactly<'}'> >(position) ||
1024-
peek< exactly<'{'> >(position) ||
1025-
peek< exactly<')'> >(position) ||
1026-
//peek< exactly<':'> >(position) ||
1027-
peek< exactly<ellipsis> >(position))
1011+
if (peek< alternatives <
1012+
// exactly<'!'>,
1013+
// exactly<':'>,
1014+
exactly<';'>,
1015+
exactly<'}'>,
1016+
exactly<'{'>,
1017+
exactly<')'>,
1018+
exactly<ellipsis>
1019+
> >(position))
10281020
{ return new (ctx.mem) List(pstate, 0); }
10291021
Expression* list1 = parse_space_list();
10301022
// if it's a singleton, return it directly; don't wrap it
@@ -1035,15 +1027,16 @@ namespace Sass {
10351027

10361028
while (lex< exactly<','> >())
10371029
{
1038-
if (//peek< exactly<'!'> >(position) ||
1039-
peek< exactly<';'> >(position) ||
1040-
peek< exactly<'}'> >(position) ||
1041-
peek< exactly<'{'> >(position) ||
1042-
peek< exactly<')'> >(position) ||
1043-
peek< exactly<':'> >(position) ||
1044-
peek< exactly<ellipsis> >(position)) {
1045-
break;
1046-
}
1030+
if (peek< alternatives <
1031+
// exactly<'!'>,
1032+
exactly<';'>,
1033+
exactly<'}'>,
1034+
exactly<'{'>,
1035+
exactly<')'>,
1036+
exactly<':'>,
1037+
exactly<ellipsis>
1038+
> >(position)
1039+
) { break; }
10471040
Expression* list = parse_space_list();
10481041
(*comma_list) << list;
10491042
}
@@ -1055,32 +1048,36 @@ namespace Sass {
10551048
{
10561049
Expression* disj1 = parse_disjunction();
10571050
// if it's a singleton, return it directly; don't wrap it
1058-
if (//peek< exactly<'!'> >(position) ||
1059-
peek< exactly<';'> >(position) ||
1060-
peek< exactly<'}'> >(position) ||
1061-
peek< exactly<'{'> >(position) ||
1062-
peek< exactly<')'> >(position) ||
1063-
peek< exactly<','> >(position) ||
1064-
peek< exactly<':'> >(position) ||
1065-
peek< exactly<ellipsis> >(position) ||
1066-
peek< default_flag >(position) ||
1067-
peek< global_flag >(position))
1068-
{ return disj1; }
1051+
if (peek< alternatives <
1052+
// exactly<'!'>,
1053+
exactly<';'>,
1054+
exactly<'}'>,
1055+
exactly<'{'>,
1056+
exactly<')'>,
1057+
exactly<','>,
1058+
exactly<':'>,
1059+
exactly<ellipsis>,
1060+
default_flag,
1061+
global_flag
1062+
> >(position)
1063+
) { return disj1; }
10691064

10701065
List* space_list = new (ctx.mem) List(pstate, 2, List::SPACE);
10711066
(*space_list) << disj1;
10721067

1073-
while (!(//peek< exactly<'!'> >(position) ||
1074-
peek< exactly<';'> >(position) ||
1075-
peek< exactly<'}'> >(position) ||
1076-
peek< exactly<'{'> >(position) ||
1077-
peek< exactly<')'> >(position) ||
1078-
peek< exactly<','> >(position) ||
1079-
peek< exactly<':'> >(position) ||
1080-
peek< exactly<ellipsis> >(position) ||
1081-
peek< default_flag >(position) ||
1082-
peek< global_flag >(position)))
1083-
{
1068+
while (!(peek< alternatives <
1069+
// exactly<'!'>,
1070+
exactly<';'>,
1071+
exactly<'}'>,
1072+
exactly<'{'>,
1073+
exactly<')'>,
1074+
exactly<','>,
1075+
exactly<':'>,
1076+
exactly<ellipsis>,
1077+
default_flag,
1078+
global_flag
1079+
> >(position))
1080+
) {
10841081
(*space_list) << parse_disjunction();
10851082
}
10861083

@@ -1117,12 +1114,13 @@ namespace Sass {
11171114
{
11181115
Expression* expr1 = parse_expression();
11191116
// if it's a singleton, return it directly; don't wrap it
1120-
if (!(peek< eq_op >(position) ||
1121-
peek< neq_op >(position) ||
1122-
peek< gte_op >(position) ||
1123-
peek< gt_op >(position) ||
1124-
peek< lte_op >(position) ||
1125-
peek< lt_op >(position)))
1117+
if (!(peek< alternatives < eq_op,
1118+
neq_op,
1119+
gte_op,
1120+
gt_op,
1121+
lte_op,
1122+
lt_op
1123+
> >(position)))
11261124
{ return expr1; }
11271125

11281126
Binary_Expression::Type op

parser.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ namespace Sass {
283283
Error* parse_error();
284284
Debug* parse_debug();
285285

286+
void parse_block_comments(Block* block);
287+
286288
Selector_Lookahead lookahead_for_selector(const char* start = 0);
287289
Selector_Lookahead lookahead_for_extension_target(const char* start = 0);
288290

0 commit comments

Comments
 (0)