forked from m2osw/csspp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompiler.cpp
More file actions
4175 lines (3780 loc) · 144 KB
/
compiler.cpp
File metadata and controls
4175 lines (3780 loc) · 144 KB
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
// CSS Preprocessor
// Copyright (c) 2015-2019 Made to Order Software Corp. All Rights Reserved
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
/** \file
* \brief Implementation of the CSS Preprocessor compiler.
*
* The CSS Preprocessor compiler applies the script rules and transform
* the tree of nodes so it can be output as standard CSS.
*
* \sa \ref compiler_reference
*/
#include "csspp/compiler.h"
#include "csspp/exceptions.h"
#include "csspp/nth_child.h"
#include "csspp/parser.h"
#include <cmath>
#include <fstream>
#include <iostream>
#include <unistd.h>
namespace csspp
{
namespace
{
integer_t const g_if_or_else_undefined = 0;
integer_t const g_if_or_else_false_so_far = 1;
integer_t const g_if_or_else_executed = 2;
} // no name namespace
class safe_parents_t
{
public:
safe_parents_t(compiler::compiler_state_t & state, node::pointer_t n)
: f_state(state)
{
f_state.push_parent(n);
}
~safe_parents_t()
{
f_state.pop_parent();
}
private:
compiler::compiler_state_t & f_state;
};
class safe_compiler_state_t
{
public:
safe_compiler_state_t(compiler::compiler_state_t & state)
: f_state(state)
, f_state_copy(state)
{
}
~safe_compiler_state_t()
{
f_state = f_state_copy;
}
private:
compiler::compiler_state_t & f_state;
compiler::compiler_state_t f_state_copy;
};
void compiler::compiler_state_t::set_root(node::pointer_t root)
{
f_root = root;
f_parents.clear();
}
node::pointer_t compiler::compiler_state_t::get_root() const
{
return f_root;
}
void compiler::compiler_state_t::clear_paths()
{
f_paths.clear();
}
void compiler::compiler_state_t::add_path(std::string const & path)
{
f_paths.push_back(path);
}
void compiler::compiler_state_t::set_paths(compiler_state_t const & state)
{
// replace out paths with another set
f_paths = state.f_paths;
}
void compiler::compiler_state_t::push_parent(node::pointer_t parent)
{
f_parents.push_back(parent);
}
void compiler::compiler_state_t::pop_parent()
{
f_parents.pop_back();
}
bool compiler::compiler_state_t::empty_parents() const
{
return f_parents.empty();
}
node::pointer_t compiler::compiler_state_t::get_previous_parent() const
{
if(f_parents.size() < 2)
{
throw csspp_exception_logic("compiler.cpp:compiler::compiler_state_t::get_current_parent(): no previous parents available."); // LCOV_EXCL_LINE
}
// return the parent before last
return f_parents[f_parents.size() - 2];
}
void compiler::compiler_state_t::set_variable(node::pointer_t name, node::pointer_t value, bool global) const
{
// the name is used in a map to quickly save/retrieve variables
// so we save the variable / mixin definitions in a list saved
// along the value of this variable
std::string const variable_name(name->get_string());
node::pointer_t v(new node(node_type_t::LIST, value->get_position()));
v->add_child(name);
v->add_child(value);
if(!global)
{
size_t pos(f_parents.size());
while(pos > 0)
{
--pos;
node::pointer_t s(f_parents[pos]);
if(s->is(node_type_t::OPEN_CURLYBRACKET)
&& s->get_boolean())
{
s->set_variable(variable_name, v);
return;
}
}
}
// if nothing else make it a global variable
f_root->set_variable(variable_name, v);
}
node::pointer_t compiler::compiler_state_t::get_variable(std::string const & variable_name, bool global_only) const
{
if(!global_only)
{
size_t pos(f_parents.size());
while(pos > 0)
{
--pos;
node::pointer_t s(f_parents[pos]);
switch(s->get_type())
{
case node_type_t::OPEN_CURLYBRACKET:
if(s->get_boolean())
{
node::pointer_t value(s->get_variable(variable_name));
if(value)
{
return value;
}
}
break;
default:
break;
}
}
}
return f_root->get_variable(variable_name);
}
node::pointer_t compiler::compiler_state_t::execute_user_function(node::pointer_t func)
{
// search the parents for the node where the function will be set
node::pointer_t value(get_variable(func->get_string()));
if(!value)
{
// no function (or variables) with that name found, return the
// input function as is
return func;
}
// internal validity check
if(!value->is(node_type_t::LIST)
|| value->size() != 2)
{
throw csspp_exception_logic("compiler.cpp:compiler::compiler_state_t::execute_user_function(): all functions must be two sub-values in a LIST, the first item being the variable."); // LCOV_EXCL_LINE
}
node::pointer_t var(value->get_child(0));
node::pointer_t val(value->get_child(1));
if(!var->is(node_type_t::FUNCTION))
//&& !var->is(node_type_t::VARIABLE_FUNCTION)) -- TBD
{
// found something, but that is not a @mixin function...
return func;
}
// the function was already argified in expression::unary()
//parser::argify(func);
// define value of each argument
node::pointer_t root(new node(node_type_t::LIST, val->get_position()));
if(!val->is(node_type_t::OPEN_CURLYBRACKET))
{
throw csspp_exception_logic("compiler.cpp:compiler::compiler_state_t::execute_user_function(): @mixin function is not defined inside a {}-block."); // LCOV_EXCL_LINE
}
// make sure we get a copy of the current global variables
root->copy_variable(f_root);
size_t max_val_children(val->size());
for(size_t j(0); j < max_val_children; ++j)
{
root->add_child(val->get_child(j)->clone());
}
size_t const max_children(var->size());
size_t const max_input(func->size());
for(size_t i(0); i < max_children; ++i)
{
node::pointer_t arg(var->get_child(i));
if(!arg->is(node_type_t::ARG))
{
// function declaration is invalid!
throw csspp_exception_logic("compiler.cpp:compiler::compiler_state_t::execute_user_function(): FUNCTION children are not all ARG nodes."); // LCOV_EXCL_LINE
}
if(arg->empty())
{
throw csspp_exception_logic("compiler.cpp:compiler::compiler_state_t::execute_user_function(): ARG is empty."); // LCOV_EXCL_LINE
}
node::pointer_t arg_name(arg->get_child(0));
if(!arg_name->is(node_type_t::VARIABLE))
{
// this was already erred when we created the variable
//error::instance() << val->get_position()
// << "function declaration requires all parameters to be variables, "
// << arg_name->get_type()
// << " is not acceptable."
// << error_mode_t::ERROR_ERROR;
return func;
}
if(i >= max_input)
{
// user did not specify this value, check whether we have
// an optional value
if(arg->size() > 1)
{
// use default value
node::pointer_t default_param(arg->clone());
default_param->remove_child(0); // remove the variable name
if(default_param->size() == 1)
{
default_param = default_param->get_child(0);
}
else
{
node::pointer_t value_list(new node(node_type_t::LIST, arg->get_position()));
value_list->take_over_children_of(default_param);
default_param = value_list;
}
node::pointer_t param_value(new node(node_type_t::LIST, arg->get_position()));
param_value->add_child(arg_name);
param_value->add_child(default_param);
root->set_variable(arg_name->get_string(), param_value);
}
else
{
// value is missing
error::instance() << val->get_position()
<< "missing function variable named \""
<< arg_name->get_string()
<< "\" when calling "
<< func->get_string()
<< "();."
<< error_mode_t::ERROR_ERROR;
return func;
}
}
else
{
// copy user provided value
node::pointer_t user_param(func->get_child(i));
if(!user_param->is(node_type_t::ARG))
{
throw csspp_exception_logic("compiler.cpp:compiler::replace_variable(): user parameter is not an ARG."); // LCOV_EXCL_LINE
}
if(user_param->size() == 1)
{
user_param = user_param->get_child(0);
}
else
{
// is that really correct?
// we may need a component_value instead...
node::pointer_t list(new node(node_type_t::LIST, user_param->get_position()));
list->take_over_children_of(user_param);
user_param = list;
}
node::pointer_t param_value(new node(node_type_t::LIST, user_param->get_position()));
param_value->add_child(arg_name);
param_value->add_child(user_param->clone());
root->set_variable(arg_name->get_string(), param_value);
}
}
compiler c(true);
c.set_root(root);
c.f_state.f_paths = f_paths;
c.f_state.f_empty_on_undefined_variable = f_empty_on_undefined_variable;
// use 'true' here otherwise it would reload the header/footer each time!
c.compile(true);
return c.get_result();
}
void compiler::compiler_state_t::set_empty_on_undefined_variable(bool empty_on_undefined_variable)
{
f_empty_on_undefined_variable = empty_on_undefined_variable;
}
bool compiler::compiler_state_t::get_empty_on_undefined_variable() const
{
return f_empty_on_undefined_variable;
}
std::string compiler::compiler_state_t::find_file(std::string const & script_name)
{
// no name?!
if(script_name.empty())
{
return std::string();
}
// an absolute path?
if(script_name[0] == '/')
{
if(access(script_name.c_str(), R_OK) == 0)
{
return script_name;
}
// absolute does not mean we can find the file
return std::string();
}
// no paths at all???
if(f_paths.empty())
{
// should this be "." here instead of the default?
f_paths.push_back("/usr/lib/csspp/scripts");
}
// check with each path and return the first match
for(auto it : f_paths)
{
std::string const name(it == "" ? script_name : it + "/" + script_name);
if(access(name.c_str(), R_OK) == 0)
{
return name;
}
}
// in case we cannot find a file
return std::string();
}
compiler::compiler(bool validating)
: f_compiler_validating(validating)
{
f_state.add_path("/usr/lib/csspp/scripts");
}
void compiler::set_root(node::pointer_t root)
{
f_state.set_root(root);
}
node::pointer_t compiler::get_root() const
{
return f_state.get_root();
}
node::pointer_t compiler::get_result() const
{
return f_return_result;
}
void compiler::set_date_time_variables(time_t now)
{
// make sure we're ready to setup the date and time
node::pointer_t root(get_root());
if(!root)
{
throw csspp_exception_logic("compiler.cpp: compiler::set_date_time_variables(): function called too soon, root not set yet.");
}
// convert date/time in a string
struct tm t;
localtime_r(&now, &t);
char buf[20];
strftime(buf, sizeof(buf), "%m/%d/%Y%T", &t);
// save the result in variables
// usdate
csspp::node::pointer_t var(new csspp::node(csspp::node_type_t::VARIABLE, root->get_position()));
var->set_string("_csspp_usdate");
csspp::node::pointer_t arg(new csspp::node(csspp::node_type_t::STRING, root->get_position()));
arg->set_string(std::string(buf, 10));
f_state.set_variable(var, arg, true);
// month
var.reset(new csspp::node(csspp::node_type_t::VARIABLE, root->get_position()));
var->set_string("_csspp_month");
arg.reset(new csspp::node(csspp::node_type_t::STRING, root->get_position()));
arg->set_string(std::string(buf, 2));
f_state.set_variable(var, arg, true);
// day
var.reset(new csspp::node(csspp::node_type_t::VARIABLE, root->get_position()));
var->set_string("_csspp_day");
arg.reset(new csspp::node(csspp::node_type_t::STRING, root->get_position()));
arg->set_string(std::string(buf + 3, 2));
f_state.set_variable(var, arg, true);
// year
var.reset(new csspp::node(csspp::node_type_t::VARIABLE, root->get_position()));
var->set_string("_csspp_year");
arg.reset(new csspp::node(csspp::node_type_t::STRING, root->get_position()));
arg->set_string(std::string(buf + 6, 4));
f_state.set_variable(var, arg, true);
// time
var.reset(new csspp::node(csspp::node_type_t::VARIABLE, root->get_position()));
var->set_string("_csspp_time");
arg.reset(new csspp::node(csspp::node_type_t::STRING, root->get_position()));
arg->set_string(std::string(buf + 10, 8));
f_state.set_variable(var, arg, true);
// hour
var.reset(new csspp::node(csspp::node_type_t::VARIABLE, root->get_position()));
var->set_string("_csspp_hour");
arg.reset(new csspp::node(csspp::node_type_t::STRING, root->get_position()));
arg->set_string(std::string(buf + 10, 2));
f_state.set_variable(var, arg, true);
// minute
var.reset(new csspp::node(csspp::node_type_t::VARIABLE, root->get_position()));
var->set_string("_csspp_minute");
arg.reset(new csspp::node(csspp::node_type_t::STRING, root->get_position()));
arg->set_string(std::string(buf + 13, 2));
f_state.set_variable(var, arg, true);
// second
var.reset(new csspp::node(csspp::node_type_t::VARIABLE, root->get_position()));
var->set_string("_csspp_second");
arg.reset(new csspp::node(csspp::node_type_t::STRING, root->get_position()));
arg->set_string(std::string(buf + 16, 2));
f_state.set_variable(var, arg, true);
}
void compiler::set_empty_on_undefined_variable(bool empty_on_undefined_variable)
{
f_state.set_empty_on_undefined_variable(empty_on_undefined_variable);
}
void compiler::set_no_logo(bool no_logo)
{
f_no_logo = no_logo;
}
void compiler::clear_paths()
{
f_state.clear_paths();
}
void compiler::add_path(std::string const & path)
{
f_state.add_path(path);
}
void compiler::compile(bool bare)
{
if(!f_state.get_root())
{
throw csspp_exception_logic("compiler.cpp: compiler::compile(): compile() called without a root node pointer, call set_root() first."); // LCOV_EXCL_LINE
}
// before we compile anything we want to transform all the variables
// with their verbatim contents; otherwise the compiler would be way
// more complex for nothing...
//
// Also for the variables to work properly, we immediately handle
// the @import and @mixins since both may define additional variables.
// Similarly, we handle control flow (@if, @else, @include, ...)
//
//std::cerr << "************* COMPILING:\n" << *f_state.get_root() << "-----------------\n";
if(!bare)
{
add_header_and_footer();
}
mark_selectors(f_state.get_root());
if(!f_state.empty_parents())
{
throw csspp_exception_logic("compiler.cpp: the stack of parents must always be empty before mark_selectors() returns."); // LCOV_EXCL_LINE
}
replace_variables(f_state.get_root());
if(!f_state.empty_parents())
{
throw csspp_exception_logic("compiler.cpp: the stack of parents must always be empty before replace_variables() returns."); // LCOV_EXCL_LINE
}
compile(f_state.get_root());
if(!f_state.empty_parents())
{
throw csspp_exception_logic("compiler.cpp: the stack of parents must always be empty before compile() returns"); // LCOV_EXCL_LINE
}
remove_empty_rules(f_state.get_root());
if(!f_state.empty_parents())
{
throw csspp_exception_logic("compiler.cpp: the stack of parents must always be empty before remove_empty_rules() returns"); // LCOV_EXCL_LINE
}
expand_nested_components(f_state.get_root());
if(!f_state.empty_parents())
{
throw csspp_exception_logic("compiler.cpp: the stack of parents must always be empty before expand_nested_components() returns"); // LCOV_EXCL_LINE
}
}
void compiler::add_header_and_footer()
{
// the header is @import "scripts/init.scss"
//
{
position pos("header.scss");
node::pointer_t header(new node(node_type_t::AT_KEYWORD, pos));
header->set_string("import");
node::pointer_t header_string(new node(node_type_t::STRING, pos));
header_string->set_string("system/init.scss");
header->add_child(header_string);
f_state.get_root()->insert_child(0, header);
}
// the footer is @import "script/close.scss"
//
{
position pos("footer.scss");
node::pointer_t footer(new node(node_type_t::AT_KEYWORD, pos));
footer->set_string("import");
node::pointer_t footer_string(new node(node_type_t::STRING, pos));
footer_string->set_string("system/close.scss");
footer->add_child(footer_string);
f_state.get_root()->add_child(footer);
}
// the close.scss checks this flag
//
{
position pos("close.scss");
node::pointer_t no_logo(new node(node_type_t::VARIABLE, pos));
no_logo->set_string("_csspp_no_logo");
node::pointer_t value(new node(node_type_t::BOOLEAN, pos));
value->set_boolean(f_no_logo);
f_state.set_variable(no_logo, value, true);
}
}
void compiler::compile(node::pointer_t n)
{
safe_parents_t safe_parents(f_state, n);
switch(n->get_type())
{
case node_type_t::LIST:
case node_type_t::FRAME:
// transparent item, just compile all the children
{
size_t idx(0);
while(idx < n->size() && !f_return_result)
{
node::pointer_t child(n->get_child(idx));
compile(child);
// the child may replace itself with something else
// in which case we do not want the ++idx
if(idx < n->size()
&& n->get_child(idx) == child)
{
++idx;
}
}
// TODO: remove LIST if it now is empty or has 1 item
}
break;
case node_type_t::DECLARATION:
// because of lists of compolent values this can happen...
// we just ignore those since it is already compiled
//
// (we get it to happen with @framekeys ... { ... } within the list
// of declarations at a given position)
//
break;
case node_type_t::COMPONENT_VALUE:
compile_component_value(n);
break;
case node_type_t::AT_KEYWORD:
compile_at_keyword(n);
break;
case node_type_t::COMMENT:
// passthrough tokens
break;
default:
{
std::stringstream ss;
ss << "unexpected token (type: " << n->get_type() << ") in compile().";
throw csspp_exception_unexpected_token(ss.str());
}
}
}
void compiler::compile_component_value(node::pointer_t n)
{
// already compiled?
if(n->is(node_type_t::DECLARATION))
{
// This is really double ugly, I'll have to look into getting
// my loops straighten up because having to test such in
// various places is bad!
//
// We may want to find a better way to skip these entries...
// we replace a COMPONENT_VALUE with a DECLARATION and return
// and the loops think that the COMPONENT_VALUE was "replaced"
// by new code that needs to be compiled; only we replaced the
// entry with already compiled data! The best way may be to have
// a state with a position that we pass around...
return;
}
// there are quite a few cases to handle here:
//
// $variable ':' '{' ... '}'
// <field-prefix> ':' '{' ... '}'
// <selector-list> '{' ... '}'
// $variable ':' ...
// <field-name> ':' ...
//
if(n->empty())
{
// we have a problem, we should already have had an error
// somewhere?
return; // LCOV_EXCL_LINE
}
if(n->get_child(0)->is(node_type_t::COMMENT))
{
// XXX: verify that this is the right location to chek this
// special case, we may want to do it only in the loop
// that also accepts plain comments instead of here
// which is a function that can get called from deep
// inside...
// get parent of n, remove n from there, replace it by
// the comment
node::pointer_t parent(f_state.get_previous_parent());
size_t pos(parent->child_position(n));
parent->remove_child(pos);
parent->insert_child(pos, n->get_child(0));
return;
}
// was that COMPONENT_VALUE already compiled?
if(n->get_child(0)->is(node_type_t::ARG))
{
// the following fix prevents this from happening so at this time
// I mark this as a problem; we could just return otherwise
// (like the case the list is a declaration)
throw csspp_exception_logic("compiler.cpp: found an ARG as the first child of COMPONENT_VALUE, compile_component_value() called twice on the same object?"); // LCOV_EXCL_LINE
}
size_t const max_children(n->size());
size_t count_cv(0);
for(size_t idx(0); idx < max_children; ++idx)
{
node::pointer_t child(n->get_child(idx));
if(child->is(node_type_t::COMPONENT_VALUE))
{
++count_cv;
}
}
if(count_cv == max_children)
{
// this happens when we add elements from a sub {}-block
// for example, a verbatim:
//
// @if (true) { foo { a: b; } blah { c: d; } }
//
node::pointer_t parent(f_state.get_previous_parent());
size_t pos(parent->child_position(n));
parent->remove_child(pos);
for(size_t idx(0); idx < max_children; ++idx, ++pos)
{
parent->insert_child(pos, n->get_child(idx));
}
// the caller will call us again with the new list of
// COMPONENT_VALUE nodes as expected
return;
}
else if(count_cv != 0)
{
std::cerr << "Invalid node:\n" << *n; // LCOV_EXCL_LINE
throw csspp_exception_logic("compiler.cpp: found a COMPONENT_VALUE with a mix of children."); // LCOV_EXCL_LINE
}
// this may be only temporary (until I fix the parser) but at this
// point we may get an @-keyword inside a COMPONENT_VALUE
if(n->get_child(0)->is(node_type_t::AT_KEYWORD))
{
{
safe_parents_t safe_parents(f_state, n->get_child(0));
compile_at_keyword(n->get_child(0));
}
if(n->empty())
{
// the @-keyword was removed and now the COMPONENT_VALUE is
// empty so we can just get rid of it
f_state.get_previous_parent()->remove_child(n);
}
return;
}
// $variable ':' '{' ... '}'
if(parser::is_variable_set(n, true))
{
throw csspp_exception_logic("compiler.cpp: somehow a variable definition was found while compiling (1)."); // LCOV_EXCL_LINE
}
// <field-prefix> ':' '{' ... '}'
if(parser::is_nested_declaration(n))
{
compile_declaration(n);
return;
}
// <selector-list> '{' ... '}'
if(n->get_last_child()->is(node_type_t::OPEN_CURLYBRACKET))
{
// this is a selector list followed by a block of
// definitions and sub-blocks
compile_qualified_rule(n);
return;
}
// $variable ':' ... ';'
if(parser::is_variable_set(n, false))
{
throw csspp_exception_logic("compiler.cpp: somehow a variable definition was found while compiling (1)."); // LCOV_EXCL_LINE
}
// <field-name> ':' ...
compile_declaration(n);
}
void compiler::compile_qualified_rule(node::pointer_t n)
{
// so here we have a list of selectors, that means we can verify
// that said list is valid (i.e. binary operators are used properly,
// only valid operators were used, etc.)
// any selectors?
if(n->size() <= 1)
{
error::instance() << n->get_position()
<< "a qualified rule without selectors is not valid."
<< error_mode_t::ERROR_ERROR;
return;
}
// compile the selectors using a node parser
// \ref selectors_rules#grammar
if(!parse_selector(n))
{
// an error occurred, forget this entry and move on
return;
}
// compile the block of contents
node::pointer_t brackets(n->get_last_child());
if(brackets->empty())
{
// an empty block is perfectly valid, it means the whole rule
// "exists" but is really useless; in SCSS it could be useful
// for @extends and %placeholder to have such empty rules
//error::instance() << n->get_position()
// << "the {}-block of a qualified rule is missing."
// << error_mode_t::ERROR_ERROR;
return;
}
safe_parents_t safe_parents(f_state, brackets);
for(size_t b(0); b < brackets->size();)
{
node::pointer_t child(brackets->get_child(b));
safe_parents_t safe_list_parents(f_state, child);
if(child->is(node_type_t::LIST))
{
for(size_t idx(0); idx < child->size();)
{
node::pointer_t item(child->get_child(idx));
safe_parents_t safe_sub_parents(f_state, item);
compile_component_value(item);
if(idx < child->size()
&& item == child->get_child(idx))
{
++idx;
}
}
}
else if(child->is(node_type_t::COMPONENT_VALUE))
{
compile_component_value(child);
}
if(b < brackets->size()
&& child == brackets->get_child(b))
{
++b;
}
}
}
void compiler::compile_declaration(node::pointer_t n)
{
// already compiled?
if(n->is(node_type_t::DECLARATION))
{
// We may want to find a better way to skip these entries...
// we replace a COMPONENT_VALUE with a DECLARATION and return
// and the loops think that the COMPONENT_VALUE was "replaced"
// by new code that needs to be compiled; only we replaced the
// entry with already compiled data! The best way may be to have
// a state with a position that we pass around...
return;
}
if(n->size() < 2)
{
// A "declaration" without the ':' and values reaches here:
// font-style; italic; (notice the ';' instead of ':')
error::instance() << n->get_position()
<< "somehow a declaration list is missing a field name or ':'."
<< error_mode_t::ERROR_ERROR;
return;
}
// first make sure we have a declaration
// (i.e. IDENTIFIER WHITESPACE ':' ...)
//
node::pointer_t identifier(n->get_child(0));
if(!identifier->is(node_type_t::IDENTIFIER))
{
if((identifier->is(node_type_t::MULTIPLY)
|| identifier->is(node_type_t::PERIOD))
&& n->get_child(1)->is(node_type_t::IDENTIFIER))
{
// get rid of the asterisk or period
// This was an IE 7 and earlier web browser trick to allow
// various CSS entries only for IE...
n->remove_child(0);
identifier = n->get_child(0);
error::instance() << identifier->get_position()
<< "the '[*|.|!]<field-name>: ...' syntax is not allowed in csspp, we offer other ways to control field names per browser and do not allow such tricks."
<< error_mode_t::ERROR_WARNING;
}
else if(identifier->is(node_type_t::HASH)
|| identifier->is(node_type_t::EXCLAMATION))
{
// the !<id> and #<id> will be converted to a declaration so
// we do not need to do anything more about it
//
// This was an IE 7 and earlier web browser trick to allow
// various CSS entries only for IE...
error::instance() << identifier->get_position()
<< "the '#<field-name>: ...' syntax is not allowed in csspp, we offer other ways to control field names per browser and do not allow such tricks."
<< error_mode_t::ERROR_WARNING;
}
else
{
error::instance() << identifier->get_position()
<< "expected an identifier to start a declaration value; got a: " << identifier->get_type() << " instead."
<< error_mode_t::ERROR_ERROR;
return;
}
}
// the WHITESPACE is optional, if present, remove it
node::pointer_t next(n->get_child(1));
if(next->is(node_type_t::WHITESPACE))
{
n->remove_child(1);
next = n->get_child(1);
}
// now we must have a COLON, also remove that COLON
if(!next->is(node_type_t::COLON))
{
error::instance() << n->get_position()
<< "expected a ':' after the identifier of this declaration value; got a: " << n->get_type() << " instead."
<< error_mode_t::ERROR_ERROR;
return;
}
n->remove_child(1);
if(n->size() < 2)
{
error::instance() << n->get_position()
<< "somehow a declaration list is missing fields, this happens if you used an invalid variable."
<< error_mode_t::ERROR_ERROR;
return;
}
// no need to keep the next whitespace if there is one,
// plus we often do not expect such at the start of a
// list like we are about to generate.
if(n->get_child(1)->is(node_type_t::WHITESPACE))
{
n->remove_child(1);
}
if(n->size() < 2)
{
error::instance() << n->get_position()
<< "somehow a declaration list is missing fields, this happens if you used an invalid variable."
<< error_mode_t::ERROR_ERROR;
return;
}
// create a declaration to replace the identifier
node::pointer_t declaration(new node(node_type_t::DECLARATION, n->get_position()));
declaration->set_string(identifier->get_string());
// copy the following children as the children of the declaration
// (i.e. identifier is element 0, so we copy elements 1 to n)
size_t const max_children(n->size());
for(size_t i(1); i < max_children; ++i)
{
// since we are removing the children, we always seemingly
// copy child 1...
declaration->add_child(n->get_child(1));
n->remove_child(1);
}
// now replace that identifier by its declaration in the parent
if(n->is(node_type_t::COMPONENT_VALUE))
{
// replace the COMPONENT_VALUE instead of the identifier
// (this happens when a component value has multiple entries)
f_state.get_previous_parent()->replace_child(n, declaration);
}