-
Notifications
You must be signed in to change notification settings - Fork 10
/
Parser.php
1205 lines (1027 loc) · 33 KB
/
Parser.php
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
<?php
/**
* The Tale Jade Parser.
*
* Contains the parser that takes tokens from the lexer
* and converts it to an Abstract Syntax Tree (AST)
*
* This file is part of the Tale Jade Template Engine for PHP
*
* LICENSE:
* The code of this file is distributed under the MIT license.
* If you didn't receive a copy of the license text, you can
* read it here https://github.com/Talesoft/tale-jade/blob/master/LICENSE.md
*
* @category Presentation
* @package Tale\Jade
* @author Torben Koehn <torben@talesoft.codes>
* @author Talesoft <info@talesoft.codes>
* @copyright Copyright (c) 2015-2016 Torben Köhn (http://talesoft.codes)
* @license https://github.com/Talesoft/tale-jade/blob/master/LICENSE.md MIT License
* @version 1.4.5
* @link http://jade.talesoft.codes/docs/files/Parser.html
* @since File available since Release 1.0
*/
namespace Tale\Jade;
use Tale\ConfigurableTrait;
use Tale\Jade\Parser\Node;
use Tale\Jade\Parser\Exception;
/**
* Takes tokens from the Lexer and creates an AST out of it.
*
* This class takes generated tokens from the Lexer sequentially
* and produces an Abstract Syntax Tree (AST) out of it
*
* The AST is an object-tree containing Node-instances
* with parent/child relations
*
* This AST is passed to the compiler to generate PHTML out of it
*
* Usage example:
* <code>
*
* use Tale\Jade\Parser;
*
* $parser = new Parser();
*
* echo $parser->parse($jadeInput);
* //Prints a human-readable dump of the parsed nodes
*
* </code>
*
* @category Presentation
* @package Tale\Jade
* @author Torben Koehn <torben@talesoft.codes>
* @author Talesoft <info@talesoft.codes>
* @copyright Copyright (c) 2015-2016 Torben Köhn (http://talesoft.codes)
* @license https://github.com/Talesoft/tale-jade/blob/master/LICENSE.md MIT License
* @version 1.4.5
* @link http://jade.talesoft.codes/docs/classes/Tale.Jade.Parser.html
* @since File available since Release 1.0
*/
class Parser
{
use ConfigurableTrait;
/**
* The lexer used in this parser instance.
*
* @var Lexer
*/
private $lexer;
/**
* The level we're currently on.
*
* This does not equal the Lexer-level or Compiler-level,
* it's an internal level to get the child/parent-relation between
* nodes right
*
* @var int
*/
private $level;
/**
* The Generator returned by the ->lex() method of the lexer.
*
* @var \Generator
*/
private $tokens;
/**
* The root node of the currently parsed document.
*
* @var Node
*/
private $document;
/**
* The parent that currently found childs are appended to.
*
* When an <outdent>-token is encountered, it moves one parent up
* ($_currentParent->parent becomes the new $_currentParent)
*
* @var Node
*/
private $currentParent;
/**
* The current element in the queue.
*
* Will be appended to $_currentParent when a <newLine>-token is encountered
* It will become the current parent, if an <indent>-token is encountered
*
* @var Node
*/
private $current;
/**
* The last element that was completely put together.
*
* Will be set on a <newLine>-token ($_current will become last)
*
* @var Node
*/
private $last;
/**
* States if we're in a mixin or not.
*
* Used to check for the mixin-block and nested mixins
*
* @var bool
*/
private $inMixin;
/**
* The level we're on inside a mixin.
*
* Used to check for the mixin-block and nested mixins
*
* @var int
*/
private $mixinLevel;
/**
* Stores an expanded node to attach it to the expanding node later.
*
* @var Node
*/
private $expansion;
/**
* Creates a new parser instance.
*
* The parser will run the provided input through the lexer
* and generate an AST out of it.
*
* The AST will be an object-tree consisting of \Tale\Jade\Parser\Node instances
*
* You can take the AST and either compile it with the Compiler or handle it yourself
*
* Possible options are:
*
* lexerOptions: The options for the lexer
*
* @param array|null $options the options array
* @param Lexer|null $lexer an existing lexer instance (lexer-option will be ignored)
*/
public function __construct(array $options = null, Lexer $lexer = null)
{
$this->defineOptions(['lexer_options' => []], $options);
$this->lexer = $lexer ? $lexer : new Lexer($this->options['lexer_options']);
}
/**
* Returns the currently used Lexer instance.
*
* @return Lexer
*/
public function getLexer()
{
return $this->lexer;
}
/**
* Parses the provided input-string to an AST.
*
* The Abstract Syntax Tree (AST) will be an object-tree consisting of \Tale\Jade\Parser\Node instances.
*
* You can either let the compiler compile it or compile it yourself
*
* The root-node will always be of type 'document',
* from there on it can contain several kinds of nodes
*
* @param string $input the input jade string that is to be parsed
*
* @return Node the root-node of the parsed AST
*/
public function parse($input)
{
$this->level = 0;
$this->tokens = $this->lexer->lex($input);
$this->document = $this->createNode('document', ['line' => 0, 'offset' => 0]);
$this->currentParent = $this->document;
$this->current = null;
$this->last = null;
$this->inMixin = false;
$this->mixinLevel = null;
$this->expansion = null;
//Fix HHVM generators needing ->next() before ->current()
//This will actually work as expected, no node will be skipped
//HHVM always needs a first ->next() (I don't know if this is a bug or
//expected behaviour)
if (defined('HHVM_VERSION')) {
$this->tokens->next();
}
//While we have tokens, handle current token, then go to next token
//rinse and repeat
while ($this->hasTokens()) {
$this->handleToken();
$this->nextToken();
}
//Return the final document node with all its awesome child nodes
return $this->document;
}
/**
* Handles any kind of token returned by the lexer dynamically.
*
* The token-type will be translated into a method name
* e.g.
*
* newLine => handleNewLine
* attribute => handleAttribute
* tag => handleTag
*
* First argument of that method will always be the token array
*
* If no token is passed, it will take the current token
* in the lexer's token generator
*
* @param array|null $token a token or the current lexer's generator token
*
* @throws Exception when no token handler has been found
*/
protected function handleToken(array $token = null)
{
$token = $token ? $token : $this->getToken();
//Put together the method name
$method = 'handle'.ucfirst($token['type']);
//If the token has no handler, we throw an error
if (!method_exists($this, $method)) {
$this->throwException(
"Unexpected token `{$token['type']}` encountered, no handler $method found. ".
"It seems you added custom tokens. Please extend the Parser and add a $method-method for that token",
$token
);
} else {
//Call the handler method and pass the token array as the first argument
call_user_func([$this, $method], $token);
}
}
/**
* Yields tokens as long as the given types match.
*
* Yields tokens of the given types until
* a token is encountered, that is not given
* in the types-array
*
* @param array $types the token types that are allowed
*
* @return \Generator
*/
protected function lookUp(array $types)
{
while ($this->hasTokens()) {
$token = $this->getToken();
if (in_array($token['type'], $types, true))
yield $token;
else
break;
$this->nextToken();
}
}
/**
* Moves on the token generator by one and does ->lookUp().
*
* @see Parser->nextToken
* @see Parser->lookUp
*
* @param array $types the types that are allowed
*
* @return \Generator
*/
protected function lookUpNext(array $types)
{
return $this->nextToken()->lookUp($types);
}
/**
* Returns the token of the given type if it is in the token queue.
*
* If the given token in the queue is not of the given type,
* this method returns null
*
* @param array $types the types that are expected
*
* @return Node|null
*/
protected function expect(array $types)
{
foreach ($this->lookUp($types) as $token) {
return $token;
}
return null;
}
/**
* Moves the generator on by one and does ->expect().
*
* @see Parser->nextToken
* @see Parser->expect
*
* @param array $types the types that are expected
*
* @return Node|null
*/
protected function expectNext(array $types)
{
return $this->nextToken()->expect($types);
}
/**
* Throws an exception if the next token is not a newLine token.
*
* This states that "a line of instructions should end here"
*
* Notice that if the next token is _not_ a newLine, it gets
* handled through handleToken automatically
*
* @param array|null $relatedToken the token to relate the exception to
*
* @throws Exception when the next token is not a newLine token
*/
protected function expectEnd(array $relatedToken = null)
{
foreach ($this->lookUpNext(['newLine']) as $token) {
$this->handleToken($token);
return;
}
if (!$this->expectNext(['newLine'])) {
$this->throwException(
"Nothing should be following this statement, the end of line is expected.",
$relatedToken
);
} else
$this->handleToken();
}
/**
* Returns true, if there are still tokens left to be generated.
*
* If the lexer-generator still has tokens to generate,
* this returns true and false, if it doesn't
*
* @see \Generator->valid
*
* @return bool
*/
protected function hasTokens()
{
return $this->tokens->valid();
}
/**
* Moves the generator on by one token.
*
* (It calls ->next() on the generator, look at the PHP doc)
*
* @see \Generator->next
*
* @return $this
*/
protected function nextToken()
{
$this->tokens->next();
return $this;
}
/**
* Returns the current token in the lexer generator.
*
* @see \Generator->current
*
* @return array the token array (always _one_ token, as an array)
*/
protected function getToken()
{
return $this->tokens->current();
}
/**
* Creates a new node instance with the given type.
*
* If a token is given, the location in the code of that token
* is also passed to the Node instance
*
* If no token is passed, a dummy-token with the current
* lexer's offset and line is created
*
* Notice that nodes are expando-objects, you can add properties on-the-fly
* and retrieve them as an array later
*
* @param string $type the type the node should have
* @param array|null $token the token to relate this node to
*
* @return Node The newly created node
*/
protected function createNode($type, array $token = null)
{
$token = $token ? $token : ['line' => $this->lexer->getLine(), 'offset' => $this->lexer->getOffset()];
$node = new Node($type, $token['line'], $token['offset']);
return $node;
}
/**
* Creates an element-node with the properties it should have consistently.
*
* This will create the following properties on the Node instance:
*
* @todo Do this for a bunch of other elements as well, maybe all, maybe a centralized way?
*
* @param array|null $token the token to relate this element to
*
* @return Node the newly created element-node
*/
protected function createElement(array $token = null)
{
$node = $this->createNode('element', $token);
$node->tag = null;
$node->attributes = [];
$node->assignments = [];
return $node;
}
/**
* Parses an <assignment>-token into element assignments.
*
* If no there is no $_current element, a new one is created
*
* Assignments are possible on elements and mixinCalls only
*
* After an assignment, an attribute block is required
*
* @param array $token the <assignment>-token
*
* @throws Exception
*/
protected function handleAssignment(array $token)
{
if (!$this->current)
$this->current = $this->createElement();
if (!in_array($this->current->type, ['element', 'mixinCall']))
$this->throwException(
"You can use assignment-syntax ([`&name(..values..)`]) only mixin calls and HTML elements only, not on {$this->current->type}"
);
$node = $this->createNode('assignment', $token);
$node->name = $token['name'];
$this->current->assignments[] = $node;
if ($this->expectNext(['attributeStart'])) {
$element = $this->current;
$this->current = $node;
$this->handleToken();
$this->current = $element;
} else
$this->throwException(
"The assignment-syntax ([`&name(..values..)`]) requires an attribute block with passed values."
);
}
/**
* Parses an <attribute>-token into an attribute-node.
*
* That node is appended to the $_current element.
*
* If no $_current element exists, a new one is created
*
* Attributes in elements and mixins always need a valid name
*
* @param array $token the <attribute>-token
*
* @throws Exception
*/
protected function handleAttribute(array $token)
{
if (!$this->current)
$this->current = $this->createElement();
$node = $this->createNode('attribute', $token);
$node->name = $token['name'];
$node->value = $token['value'];
$node->escaped = $token['escaped'];
$node->unchecked = $token['unchecked'];
if (!$node->name && in_array($this->current->type, ['element', 'mixin']))
$this->throwException(
'Attributes in elements and mixins always need a name, it seems you only passed a value.',
$token
);
if ($this->current->type === 'mixinCall' && !$node->value) {
$node->value = $node->name;
$node->name = null;
}
$this->current->attributes[] = $node;
}
/**
* Handles an <attributeStart>-token.
*
* Attributes can only start on elements, assignments, imports, mixins and mixinCalls
*
* After that, all following <attribute>-tokens are handled.
* After that, an <attributeEnd>-token is expected
* (When I think about it, the Lexer kind of does that already)
*
* @param array $token the <attributeStart>-token
*
* @throws Exception
*/
protected function handleAttributeStart(array $token)
{
if (!$this->current)
$this->current = $this->createElement();
if (!in_array($this->current->type, ['element', 'assignment', 'import', 'variable', 'mixin', 'mixinCall']))
$this->throwException(
"Attributes can only be placed on elements, assignments, imports, variables, mixins and mixin calls, not on {$this->current->type}"
);
foreach ($this->lookUpNext(['attribute']) as $subToken) {
$this->handleToken($subToken);
}
if (!$this->expect(['attributeEnd']))
$this->throwException(
"No attributeEnd token was found. That is unexpected. Please report this behavior.",
$token
);
}
/**
* Handles an <attributeEnd>-token.
*
* It does nothing (right now?)
*
* @param array $token the <attributeEnd>-token
*/
protected function handleAttributeEnd(array $token)
{
}
/**
* Handles a <block>-token and parses it into a block-node.
*
* Blocks outside a mixin always need a name! (That's what $_inMixin is for)
*
* @param array $token the <block>-token
*
* @throws Exception
*/
protected function handleBlock(array $token)
{
$node = $this->createNode('block', $token);
$node->name = isset($token['name']) ? $token['name'] : null;
$node->mode = isset($token['mode']) ? $token['mode'] : null;
if (!$node->name && !$this->inMixin)
$this->throwException(
"Blocks outside of a mixin always need a valid name."
);
$this->current = $node;
$this->expectEnd($token);
}
/**
* Handles a <class>-token and parses it into an element.
*
* If there's no $_current-node, a new one is created
*
* It will be converted to a regular <attribute>-node on the element
* (There is no class-node)
*
* Classes can only exist on elements and mixinCalls
*
* @param array $token the <class>-token
*
* @throws Exception
*/
protected function handleClass(array $token)
{
if (!$this->current)
$this->current = $this->createElement();
if (!in_array($this->current->type, ['element', 'mixinCall']))
$this->throwException(
"Classes can only be used on elements and mixin calls, not on {$this->current->type}",
$token
);
$attr = $this->createNode('attribute', $token);
$attr->name = 'class';
$attr->value = $token['name'];
$attr->escaped = false;
$this->current->attributes[] = $attr;
}
/**
* Handles a <comment>-token and parses it into a comment-node.
*
* The comment node is set as the $_current element
*
* @param array $token the <comment>-token
*/
protected function handleComment(array $token)
{
$node = $this->createNode('comment', $token);
$node->rendered = $token['rendered'];
$this->current = $node;
}
/**
* Handles a <case>-token and parses it into a case-node.
*
* @param array $token the <case>-token
*/
protected function handleCase(array $token)
{
$node = $this->createNode('case', $token);
$node->subject = $token['subject'];
$this->current = $node;
}
/**
* Handles a <conditional>-token and parses it into a conditional-node.
*
* @param array $token the <conditional>-token
*/
protected function handleConditional(array $token)
{
$node = $this->createNode('conditional', $token);
$node->subject = $token['subject'];
$node->conditionType = $token['conditionType'];
$this->current = $node;
}
/**
* Handles a <do>-token and parses it into a do-node.
*
* @param array $token the <do>-token
*/
protected function handleDo(array $token)
{
$node = $this->createNode('do', $token);
$this->current = $node;
}
/**
* Handles a <doctype>-token and parses it into a doctype-node.
*
* @param array $token the <doctype>-token
*/
protected function handleDoctype(array $token)
{
$node = $this->createNode('doctype', $token);
$node->name = $token['name'];
$this->current = $node;
}
/**
* Handles an <each>-token and parses it into an each-node.
*
* @param array $token the <each>-token
*/
protected function handleEach(array $token)
{
$node = $this->createNode('each', $token);
$node->subject = $token['subject'];
$node->itemName = $token['itemName'];
$node->keyName = isset($token['keyName']) ? $token['keyName'] : null;
$this->current = $node;
}
/**
* Handles an <expression>-token into an expression-node.
*
* If there's a $_current-element, the expression gets appended
* to the $_current-element. If not, the expression itself
* becomes the $_current element
*
* @param array $token the <expression>-token
*
* @throws Exception
*/
protected function handleExpression(array $token)
{
$node = $this->createNode('expression', $token);
$node->escaped = $token['escaped'];
$node->unchecked = $token['unchecked'];
$node->value = $token['value'];
if ($this->current)
$this->current->append($node);
else
$this->current = $node;
}
/**
* Handles an <code>-token into an code-node.
*
* @param array $token the <code>-token
*
* @throws Exception
*/
protected function handleCode(array $token)
{
$node = $this->createNode('code', $token);
$node->value = $token['value'];
$node->block = $token['block'];
$this->current = $node;
}
/**
* Handles a <filter>-token and parses it into a filter-node.
*
* @param array $token the <filter>-token
*/
protected function handleFilter(array $token)
{
$node = $this->createNode('filter', $token);
$node->name = $token['name'];
$this->current = $node;
}
/**
* Handles an <id>-token and parses it into an element.
*
* If no $_current element exists, a new one is created
*
* IDs can only exist on elements an mixin calls
*
* They will get converted to attribute-nodes and appended to the current element
*
* @param array $token the <id>-token
*
* @throws Exception
*/
protected function handleId(array $token)
{
if (!$this->current)
$this->current = $this->createElement();
if (!in_array($this->current->type, ['element', 'mixinCall']))
$this->throwException("IDs can only be used on elements and mixin calls, not on {$this->current->type}", $token);
$attr = $this->createNode('attribute', $token);
$attr->name = 'id';
$attr->value = $token['name'];
$attr->escaped = false;
$this->current->attributes[] = $attr;
}
/**
* Handles a <variable>-token and parses it into a variable assignment.
*
* @param array $token the <variable>-token
*
* @throws Exception
*/
protected function handleVariable(array $token)
{
$node = $this->createNode('variable');
$node->name = $token['name'];
$node->attributes = [];
$this->current = $node;
}
/**
* Handles an <import>-token and parses it into an import-node.
*
* Notice that "extends" and "include" are basically the same thing.
* The only difference is that "extends" can only exist at the very
* beginning of a jade-block
*
* Only "include" can have filters, though.
* This gets checked in the Compiler, not here
*
* @param array $token the <import>-token
*
* @throws Exception
*/
protected function handleImport(array $token)
{
if ($token['importType'] === 'extends' && count($this->document->children) > 0)
$this->throwException(
"extends should be the very first statement in a document, only whitespace can precede it",
$token
);
$node = $this->createNode('import', $token);
$node->importType = $token['importType'];
$node->path = $token['path'];
$node->filter = $token['filter'];
$node->attributes = [];
$node->assignments = [];
$this->current = $node;
}
/**
* Handles an <indent>-token.
*
* The $_level will be increased by 1 for each <indent>
*
* If there's no $_last element (which is set on a newLine), we do nothing
* (because there's nothing to indent into)
*
* The $_last node is set as the $_currentParent node and acts as a parent-node
* for further created nodes (They get appended in handleNewLine)
*
* import-nodes can't be indented into, because they can't have children (poor imports :'( )
*
* The opposite of this is, obviously, handleOutdent with <outdent>-tokens
*
* @todo Are there other nodes that shouldn't have children?
*
* @param array|null $token the <indent>-token
*
* @throws Exception
*/
protected function handleIndent(array $token = null)
{
$this->level++;
if (!$this->last)
return;
if (in_array($this->last->type, ['import', 'expression', 'doctype']))
$this->throwException(
'The '.$this->last->type.' instruction can\'t have any children',
$token
);
$this->currentParent = $this->last;
}
/**
* Handles a <tag>-token and parses it into a tag-node.
*
* If no $_current element exists, a new one is created
* A tag can only exist once on an element
* Only elements can have tags
*
* @todo Maybe multiple tags could combine with :? Would be ugly and senseless to write a(...)b tho
*
* @param array $token the <tag>-token
*
* @throws Exception
*/
protected function handleTag(array $token)
{
if (!$this->current)
$this->current = $this->createElement();
if ($this->current->type !== 'element')
$this->throwException("Tags can only be used on elements, not on {$this->current->type}", $token);
if ($this->current->tag)
$this->throwException('This element already has a tag name, you can\'t pass a second one', $token);
$this->current->tag = $token['name'];
}
/**
* Handles a <mixin>-token and parses it into a mixin-node.
*
* Mixins can't be inside other mixins.
* We use $_inMixin and $_mixinLevel for that
* $_mixinLevel gets reset in handleOutdent
*
* @param array $token the <mixin>-token
*
* @throws Exception
*/
protected function handleMixin(array $token)
{
if ($this->inMixin)
$this->throwException(
"Failed to define mixin {$token['name']}: Mixins can't be nested. Please each mixin outside of each other."
);
$node = $this->createNode('mixin', $token);
$node->name = $token['name'];
$node->attributes = [];
$node->assignments = [];
$this->inMixin = true;
$this->mixinLevel = $this->level;
$this->current = $node;
}
/**
* Handles a <mixinCall>-token and parses it into a mixinCall-node.
*
* @param array $token the <mixinCall>-token
*/
protected function handleMixinCall(array $token)
{
$node = $this->createNode('mixinCall', $token);
$node->name = $token['name'];
$node->attributes = [];
$node->assignments = [];