-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.php
437 lines (385 loc) · 15.8 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
<?php
namespace Phug;
use Phug\Lexer\Token\AssignmentToken;
use Phug\Lexer\Token\AttributeEndToken;
use Phug\Lexer\Token\AttributeStartToken;
use Phug\Lexer\Token\AttributeToken;
use Phug\Lexer\Token\AutoCloseToken;
use Phug\Lexer\Token\BlockToken;
use Phug\Lexer\Token\CaseToken;
use Phug\Lexer\Token\ClassToken;
use Phug\Lexer\Token\CodeToken;
use Phug\Lexer\Token\CommentToken;
use Phug\Lexer\Token\ConditionalToken;
use Phug\Lexer\Token\DoctypeToken;
use Phug\Lexer\Token\DoToken;
use Phug\Lexer\Token\EachToken;
use Phug\Lexer\Token\ExpansionToken;
use Phug\Lexer\Token\ExpressionToken;
use Phug\Lexer\Token\FilterToken;
use Phug\Lexer\Token\ForToken;
use Phug\Lexer\Token\IdToken;
use Phug\Lexer\Token\ImportToken;
use Phug\Lexer\Token\IndentToken;
use Phug\Lexer\Token\InterpolationEndToken;
use Phug\Lexer\Token\InterpolationStartToken;
use Phug\Lexer\Token\KeywordToken;
use Phug\Lexer\Token\MixinCallToken;
use Phug\Lexer\Token\MixinToken;
use Phug\Lexer\Token\NewLineToken;
use Phug\Lexer\Token\OutdentToken;
use Phug\Lexer\Token\TagInterpolationEndToken;
use Phug\Lexer\Token\TagInterpolationStartToken;
use Phug\Lexer\Token\TagToken;
use Phug\Lexer\Token\TextToken;
use Phug\Lexer\Token\VariableToken;
use Phug\Lexer\Token\WhenToken;
use Phug\Lexer\Token\WhileToken;
use Phug\Lexer\Token\YieldToken;
use Phug\Parser\Event\NodeEvent;
use Phug\Parser\Event\ParseEvent;
use Phug\Parser\Node\ElementNode;
use Phug\Parser\NodeInterface;
use Phug\Parser\State;
use Phug\Parser\TokenHandler\AssignmentTokenHandler;
use Phug\Parser\TokenHandler\AttributeEndTokenHandler;
use Phug\Parser\TokenHandler\AttributeStartTokenHandler;
use Phug\Parser\TokenHandler\AttributeTokenHandler;
use Phug\Parser\TokenHandler\AutoCloseTokenHandler;
use Phug\Parser\TokenHandler\BlockTokenHandler;
use Phug\Parser\TokenHandler\CaseTokenHandler;
use Phug\Parser\TokenHandler\ClassTokenHandler;
use Phug\Parser\TokenHandler\CodeTokenHandler;
use Phug\Parser\TokenHandler\CommentTokenHandler;
use Phug\Parser\TokenHandler\ConditionalTokenHandler;
use Phug\Parser\TokenHandler\DoctypeTokenHandler;
use Phug\Parser\TokenHandler\DoTokenHandler;
use Phug\Parser\TokenHandler\EachTokenHandler;
use Phug\Parser\TokenHandler\ExpansionTokenHandler;
use Phug\Parser\TokenHandler\ExpressionTokenHandler;
use Phug\Parser\TokenHandler\FilterTokenHandler;
use Phug\Parser\TokenHandler\ForTokenHandler;
use Phug\Parser\TokenHandler\IdTokenHandler;
use Phug\Parser\TokenHandler\ImportTokenHandler;
use Phug\Parser\TokenHandler\IndentTokenHandler;
use Phug\Parser\TokenHandler\InterpolationEndTokenHandler;
use Phug\Parser\TokenHandler\InterpolationStartTokenHandler;
use Phug\Parser\TokenHandler\KeywordTokenHandler;
use Phug\Parser\TokenHandler\MixinCallTokenHandler;
use Phug\Parser\TokenHandler\MixinTokenHandler;
use Phug\Parser\TokenHandler\NewLineTokenHandler;
use Phug\Parser\TokenHandler\OutdentTokenHandler;
use Phug\Parser\TokenHandler\TagInterpolationEndTokenHandler;
use Phug\Parser\TokenHandler\TagInterpolationStartTokenHandler;
use Phug\Parser\TokenHandler\TagTokenHandler;
use Phug\Parser\TokenHandler\TextTokenHandler;
use Phug\Parser\TokenHandler\VariableTokenHandler;
use Phug\Parser\TokenHandler\WhenTokenHandler;
use Phug\Parser\TokenHandler\WhileTokenHandler;
use Phug\Parser\TokenHandler\YieldTokenHandler;
use Phug\Parser\TokenHandlerInterface;
use Phug\Util\ModuleContainerInterface;
use Phug\Util\Partial\ModuleContainerTrait;
/**
* 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 Phug\Parser\Node instances
* with parent/child relations
*
* This AST is passed to the compiler to generate PHTML out of it
*
* Usage example:
* <code>
*
* use Phug\Parser;
*
* $parser = new Parser();
* var_dump($parser->parse($pugInput));
*
* </code>
*/
class Parser implements ModuleContainerInterface
{
use ModuleContainerTrait;
/**
* The lexer used in this parser instance.
*
* @var Lexer
*/
private $lexer;
/**
* @var State
*/
private $state;
/**
* @var callable[]
*/
private $tokenHandlers;
/**
* 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 Phug\Parser\Node instances
*
* You can take the AST and either compile it with the Compiler or handle it yourself
*
* @param array|null $options the options array
*
* @throws ParserException
*/
public function __construct($options = null)
{
$this->setOptionsDefaults($options ?: [], [
'lexer_class_name' => Lexer::class,
'parser_state_class_name' => State::class,
'parser_modules' => [],
'keywords' => [],
'detailed_dump' => false,
'token_handlers' => [
AssignmentToken::class => AssignmentTokenHandler::class,
AttributeEndToken::class => AttributeEndTokenHandler::class,
AttributeStartToken::class => AttributeStartTokenHandler::class,
AttributeToken::class => AttributeTokenHandler::class,
AutoCloseToken::class => AutoCloseTokenHandler::class,
BlockToken::class => BlockTokenHandler::class,
YieldToken::class => YieldTokenHandler::class,
CaseToken::class => CaseTokenHandler::class,
ClassToken::class => ClassTokenHandler::class,
CodeToken::class => CodeTokenHandler::class,
CommentToken::class => CommentTokenHandler::class,
ConditionalToken::class => ConditionalTokenHandler::class,
DoToken::class => DoTokenHandler::class,
DoctypeToken::class => DoctypeTokenHandler::class,
EachToken::class => EachTokenHandler::class,
ExpansionToken::class => ExpansionTokenHandler::class,
ExpressionToken::class => ExpressionTokenHandler::class,
FilterToken::class => FilterTokenHandler::class,
ForToken::class => ForTokenHandler::class,
IdToken::class => IdTokenHandler::class,
InterpolationStartToken::class => InterpolationStartTokenHandler::class,
InterpolationEndToken::class => InterpolationEndTokenHandler::class,
ImportToken::class => ImportTokenHandler::class,
IndentToken::class => IndentTokenHandler::class,
MixinCallToken::class => MixinCallTokenHandler::class,
MixinToken::class => MixinTokenHandler::class,
NewLineToken::class => NewLineTokenHandler::class,
OutdentToken::class => OutdentTokenHandler::class,
TagInterpolationStartToken::class => TagInterpolationStartTokenHandler::class,
TagInterpolationEndToken::class => TagInterpolationEndTokenHandler::class,
KeywordToken::class => KeywordTokenHandler::class,
TagToken::class => TagTokenHandler::class,
TextToken::class => TextTokenHandler::class,
VariableToken::class => VariableTokenHandler::class,
WhenToken::class => WhenTokenHandler::class,
WhileToken::class => WhileTokenHandler::class,
],
//Events
'on_parse' => null,
'on_document' => null,
'on_state_enter' => null,
'on_state_leave' => null,
'on_state_store' => null,
]);
$lexerClassName = $this->getOption('lexer_class_name');
if (!is_a($lexerClassName, Lexer::class, true)) {
throw new \InvalidArgumentException(
"Passed lexer class $lexerClassName is ".
'not a valid '.Lexer::class
);
}
$this->lexer = new $lexerClassName($this->getOptions());
$this->state = null;
$this->tokenHandlers = [];
foreach ($this->getOption('token_handlers') as $className => $handler) {
$this->setTokenHandler($className, $handler);
}
if ($onParse = $this->getOption('on_parse')) {
$this->attach(ParserEvent::PARSE, $onParse);
}
if ($onDocument = $this->getOption('on_document')) {
$this->attach(ParserEvent::DOCUMENT, $onDocument);
}
if ($onStateEnter = $this->getOption('on_state_enter')) {
$this->attach(ParserEvent::STATE_ENTER, $onStateEnter);
}
if ($onStateLeave = $this->getOption('on_state_leave')) {
$this->attach(ParserEvent::STATE_LEAVE, $onStateLeave);
}
if ($onStateStore = $this->getOption('on_state_store')) {
$this->attach(ParserEvent::STATE_STORE, $onStateStore);
}
$this->addModules($this->getOption('parser_modules'));
}
/**
* Returns the currently used Lexer instance.
*
* @return Lexer
*/
public function getLexer()
{
return $this->lexer;
}
public function setTokenHandler($className, $handler)
{
if (!is_subclass_of($handler, TokenHandlerInterface::class)) {
throw new \InvalidArgumentException(
'Passed token handler needs to implement '.TokenHandlerInterface::class
);
}
$this->tokenHandlers[$className] = $handler;
return $this;
}
/**
* Parses the provided input-string to an AST.
*
* The Abstract Syntax Tree (AST) will be an object-tree consisting
* of \Phug\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
* @param string $path optional path of file the input comes from
*
* @return NodeInterface the root-node of the parsed AST
*/
public function parse($input, $path = null)
{
$stateClassName = $this->getOption('parser_state_class_name');
$event = new ParseEvent($input, $path, $stateClassName, [
'token_handlers' => $this->tokenHandlers,
]);
$this->trigger($event);
$input = $event->getInput();
$path = $event->getPath();
$stateClassName = $event->getStateClassName();
$stateOptions = $event->getStateOptions();
$stateOptions['path'] = $path;
if (!is_a($stateClassName, State::class, true)) {
throw new \InvalidArgumentException(
'parser_state_class_name needs to be a valid '.State::class.' sub class'
);
}
$this->state = new $stateClassName(
$this,
//Append a new line to get last outdents and tag if not ending with \n
$this->lexer->lex($input."\n", $path),
$stateOptions
);
$forward = function (NodeEvent $event) {
return $this->trigger($event);
};
//Forward events from the state
$this->state->attach(ParserEvent::STATE_ENTER, $forward);
$this->state->attach(ParserEvent::STATE_LEAVE, $forward);
$this->state->attach(ParserEvent::STATE_STORE, $forward);
//While we have tokens, handle current token, then go to next token
//rinse and repeat
while ($this->state->hasTokens()) {
$this->state->handleToken();
$this->state->nextToken();
}
$this->state->store();
$document = $this->state->getDocumentNode();
//Some work after parsing needed
//Resolve expansions/outer nodes
/** @var NodeInterface[] $expandingNodes */
$expandingNodes = $document->find(function (NodeInterface $node) {
return $node->getOuterNode() !== null;
});
foreach ($expandingNodes as $expandingNode) {
$current = $expandingNode;
while ($outerNode = $expandingNode->getOuterNode()) {
/** @var NodeInterface $expandedNode */
$expandedNode = $outerNode;
$current->setOuterNode(null);
$current->prepend($expandedNode);
$current->remove();
$expandedNode->appendChild($current);
$current = $expandedNode;
}
}
$this->state->clearListeners(ParserEvent::STATE_ENTER);
$this->state->clearListeners(ParserEvent::STATE_LEAVE);
$this->state->clearListeners(ParserEvent::STATE_STORE);
$this->state = null;
$event = new NodeEvent(ParserEvent::DOCUMENT, $document);
$this->trigger($event);
//Return the final document node with all its awesome child nodes
return $event->getNode();
}
/**
* Dump a representation of the parser rendering.
*
* @param string $input the input jade string that is to be parsed
*
* @return string representation of parser rendering
*/
public function dump($input)
{
return $this->dumpNode($this->parse($input));
}
protected function getNodeName(NodeInterface $node)
{
switch (get_class($node)) {
case ElementNode::class:
$text = get_class($node);
if (!$this->getOption('detailed_dump')) {
if ($outerNode = $node->getOuterNode()) {
$text .= ' outer='.$this->getNodeName($outerNode);
}
return $text;
}
$text .= ':'.$node->getName() ?: 'div';
$ids = '';
$classes = '';
$attributes = [];
foreach ($node->getAttributes() as $attribute) {
if ($attribute->getName() === 'id') {
$ids .= '#'.$attribute->getValue();
continue;
}
if ($attribute->getName() === 'class') {
$classes .= '.'.$attribute->getValue();
continue;
}
$attributes[] = $attribute->getName().'='.$attribute->getValue();
}
$attributes = count($attributes) ? '('.implode(' ', $attributes).')' : '';
$text .= $ids.$classes.$attributes;
if ($outerNode = $node->getOuterNode()) {
$text .= ' outer='.$this->getNodeName($outerNode);
}
return $text;
default:
$text = get_class($node);
if ($outerNode = $node->getOuterNode()) {
$text .= ' outer='.$this->getNodeName($outerNode);
}
return $text;
}
}
protected function dumpNode(NodeInterface $node, $level = null)
{
$level = $level ?: 0;
$text = $this->getNodeName($node);
$text = str_repeat(' ', $level)."[$text]";
if (count($node) > 0) {
foreach ($node as $child) {
$text .= "\n".$this->dumpNode($child, $level + 1);
}
}
return $text;
}
public function getModuleBaseClassName()
{
return ParserModuleInterface::class;
}
}