-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathparser.cpp
2240 lines (2057 loc) · 80.1 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 <cstdlib>
#include <iostream>
#include <vector>
#include "parser.hpp"
#include "file.hpp"
#include "inspect.hpp"
#include "to_string.hpp"
#include "constants.hpp"
#include "util.hpp"
#include "prelexer.hpp"
#include "sass_functions.h"
#include <typeinfo>
namespace Sass {
using namespace std;
using namespace Constants;
Parser Parser::from_c_str(const char* str, Context& ctx, ParserState pstate)
{
Parser p(ctx, pstate);
p.source = str;
p.position = p.source;
p.end = str + strlen(str);
Block* root = new (ctx.mem) 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, ParserState pstate)
{
Parser p(ctx, pstate);
p.source = beg;
p.position = p.source;
p.end = end;
Block* root = new (ctx.mem) Block(pstate);
p.block_stack.push_back(root);
root->is_root(true);
return p;
}
bool Parser::peek_newline(const char* start)
{
return peek_linefeed(start ? start : position);
}
Parser Parser::from_token(Token t, Context& ctx, ParserState pstate)
{
Parser p(ctx, pstate);
p.source = t.begin;
p.position = p.source;
p.end = t.end;
Block* root = new (ctx.mem) Block(pstate);
p.block_stack.push_back(root);
root->is_root(true);
return p;
}
Block* Parser::parse()
{
Block* root = new (ctx.mem) Block(pstate);
block_stack.push_back(root);
root->is_root(true);
read_bom();
if (ctx.queue.size() == 1) {
Import* pre = new (ctx.mem) Import(pstate);
string load_path(ctx.queue[0].load_path);
do_import(load_path, pre, ctx.c_headers, false);
ctx.head_imports = ctx.queue.size() - 1;
if (!pre->urls().empty()) (*root) << pre;
if (!pre->files().empty()) {
for (size_t i = 0, S = pre->files().size(); i < S; ++i) {
(*root) << new (ctx.mem) Import_Stub(pstate, pre->files()[i]);
}
}
}
lex< optional_spaces >();
Selector_Lookahead lookahead_result;
while (position < end) {
parse_block_comments(root);
if (peek< kwd_import >()) {
Import* imp = parse_import();
if (!imp->urls().empty()) (*root) << imp;
if (!imp->files().empty()) {
for (size_t i = 0, S = imp->files().size(); i < S; ++i) {
(*root) << new (ctx.mem) Import_Stub(pstate, imp->files()[i]);
}
}
if (!lex< one_plus< exactly<';'> > >()) error("top-level @import directive must be terminated by ';'", pstate);
}
else if (peek< kwd_mixin >() || peek< kwd_function >()) {
(*root) << parse_definition();
}
else if (peek< variable >()) {
(*root) << parse_assignment();
if (!lex< one_plus< exactly<';'> > >()) error("top-level variable binding must be terminated by ';'", pstate);
}
/*else if (peek< sequence< optional< exactly<'*'> >, alternatives< identifier_schema, identifier >, optional_spaces, exactly<':'>, optional_spaces, exactly<'{'> > >(position)) {
(*root) << parse_propset();
}*/
else if (peek< kwd_include >() /* || peek< exactly<'+'> >() */) {
Mixin_Call* mixin_call = parse_mixin_call();
(*root) << mixin_call;
if (!mixin_call->block() && !lex< one_plus< exactly<';'> > >()) error("top-level @include directive must be terminated by ';'", pstate);
}
else if (peek< kwd_if_directive >()) {
(*root) << parse_if_directive();
}
else if (peek< kwd_for_directive >()) {
(*root) << parse_for_directive();
}
else if (peek< kwd_each_directive >()) {
(*root) << parse_each_directive();
}
else if (peek< kwd_while_directive >()) {
(*root) << parse_while_directive();
}
else if (peek< kwd_media >()) {
(*root) << parse_media_block();
}
else if (peek< kwd_at_root >()) {
(*root) << parse_at_root_block();
}
else if (peek< kwd_supports >()) {
(*root) << parse_feature_block();
}
else if (peek< kwd_warn >()) {
(*root) << parse_warning();
if (!lex< one_plus< exactly<';'> > >()) error("top-level @warn directive must be terminated by ';'", pstate);
}
else if (peek< kwd_err >()) {
(*root) << parse_error();
if (!lex< one_plus< exactly<';'> > >()) error("top-level @error directive must be terminated by ';'", pstate);
}
else if (peek< kwd_dbg >()) {
(*root) << parse_debug();
if (!lex< one_plus< exactly<';'> > >()) error("top-level @debug directive must be terminated by ';'", pstate);
}
// ignore the @charset directive for now
else if (lex< exactly< charset_kwd > >()) {
lex< quoted_string >();
lex< one_plus< exactly<';'> > >();
}
else if (peek< at_keyword >()) {
At_Rule* at_rule = parse_at_rule();
(*root) << at_rule;
if (!at_rule->block() && !lex< one_plus< exactly<';'> > >()) error("top-level directive must be terminated by ';'", pstate);
}
else if ((lookahead_result = lookahead_for_selector(position)).found) {
(*root) << parse_ruleset(lookahead_result);
}
else if (peek< exactly<';'> >()) {
lex< one_plus< exactly<';'> > >();
}
else {
lex< css_whitespace >();
if (position >= end) break;
error("invalid top-level expression", after_token);
}
lex< optional_spaces >();
}
block_stack.pop_back();
return root;
}
void Parser::add_single_file (Import* imp, string import_path) {
string extension;
string unquoted(unquote(import_path));
if (unquoted.length() > 4) { // 2 quote marks + the 4 chars in .css
// a string constant is guaranteed to end with a quote mark, so make sure to skip it when indexing from the end
extension = unquoted.substr(unquoted.length() - 4, 4);
}
if (extension == ".css") {
String_Constant* loc = new (ctx.mem) String_Constant(pstate, unquote(import_path));
Argument* loc_arg = new (ctx.mem) Argument(pstate, loc);
Arguments* loc_args = new (ctx.mem) Arguments(pstate);
(*loc_args) << loc_arg;
Function_Call* new_url = new (ctx.mem) Function_Call(pstate, "url", loc_args);
imp->urls().push_back(new_url);
}
else {
string current_dir = File::dir_name(path);
string resolved(ctx.add_file(current_dir, unquoted));
if (resolved.empty()) error("file to import not found or unreadable: " + unquoted + "\nCurrent dir: " + current_dir, pstate);
imp->files().push_back(resolved);
}
}
void Parser::import_single_file (Import* imp, string import_path) {
if (!unquote(import_path).substr(0, 7).compare("http://") ||
!unquote(import_path).substr(0, 8).compare("https://") ||
!unquote(import_path).substr(0, 2).compare("//"))
{
imp->urls().push_back(new (ctx.mem) String_Quoted(pstate, import_path));
}
else {
add_single_file(imp, import_path);
}
}
bool Parser::do_import(const string& import_path, Import* imp, vector<Sass_Importer_Entry> importers, bool only_one)
{
bool has_import = false;
string load_path = unquote(import_path);
for (auto importer : importers) {
// int priority = sass_importer_get_priority(importer);
Sass_Importer_Fn fn = sass_importer_get_function(importer);
if (Sass_Import_List includes =
fn(load_path.c_str(), importer, ctx.c_compiler)
) {
Sass_Import_List list = includes;
while (*includes) {
Sass_Import_Entry include = *includes;
const char *file = sass_import_get_path(include);
char* source = sass_import_take_source(include);
size_t line = sass_import_get_error_line(include);
size_t column = sass_import_get_error_column(include);
const char* message = sass_import_get_error_message(include);
if (message) {
if (line == string::npos && column == string::npos) error(message, pstate);
else error(message, ParserState(message, source, Position(line, column)));
} else if (source) {
if (file) {
ctx.add_source(file, load_path, source);
imp->files().push_back(file);
} else {
ctx.add_source(load_path, load_path, source);
imp->files().push_back(load_path);
}
} else if(file) {
import_single_file(imp, file);
}
++includes;
}
// deallocate returned memory
sass_delete_import_list(list);
// set success flag
has_import = true;
// break import chain
if (only_one) return true;
}
}
// return result
return has_import;
}
Import* Parser::parse_import()
{
lex< kwd_import >();
Import* imp = new (ctx.mem) Import(pstate);
bool first = true;
do {
while (lex< block_comment >());
if (lex< quoted_string >()) {
if (!do_import(lexed, imp, ctx.c_importers, true))
{
// push single file import
import_single_file(imp, lexed);
}
}
else if (lex< uri_prefix >()) {
Arguments* args = new (ctx.mem) Arguments(pstate);
Function_Call* result = new (ctx.mem) Function_Call(pstate, "url", args);
if (lex < uri_value >()) { // chunk seems to work too!
String* the_url = parse_interpolated_chunk(lexed);
*args << new (ctx.mem) Argument(the_url->pstate(), the_url);
}
else {
error("malformed URL", pstate);
}
if (!lex< exactly<')'> >()) error("URI is missing ')'", pstate);
imp->urls().push_back(result);
}
else {
if (first) error("@import directive requires a url or quoted path", pstate);
else error("expecting another url or quoted path in @import list", pstate);
}
first = false;
} while (lex_css< exactly<','> >());
return imp;
}
Definition* Parser::parse_definition()
{
Definition::Type which_type = Definition::MIXIN;
if (lex< kwd_mixin >()) which_type = Definition::MIXIN;
else if (lex< kwd_function >()) which_type = Definition::FUNCTION;
string which_str(lexed);
if (!lex< identifier >()) error("invalid name in " + which_str + " definition", pstate);
string name(Util::normalize_underscores(lexed));
if (which_type == Definition::FUNCTION && (name == "and" || name == "or" || name == "not"))
{ error("Invalid function name \"" + name + "\".", pstate); }
ParserState source_position_of_def = pstate;
Parameters* params = parse_parameters();
if (!peek< exactly<'{'> >()) error("body for " + which_str + " " + name + " must begin with a '{'", pstate);
if (which_type == Definition::MIXIN) stack.push_back(mixin_def);
else stack.push_back(function_def);
Block* body = parse_block();
stack.pop_back();
Definition* def = new (ctx.mem) Definition(source_position_of_def, name, params, body, &ctx, which_type);
return def;
}
Parameters* Parser::parse_parameters()
{
string name(lexed);
Position position = after_token;
Parameters* params = new (ctx.mem) Parameters(pstate);
if (lex_css< exactly<'('> >()) {
// if there's anything there at all
if (!peek_css< exactly<')'> >()) {
do (*params) << parse_parameter();
while (lex_css< exactly<','> >());
}
if (!lex_css< exactly<')'> >()) error("expected a variable name (e.g. $x) or ')' for the parameter list for " + name, position);
}
return params;
}
Parameter* Parser::parse_parameter()
{
while (lex< alternatives < spaces, block_comment > >());
lex< variable >();
string name(Util::normalize_underscores(lexed));
ParserState pos = pstate;
Expression* val = 0;
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();
val->is_delayed(false);
}
else if (lex< exactly< ellipsis > >()) {
is_rest = true;
}
Parameter* p = new (ctx.mem) Parameter(pos, name, val, is_rest);
return p;
}
Mixin_Call* Parser::parse_mixin_call()
{
lex< kwd_include >() /* || lex< exactly<'+'> >() */;
if (!lex< identifier >()) error("invalid name in @include directive", pstate);
ParserState source_position_of_call = pstate;
string name(Util::normalize_underscores(lexed));
Arguments* args = parse_arguments();
Block* content = 0;
if (peek< exactly<'{'> >()) {
content = parse_block();
}
Mixin_Call* the_call = new (ctx.mem) Mixin_Call(source_position_of_call, name, args, content);
return the_call;
}
Arguments* Parser::parse_arguments(bool has_url)
{
string name(lexed);
Position position = after_token;
Arguments* args = new (ctx.mem) Arguments(pstate);
if (lex_css< exactly<'('> >()) {
// if there's anything there at all
if (!peek_css< exactly<')'> >()) {
do (*args) << parse_argument(has_url);
while (lex_css< exactly<','> >());
}
if (!lex_css< exactly<')'> >()) error("expected a variable name (e.g. $x) or ')' for the parameter list for " + name, position);
}
return args;
}
Argument* Parser::parse_argument(bool has_url)
{
Argument* arg;
// some urls can look like line comments (parse literally - chunk would not work)
if (has_url && lex< sequence < uri_value, lookahead < loosely<')'> > > >(false)) {
String* the_url = parse_interpolated_chunk(lexed);
arg = new (ctx.mem) Argument(the_url->pstate(), the_url);
}
else if (peek_css< sequence < variable, optional_css_comments, exactly<':'> > >()) {
lex_css< variable >();
string name(Util::normalize_underscores(lexed));
ParserState p = pstate;
lex_css< exactly<':'> >();
Expression* val = parse_space_list();
val->is_delayed(false);
arg = new (ctx.mem) Argument(p, val, name);
}
else {
bool is_arglist = false;
bool is_keyword = false;
Expression* val = parse_space_list();
val->is_delayed(false);
if (lex_css< exactly< ellipsis > >()) {
if (val->concrete_type() == Expression::MAP) is_keyword = true;
else is_arglist = true;
}
arg = new (ctx.mem) Argument(pstate, val, "", is_arglist, is_keyword);
}
return arg;
}
Assignment* Parser::parse_assignment()
{
lex< variable >();
string name(Util::normalize_underscores(lexed));
ParserState var_source_position = pstate;
if (!lex< exactly<':'> >()) error("expected ':' after " + name + " in assignment statement", pstate);
Expression* val = parse_list();
val->is_delayed(false);
bool is_default = false;
bool is_global = false;
while (peek< default_flag >() || peek< global_flag >()) {
is_default = lex< default_flag >() || is_default;
is_global = lex< global_flag >() || is_global;
}
Assignment* var = new (ctx.mem) Assignment(var_source_position, name, val, is_default, is_global);
return var;
}
/* not used anymore - remove?
Propset* Parser::parse_propset()
{
String* property_segment;
if (peek< sequence< optional< exactly<'*'> >, identifier_schema > >()) {
property_segment = parse_identifier_schema();
}
else {
lex< sequence< optional< exactly<'*'> >, identifier > >();
property_segment = new (ctx.mem) String_Quoted(pstate, lexed);
}
Propset* propset = new (ctx.mem) Propset(pstate, property_segment);
lex< exactly<':'> >();
if (!peek< exactly<'{'> >()) error("expected a '{' after namespaced property", pstate);
propset->block(parse_block());
propset->tabs(indentation);
return propset;
} */
Ruleset* Parser::parse_ruleset(Selector_Lookahead lookahead)
{
Selector* sel;
if (lookahead.has_interpolants) {
sel = parse_selector_schema(lookahead.found);
}
else {
sel = parse_selector_group();
}
bool old_in_at_root = in_at_root;
ParserState r_source_position = pstate;
lex < css_comments >();
in_at_root = false;
if (!peek< exactly<'{'> >()) error("expected a '{' after the selector", pstate);
Block* block = parse_block();
in_at_root = old_in_at_root;
old_in_at_root = false;
Ruleset* ruleset = new (ctx.mem) Ruleset(r_source_position, sel, block);
return ruleset;
}
Selector_Schema* Parser::parse_selector_schema(const char* end_of_selector)
{
lex< optional_spaces >();
const char* i = position;
String_Schema* schema = new (ctx.mem) String_Schema(pstate);
while (i < end_of_selector) {
// try to parse mutliple interpolants
if (const char* p = find_first_in_interval< exactly<hash_lbrace> >(i, end_of_selector)) {
// accumulate the preceding segment if the position has advanced
if (i < p) (*schema) << new (ctx.mem) String_Quoted(pstate, string(i, p));
// skip to the delimiter by skipping occurences in quoted strings
const char* j = skip_over_scopes< exactly<hash_lbrace>, exactly<rbrace> >(p + 2, end_of_selector);
Expression* interpolant = Parser::from_c_str(p+2, j, ctx, pstate).parse_list();
interpolant->is_interpolant(true);
(*schema) << interpolant;
i = j;
}
// no more interpolants have been found
// add the last segment if there is one
else {
if (i < end_of_selector) (*schema) << new (ctx.mem) String_Quoted(pstate, string(i, end_of_selector));
break;
}
}
position = end_of_selector;
Selector_Schema* selector_schema = new (ctx.mem) Selector_Schema(pstate, schema);
selector_schema->media_block(last_media_block);
selector_schema->last_block(block_stack.back());
return selector_schema;
}
Selector_List* Parser::parse_selector_group()
{
bool reloop = true;
To_String to_string(&ctx);
lex< css_whitespace >();
Selector_List* group = new (ctx.mem) Selector_List(pstate);
group->media_block(last_media_block);
group->last_block(block_stack.back());
do {
reloop = false;
if (peek< alternatives <
exactly<'{'>,
exactly<'}'>,
exactly<')'>,
exactly<';'>
> >())
break; // in case there are superfluous commas at the end
Complex_Selector* comb = parse_selector_combination();
if (!comb->has_reference() && !in_at_root) {
ParserState sel_source_position = pstate;
Selector_Reference* ref = new (ctx.mem) Selector_Reference(sel_source_position);
Compound_Selector* ref_wrap = new (ctx.mem) Compound_Selector(sel_source_position);
ref_wrap->media_block(last_media_block);
ref_wrap->last_block(block_stack.back());
(*ref_wrap) << ref;
if (!comb->head()) {
comb->head(ref_wrap);
comb->has_reference(true);
}
else {
comb = new (ctx.mem) Complex_Selector(sel_source_position, Complex_Selector::ANCESTOR_OF, ref_wrap, comb);
comb->media_block(last_media_block);
comb->last_block(block_stack.back());
comb->has_reference(true);
}
if (peek_newline()) ref_wrap->has_line_break(true);
}
while (peek_css< exactly<','> >())
{
// consume everything up and including the comma speparator
reloop = lex< sequence < optional_css_comments, exactly<','> > >() != 0;
// remember line break (also between some commas)
if (peek_newline()) comb->has_line_feed(true);
if (comb->tail() && peek_newline()) comb->tail()->has_line_feed(true);
if (comb->tail() && comb->tail()->head() && peek_newline()) comb->tail()->head()->has_line_feed(true);
// remember line break (also between some commas)
}
(*group) << comb;
}
while (reloop);
while (lex< optional >()) {
group->is_optional(true);
}
return group;
}
Complex_Selector* Parser::parse_selector_combination()
{
Position sel_source_position(-1);
Compound_Selector* lhs;
if (peek_css< alternatives <
exactly<'+'>,
exactly<'~'>,
exactly<'>'>
> >())
// no selector before the combinator
{ lhs = 0; }
else {
lhs = parse_simple_selector_sequence();
sel_source_position = before_token;
lhs->has_line_break(peek_newline());
}
Complex_Selector::Combinator cmb;
if (lex< exactly<'+'> >()) cmb = Complex_Selector::ADJACENT_TO;
else if (lex< exactly<'~'> >()) cmb = Complex_Selector::PRECEDES;
else if (lex< exactly<'>'> >()) cmb = Complex_Selector::PARENT_OF;
else cmb = Complex_Selector::ANCESTOR_OF;
bool cpx_lf = peek_newline();
Complex_Selector* rhs;
if (peek_css< alternatives <
exactly<','>,
exactly<')'>,
exactly<'{'>,
exactly<'}'>,
exactly<';'>,
optional
> >())
// no selector after the combinator
{ rhs = 0; }
else {
rhs = parse_selector_combination();
sel_source_position = before_token;
}
if (!sel_source_position.line) sel_source_position = before_token;
Complex_Selector* cpx = new (ctx.mem) Complex_Selector(ParserState(path, source, sel_source_position), cmb, lhs, rhs);
cpx->media_block(last_media_block);
cpx->last_block(block_stack.back());
if (cpx_lf) cpx->has_line_break(cpx_lf);
return cpx;
}
Compound_Selector* Parser::parse_simple_selector_sequence()
{
Compound_Selector* seq = new (ctx.mem) Compound_Selector(pstate);
seq->media_block(last_media_block);
seq->last_block(block_stack.back());
bool sawsomething = false;
if (lex< exactly<'&'> >()) {
// check if we have a parent selector on the root level block
if (block_stack.back() && block_stack.back()->is_root()) {
//error("Base-level rules cannot contain the parent-selector-referencing character '&'.", pstate);
}
(*seq) << new (ctx.mem) Selector_Reference(pstate);
sawsomething = true;
// if you see a space after a &, then you're done
if(peek< spaces >() || peek< alternatives < spaces, exactly<';'> > >()) {
return seq;
}
}
if (sawsomething && lex_css< sequence< negate< functional >, alternatives< identifier_alnums, universal, quoted_string, dimension, percentage, number > > >()) {
// saw an ampersand, then allow type selectors with arbitrary number of hyphens at the beginning
(*seq) << new (ctx.mem) Type_Selector(pstate, unquote(lexed));
} else if (lex_css< sequence< negate< functional >, alternatives< type_selector, universal, quoted_string, dimension, percentage, number > > >()) {
// if you see a type selector
(*seq) << new (ctx.mem) Type_Selector(pstate, lexed);
sawsomething = true;
}
if (!sawsomething) {
// don't blindly do this if you saw a & or selector
(*seq) << parse_simple_selector();
}
while (!peek< spaces >(position) &&
!(peek_css < alternatives <
exactly<'+'>,
exactly<'~'>,
exactly<'>'>,
exactly<','>,
exactly<')'>,
exactly<'{'>,
exactly<'}'>,
exactly<';'>
> >(position))) {
(*seq) << parse_simple_selector();
}
return seq;
}
Simple_Selector* Parser::parse_simple_selector()
{
lex < css_comments >();
if (lex< alternatives < id_name, class_name > >()) {
return new (ctx.mem) Selector_Qualifier(pstate, unquote(lexed));
}
else if (lex< quoted_string >()) {
return new (ctx.mem) Type_Selector(pstate, unquote(lexed));
}
else if (lex< alternatives < number, kwd_sel_deep > >()) {
return new (ctx.mem) Type_Selector(pstate, lexed);
}
else if (peek< pseudo_not >()) {
return parse_negated_selector();
}
else if (peek< exactly<':'> >(position) || peek< functional >()) {
return parse_pseudo_selector();
}
else if (peek< exactly<'['> >(position)) {
return parse_attribute_selector();
}
else if (lex< placeholder >()) {
Selector_Placeholder* sel = new (ctx.mem) Selector_Placeholder(pstate, unquote(lexed));
sel->media_block(last_media_block);
sel->last_block(block_stack.back());
return sel;
}
else {
error("invalid selector after " + lexed.to_string(), pstate);
}
// unreachable statement
return 0;
}
Wrapped_Selector* Parser::parse_negated_selector()
{
lex< pseudo_not >();
string name(lexed);
ParserState nsource_position = pstate;
Selector* negated = parse_selector_group();
if (!lex< exactly<')'> >()) {
error("negated selector is missing ')'", pstate);
}
return new (ctx.mem) Wrapped_Selector(nsource_position, name, negated);
}
Simple_Selector* Parser::parse_pseudo_selector() {
if (lex< sequence< pseudo_prefix, functional > >() || lex< functional >()) {
string name(lexed);
String* expr = 0;
ParserState p = pstate;
Selector* wrapped = 0;
if (lex< alternatives< even, odd > >()) {
expr = new (ctx.mem) String_Quoted(p, lexed);
}
else if (peek< binomial >(position)) {
lex< sequence< optional< coefficient >, exactly<'n'> > >();
String_Constant* var_coef = new (ctx.mem) String_Quoted(p, lexed);
lex< sign >();
String_Constant* op = new (ctx.mem) String_Quoted(p, lexed);
// Binary_Expression::Type op = (lexed == "+" ? Binary_Expression::ADD : Binary_Expression::SUB);
lex< one_plus < digit > >();
String_Constant* constant = new (ctx.mem) String_Quoted(p, lexed);
// expr = new (ctx.mem) Binary_Expression(p, op, var_coef, constant);
String_Schema* schema = new (ctx.mem) String_Schema(p, 3);
*schema << var_coef << op << constant;
expr = schema;
}
else if (peek< sequence< optional<sign>,
zero_plus<digit>,
exactly<'n'>,
optional_css_whitespace,
exactly<')'> > >()) {
lex< sequence< optional<sign>,
zero_plus<digit>,
exactly<'n'> > >();
expr = new (ctx.mem) String_Quoted(p, lexed);
}
else if (lex< sequence< optional<sign>, one_plus < digit > > >()) {
expr = new (ctx.mem) String_Quoted(p, lexed);
}
else if (peek< sequence< identifier, optional_css_whitespace, exactly<')'> > >()) {
lex< identifier >();
expr = new (ctx.mem) String_Quoted(p, lexed);
}
else if (lex< quoted_string >()) {
expr = new (ctx.mem) String_Quoted(p, lexed);
}
else if (peek< exactly<')'> >()) {
expr = new (ctx.mem) String_Constant(p, "");
}
else {
wrapped = parse_selector_group();
}
if (!lex< exactly<')'> >()) error("unterminated argument to " + name + "...)", pstate);
if (wrapped) {
return new (ctx.mem) Wrapped_Selector(p, name, wrapped);
}
return new (ctx.mem) Pseudo_Selector(p, name, expr);
}
else if (lex < sequence< pseudo_prefix, identifier > >()) {
return new (ctx.mem) Pseudo_Selector(pstate, unquote(lexed));
}
else {
error("unrecognized pseudo-class or pseudo-element", pstate);
}
// unreachable statement
return 0;
}
Attribute_Selector* Parser::parse_attribute_selector()
{
lex_css< exactly<'['> >();
ParserState p = pstate;
if (!lex_css< attribute_name >()) error("invalid attribute name in attribute selector", pstate);
string name(lexed);
if (lex_css< exactly<']'> >()) return new (ctx.mem) Attribute_Selector(p, name, "", 0);
if (!lex_css< alternatives< exact_match, class_match, dash_match,
prefix_match, suffix_match, substring_match > >()) {
error("invalid operator in attribute selector for " + name, pstate);
}
string matcher(lexed);
String* value = 0;
if (lex_css< identifier >()) {
value = new (ctx.mem) 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, pstate);
}
if (!lex_css< exactly<']'> >()) error("unterminated attribute selector for " + name, pstate);
return new (ctx.mem) Attribute_Selector(p, name, matcher, value);
}
/* parse block comment and add to block */
void Parser::parse_block_comments(Block* block)
{
while (lex< block_comment >()) {
bool is_important = lexed.begin[2] == '!';
String* contents = parse_interpolated_chunk(lexed);
(*block) << new (ctx.mem) Comment(pstate, contents, is_important);
}
}
Block* Parser::parse_block()
{
lex< exactly<'{'> >();
bool semicolon = false;
Selector_Lookahead lookahead_result;
Block* block = new (ctx.mem) Block(pstate);
block_stack.push_back(block);
lex< zero_plus < alternatives < space, line_comment > > >();
// JMA - ensure that a block containing only block_comments is parsed
parse_block_comments(block);
while (!lex< exactly<'}'> >()) {
parse_block_comments(block);
if (semicolon) {
if (!lex< one_plus< exactly<';'> > >()) {
error("non-terminal statement or declaration must end with ';'", pstate);
}
semicolon = false;
parse_block_comments(block);
if (lex< sequence< exactly<'}'>, zero_plus< exactly<';'> > > >()) break;
}
else if (peek< kwd_import >(position)) {
if (stack.back() == mixin_def || stack.back() == function_def) {
lex< kwd_import >(); // to adjust the before_token number
error("@import directives are not allowed inside mixins and functions", pstate);
}
Import* imp = parse_import();
if (!imp->urls().empty()) (*block) << imp;
if (!imp->files().empty()) {
for (size_t i = 0, S = imp->files().size(); i < S; ++i) {
(*block) << new (ctx.mem) Import_Stub(pstate, imp->files()[i]);
}
}
semicolon = true;
}
else if (lex< variable >()) {
(*block) << parse_assignment();
semicolon = true;
}
else if (lex< line_comment >()) {
// throw line comments away
}
else if (peek< kwd_if_directive >()) {
(*block) << parse_if_directive();
}
else if (peek< kwd_for_directive >()) {
(*block) << parse_for_directive();
}
else if (peek< kwd_each_directive >()) {
(*block) << parse_each_directive();
}
else if (peek < kwd_while_directive >()) {
(*block) << parse_while_directive();
}
else if (lex < kwd_return_directive >()) {
(*block) << new (ctx.mem) Return(pstate, parse_list());
semicolon = true;
}
else if (peek< kwd_warn >()) {
(*block) << parse_warning();
semicolon = true;
}
else if (peek< kwd_err >()) {
(*block) << parse_error();
semicolon = true;
}
else if (peek< kwd_dbg >()) {
(*block) << parse_debug();
semicolon = true;
}
else if (stack.back() == function_def) {
error("only variable declarations and control directives are allowed inside functions", pstate);
}
else if (peek< kwd_mixin >() || peek< kwd_function >()) {
(*block) << parse_definition();
}
else if (peek< kwd_include >(position)) {
Mixin_Call* the_call = parse_mixin_call();
(*block) << the_call;
// don't need a semicolon after a content block
semicolon = (the_call->block()) ? false : true;
}
else if (lex< kwd_content >()) {
if (stack.back() != mixin_def) {
error("@content may only be used within a mixin", pstate);
}
(*block) << new (ctx.mem) Content(pstate);
semicolon = true;
}
/*
else if (peek< exactly<'+'> >()) {
(*block) << parse_mixin_call();
semicolon = true;
}
*/
else if (lex< kwd_extend >()) {
Selector_Lookahead lookahead = lookahead_for_extension_target(position);
if (!lookahead.found) error("invalid selector for @extend", pstate);
Selector* target;
if (lookahead.has_interpolants) target = parse_selector_schema(lookahead.found);
else target = parse_selector_group();
(*block) << new (ctx.mem) Extension(pstate, target);
semicolon = true;
}
else if (peek< kwd_media >()) {
(*block) << parse_media_block();
}
else if (peek< kwd_supports >()) {
(*block) << parse_feature_block();
}
else if (peek< kwd_at_root >()) {
(*block) << parse_at_root_block();
}
// ignore the @charset directive for now
else if (lex< exactly< charset_kwd > >()) {
lex< quoted_string >();
lex< one_plus< exactly<';'> > >();
}
else if (peek< at_keyword >()) {
At_Rule* at_rule = parse_at_rule();
(*block) << at_rule;
if (!at_rule->block()) semicolon = true;
}
else if ((lookahead_result = lookahead_for_selector(position)).found) {
(*block) << parse_ruleset(lookahead_result);
}/* not used anymore - remove?
else if (peek< sequence< optional< exactly<'*'> >, alternatives< identifier_schema, identifier >, optional_spaces, exactly<':'>, optional_spaces, exactly<'{'> > >(position)) {
(*block) << parse_propset();
}*/
else if (!peek< exactly<';'> >()) {
bool indent = ! peek< sequence< optional< exactly<'*'> >, alternatives< identifier_schema, identifier >, optional_spaces, exactly<':'>, optional_spaces, exactly<'{'> > >(position);
/* not used anymore - remove?
if (peek< sequence< optional< exactly<'*'> >, identifier_schema, exactly<':'>, exactly<'{'> > >()) {
(*block) << parse_propset();
}
else if (peek< sequence< optional< exactly<'*'> >, identifier, exactly<':'>, exactly<'{'> > >()) {
(*block) << parse_propset();
}
else */ {
Declaration* decl = parse_declaration();
decl->tabs(indentation);
(*block) << decl;
if (peek< exactly<'{'> >()) {
// parse a propset that rides on the declaration's property
if (indent) indentation++;
Propset* ps = new (ctx.mem) Propset(pstate, decl->property(), parse_block());
if (indent) indentation--;
(*block) << ps;
}
else {
// finish and let the semicolon get munched
semicolon = true;
}
}
}
else lex< one_plus< exactly<';'> > >();
parse_block_comments(block);
}
block_stack.pop_back();
return block;
}
Declaration* Parser::parse_declaration() {
String* prop = 0;
if (peek< sequence< optional< exactly<'*'> >, identifier_schema > >()) {
prop = parse_identifier_schema();
}
else if (lex< sequence< optional< exactly<'*'> >, identifier > >()) {
prop = new (ctx.mem) String_Quoted(pstate, lexed);
}
else {
error("invalid property name", pstate);
}
const string property(lexed);
if (!lex_css< one_plus< exactly<':'> > >()) error("property \"" + property + "\" must be followed by a ':'", pstate);
if (peek_css< exactly<';'> >()) error("style declaration must contain a value", pstate);
if (peek_css< static_value >()) {
return new (ctx.mem) Declaration(prop->pstate(), prop, parse_static_value()/*, lex<important>()*/);
}
else {
Expression* list_ex = parse_list();
if (List* list = dynamic_cast<List*>(list_ex)) {
if (list->length() == 0 && !peek< exactly <'{'> >()) {
css_error("Invalid CSS", " after ", ": expected expression (e.g. 1px, bold), was ");
}
}
return new (ctx.mem) Declaration(prop->pstate(), prop, list_ex/*, lex<important>()*/);
}
}
// 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 < '-' > >()) {