forked from sass/node-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cpp
3133 lines (2813 loc) · 106 KB
/
parser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "sass.hpp"
#include "parser.hpp"
#include "file.hpp"
#include "inspect.hpp"
#include "constants.hpp"
#include "util.hpp"
#include "prelexer.hpp"
#include "color_maps.hpp"
#include "sass/functions.h"
#include "error_handling.hpp"
// Notes about delayed: some ast nodes can have delayed evaluation so
// they can preserve their original semantics if needed. This is most
// prominently exhibited by the division operation, since it is not
// only a valid operation, but also a valid css statement (i.e. for
// fonts, as in `16px/24px`). When parsing lists and expression we
// unwrap single items from lists and other operations. A nested list
// must not be delayed, only the items of the first level sometimes
// are delayed (as with argument lists). To achieve this we need to
// pass status to the list parser, so this can be set correctly.
// Another case with delayed values are colors. In compressed mode
// only processed values get compressed (other are left as written).
#include <cstdlib>
#include <iostream>
#include <vector>
#include <typeinfo>
namespace Sass {
using namespace Constants;
using namespace Prelexer;
Parser Parser::from_c_str(const char* beg, Context& ctx, Backtraces traces, ParserState pstate, const char* source)
{
pstate.offset.column = 0;
pstate.offset.line = 0;
Parser p(ctx, pstate, traces);
p.source = source ? source : beg;
p.position = beg ? beg : p.source;
p.end = p.position + strlen(p.position);
Block_Obj root = SASS_MEMORY_NEW(Block, pstate);
p.block_stack.push_back(root);
root->is_root(true);
return p;
}
Parser Parser::from_c_str(const char* beg, const char* end, Context& ctx, Backtraces traces, ParserState pstate, const char* source)
{
pstate.offset.column = 0;
pstate.offset.line = 0;
Parser p(ctx, pstate, traces);
p.source = source ? source : beg;
p.position = beg ? beg : p.source;
p.end = end ? end : p.position + strlen(p.position);
Block_Obj root = SASS_MEMORY_NEW(Block, pstate);
p.block_stack.push_back(root);
root->is_root(true);
return p;
}
void Parser::advanceToNextToken() {
lex < css_comments >(false);
// advance to position
pstate += pstate.offset;
pstate.offset.column = 0;
pstate.offset.line = 0;
}
Selector_List_Obj Parser::parse_selector(const char* beg, Context& ctx, Backtraces traces, ParserState pstate, const char* source)
{
Parser p = Parser::from_c_str(beg, ctx, traces, pstate, source);
// ToDo: ruby sass errors on parent references
// ToDo: remap the source-map entries somehow
return p.parse_selector_list(false);
}
bool Parser::peek_newline(const char* start)
{
return peek_linefeed(start ? start : position)
&& ! peek_css<exactly<'{'>>(start);
}
Parser Parser::from_token(Token t, Context& ctx, Backtraces traces, ParserState pstate, const char* source)
{
Parser p(ctx, pstate, traces);
p.source = source ? source : t.begin;
p.position = t.begin ? t.begin : p.source;
p.end = t.end ? t.end : p.position + strlen(p.position);
Block_Obj root = SASS_MEMORY_NEW(Block, pstate);
p.block_stack.push_back(root);
root->is_root(true);
return p;
}
/* main entry point to parse root block */
Block_Obj Parser::parse()
{
// consume unicode BOM
read_bom();
// scan the input to find invalid utf8 sequences
const char* it = utf8::find_invalid(position, end);
// report invalid utf8
if (it != end) {
pstate += Offset::init(position, it);
traces.push_back(Backtrace(pstate));
throw Exception::InvalidSass(pstate, traces, "Invalid UTF-8 sequence");
}
// create a block AST node to hold children
Block_Obj root = SASS_MEMORY_NEW(Block, pstate, 0, true);
// check seems a bit esoteric but works
if (ctx.resources.size() == 1) {
// apply headers only on very first include
ctx.apply_custom_headers(root, path, pstate);
}
// parse children nodes
block_stack.push_back(root);
parse_block_nodes(true);
block_stack.pop_back();
// update final position
root->update_pstate(pstate);
if (position != end) {
css_error("Invalid CSS", " after ", ": expected selector or at-rule, was ");
}
return root;
}
// convenience function for block parsing
// will create a new block ad-hoc for you
// this is the base block parsing function
Block_Obj Parser::parse_css_block(bool is_root)
{
// parse comments before block
// lex < optional_css_comments >();
// lex mandatory opener or error out
if (!lex_css < exactly<'{'> >()) {
css_error("Invalid CSS", " after ", ": expected \"{\", was ");
}
// create new block and push to the selector stack
Block_Obj block = SASS_MEMORY_NEW(Block, pstate, 0, is_root);
block_stack.push_back(block);
if (!parse_block_nodes(is_root)) css_error("Invalid CSS", " after ", ": expected \"}\", was ");
if (!lex_css < exactly<'}'> >()) {
css_error("Invalid CSS", " after ", ": expected \"}\", was ");
}
// update for end position
// this seems to be done somewhere else
// but that fixed selector schema issue
// block->update_pstate(pstate);
// parse comments after block
// lex < optional_css_comments >();
block_stack.pop_back();
return block;
}
// convenience function for block parsing
// will create a new block ad-hoc for you
// also updates the `in_at_root` flag
Block_Obj Parser::parse_block(bool is_root)
{
return parse_css_block(is_root);
}
// the main block parsing function
// parses stuff between `{` and `}`
bool Parser::parse_block_nodes(bool is_root)
{
// loop until end of string
while (position < end) {
// we should be able to refactor this
parse_block_comments();
lex < css_whitespace >();
if (lex < exactly<';'> >()) continue;
if (peek < end_of_file >()) return true;
if (peek < exactly<'}'> >()) return true;
if (parse_block_node(is_root)) continue;
parse_block_comments();
if (lex_css < exactly<';'> >()) continue;
if (peek_css < end_of_file >()) return true;
if (peek_css < exactly<'}'> >()) return true;
// illegal sass
return false;
}
// return success
return true;
}
// parser for a single node in a block
// semicolons must be lexed beforehand
bool Parser::parse_block_node(bool is_root) {
Block_Obj block = block_stack.back();
parse_block_comments();
// throw away white-space
// includes line comments
lex < css_whitespace >();
Lookahead lookahead_result;
// also parse block comments
// first parse everything that is allowed in functions
if (lex < variable >(true)) { block->append(parse_assignment()); }
else if (lex < kwd_err >(true)) { block->append(parse_error()); }
else if (lex < kwd_dbg >(true)) { block->append(parse_debug()); }
else if (lex < kwd_warn >(true)) { block->append(parse_warning()); }
else if (lex < kwd_if_directive >(true)) { block->append(parse_if_directive()); }
else if (lex < kwd_for_directive >(true)) { block->append(parse_for_directive()); }
else if (lex < kwd_each_directive >(true)) { block->append(parse_each_directive()); }
else if (lex < kwd_while_directive >(true)) { block->append(parse_while_directive()); }
else if (lex < kwd_return_directive >(true)) { block->append(parse_return_directive()); }
// parse imports to process later
else if (lex < kwd_import >(true)) {
Scope parent = stack.empty() ? Scope::Rules : stack.back();
if (parent != Scope::Function && parent != Scope::Root && parent != Scope::Rules && parent != Scope::Media) {
if (! peek_css< uri_prefix >(position)) { // this seems to go in ruby sass 3.4.20
error("Import directives may not be used within control directives or mixins.");
}
}
// this puts the parsed doc into sheets
// import stub will fetch this in expand
Import_Obj imp = parse_import();
// if it is a url, we only add the statement
if (!imp->urls().empty()) block->append(imp);
// process all resources now (add Import_Stub nodes)
for (size_t i = 0, S = imp->incs().size(); i < S; ++i) {
block->append(SASS_MEMORY_NEW(Import_Stub, pstate, imp->incs()[i]));
}
}
else if (lex < kwd_extend >(true)) {
Lookahead lookahead = lookahead_for_include(position);
if (!lookahead.found) css_error("Invalid CSS", " after ", ": expected selector, was ");
Selector_List_Obj target;
if (!lookahead.has_interpolants) {
target = parse_selector_list(true);
}
else {
target = SASS_MEMORY_NEW(Selector_List, pstate);
target->schema(parse_selector_schema(lookahead.found, true));
}
block->append(SASS_MEMORY_NEW(Extension, pstate, target));
}
// selector may contain interpolations which need delayed evaluation
else if (
!(lookahead_result = lookahead_for_selector(position)).error &&
!lookahead_result.is_custom_property
)
{
block->append(parse_ruleset(lookahead_result));
}
// parse multiple specific keyword directives
else if (lex < kwd_media >(true)) { block->append(parse_media_block()); }
else if (lex < kwd_at_root >(true)) { block->append(parse_at_root_block()); }
else if (lex < kwd_include_directive >(true)) { block->append(parse_include_directive()); }
else if (lex < kwd_content_directive >(true)) { block->append(parse_content_directive()); }
else if (lex < kwd_supports_directive >(true)) { block->append(parse_supports_directive()); }
else if (lex < kwd_mixin >(true)) { block->append(parse_definition(Definition::MIXIN)); }
else if (lex < kwd_function >(true)) { block->append(parse_definition(Definition::FUNCTION)); }
// ignore the @charset directive for now
else if (lex< kwd_charset_directive >(true)) { parse_charset_directive(); }
// generic at keyword (keep last)
else if (lex< re_special_directive >(true)) { block->append(parse_special_directive()); }
else if (lex< re_prefixed_directive >(true)) { block->append(parse_prefixed_directive()); }
else if (lex< at_keyword >(true)) { block->append(parse_directive()); }
else if (is_root && stack.back() != Scope::AtRoot /* && block->is_root() */) {
lex< css_whitespace >();
if (position >= end) return true;
css_error("Invalid CSS", " after ", ": expected 1 selector or at-rule, was ");
}
// parse a declaration
else
{
// ToDo: how does it handle parse errors?
// maybe we are expected to parse something?
Declaration_Obj decl = parse_declaration();
decl->tabs(indentation);
block->append(decl);
// maybe we have a "sub-block"
if (peek< exactly<'{'> >()) {
if (decl->is_indented()) ++ indentation;
// parse a propset that rides on the declaration's property
stack.push_back(Scope::Properties);
decl->block(parse_block());
stack.pop_back();
if (decl->is_indented()) -- indentation;
}
}
// something matched
return true;
}
// EO parse_block_nodes
// parse imports inside the
Import_Obj Parser::parse_import()
{
Import_Obj imp = SASS_MEMORY_NEW(Import, pstate);
std::vector<std::pair<std::string,Function_Call_Obj>> to_import;
bool first = true;
do {
while (lex< block_comment >());
if (lex< quoted_string >()) {
to_import.push_back(std::pair<std::string,Function_Call_Obj>(std::string(lexed), 0));
}
else if (lex< uri_prefix >()) {
Arguments_Obj args = SASS_MEMORY_NEW(Arguments, pstate);
Function_Call_Obj result = SASS_MEMORY_NEW(Function_Call, pstate, "url", args);
if (lex< quoted_string >()) {
Expression_Obj quoted_url = parse_string();
args->append(SASS_MEMORY_NEW(Argument, quoted_url->pstate(), quoted_url));
}
else if (String_Obj string_url = parse_url_function_argument()) {
args->append(SASS_MEMORY_NEW(Argument, string_url->pstate(), string_url));
}
else if (peek < skip_over_scopes < exactly < '(' >, exactly < ')' > > >(position)) {
Expression_Obj braced_url = parse_list(); // parse_interpolated_chunk(lexed);
args->append(SASS_MEMORY_NEW(Argument, braced_url->pstate(), braced_url));
}
else {
error("malformed URL");
}
if (!lex< exactly<')'> >()) error("URI is missing ')'");
to_import.push_back(std::pair<std::string, Function_Call_Obj>("", result));
}
else {
if (first) error("@import directive requires a url or quoted path");
else error("expecting another url or quoted path in @import list");
}
first = false;
} while (lex_css< exactly<','> >());
if (!peek_css< alternatives< exactly<';'>, exactly<'}'>, end_of_file > >()) {
List_Obj import_queries = parse_media_queries();
imp->import_queries(import_queries);
}
for(auto location : to_import) {
if (location.second) {
imp->urls().push_back(location.second);
}
// check if custom importers want to take over the handling
else if (!ctx.call_importers(unquote(location.first), path, pstate, imp)) {
// nobody wants it, so we do our import
ctx.import_url(imp, location.first, path);
}
}
return imp;
}
Definition_Obj Parser::parse_definition(Definition::Type which_type)
{
std::string which_str(lexed);
if (!lex< identifier >()) error("invalid name in " + which_str + " definition");
std::string name(Util::normalize_underscores(lexed));
if (which_type == Definition::FUNCTION && (name == "and" || name == "or" || name == "not"))
{ error("Invalid function name \"" + name + "\"."); }
ParserState source_position_of_def = pstate;
Parameters_Obj params = parse_parameters();
if (which_type == Definition::MIXIN) stack.push_back(Scope::Mixin);
else stack.push_back(Scope::Function);
Block_Obj body = parse_block();
stack.pop_back();
return SASS_MEMORY_NEW(Definition, source_position_of_def, name, params, body, which_type);
}
Parameters_Obj Parser::parse_parameters()
{
Parameters_Obj params = SASS_MEMORY_NEW(Parameters, pstate);
if (lex_css< exactly<'('> >()) {
// if there's anything there at all
if (!peek_css< exactly<')'> >()) {
do {
if (peek< exactly<')'> >()) break;
params->append(parse_parameter());
} while (lex_css< exactly<','> >());
}
if (!lex_css< exactly<')'> >()) {
css_error("Invalid CSS", " after ", ": expected \")\", was ");
}
}
return params;
}
Parameter_Obj Parser::parse_parameter()
{
if (peek< alternatives< exactly<','>, exactly< '{' >, exactly<';'> > >()) {
css_error("Invalid CSS", " after ", ": expected variable (e.g. $foo), was ");
}
while (lex< alternatives < spaces, block_comment > >());
lex < variable >();
std::string name(Util::normalize_underscores(lexed));
ParserState pos = pstate;
Expression_Obj val;
bool is_rest = false;
while (lex< alternatives < spaces, block_comment > >());
if (lex< exactly<':'> >()) { // there's a default value
while (lex< block_comment >());
val = parse_space_list();
}
else if (lex< exactly< ellipsis > >()) {
is_rest = true;
}
return SASS_MEMORY_NEW(Parameter, pos, name, val, is_rest);
}
Arguments_Obj Parser::parse_arguments()
{
Arguments_Obj args = SASS_MEMORY_NEW(Arguments, pstate);
if (lex_css< exactly<'('> >()) {
// if there's anything there at all
if (!peek_css< exactly<')'> >()) {
do {
if (peek< exactly<')'> >()) break;
args->append(parse_argument());
} while (lex_css< exactly<','> >());
}
if (!lex_css< exactly<')'> >()) {
css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
}
}
return args;
}
Argument_Obj Parser::parse_argument()
{
if (peek< alternatives< exactly<','>, exactly< '{' >, exactly<';'> > >()) {
css_error("Invalid CSS", " after ", ": expected \")\", was ");
}
if (peek_css< sequence < exactly< hash_lbrace >, exactly< rbrace > > >()) {
position += 2;
css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
}
Argument_Obj arg;
if (peek_css< sequence < variable, optional_css_comments, exactly<':'> > >()) {
lex_css< variable >();
std::string name(Util::normalize_underscores(lexed));
ParserState p = pstate;
lex_css< exactly<':'> >();
Expression_Obj val = parse_space_list();
arg = SASS_MEMORY_NEW(Argument, p, val, name);
}
else {
bool is_arglist = false;
bool is_keyword = false;
Expression_Obj val = parse_space_list();
List_Ptr l = Cast<List>(val);
if (lex_css< exactly< ellipsis > >()) {
if (val->concrete_type() == Expression::MAP || (
(l != NULL && l->separator() == SASS_HASH)
)) is_keyword = true;
else is_arglist = true;
}
arg = SASS_MEMORY_NEW(Argument, pstate, val, "", is_arglist, is_keyword);
}
return arg;
}
Assignment_Obj Parser::parse_assignment()
{
std::string name(Util::normalize_underscores(lexed));
ParserState var_source_position = pstate;
if (!lex< exactly<':'> >()) error("expected ':' after " + name + " in assignment statement");
if (peek_css< alternatives < exactly<';'>, end_of_file > >()) {
css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
}
Expression_Obj val;
Lookahead lookahead = lookahead_for_value(position);
if (lookahead.has_interpolants && lookahead.found) {
val = parse_value_schema(lookahead.found);
} else {
val = parse_list();
}
bool is_default = false;
bool is_global = false;
while (peek< alternatives < default_flag, global_flag > >()) {
if (lex< default_flag >()) is_default = true;
else if (lex< global_flag >()) is_global = true;
}
return SASS_MEMORY_NEW(Assignment, var_source_position, name, val, is_default, is_global);
}
// a ruleset connects a selector and a block
Ruleset_Obj Parser::parse_ruleset(Lookahead lookahead)
{
NESTING_GUARD(nestings);
// inherit is_root from parent block
Block_Obj parent = block_stack.back();
bool is_root = parent && parent->is_root();
// make sure to move up the the last position
lex < optional_css_whitespace >(false, true);
// create the connector object (add parts later)
Ruleset_Obj ruleset = SASS_MEMORY_NEW(Ruleset, pstate);
// parse selector static or as schema to be evaluated later
if (lookahead.parsable) ruleset->selector(parse_selector_list(false));
else {
Selector_List_Obj list = SASS_MEMORY_NEW(Selector_List, pstate);
list->schema(parse_selector_schema(lookahead.position, false));
ruleset->selector(list);
}
// then parse the inner block
stack.push_back(Scope::Rules);
ruleset->block(parse_block());
stack.pop_back();
// update for end position
ruleset->update_pstate(pstate);
ruleset->block()->update_pstate(pstate);
// need this info for sanity checks
ruleset->is_root(is_root);
// return AST Node
return ruleset;
}
// parse a selector schema that will be evaluated in the eval stage
// uses a string schema internally to do the actual schema handling
// in the eval stage we will be re-parse it into an actual selector
Selector_Schema_Obj Parser::parse_selector_schema(const char* end_of_selector, bool chroot)
{
NESTING_GUARD(nestings);
// move up to the start
lex< optional_spaces >();
const char* i = position;
// selector schema re-uses string schema implementation
String_Schema_Ptr schema = SASS_MEMORY_NEW(String_Schema, pstate);
// the selector schema is pretty much just a wrapper for the string schema
Selector_Schema_Obj selector_schema = SASS_MEMORY_NEW(Selector_Schema, pstate, schema);
selector_schema->connect_parent(chroot == false);
selector_schema->media_block(last_media_block);
// process until end
while (i < end_of_selector) {
// try to parse mutliple interpolants
if (const char* p = find_first_in_interval< exactly<hash_lbrace>, block_comment >(i, end_of_selector)) {
// accumulate the preceding segment if the position has advanced
if (i < p) {
std::string parsed(i, p);
String_Constant_Obj str = SASS_MEMORY_NEW(String_Constant, pstate, parsed);
pstate += Offset(parsed);
str->update_pstate(pstate);
schema->append(str);
}
// skip over all nested inner interpolations up to our own delimiter
const char* j = skip_over_scopes< exactly<hash_lbrace>, exactly<rbrace> >(p + 2, end_of_selector);
// check if the interpolation never ends of only contains white-space (error out)
if (!j || peek < sequence < optional_spaces, exactly<rbrace> > >(p+2)) {
position = p+2;
css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
}
// pass inner expression to the parser to resolve nested interpolations
pstate.add(p, p+2);
Expression_Obj interpolant = Parser::from_c_str(p+2, j, ctx, traces, pstate).parse_list();
// set status on the list expression
interpolant->is_interpolant(true);
// schema->has_interpolants(true);
// add to the string schema
schema->append(interpolant);
// advance parser state
pstate.add(p+2, j);
// advance position
i = j;
}
// no more interpolants have been found
// add the last segment if there is one
else {
// make sure to add the last bits of the string up to the end (if any)
if (i < end_of_selector) {
std::string parsed(i, end_of_selector);
String_Constant_Obj str = SASS_MEMORY_NEW(String_Constant, pstate, parsed);
pstate += Offset(parsed);
str->update_pstate(pstate);
i = end_of_selector;
schema->append(str);
}
// exit loop
}
}
// EO until eos
// update position
position = i;
// update for end position
selector_schema->update_pstate(pstate);
schema->update_pstate(pstate);
after_token = before_token = pstate;
// return parsed result
return selector_schema.detach();
}
// EO parse_selector_schema
void Parser::parse_charset_directive()
{
lex <
sequence <
quoted_string,
optional_spaces,
exactly <';'>
>
>();
}
// called after parsing `kwd_include_directive`
Mixin_Call_Obj Parser::parse_include_directive()
{
// lex identifier into `lexed` var
lex_identifier(); // may error out
// normalize underscores to hyphens
std::string name(Util::normalize_underscores(lexed));
// create the initial mixin call object
Mixin_Call_Obj call = SASS_MEMORY_NEW(Mixin_Call, pstate, name, 0, 0);
// parse mandatory arguments
call->arguments(parse_arguments());
// parse optional block
if (peek < exactly <'{'> >()) {
call->block(parse_block());
}
// return ast node
return call.detach();
}
// EO parse_include_directive
// parse a list of complex selectors
// this is the main entry point for most
Selector_List_Obj Parser::parse_selector_list(bool chroot)
{
bool reloop;
bool had_linefeed = false;
NESTING_GUARD(nestings);
Complex_Selector_Obj sel;
Selector_List_Obj group = SASS_MEMORY_NEW(Selector_List, pstate);
group->media_block(last_media_block);
if (peek_css< alternatives < end_of_file, exactly <'{'>, exactly <','> > >()) {
css_error("Invalid CSS", " after ", ": expected selector, was ");
}
do {
reloop = false;
had_linefeed = had_linefeed || peek_newline();
if (peek_css< alternatives < class_char < selector_list_delims > > >())
break; // in case there are superfluous commas at the end
// now parse the complex selector
sel = parse_complex_selector(chroot);
if (!sel) return group.detach();
sel->has_line_feed(had_linefeed);
had_linefeed = false;
while (peek_css< exactly<','> >())
{
lex< css_comments >(false);
// consume everything up and including the comma separator
reloop = lex< exactly<','> >() != 0;
// remember line break (also between some commas)
had_linefeed = had_linefeed || peek_newline();
// remember line break (also between some commas)
}
group->append(sel);
}
while (reloop);
while (lex_css< kwd_optional >()) {
group->is_optional(true);
}
// update for end position
group->update_pstate(pstate);
if (sel) sel->last()->has_line_break(false);
return group.detach();
}
// EO parse_selector_list
// a complex selector combines a compound selector with another
// complex selector, with one of four combinator operations.
// the compound selector (head) is optional, since the combinator
// can come first in the whole selector sequence (like `> DIV').
Complex_Selector_Obj Parser::parse_complex_selector(bool chroot)
{
NESTING_GUARD(nestings);
String_Obj reference = 0;
lex < block_comment >();
advanceToNextToken();
Complex_Selector_Obj sel = SASS_MEMORY_NEW(Complex_Selector, pstate);
if (peek < end_of_file >()) return 0;
// parse the left hand side
Compound_Selector_Obj lhs;
// special case if it starts with combinator ([+~>])
if (!peek_css< class_char < selector_combinator_ops > >()) {
// parse the left hand side
lhs = parse_compound_selector();
}
// parse combinator between lhs and rhs
Complex_Selector::Combinator combinator = Complex_Selector::ANCESTOR_OF;
if (lex< exactly<'+'> >()) combinator = Complex_Selector::ADJACENT_TO;
else if (lex< exactly<'~'> >()) combinator = Complex_Selector::PRECEDES;
else if (lex< exactly<'>'> >()) combinator = Complex_Selector::PARENT_OF;
else if (lex< sequence < exactly<'/'>, negate < exactly < '*' > > > >()) {
// comments are allowed, but not spaces?
combinator = Complex_Selector::REFERENCE;
if (!lex < re_reference_combinator >()) return 0;
reference = SASS_MEMORY_NEW(String_Constant, pstate, lexed);
if (!lex < exactly < '/' > >()) return 0; // ToDo: error msg?
}
if (!lhs && combinator == Complex_Selector::ANCESTOR_OF) return 0;
// lex < block_comment >();
sel->head(lhs);
sel->combinator(combinator);
sel->media_block(last_media_block);
if (combinator == Complex_Selector::REFERENCE) sel->reference(reference);
// has linfeed after combinator?
sel->has_line_break(peek_newline());
// sel->has_line_feed(has_line_feed);
// check if we got the abort condition (ToDo: optimize)
if (!peek_css< class_char < complex_selector_delims > >()) {
// parse next selector in sequence
sel->tail(parse_complex_selector(true));
}
// add a parent selector if we are not in a root
// also skip adding parent ref if we only have refs
if (!sel->has_parent_ref() && !chroot) {
// create the objects to wrap parent selector reference
Compound_Selector_Obj head = SASS_MEMORY_NEW(Compound_Selector, pstate);
Parent_Selector_Ptr parent = SASS_MEMORY_NEW(Parent_Selector, pstate, false);
parent->media_block(last_media_block);
head->media_block(last_media_block);
// add simple selector
head->append(parent);
// selector may not have any head yet
if (!sel->head()) { sel->head(head); }
// otherwise we need to create a new complex selector and set the old one as its tail
else {
sel = SASS_MEMORY_NEW(Complex_Selector, pstate, Complex_Selector::ANCESTOR_OF, head, sel);
sel->media_block(last_media_block);
}
// peek for linefeed and remember result on head
// if (peek_newline()) head->has_line_break(true);
}
sel->update_pstate(pstate);
// complex selector
return sel;
}
// EO parse_complex_selector
// parse one compound selector, which is basically
// a list of simple selectors (directly adjacent)
// lex them exactly (without skipping white-space)
Compound_Selector_Obj Parser::parse_compound_selector()
{
// init an empty compound selector wrapper
Compound_Selector_Obj seq = SASS_MEMORY_NEW(Compound_Selector, pstate);
seq->media_block(last_media_block);
// skip initial white-space
lex< css_whitespace >();
// parse list
while (true)
{
// remove all block comments (don't skip white-space)
lex< delimited_by< slash_star, star_slash, false > >(false);
// parse functional
if (match < re_pseudo_selector >())
{
seq->append(parse_simple_selector());
}
// parse parent selector
else if (lex< exactly<'&'> >(false))
{
// this produces a linefeed!?
seq->has_parent_reference(true);
seq->append(SASS_MEMORY_NEW(Parent_Selector, pstate));
// parent selector only allowed at start
// upcoming Sass may allow also trailing
if (seq->length() > 1) {
ParserState state(pstate);
Simple_Selector_Obj cur = (*seq)[seq->length()-1];
Simple_Selector_Obj prev = (*seq)[seq->length()-2];
std::string sel(prev->to_string({ NESTED, 5 }));
std::string found(cur->to_string({ NESTED, 5 }));
if (lex < identifier >()) { found += std::string(lexed); }
error("Invalid CSS after \"" + sel + "\": expected \"{\", was \"" + found + "\"\n\n"
"\"" + found + "\" may only be used at the beginning of a compound selector.", state);
}
}
// parse type selector
else if (lex< re_type_selector >(false))
{
seq->append(SASS_MEMORY_NEW(Element_Selector, pstate, lexed));
}
// peek for abort conditions
else if (peek< spaces >()) break;
else if (peek< end_of_file >()) { break; }
else if (peek_css < class_char < selector_combinator_ops > >()) break;
else if (peek_css < class_char < complex_selector_delims > >()) break;
// otherwise parse another simple selector
else {
Simple_Selector_Obj sel = parse_simple_selector();
if (!sel) return 0;
seq->append(sel);
}
}
if (seq && !peek_css<alternatives<end_of_file,exactly<'{'>>>()) {
seq->has_line_break(peek_newline());
}
// EO while true
return seq;
}
// EO parse_compound_selector
Simple_Selector_Obj Parser::parse_simple_selector()
{
lex < css_comments >(false);
if (lex< class_name >()) {
return SASS_MEMORY_NEW(Class_Selector, pstate, lexed);
}
else if (lex< id_name >()) {
return SASS_MEMORY_NEW(Id_Selector, pstate, lexed);
}
else if (lex< alternatives < variable, number, static_reference_combinator > >()) {
return SASS_MEMORY_NEW(Element_Selector, pstate, lexed);
}
else if (peek< pseudo_not >()) {
return parse_negated_selector();
}
else if (peek< re_pseudo_selector >()) {
return parse_pseudo_selector();
}
else if (peek< exactly<':'> >()) {
return parse_pseudo_selector();
}
else if (lex < exactly<'['> >()) {
return parse_attribute_selector();
}
else if (lex< placeholder >()) {
Placeholder_Selector_Ptr sel = SASS_MEMORY_NEW(Placeholder_Selector, pstate, lexed);
sel->media_block(last_media_block);
return sel;
}
else {
css_error("Invalid CSS", " after ", ": expected selector, was ");
}
// failed
return 0;
}
Wrapped_Selector_Obj Parser::parse_negated_selector()
{
lex< pseudo_not >();
std::string name(lexed);
ParserState nsource_position = pstate;
Selector_List_Obj negated = parse_selector_list(true);
if (!lex< exactly<')'> >()) {
error("negated selector is missing ')'");
}
name.erase(name.size() - 1);
return SASS_MEMORY_NEW(Wrapped_Selector, nsource_position, name, negated);
}
// a pseudo selector often starts with one or two colons
// it can contain more selectors inside parentheses
Simple_Selector_Obj Parser::parse_pseudo_selector() {
if (lex< sequence<
optional < pseudo_prefix >,
// we keep the space within the name, strange enough
// ToDo: refactor output to schedule the space for it
// or do we really want to keep the real white-space?
sequence< identifier, optional < block_comment >, exactly<'('> >
> >())
{
std::string name(lexed);
name.erase(name.size() - 1);
ParserState p = pstate;
// specially parse static stuff
// ToDo: really everything static?
if (peek_css <
sequence <
alternatives <
static_value,
binomial
>,
optional_css_whitespace,
exactly<')'>
>
>()
) {
lex_css< alternatives < static_value, binomial > >();
String_Constant_Obj expr = SASS_MEMORY_NEW(String_Constant, pstate, lexed);
if (lex_css< exactly<')'> >()) {
expr->can_compress_whitespace(true);
return SASS_MEMORY_NEW(Pseudo_Selector, p, name, expr);
}
}
else if (Selector_List_Obj wrapped = parse_selector_list(true)) {
if (wrapped && lex_css< exactly<')'> >()) {
return SASS_MEMORY_NEW(Wrapped_Selector, p, name, wrapped);
}
}
}
// EO if pseudo selector
else if (lex < sequence< optional < pseudo_prefix >, identifier > >()) {
return SASS_MEMORY_NEW(Pseudo_Selector, pstate, lexed);
}
else if(lex < pseudo_prefix >()) {
css_error("Invalid CSS", " after ", ": expected pseudoclass or pseudoelement, was ");
}
css_error("Invalid CSS", " after ", ": expected \")\", was ");
// unreachable statement
return 0;
}
const char* Parser::re_attr_sensitive_close(const char* src)
{
return alternatives < exactly<']'>, exactly<'/'> >(src);
}
const char* Parser::re_attr_insensitive_close(const char* src)
{
return sequence < insensitive<'i'>, re_attr_sensitive_close >(src);
}
Attribute_Selector_Obj Parser::parse_attribute_selector()
{
ParserState p = pstate;
if (!lex_css< attribute_name >()) error("invalid attribute name in attribute selector");
std::string name(lexed);
if (lex_css< re_attr_sensitive_close >()) {
return SASS_MEMORY_NEW(Attribute_Selector, p, name, "", 0, 0);
}
else if (lex_css< re_attr_insensitive_close >()) {
char modifier = lexed.begin[0];
return SASS_MEMORY_NEW(Attribute_Selector, p, name, "", 0, modifier);
}
if (!lex_css< alternatives< exact_match, class_match, dash_match,
prefix_match, suffix_match, substring_match > >()) {
error("invalid operator in attribute selector for " + name);
}
std::string matcher(lexed);
String_Obj value = 0;