-
Notifications
You must be signed in to change notification settings - Fork 464
/
Copy pathparser.cpp
2979 lines (2677 loc) · 101 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
// sass.hpp must go before all system headers to get the
// __EXTENSIONS__ fix on Solaris.
#include "sass.hpp"
#include "parser.hpp"
#include "color_maps.hpp"
#include "util_string.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).
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, bool allow_parent)
{
pstate.offset.column = 0;
pstate.offset.line = 0;
Parser p(ctx, pstate, traces, allow_parent);
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, bool allow_parent)
{
pstate.offset.column = 0;
pstate.offset.line = 0;
Parser p(ctx, pstate, traces, allow_parent);
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;
}
SelectorListObj Parser::parse_selector(const char* beg, Context& ctx, Backtraces traces, ParserState pstate, const char* source, bool allow_parent)
{
Parser p = Parser::from_c_str(beg, ctx, traces, pstate, source, allow_parent);
// ToDo: remap the source-map entries somehow
return p.parseSelectorList(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 ");
SelectorListObj target;
if (!lookahead.has_interpolants) {
LOCAL_FLAG(allow_parent, false);
auto selector = parseSelectorList(true);
auto extender = SASS_MEMORY_NEW(ExtendRule, pstate, selector);
extender->isOptional(selector && selector->is_optional());
block->append(extender);
}
else {
LOCAL_FLAG(allow_parent, false);
auto selector = parse_selector_schema(lookahead.found, true);
auto extender = SASS_MEMORY_NEW(ExtendRule, pstate, selector);
// A schema is not optional yet, check once it is evaluated
// extender->isOptional(selector && selector->is_optional());
block->append(extender);
}
}
// 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(parseMediaRule()); }
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(); }
else if (lex < exactly < else_kwd >>(true)) { error("Invalid CSS: @else must come after @if"); }
// generic at keyword (keep last)
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), {}));
}
else if (lex< uri_prefix >()) {
Arguments_Obj args = SASS_MEMORY_NEW(Arguments, pstate);
Function_Call_Obj result = SASS_MEMORY_NEW(Function_Call, pstate, std::string("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* 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(parseSelectorList(false));
}
else {
SelectorListObj list = SASS_MEMORY_NEW(SelectorList, pstate);
auto sc = parse_selector_schema(lookahead.position, false);
ruleset->schema(sc);
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* 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);
// process until end
while (i < end_of_selector) {
// try to parse multiple 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, {}, {}, {});
// parse mandatory arguments
call->arguments(parse_arguments());
// parse using and optional block parameters
bool has_parameters = lex< kwd_using >() != nullptr;
if (has_parameters) {
if (!peek< exactly<'('> >()) css_error("Invalid CSS", " after ", ": expected \"(\", was ");
} else {
if (peek< exactly<'('> >()) css_error("Invalid CSS", " after ", ": expected \";\", was ");
}
if (has_parameters) call->block_parameters(parse_parameters());
// parse optional block
if (peek < exactly <'{'> >()) {
call->block(parse_block());
}
else if (has_parameters) {
css_error("Invalid CSS", " after ", ": expected \"{\", was ");
}
// return ast node
return call.detach();
}
// EO parse_include_directive
SimpleSelectorObj 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(Type_Selector, pstate, lexed);
}
else if (peek< pseudo_not >()) {
return parse_negated_selector2();
}
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 >()) {
return SASS_MEMORY_NEW(Placeholder_Selector, pstate, lexed);
}
else {
css_error("Invalid CSS", " after ", ": expected selector, was ");
}
// failed
return {};
}
Pseudo_Selector_Obj Parser::parse_negated_selector2()
{
lex< pseudo_not >();
std::string name(lexed);
ParserState nsource_position = pstate;
SelectorListObj negated = parseSelectorList(true);
if (!lex< exactly<')'> >()) {
error("negated selector is missing ')'");
}
name.erase(name.size() - 1);
Pseudo_Selector* sel = SASS_MEMORY_NEW(Pseudo_Selector, nsource_position, name.substr(1));
sel->selector(negated);
return sel;
}
// Helper to clean binominal string
bool BothAreSpaces(char lhs, char rhs) { return isspace(lhs) && isspace(rhs); }
// a pseudo selector often starts with one or two colons
// it can contain more selectors inside parentheses
SimpleSelectorObj Parser::parse_pseudo_selector() {
// Lex one or two colon characters
if (lex<pseudo_prefix>()) {
std::string colons(lexed);
// Check if it is a pseudo element
bool element = colons.size() == 2;
if (lex< sequence<
// 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 nth-child pseudo selectors
if (lex_css < sequence < binomial, word_boundary >>()) {
std::string parsed(lexed); // always compacting binominals (as dart-sass)
parsed.erase(std::unique(parsed.begin(), parsed.end(), BothAreSpaces), parsed.end());
String_Constant_Obj arg = SASS_MEMORY_NEW(String_Constant, pstate, parsed);
Pseudo_Selector* pseudo = SASS_MEMORY_NEW(Pseudo_Selector, p, name, element);
if (lex < sequence < css_whitespace, insensitive < of_kwd >>>(false)) {
pseudo->selector(parseSelectorList(true));
}
pseudo->argument(arg);
if (lex_css< exactly<')'> >()) {
return pseudo;
}
}
else {
if (peek_css< exactly<')'>>() && Util::equalsLiteral("nth-", name.substr(0, 4))) {
css_error("Invalid CSS", " after ", ": expected An+B expression, was ");
}
std::string unvendored = Util::unvendor(name);
if (unvendored == "not" || unvendored == "matches" || unvendored == "current" || unvendored == "any" || unvendored == "has" || unvendored == "host" || unvendored == "host-context" || unvendored == "slotted") {
if (SelectorListObj wrapped = parseSelectorList(true)) {
if (wrapped && lex_css< exactly<')'> >()) {
Pseudo_Selector* pseudo = SASS_MEMORY_NEW(Pseudo_Selector, p, name, element);
pseudo->selector(wrapped);
return pseudo;
}
}
} else {
String_Schema_Obj arg = parse_css_variable_value();
Pseudo_Selector* pseudo = SASS_MEMORY_NEW(Pseudo_Selector, p, name, element);
pseudo->argument(arg);
if (lex_css< exactly<')'> >()) {
return pseudo;
}
}
}
}
// EO if pseudo selector
else if (lex < sequence< optional < pseudo_prefix >, identifier > >()) {
return SASS_MEMORY_NEW(Pseudo_Selector, pstate, lexed, element);
}
else if (lex < pseudo_prefix >()) {
css_error("Invalid CSS", " after ", ": expected pseudoclass or pseudoelement, was ");
}
}
else {
lex < identifier >(); // needed for error message?
css_error("Invalid CSS", " after ", ": expected selector, was ");
}
css_error("Invalid CSS", " after ", ": expected \")\", was ");
// unreachable statement
return {};
}
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, "", {}, {});
}
else if (lex_css< re_attr_insensitive_close >()) {
char modifier = lexed.begin[0];
return SASS_MEMORY_NEW(Attribute_Selector, p, name, "", {}, 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;
if (lex_css< identifier >()) {
value = SASS_MEMORY_NEW(String_Constant, p, lexed);
}
else if (lex_css< quoted_string >()) {
value = parse_interpolated_chunk(lexed, true); // needed!
}
else {
error("expected a string constant or identifier in attribute selector for " + name);
}
if (lex_css< re_attr_sensitive_close >()) {
return SASS_MEMORY_NEW(Attribute_Selector, p, name, matcher, value, 0);
}
else if (lex_css< re_attr_insensitive_close >()) {
char modifier = lexed.begin[0];
return SASS_MEMORY_NEW(Attribute_Selector, p, name, matcher, value, modifier);
}
error("unterminated attribute selector for " + name);
return {}; // to satisfy compilers (error must not return)
}
/* parse block comment and add to block */
void Parser::parse_block_comments(bool store)
{
Block_Obj block = block_stack.back();
while (lex< block_comment >()) {
bool is_important = lexed.begin[2] == '!';
// flag on second param is to skip loosely over comments
String_Obj contents = parse_interpolated_chunk(lexed, true, false);
if (store) block->append(SASS_MEMORY_NEW(Comment, pstate, contents, is_important));
}
}
Declaration_Obj Parser::parse_declaration() {
String_Obj prop;
bool is_custom_property = false;
if (lex< sequence< optional< exactly<'*'> >, identifier_schema > >()) {
const std::string property(lexed);
is_custom_property = property.compare(0, 2, "--") == 0;
prop = parse_identifier_schema();
}
else if (lex< sequence< optional< exactly<'*'> >, identifier, zero_plus< block_comment > > >()) {
const std::string property(lexed);
is_custom_property = property.compare(0, 2, "--") == 0;
prop = SASS_MEMORY_NEW(String_Constant, pstate, lexed);
}
else {
css_error("Invalid CSS", " after ", ": expected \"}\", was ");
}
bool is_indented = true;
const std::string property(lexed);
if (!lex_css< one_plus< exactly<':'> > >()) error("property \"" + escape_string(property) + "\" must be followed by a ':'");
if (!is_custom_property && match< sequence< optional_css_comments, exactly<';'> > >()) error("style declaration must contain a value");
if (match< sequence< optional_css_comments, exactly<'{'> > >()) is_indented = false; // don't indent if value is empty
if (is_custom_property) {
return SASS_MEMORY_NEW(Declaration, prop->pstate(), prop, parse_css_variable_value(), false, true);
}
lex < css_comments >(false);
if (peek_css< static_value >()) {
return SASS_MEMORY_NEW(Declaration, prop->pstate(), prop, parse_static_value()/*, lex<kwd_important>()*/);
}
else {
Expression_Obj value;
Lookahead lookahead = lookahead_for_value(position);
if (lookahead.found) {
if (lookahead.has_interpolants) {
value = parse_value_schema(lookahead.found);
} else {
value = parse_list(DELAYED);
}
}
else {
value = parse_list(DELAYED);
if (List* list = Cast<List>(value)) {
if (!list->is_bracketed() && list->length() == 0 && !peek< exactly <'{'> >()) {
css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
}
}
}
lex < css_comments >(false);
Declaration_Obj decl = SASS_MEMORY_NEW(Declaration, prop->pstate(), prop, value/*, lex<kwd_important>()*/);
decl->is_indented(is_indented);
decl->update_pstate(pstate);
return decl;
}
}
Expression_Obj Parser::parse_map()
{
NESTING_GUARD(nestings);
Expression_Obj key = parse_list();
List_Obj map = SASS_MEMORY_NEW(List, pstate, 0, SASS_HASH);
// it's not a map so return the lexed value as a list value
if (!lex_css< exactly<':'> >())
{ return key; }
List_Obj l = Cast<List>(key);
if (l && l->separator() == SASS_COMMA) {
css_error("Invalid CSS", " after ", ": expected \")\", was ");
}
Expression_Obj value = parse_space_list();
map->append(key);
map->append(value);
while (lex_css< exactly<','> >())
{
// allow trailing commas - #495
if (peek_css< exactly<')'> >(position))
{ break; }
key = parse_space_list();
if (!(lex< exactly<':'> >()))
{ css_error("Invalid CSS", " after ", ": expected \":\", was "); }
value = parse_space_list();
map->append(key);
map->append(value);
}
ParserState ps = map->pstate();
ps.offset = pstate - ps + pstate.offset;
map->pstate(ps);
return map;
}
Expression_Obj Parser::parse_bracket_list()
{
NESTING_GUARD(nestings);
// check if we have an empty list
// return the empty list as such
if (peek_css< list_terminator >(position))
{
// return an empty list (nothing to delay)
return SASS_MEMORY_NEW(List, pstate, 0, SASS_SPACE, false, true);
}
bool has_paren = peek_css< exactly<'('> >() != NULL;
// now try to parse a space list
Expression_Obj list = parse_space_list();
// if it's a singleton, return it (don't wrap it)
if (!peek_css< exactly<','> >(position)) {
List_Obj l = Cast<List>(list);
if (!l || l->is_bracketed() || has_paren) {
List_Obj bracketed_list = SASS_MEMORY_NEW(List, pstate, 1, SASS_SPACE, false, true);
bracketed_list->append(list);
return bracketed_list;
}
l->is_bracketed(true);
return l;
}