forked from sass/node-sass
/
ast.hpp
3048 lines (2839 loc) · 99.8 KB
/
ast.hpp
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
#ifndef SASS_AST_H
#define SASS_AST_H
#include "sass.hpp"
#include <set>
#include <deque>
#include <vector>
#include <string>
#include <sstream>
#include <iostream>
#include <typeinfo>
#include <algorithm>
#include "sass/base.h"
#include "ast_fwd_decl.hpp"
#ifdef DEBUG_SHARED_PTR
#define ATTACH_VIRTUAL_AST_OPERATIONS(klass) \
virtual klass##_Ptr copy(std::string, size_t) const = 0; \
virtual klass##_Ptr clone(std::string, size_t) const = 0; \
#define ATTACH_AST_OPERATIONS(klass) \
virtual klass##_Ptr copy(std::string, size_t) const; \
virtual klass##_Ptr clone(std::string, size_t) const; \
#else
#define ATTACH_VIRTUAL_AST_OPERATIONS(klass) \
virtual klass##_Ptr copy() const = 0; \
virtual klass##_Ptr clone() const = 0; \
#define ATTACH_AST_OPERATIONS(klass) \
virtual klass##_Ptr copy() const; \
virtual klass##_Ptr clone() const; \
#endif
#ifdef __clang__
/*
* There are some overloads used here that trigger the clang overload
* hiding warning. Specifically:
*
* Type type() which hides string type() from Expression
*
*/
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Woverloaded-virtual"
#endif
#include "util.hpp"
#include "units.hpp"
#include "context.hpp"
#include "position.hpp"
#include "constants.hpp"
#include "operation.hpp"
#include "position.hpp"
#include "inspect.hpp"
#include "source_map.hpp"
#include "environment.hpp"
#include "error_handling.hpp"
#include "ast_def_macros.hpp"
#include "ast_fwd_decl.hpp"
#include "source_map.hpp"
#include "sass.h"
namespace Sass {
// easier to search with name
const bool DELAYED = true;
// ToDo: should this really be hardcoded
// Note: most methods follow precision option
const double NUMBER_EPSILON = 0.00000000000001;
// macro to test if numbers are equal within a small error margin
#define NEAR_EQUAL(lhs, rhs) std::fabs(lhs - rhs) < NUMBER_EPSILON
// ToDo: where does this fit best?
// We don't share this with C-API?
class Operand {
public:
Operand(Sass_OP operand, bool ws_before = false, bool ws_after = false)
: operand(operand), ws_before(ws_before), ws_after(ws_after)
{ }
public:
enum Sass_OP operand;
bool ws_before;
bool ws_after;
};
//////////////////////////////////////////////////////////
// `hash_combine` comes from boost (functional/hash):
// http://www.boost.org/doc/libs/1_35_0/doc/html/hash/combine.html
// Boost Software License - Version 1.0
// http://www.boost.org/users/license.html
template <typename T>
void hash_combine (std::size_t& seed, const T& val)
{
seed ^= std::hash<T>()(val) + 0x9e3779b9
+ (seed<<6) + (seed>>2);
}
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
// Abstract base class for all abstract syntax tree nodes.
//////////////////////////////////////////////////////////
class AST_Node : public SharedObj {
ADD_PROPERTY(ParserState, pstate)
public:
AST_Node(ParserState pstate)
: pstate_(pstate)
{ }
AST_Node(const AST_Node* ptr)
: pstate_(ptr->pstate_)
{ }
// AST_Node(AST_Node& ptr) = delete;
virtual ~AST_Node() = 0;
virtual size_t hash() { return 0; }
ATTACH_VIRTUAL_AST_OPERATIONS(AST_Node);
virtual std::string inspect() const { return to_string({ INSPECT, 5 }); }
virtual std::string to_sass() const { return to_string({ TO_SASS, 5 }); }
virtual const std::string to_string(Sass_Inspect_Options opt) const;
virtual const std::string to_string() const;
virtual void cloneChildren() {};
// generic find function (not fully implemented yet)
// ToDo: add specific implementions to all children
virtual bool find ( bool (*f)(AST_Node_Obj) ) { return f(this); };
public:
void update_pstate(const ParserState& pstate);
public:
Offset off() { return pstate(); }
Position pos() { return pstate(); }
ATTACH_OPERATIONS()
};
inline AST_Node::~AST_Node() { }
//////////////////////////////////////////////////////////////////////
// define cast template now (need complete type)
//////////////////////////////////////////////////////////////////////
template<class T>
T* Cast(AST_Node* ptr) {
return ptr && typeid(T) == typeid(*ptr) ?
static_cast<T*>(ptr) : NULL;
};
template<class T>
const T* Cast(const AST_Node* ptr) {
return ptr && typeid(T) == typeid(*ptr) ?
static_cast<const T*>(ptr) : NULL;
};
//////////////////////////////////////////////////////////////////////
// Abstract base class for expressions. This side of the AST hierarchy
// represents elements in value contexts, which exist primarily to be
// evaluated and returned.
//////////////////////////////////////////////////////////////////////
class Expression : public AST_Node {
public:
enum Concrete_Type {
NONE,
BOOLEAN,
NUMBER,
COLOR,
STRING,
LIST,
MAP,
SELECTOR,
NULL_VAL,
FUNCTION_VAL,
C_WARNING,
C_ERROR,
FUNCTION,
VARIABLE,
NUM_TYPES
};
enum Simple_Type {
SIMPLE,
ATTR_SEL,
PSEUDO_SEL,
WRAPPED_SEL,
};
private:
// expressions in some contexts shouldn't be evaluated
ADD_PROPERTY(bool, is_delayed)
ADD_PROPERTY(bool, is_expanded)
ADD_PROPERTY(bool, is_interpolant)
ADD_PROPERTY(Concrete_Type, concrete_type)
public:
Expression(ParserState pstate,
bool d = false, bool e = false, bool i = false, Concrete_Type ct = NONE)
: AST_Node(pstate),
is_delayed_(d),
is_expanded_(e),
is_interpolant_(i),
concrete_type_(ct)
{ }
Expression(const Expression* ptr)
: AST_Node(ptr),
is_delayed_(ptr->is_delayed_),
is_expanded_(ptr->is_expanded_),
is_interpolant_(ptr->is_interpolant_),
concrete_type_(ptr->concrete_type_)
{ }
virtual operator bool() { return true; }
virtual ~Expression() { }
virtual std::string type() const { return ""; /* TODO: raise an error? */ }
virtual bool is_invisible() const { return false; }
static std::string type_name() { return ""; }
virtual bool is_false() { return false; }
// virtual bool is_true() { return !is_false(); }
virtual bool operator== (const Expression& rhs) const { return false; }
virtual bool eq(const Expression& rhs) const { return *this == rhs; };
virtual void set_delayed(bool delayed) { is_delayed(delayed); }
virtual bool has_interpolant() const { return is_interpolant(); }
virtual bool is_left_interpolant() const { return is_interpolant(); }
virtual bool is_right_interpolant() const { return is_interpolant(); }
virtual std::string inspect() const { return to_string({ INSPECT, 5 }); }
virtual std::string to_sass() const { return to_string({ TO_SASS, 5 }); }
ATTACH_VIRTUAL_AST_OPERATIONS(Expression);
virtual size_t hash() { return 0; }
};
//////////////////////////////////////////////////////////////////////
// Still just an expression, but with a to_string method
//////////////////////////////////////////////////////////////////////
class PreValue : public Expression {
public:
PreValue(ParserState pstate,
bool d = false, bool e = false, bool i = false, Concrete_Type ct = NONE)
: Expression(pstate, d, e, i, ct)
{ }
PreValue(const PreValue* ptr)
: Expression(ptr)
{ }
ATTACH_VIRTUAL_AST_OPERATIONS(PreValue);
virtual ~PreValue() { }
};
//////////////////////////////////////////////////////////////////////
// base class for values that support operations
//////////////////////////////////////////////////////////////////////
class Value : public Expression {
public:
Value(ParserState pstate,
bool d = false, bool e = false, bool i = false, Concrete_Type ct = NONE)
: Expression(pstate, d, e, i, ct)
{ }
Value(const Value* ptr)
: Expression(ptr)
{ }
ATTACH_VIRTUAL_AST_OPERATIONS(Value);
virtual bool operator== (const Expression& rhs) const = 0;
};
}
/////////////////////////////////////////////////////////////////////////////////////
// Hash method specializations for std::unordered_map to work with Sass::Expression
/////////////////////////////////////////////////////////////////////////////////////
namespace std {
template<>
struct hash<Sass::Expression_Obj>
{
size_t operator()(Sass::Expression_Obj s) const
{
return s->hash();
}
};
template<>
struct equal_to<Sass::Expression_Obj>
{
bool operator()( Sass::Expression_Obj lhs, Sass::Expression_Obj rhs) const
{
return lhs->hash() == rhs->hash();
}
};
}
namespace Sass {
/////////////////////////////////////////////////////////////////////////////
// Mixin class for AST nodes that should behave like vectors. Uses the
// "Template Method" design pattern to allow subclasses to adjust their flags
// when certain objects are pushed.
/////////////////////////////////////////////////////////////////////////////
template <typename T>
class Vectorized {
std::vector<T> elements_;
protected:
size_t hash_;
void reset_hash() { hash_ = 0; }
virtual void adjust_after_pushing(T element) { }
public:
Vectorized(size_t s = 0) : elements_(std::vector<T>()), hash_(0)
{ elements_.reserve(s); }
virtual ~Vectorized() = 0;
size_t length() const { return elements_.size(); }
bool empty() const { return elements_.empty(); }
void clear() { return elements_.clear(); }
T last() const { return elements_.back(); }
T first() const { return elements_.front(); }
T& operator[](size_t i) { return elements_[i]; }
virtual const T& at(size_t i) const { return elements_.at(i); }
virtual T& at(size_t i) { return elements_.at(i); }
const T& operator[](size_t i) const { return elements_[i]; }
virtual void append(T element)
{
if (element) {
reset_hash();
elements_.push_back(element);
adjust_after_pushing(element);
}
}
virtual void concat(Vectorized* v)
{
for (size_t i = 0, L = v->length(); i < L; ++i) this->append((*v)[i]);
}
Vectorized& unshift(T element)
{
elements_.insert(elements_.begin(), element);
return *this;
}
std::vector<T>& elements() { return elements_; }
const std::vector<T>& elements() const { return elements_; }
std::vector<T>& elements(std::vector<T>& e) { elements_ = e; return elements_; }
virtual size_t hash()
{
if (hash_ == 0) {
for (T& el : elements_) {
hash_combine(hash_, el->hash());
}
}
return hash_;
}
typename std::vector<T>::iterator end() { return elements_.end(); }
typename std::vector<T>::iterator begin() { return elements_.begin(); }
typename std::vector<T>::const_iterator end() const { return elements_.end(); }
typename std::vector<T>::const_iterator begin() const { return elements_.begin(); }
typename std::vector<T>::iterator erase(typename std::vector<T>::iterator el) { return elements_.erase(el); }
typename std::vector<T>::const_iterator erase(typename std::vector<T>::const_iterator el) { return elements_.erase(el); }
};
template <typename T>
inline Vectorized<T>::~Vectorized() { }
/////////////////////////////////////////////////////////////////////////////
// Mixin class for AST nodes that should behave like a hash table. Uses an
// extra <std::vector> internally to maintain insertion order for interation.
/////////////////////////////////////////////////////////////////////////////
class Hashed {
private:
ExpressionMap elements_;
std::vector<Expression_Obj> list_;
protected:
size_t hash_;
Expression_Obj duplicate_key_;
void reset_hash() { hash_ = 0; }
void reset_duplicate_key() { duplicate_key_ = 0; }
virtual void adjust_after_pushing(std::pair<Expression_Obj, Expression_Obj> p) { }
public:
Hashed(size_t s = 0)
: elements_(ExpressionMap(s)),
list_(std::vector<Expression_Obj>()),
hash_(0), duplicate_key_(NULL)
{ elements_.reserve(s); list_.reserve(s); }
virtual ~Hashed();
size_t length() const { return list_.size(); }
bool empty() const { return list_.empty(); }
bool has(Expression_Obj k) const { return elements_.count(k) == 1; }
Expression_Obj at(Expression_Obj k) const;
bool has_duplicate_key() const { return duplicate_key_ != 0; }
Expression_Obj get_duplicate_key() const { return duplicate_key_; }
const ExpressionMap elements() { return elements_; }
Hashed& operator<<(std::pair<Expression_Obj, Expression_Obj> p)
{
reset_hash();
if (!has(p.first)) list_.push_back(p.first);
else if (!duplicate_key_) duplicate_key_ = p.first;
elements_[p.first] = p.second;
adjust_after_pushing(p);
return *this;
}
Hashed& operator+=(Hashed* h)
{
if (length() == 0) {
this->elements_ = h->elements_;
this->list_ = h->list_;
return *this;
}
for (auto key : h->keys()) {
*this << std::make_pair(key, h->at(key));
}
reset_duplicate_key();
return *this;
}
const ExpressionMap& pairs() const { return elements_; }
const std::vector<Expression_Obj>& keys() const { return list_; }
// std::unordered_map<Expression_Obj, Expression_Obj>::iterator end() { return elements_.end(); }
// std::unordered_map<Expression_Obj, Expression_Obj>::iterator begin() { return elements_.begin(); }
// std::unordered_map<Expression_Obj, Expression_Obj>::const_iterator end() const { return elements_.end(); }
// std::unordered_map<Expression_Obj, Expression_Obj>::const_iterator begin() const { return elements_.begin(); }
};
inline Hashed::~Hashed() { }
/////////////////////////////////////////////////////////////////////////
// Abstract base class for statements. This side of the AST hierarchy
// represents elements in expansion contexts, which exist primarily to be
// rewritten and macro-expanded.
/////////////////////////////////////////////////////////////////////////
class Statement : public AST_Node {
public:
enum Statement_Type {
NONE,
RULESET,
MEDIA,
DIRECTIVE,
SUPPORTS,
ATROOT,
BUBBLE,
CONTENT,
KEYFRAMERULE,
DECLARATION,
ASSIGNMENT,
IMPORT_STUB,
IMPORT,
COMMENT,
WARNING,
RETURN,
EXTEND,
ERROR,
DEBUGSTMT,
WHILE,
EACH,
FOR,
IF
};
private:
ADD_PROPERTY(Statement_Type, statement_type)
ADD_PROPERTY(size_t, tabs)
ADD_PROPERTY(bool, group_end)
public:
Statement(ParserState pstate, Statement_Type st = NONE, size_t t = 0)
: AST_Node(pstate), statement_type_(st), tabs_(t), group_end_(false)
{ }
Statement(const Statement* ptr)
: AST_Node(ptr),
statement_type_(ptr->statement_type_),
tabs_(ptr->tabs_),
group_end_(ptr->group_end_)
{ }
virtual ~Statement() = 0;
// needed for rearranging nested rulesets during CSS emission
virtual bool is_invisible() const { return false; }
virtual bool bubbles() { return false; }
virtual bool has_content()
{
return statement_type_ == CONTENT;
}
};
inline Statement::~Statement() { }
////////////////////////
// Blocks of statements.
////////////////////////
class Block : public Statement, public Vectorized<Statement_Obj> {
ADD_PROPERTY(bool, is_root)
// needed for properly formatted CSS emission
protected:
void adjust_after_pushing(Statement_Obj s)
{
}
public:
Block(ParserState pstate, size_t s = 0, bool r = false)
: Statement(pstate),
Vectorized<Statement_Obj>(s),
is_root_(r)
{ }
Block(const Block* ptr)
: Statement(ptr),
Vectorized<Statement_Obj>(*ptr),
is_root_(ptr->is_root_)
{ }
virtual bool has_content()
{
for (size_t i = 0, L = elements().size(); i < L; ++i) {
if (elements()[i]->has_content()) return true;
}
return Statement::has_content();
}
ATTACH_AST_OPERATIONS(Block)
ATTACH_OPERATIONS()
};
////////////////////////////////////////////////////////////////////////
// Abstract base class for statements that contain blocks of statements.
////////////////////////////////////////////////////////////////////////
class Has_Block : public Statement {
ADD_PROPERTY(Block_Obj, block)
public:
Has_Block(ParserState pstate, Block_Obj b)
: Statement(pstate), block_(b)
{ }
Has_Block(const Has_Block* ptr)
: Statement(ptr), block_(ptr->block_)
{ }
virtual bool has_content()
{
return (block_ && block_->has_content()) || Statement::has_content();
}
virtual ~Has_Block() = 0;
};
inline Has_Block::~Has_Block() { }
/////////////////////////////////////////////////////////////////////////////
// Rulesets (i.e., sets of styles headed by a selector and containing a block
// of style declarations.
/////////////////////////////////////////////////////////////////////////////
class Ruleset : public Has_Block {
ADD_PROPERTY(Selector_List_Obj, selector)
ADD_PROPERTY(bool, is_root);
public:
Ruleset(ParserState pstate, Selector_List_Obj s = 0, Block_Obj b = 0)
: Has_Block(pstate, b), selector_(s), is_root_(false)
{ statement_type(RULESET); }
Ruleset(const Ruleset* ptr)
: Has_Block(ptr),
selector_(ptr->selector_),
is_root_(ptr->is_root_)
{ statement_type(RULESET); }
bool is_invisible() const;
ATTACH_AST_OPERATIONS(Ruleset)
ATTACH_OPERATIONS()
};
/////////////////
// Bubble.
/////////////////
class Bubble : public Statement {
ADD_PROPERTY(Statement_Obj, node)
ADD_PROPERTY(bool, group_end)
public:
Bubble(ParserState pstate, Statement_Obj n, Statement_Obj g = 0, size_t t = 0)
: Statement(pstate, Statement::BUBBLE, t), node_(n), group_end_(g == 0)
{ }
Bubble(const Bubble* ptr)
: Statement(ptr),
node_(ptr->node_),
group_end_(ptr->group_end_)
{ }
bool bubbles() { return true; }
ATTACH_AST_OPERATIONS(Bubble)
ATTACH_OPERATIONS()
};
/////////////////
// Trace.
/////////////////
class Trace : public Has_Block {
ADD_CONSTREF(char, type)
ADD_CONSTREF(std::string, name)
public:
Trace(ParserState pstate, std::string n, Block_Obj b = 0, char type = 'm')
: Has_Block(pstate, b), type_(type), name_(n)
{ }
Trace(const Trace* ptr)
: Has_Block(ptr),
type_(ptr->type_),
name_(ptr->name_)
{ }
ATTACH_AST_OPERATIONS(Trace)
ATTACH_OPERATIONS()
};
/////////////////
// Media queries.
/////////////////
class Media_Block : public Has_Block {
ADD_PROPERTY(List_Obj, media_queries)
public:
Media_Block(ParserState pstate, List_Obj mqs, Block_Obj b)
: Has_Block(pstate, b), media_queries_(mqs)
{ statement_type(MEDIA); }
Media_Block(const Media_Block* ptr)
: Has_Block(ptr), media_queries_(ptr->media_queries_)
{ statement_type(MEDIA); }
bool bubbles() { return true; }
bool is_invisible() const;
ATTACH_AST_OPERATIONS(Media_Block)
ATTACH_OPERATIONS()
};
///////////////////////////////////////////////////////////////////////
// At-rules -- arbitrary directives beginning with "@" that may have an
// optional statement block.
///////////////////////////////////////////////////////////////////////
class Directive : public Has_Block {
ADD_CONSTREF(std::string, keyword)
ADD_PROPERTY(Selector_List_Obj, selector)
ADD_PROPERTY(Expression_Obj, value)
public:
Directive(ParserState pstate, std::string kwd, Selector_List_Obj sel = 0, Block_Obj b = 0, Expression_Obj val = 0)
: Has_Block(pstate, b), keyword_(kwd), selector_(sel), value_(val) // set value manually if needed
{ statement_type(DIRECTIVE); }
Directive(const Directive* ptr)
: Has_Block(ptr),
keyword_(ptr->keyword_),
selector_(ptr->selector_),
value_(ptr->value_) // set value manually if needed
{ statement_type(DIRECTIVE); }
bool bubbles() { return is_keyframes() || is_media(); }
bool is_media() {
return keyword_.compare("@-webkit-media") == 0 ||
keyword_.compare("@-moz-media") == 0 ||
keyword_.compare("@-o-media") == 0 ||
keyword_.compare("@media") == 0;
}
bool is_keyframes() {
return keyword_.compare("@-webkit-keyframes") == 0 ||
keyword_.compare("@-moz-keyframes") == 0 ||
keyword_.compare("@-o-keyframes") == 0 ||
keyword_.compare("@keyframes") == 0;
}
ATTACH_AST_OPERATIONS(Directive)
ATTACH_OPERATIONS()
};
///////////////////////////////////////////////////////////////////////
// Keyframe-rules -- the child blocks of "@keyframes" nodes.
///////////////////////////////////////////////////////////////////////
class Keyframe_Rule : public Has_Block {
// according to css spec, this should be <keyframes-name>
// <keyframes-name> = <custom-ident> | <string>
ADD_PROPERTY(Selector_List_Obj, name)
public:
Keyframe_Rule(ParserState pstate, Block_Obj b)
: Has_Block(pstate, b), name_()
{ statement_type(KEYFRAMERULE); }
Keyframe_Rule(const Keyframe_Rule* ptr)
: Has_Block(ptr), name_(ptr->name_)
{ statement_type(KEYFRAMERULE); }
ATTACH_AST_OPERATIONS(Keyframe_Rule)
ATTACH_OPERATIONS()
};
////////////////////////////////////////////////////////////////////////
// Declarations -- style rules consisting of a property name and values.
////////////////////////////////////////////////////////////////////////
class Declaration : public Has_Block {
ADD_PROPERTY(String_Obj, property)
ADD_PROPERTY(Expression_Obj, value)
ADD_PROPERTY(bool, is_important)
ADD_PROPERTY(bool, is_custom_property)
ADD_PROPERTY(bool, is_indented)
public:
Declaration(ParserState pstate,
String_Obj prop, Expression_Obj val, bool i = false, bool c = false, Block_Obj b = 0)
: Has_Block(pstate, b), property_(prop), value_(val), is_important_(i), is_custom_property_(c), is_indented_(false)
{ statement_type(DECLARATION); }
Declaration(const Declaration* ptr)
: Has_Block(ptr),
property_(ptr->property_),
value_(ptr->value_),
is_important_(ptr->is_important_),
is_custom_property_(ptr->is_custom_property_),
is_indented_(ptr->is_indented_)
{ statement_type(DECLARATION); }
virtual bool is_invisible() const;
ATTACH_AST_OPERATIONS(Declaration)
ATTACH_OPERATIONS()
};
/////////////////////////////////////
// Assignments -- variable and value.
/////////////////////////////////////
class Assignment : public Statement {
ADD_CONSTREF(std::string, variable)
ADD_PROPERTY(Expression_Obj, value)
ADD_PROPERTY(bool, is_default)
ADD_PROPERTY(bool, is_global)
public:
Assignment(ParserState pstate,
std::string var, Expression_Obj val,
bool is_default = false,
bool is_global = false)
: Statement(pstate), variable_(var), value_(val), is_default_(is_default), is_global_(is_global)
{ statement_type(ASSIGNMENT); }
Assignment(const Assignment* ptr)
: Statement(ptr),
variable_(ptr->variable_),
value_(ptr->value_),
is_default_(ptr->is_default_),
is_global_(ptr->is_global_)
{ statement_type(ASSIGNMENT); }
ATTACH_AST_OPERATIONS(Assignment)
ATTACH_OPERATIONS()
};
////////////////////////////////////////////////////////////////////////////
// Import directives. CSS and Sass import lists can be intermingled, so it's
// necessary to store a list of each in an Import node.
////////////////////////////////////////////////////////////////////////////
class Import : public Statement {
std::vector<Expression_Obj> urls_;
std::vector<Include> incs_;
ADD_PROPERTY(List_Obj, import_queries);
public:
Import(ParserState pstate)
: Statement(pstate),
urls_(std::vector<Expression_Obj>()),
incs_(std::vector<Include>()),
import_queries_()
{ statement_type(IMPORT); }
Import(const Import* ptr)
: Statement(ptr),
urls_(ptr->urls_),
incs_(ptr->incs_),
import_queries_(ptr->import_queries_)
{ statement_type(IMPORT); }
std::vector<Expression_Obj>& urls() { return urls_; }
std::vector<Include>& incs() { return incs_; }
ATTACH_AST_OPERATIONS(Import)
ATTACH_OPERATIONS()
};
// not yet resolved single import
// so far we only know requested name
class Import_Stub : public Statement {
Include resource_;
public:
std::string abs_path() { return resource_.abs_path; };
std::string imp_path() { return resource_.imp_path; };
Include resource() { return resource_; };
Import_Stub(ParserState pstate, Include res)
: Statement(pstate), resource_(res)
{ statement_type(IMPORT_STUB); }
Import_Stub(const Import_Stub* ptr)
: Statement(ptr), resource_(ptr->resource_)
{ statement_type(IMPORT_STUB); }
ATTACH_AST_OPERATIONS(Import_Stub)
ATTACH_OPERATIONS()
};
//////////////////////////////
// The Sass `@warn` directive.
//////////////////////////////
class Warning : public Statement {
ADD_PROPERTY(Expression_Obj, message)
public:
Warning(ParserState pstate, Expression_Obj msg)
: Statement(pstate), message_(msg)
{ statement_type(WARNING); }
Warning(const Warning* ptr)
: Statement(ptr), message_(ptr->message_)
{ statement_type(WARNING); }
ATTACH_AST_OPERATIONS(Warning)
ATTACH_OPERATIONS()
};
///////////////////////////////
// The Sass `@error` directive.
///////////////////////////////
class Error : public Statement {
ADD_PROPERTY(Expression_Obj, message)
public:
Error(ParserState pstate, Expression_Obj msg)
: Statement(pstate), message_(msg)
{ statement_type(ERROR); }
Error(const Error* ptr)
: Statement(ptr), message_(ptr->message_)
{ statement_type(ERROR); }
ATTACH_AST_OPERATIONS(Error)
ATTACH_OPERATIONS()
};
///////////////////////////////
// The Sass `@debug` directive.
///////////////////////////////
class Debug : public Statement {
ADD_PROPERTY(Expression_Obj, value)
public:
Debug(ParserState pstate, Expression_Obj val)
: Statement(pstate), value_(val)
{ statement_type(DEBUGSTMT); }
Debug(const Debug* ptr)
: Statement(ptr), value_(ptr->value_)
{ statement_type(DEBUGSTMT); }
ATTACH_AST_OPERATIONS(Debug)
ATTACH_OPERATIONS()
};
///////////////////////////////////////////
// CSS comments. These may be interpolated.
///////////////////////////////////////////
class Comment : public Statement {
ADD_PROPERTY(String_Obj, text)
ADD_PROPERTY(bool, is_important)
public:
Comment(ParserState pstate, String_Obj txt, bool is_important)
: Statement(pstate), text_(txt), is_important_(is_important)
{ statement_type(COMMENT); }
Comment(const Comment* ptr)
: Statement(ptr),
text_(ptr->text_),
is_important_(ptr->is_important_)
{ statement_type(COMMENT); }
virtual bool is_invisible() const
{ return /* is_important() == */ false; }
ATTACH_AST_OPERATIONS(Comment)
ATTACH_OPERATIONS()
};
////////////////////////////////////
// The Sass `@if` control directive.
////////////////////////////////////
class If : public Has_Block {
ADD_PROPERTY(Expression_Obj, predicate)
ADD_PROPERTY(Block_Obj, alternative)
public:
If(ParserState pstate, Expression_Obj pred, Block_Obj con, Block_Obj alt = 0)
: Has_Block(pstate, con), predicate_(pred), alternative_(alt)
{ statement_type(IF); }
If(const If* ptr)
: Has_Block(ptr),
predicate_(ptr->predicate_),
alternative_(ptr->alternative_)
{ statement_type(IF); }
virtual bool has_content()
{
return Has_Block::has_content() || (alternative_ && alternative_->has_content());
}
ATTACH_AST_OPERATIONS(If)
ATTACH_OPERATIONS()
};
/////////////////////////////////////
// The Sass `@for` control directive.
/////////////////////////////////////
class For : public Has_Block {
ADD_CONSTREF(std::string, variable)
ADD_PROPERTY(Expression_Obj, lower_bound)
ADD_PROPERTY(Expression_Obj, upper_bound)
ADD_PROPERTY(bool, is_inclusive)
public:
For(ParserState pstate,
std::string var, Expression_Obj lo, Expression_Obj hi, Block_Obj b, bool inc)
: Has_Block(pstate, b),
variable_(var), lower_bound_(lo), upper_bound_(hi), is_inclusive_(inc)
{ statement_type(FOR); }
For(const For* ptr)
: Has_Block(ptr),
variable_(ptr->variable_),
lower_bound_(ptr->lower_bound_),
upper_bound_(ptr->upper_bound_),
is_inclusive_(ptr->is_inclusive_)
{ statement_type(FOR); }
ATTACH_AST_OPERATIONS(For)
ATTACH_OPERATIONS()
};
//////////////////////////////////////
// The Sass `@each` control directive.
//////////////////////////////////////
class Each : public Has_Block {
ADD_PROPERTY(std::vector<std::string>, variables)
ADD_PROPERTY(Expression_Obj, list)
public:
Each(ParserState pstate, std::vector<std::string> vars, Expression_Obj lst, Block_Obj b)
: Has_Block(pstate, b), variables_(vars), list_(lst)
{ statement_type(EACH); }
Each(const Each* ptr)
: Has_Block(ptr), variables_(ptr->variables_), list_(ptr->list_)
{ statement_type(EACH); }
ATTACH_AST_OPERATIONS(Each)
ATTACH_OPERATIONS()
};
///////////////////////////////////////
// The Sass `@while` control directive.
///////////////////////////////////////
class While : public Has_Block {
ADD_PROPERTY(Expression_Obj, predicate)
public:
While(ParserState pstate, Expression_Obj pred, Block_Obj b)
: Has_Block(pstate, b), predicate_(pred)
{ statement_type(WHILE); }
While(const While* ptr)
: Has_Block(ptr), predicate_(ptr->predicate_)
{ statement_type(WHILE); }
ATTACH_AST_OPERATIONS(While)
ATTACH_OPERATIONS()
};
/////////////////////////////////////////////////////////////
// The @return directive for use inside SassScript functions.
/////////////////////////////////////////////////////////////
class Return : public Statement {
ADD_PROPERTY(Expression_Obj, value)
public:
Return(ParserState pstate, Expression_Obj val)
: Statement(pstate), value_(val)
{ statement_type(RETURN); }
Return(const Return* ptr)
: Statement(ptr), value_(ptr->value_)
{ statement_type(RETURN); }
ATTACH_AST_OPERATIONS(Return)
ATTACH_OPERATIONS()
};
////////////////////////////////
// The Sass `@extend` directive.
////////////////////////////////
class Extension : public Statement {
ADD_PROPERTY(Selector_List_Obj, selector)
public:
Extension(ParserState pstate, Selector_List_Obj s)
: Statement(pstate), selector_(s)
{ statement_type(EXTEND); }
Extension(const Extension* ptr)
: Statement(ptr), selector_(ptr->selector_)
{ statement_type(EXTEND); }
ATTACH_AST_OPERATIONS(Extension)
ATTACH_OPERATIONS()
};
/////////////////////////////////////////////////////////////////////////////
// Definitions for both mixins and functions. The two cases are distinguished
// by a type tag.
/////////////////////////////////////////////////////////////////////////////
struct Backtrace;
typedef Expression_Ptr (*Native_Function)(Env&, Env&, Context&, Signature, ParserState, Backtraces, SelectorStack);
class Definition : public Has_Block {
public:
enum Type { MIXIN, FUNCTION };
ADD_CONSTREF(std::string, name)
ADD_PROPERTY(Parameters_Obj, parameters)
ADD_PROPERTY(Env*, environment)
ADD_PROPERTY(Type, type)
ADD_PROPERTY(Native_Function, native_function)
ADD_PROPERTY(Sass_Function_Entry, c_function)
ADD_PROPERTY(void*, cookie)
ADD_PROPERTY(bool, is_overload_stub)
ADD_PROPERTY(Signature, signature)
public:
Definition(const Definition* ptr)
: Has_Block(ptr),
name_(ptr->name_),
parameters_(ptr->parameters_),
environment_(ptr->environment_),
type_(ptr->type_),
native_function_(ptr->native_function_),
c_function_(ptr->c_function_),
cookie_(ptr->cookie_),
is_overload_stub_(ptr->is_overload_stub_),
signature_(ptr->signature_)
{ }
Definition(ParserState pstate,
std::string n,
Parameters_Obj params,
Block_Obj b,
Type t)
: Has_Block(pstate, b),
name_(n),
parameters_(params),
environment_(0),
type_(t),
native_function_(0),
c_function_(0),
cookie_(0),
is_overload_stub_(false),
signature_(0)
{ }
Definition(ParserState pstate,
Signature sig,
std::string n,
Parameters_Obj params,
Native_Function func_ptr,
bool overload_stub = false)
: Has_Block(pstate, 0),
name_(n),
parameters_(params),
environment_(0),
type_(FUNCTION),