forked from sass/libsass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.cpp
600 lines (518 loc) · 18.2 KB
/
ast.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
#include "ast.hpp"
#include "context.hpp"
#include "to_string.hpp"
#include <set>
#include <algorithm>
#include <iostream>
namespace Sass {
using namespace std;
static Null sass_null(Sass::Null(ParserState("null")));
bool Compound_Selector::operator<(const Compound_Selector& rhs) const
{
To_String to_string;
// ugly
return const_cast<Compound_Selector*>(this)->perform(&to_string) <
const_cast<Compound_Selector&>(rhs).perform(&to_string);
}
bool Complex_Selector::operator<(const Complex_Selector& rhs) const
{
To_String to_string;
return const_cast<Complex_Selector*>(this)->perform(&to_string) <
const_cast<Complex_Selector&>(rhs).perform(&to_string);
}
bool Complex_Selector::operator==(const Complex_Selector& rhs) const {
// TODO: We have to access the tail directly using tail_ since ADD_PROPERTY doesn't provide a const version.
const Complex_Selector* pOne = this;
const Complex_Selector* pTwo = &rhs;
// Consume any empty references at the beginning of the Complex_Selector
if (pOne->combinator() == Complex_Selector::ANCESTOR_OF && pOne->head()->is_empty_reference()) {
pOne = pOne->tail_;
}
if (pTwo->combinator() == Complex_Selector::ANCESTOR_OF && pTwo->head()->is_empty_reference()) {
pTwo = pTwo->tail_;
}
while (pOne && pTwo) {
if (pOne->combinator() != pTwo->combinator()) {
return false;
}
if (*(pOne->head()) != *(pTwo->head())) {
return false;
}
pOne = pOne->tail_;
pTwo = pTwo->tail_;
}
return pOne == NULL && pTwo == NULL;
}
Compound_Selector* Compound_Selector::unify_with(Compound_Selector* rhs, Context& ctx)
{
Compound_Selector* unified = rhs;
for (size_t i = 0, L = length(); i < L; ++i)
{
if (!unified) break;
else unified = (*this)[i]->unify_with(unified, ctx);
}
return unified;
}
bool Simple_Selector::operator==(const Simple_Selector& rhs) const
{
// Compare the string representations for equality.
// Cast away const here. To_String should take a const object, but it doesn't.
Simple_Selector* pLHS = const_cast<Simple_Selector*>(this);
Simple_Selector* pRHS = const_cast<Simple_Selector*>(&rhs);
To_String to_string;
return pLHS->perform(&to_string) == pRHS->perform(&to_string);
}
bool Simple_Selector::operator<(const Simple_Selector& rhs) const {
// Use the string representation for ordering.
// Cast away const here. To_String should take a const object, but it doesn't.
Simple_Selector* pLHS = const_cast<Simple_Selector*>(this);
Simple_Selector* pRHS = const_cast<Simple_Selector*>(&rhs);
To_String to_string;
return pLHS->perform(&to_string) < pRHS->perform(&to_string);
}
Compound_Selector* Simple_Selector::unify_with(Compound_Selector* rhs, Context& ctx)
{
To_String to_string(&ctx);
for (size_t i = 0, L = rhs->length(); i < L; ++i)
{ if (perform(&to_string) == (*rhs)[i]->perform(&to_string)) return rhs; }
// check for pseudo elements because they need to come last
size_t i, L;
bool found = false;
if (typeid(*this) == typeid(Pseudo_Selector) || typeid(*this) == typeid(Wrapped_Selector))
{
for (i = 0, L = rhs->length(); i < L; ++i)
{
if ((typeid(*(*rhs)[i]) == typeid(Pseudo_Selector) || typeid(*(*rhs)[i]) == typeid(Wrapped_Selector)) && (*rhs)[L-1]->is_pseudo_element())
{ found = true; break; }
}
}
else
{
for (i = 0, L = rhs->length(); i < L; ++i)
{
if (typeid(*(*rhs)[i]) == typeid(Pseudo_Selector) || typeid(*(*rhs)[i]) == typeid(Wrapped_Selector))
{ found = true; break; }
}
}
if (!found)
{
Compound_Selector* cpy = new (ctx.mem) Compound_Selector(*rhs);
(*cpy) << this;
return cpy;
}
Compound_Selector* cpy = new (ctx.mem) Compound_Selector(rhs->pstate());
for (size_t j = 0; j < i; ++j)
{ (*cpy) << (*rhs)[j]; }
(*cpy) << this;
for (size_t j = i; j < L; ++j)
{ (*cpy) << (*rhs)[j]; }
return cpy;
}
Compound_Selector* Type_Selector::unify_with(Compound_Selector* rhs, Context& ctx)
{
// TODO: handle namespaces
// if the rhs is empty, just return a copy of this
if (rhs->length() == 0) {
Compound_Selector* cpy = new (ctx.mem) Compound_Selector(rhs->pstate());
(*cpy) << this;
return cpy;
}
// if this is a universal selector and rhs is not empty, just return the rhs
if (name() == "*")
{ return new (ctx.mem) Compound_Selector(*rhs); }
Simple_Selector* rhs_0 = (*rhs)[0];
// otherwise, this is a tag name
if (typeid(*rhs_0) == typeid(Type_Selector))
{
// if rhs is universal, just return this tagname + rhs's qualifiers
if (static_cast<Type_Selector*>(rhs_0)->name() == "*")
{
Compound_Selector* cpy = new (ctx.mem) Compound_Selector(rhs->pstate());
(*cpy) << this;
for (size_t i = 1, L = rhs->length(); i < L; ++i)
{ (*cpy) << (*rhs)[i]; }
return cpy;
}
// if rhs is another tag name and it matches this, return rhs
else if (static_cast<Type_Selector*>(rhs_0)->name() == name())
{ return new (ctx.mem) Compound_Selector(*rhs); }
// else the tag names don't match; return nil
else
{ return 0; }
}
// else it's a tag name and a bunch of qualifiers -- just append them
Compound_Selector* cpy = new (ctx.mem) Compound_Selector(rhs->pstate());
(*cpy) << this;
(*cpy) += rhs;
return cpy;
}
Compound_Selector* Selector_Qualifier::unify_with(Compound_Selector* rhs, Context& ctx)
{
if (name()[0] == '#')
{
for (size_t i = 0, L = rhs->length(); i < L; ++i)
{
Simple_Selector* rhs_i = (*rhs)[i];
if (typeid(*rhs_i) == typeid(Selector_Qualifier) &&
static_cast<Selector_Qualifier*>(rhs_i)->name()[0] == '#' &&
static_cast<Selector_Qualifier*>(rhs_i)->name() != name())
return 0;
}
}
rhs->has_line_break(has_line_break());
return Simple_Selector::unify_with(rhs, ctx);
}
Compound_Selector* Pseudo_Selector::unify_with(Compound_Selector* rhs, Context& ctx)
{
if (is_pseudo_element())
{
for (size_t i = 0, L = rhs->length(); i < L; ++i)
{
Simple_Selector* rhs_i = (*rhs)[i];
if (typeid(*rhs_i) == typeid(Pseudo_Selector) &&
static_cast<Pseudo_Selector*>(rhs_i)->is_pseudo_element() &&
static_cast<Pseudo_Selector*>(rhs_i)->name() != name())
{ return 0; }
}
}
return Simple_Selector::unify_with(rhs, ctx);
}
bool Compound_Selector::is_superselector_of(Compound_Selector* rhs)
{
To_String to_string;
Simple_Selector* lbase = base();
Simple_Selector* rbase = rhs->base();
// Check if pseudo-elements are the same between the selectors
set<string> lpsuedoset, rpsuedoset;
for (size_t i = 0, L = length(); i < L; ++i)
{
if ((*this)[i]->is_pseudo_element()) {
string pseudo((*this)[i]->perform(&to_string));
pseudo = pseudo.substr(pseudo.find_first_not_of(":")); // strip off colons to ensure :after matches ::after since ruby sass is forgiving
lpsuedoset.insert(pseudo);
}
}
for (size_t i = 0, L = rhs->length(); i < L; ++i)
{
if ((*rhs)[i]->is_pseudo_element()) {
string pseudo((*rhs)[i]->perform(&to_string));
pseudo = pseudo.substr(pseudo.find_first_not_of(":")); // strip off colons to ensure :after matches ::after since ruby sass is forgiving
rpsuedoset.insert(pseudo);
}
}
if (lpsuedoset != rpsuedoset) {
return false;
}
// Check the Simple_Selectors
set<string> lset, rset;
if (!lbase) // no lbase; just see if the left-hand qualifiers are a subset of the right-hand selector
{
for (size_t i = 0, L = length(); i < L; ++i)
{ lset.insert((*this)[i]->perform(&to_string)); }
for (size_t i = 0, L = rhs->length(); i < L; ++i)
{ rset.insert((*rhs)[i]->perform(&to_string)); }
return includes(rset.begin(), rset.end(), lset.begin(), lset.end());
}
else { // there's an lbase
for (size_t i = 1, L = length(); i < L; ++i)
{ lset.insert((*this)[i]->perform(&to_string)); }
if (rbase)
{
if (lbase->perform(&to_string) != rbase->perform(&to_string)) // if there's an rbase, make sure they match
{ return false; }
else // the bases do match, so compare qualifiers
{
for (size_t i = 1, L = rhs->length(); i < L; ++i)
{ rset.insert((*rhs)[i]->perform(&to_string)); }
return includes(rset.begin(), rset.end(), lset.begin(), lset.end());
}
}
}
// catch-all
return false;
}
bool Compound_Selector::operator==(const Compound_Selector& rhs) const {
To_String to_string;
// Check if pseudo-elements are the same between the selectors
set<string> lpsuedoset, rpsuedoset;
for (size_t i = 0, L = length(); i < L; ++i)
{
if ((*this)[i]->is_pseudo_element()) {
string pseudo((*this)[i]->perform(&to_string));
pseudo = pseudo.substr(pseudo.find_first_not_of(":")); // strip off colons to ensure :after matches ::after since ruby sass is forgiving
lpsuedoset.insert(pseudo);
}
}
for (size_t i = 0, L = rhs.length(); i < L; ++i)
{
if (rhs[i]->is_pseudo_element()) {
string pseudo(rhs[i]->perform(&to_string));
pseudo = pseudo.substr(pseudo.find_first_not_of(":")); // strip off colons to ensure :after matches ::after since ruby sass is forgiving
rpsuedoset.insert(pseudo);
}
}
if (lpsuedoset != rpsuedoset) {
return false;
}
// Check the base
const Simple_Selector* const lbase = base();
const Simple_Selector* const rbase = rhs.base();
if ((lbase && !rbase) ||
(!lbase && rbase) ||
((lbase && rbase) && (*lbase != *rbase))) {
return false;
}
// Check the rest of the SimpleSelectors
// Use string representations. We can't create a set of Simple_Selector pointers because std::set == std::set is going to call ==
// on the pointers to determine equality. I don't know of a way to pass in a comparison object. The one you can specify as part of
// the template type is used for ordering, but not equality. We also can't just put in non-pointer Simple_Selectors because the
// class is intended to be subclassed, and we'd get splicing.
set<string> lset, rset;
for (size_t i = 0, L = length(); i < L; ++i)
{ lset.insert((*this)[i]->perform(&to_string)); }
for (size_t i = 0, L = rhs.length(); i < L; ++i)
{ rset.insert(rhs[i]->perform(&to_string)); }
return lset == rset;
}
bool Complex_Selector_Pointer_Compare::operator() (const Complex_Selector* const pLeft, const Complex_Selector* const pRight) const {
return *pLeft < *pRight;
}
bool Complex_Selector::is_superselector_of(Compound_Selector* rhs)
{
if (length() != 1)
{ return false; }
return base()->is_superselector_of(rhs);
}
bool Complex_Selector::is_superselector_of(Complex_Selector* rhs)
{
Complex_Selector* lhs = this;
To_String to_string;
// check for selectors with leading or trailing combinators
if (!lhs->head() || !rhs->head())
{ return false; }
Complex_Selector* l_innermost = lhs->innermost();
if (l_innermost->combinator() != Complex_Selector::ANCESTOR_OF && !l_innermost->tail())
{ return false; }
Complex_Selector* r_innermost = rhs->innermost();
if (r_innermost->combinator() != Complex_Selector::ANCESTOR_OF && !r_innermost->tail())
{ return false; }
// more complex (i.e., longer) selectors are always more specific
size_t l_len = lhs->length(), r_len = rhs->length();
if (l_len > r_len)
{ return false; }
if (l_len == 1)
{ return lhs->head()->is_superselector_of(rhs->base()); }
bool found = false;
Complex_Selector* marker = rhs;
for (size_t i = 0, L = rhs->length(); i < L; ++i) {
if (i == L-1)
{ return false; }
if (lhs->head()->is_superselector_of(marker->head()))
{ found = true; break; }
marker = marker->tail();
}
if (!found)
{ return false; }
/*
Hmm, I hope I have the logic right:
if lhs has a combinator:
if !(marker has a combinator) return false
if !(lhs.combinator == '~' ? marker.combinator != '>' : lhs.combinator == marker.combinator) return false
return lhs.tail-without-innermost.is_superselector_of(marker.tail-without-innermost)
else if marker has a combinator:
if !(marker.combinator == ">") return false
return lhs.tail.is_superselector_of(marker.tail)
else
return lhs.tail.is_superselector_of(marker.tail)
*/
if (lhs->combinator() != Complex_Selector::ANCESTOR_OF)
{
if (marker->combinator() == Complex_Selector::ANCESTOR_OF)
{ return false; }
if (!(lhs->combinator() == Complex_Selector::PRECEDES ? marker->combinator() != Complex_Selector::PARENT_OF : lhs->combinator() == marker->combinator()))
{ return false; }
return lhs->tail()->is_superselector_of(marker->tail());
}
else if (marker->combinator() != Complex_Selector::ANCESTOR_OF)
{
if (marker->combinator() != Complex_Selector::PARENT_OF)
{ return false; }
return lhs->tail()->is_superselector_of(marker->tail());
}
else
{
return lhs->tail()->is_superselector_of(marker->tail());
}
// catch-all
return false;
}
size_t Complex_Selector::length()
{
// TODO: make this iterative
if (!tail()) return 1;
return 1 + tail()->length();
}
Compound_Selector* Complex_Selector::base()
{
if (!tail()) return head();
else return tail()->base();
}
Complex_Selector* Complex_Selector::context(Context& ctx)
{
if (!tail()) return 0;
if (!head()) return tail()->context(ctx);
Complex_Selector* cpy = new (ctx.mem) Complex_Selector(pstate(), combinator(), head(), tail()->context(ctx));
cpy->media_block(media_block());
cpy->last_block(last_block());
return cpy;
}
Complex_Selector* Complex_Selector::innermost()
{
if (!tail()) return this;
else return tail()->innermost();
}
Complex_Selector::Combinator Complex_Selector::clear_innermost()
{
Combinator c;
if (!tail() || tail()->length() == 1)
{ c = combinator(); combinator(ANCESTOR_OF); tail(0); }
else
{ c = tail()->clear_innermost(); }
return c;
}
void Complex_Selector::set_innermost(Complex_Selector* val, Combinator c)
{
if (!tail())
{ tail(val); combinator(c); }
else
{ tail()->set_innermost(val, c); }
}
Complex_Selector* Complex_Selector::clone(Context& ctx) const
{
Complex_Selector* cpy = new (ctx.mem) Complex_Selector(*this);
if (tail()) cpy->tail(tail()->clone(ctx));
return cpy;
}
Complex_Selector* Complex_Selector::cloneFully(Context& ctx) const
{
Complex_Selector* cpy = new (ctx.mem) Complex_Selector(*this);
if (head()) {
cpy->head(head()->clone(ctx));
}
if (tail()) {
cpy->tail(tail()->cloneFully(ctx));
}
return cpy;
}
Compound_Selector* Compound_Selector::clone(Context& ctx) const
{
Compound_Selector* cpy = new (ctx.mem) Compound_Selector(*this);
return cpy;
}
/* not used anymore - remove?
Selector_Placeholder* Selector::find_placeholder()
{
return 0;
}*/
void Selector_List::adjust_after_pushing(Complex_Selector* c)
{
if (c->has_reference()) has_reference(true);
if (c->has_placeholder()) has_placeholder(true);
#ifdef DEBUG
To_String to_string;
this->mCachedSelector(this->perform(&to_string));
#endif
}
/* not used anymore - remove?
Selector_Placeholder* Selector_List::find_placeholder()
{
if (has_placeholder()) {
for (size_t i = 0, L = length(); i < L; ++i) {
if ((*this)[i]->has_placeholder()) return (*this)[i]->find_placeholder();
}
}
return 0;
}*/
/* not used anymore - remove?
Selector_Placeholder* Complex_Selector::find_placeholder()
{
if (has_placeholder()) {
if (head() && head()->has_placeholder()) return head()->find_placeholder();
else if (tail() && tail()->has_placeholder()) return tail()->find_placeholder();
}
return 0;
}*/
/* not used anymore - remove?
Selector_Placeholder* Compound_Selector::find_placeholder()
{
if (has_placeholder()) {
for (size_t i = 0, L = length(); i < L; ++i) {
if ((*this)[i]->has_placeholder()) return (*this)[i]->find_placeholder();
}
// return this;
}
return 0;
}*/
/* not used anymore - remove?
Selector_Placeholder* Selector_Placeholder::find_placeholder()
{
return this;
}*/
vector<string> Compound_Selector::to_str_vec()
{
To_String to_string;
vector<string> result;
result.reserve(length());
for (size_t i = 0, L = length(); i < L; ++i)
{ result.push_back((*this)[i]->perform(&to_string)); }
return result;
}
Compound_Selector* Compound_Selector::minus(Compound_Selector* rhs, Context& ctx)
{
To_String to_string(&ctx);
Compound_Selector* result = new (ctx.mem) Compound_Selector(pstate());
// not very efficient because it needs to preserve order
for (size_t i = 0, L = length(); i < L; ++i)
{
bool found = false;
string thisSelector((*this)[i]->perform(&to_string));
for (size_t j = 0, M = rhs->length(); j < M; ++j)
{
if (thisSelector == (*rhs)[j]->perform(&to_string))
{
found = true;
break;
}
}
if (!found) (*result) << (*this)[i];
}
return result;
}
void Compound_Selector::mergeSources(SourcesSet& sources, Context& ctx)
{
for (SourcesSet::iterator iterator = sources.begin(), endIterator = sources.end(); iterator != endIterator; ++iterator) {
this->sources_.insert((*iterator)->clone(ctx));
}
}
/* not used anymore - remove?
vector<Compound_Selector*> Complex_Selector::to_vector()
{
vector<Compound_Selector*> result;
Compound_Selector* h = head();
Complex_Selector* t = tail();
if (h) result.push_back(h);
while (t)
{
h = t->head();
t = t->tail();
if (h) result.push_back(h);
}
return result;
}*/
Expression* Hashed::at(Expression* k) const
{
if (elements_.count(k))
{ return elements_.at(k); }
else { return &sass_null; }
}
}