‪TYPO3CMS  ‪main
AstBuilder.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\EventDispatcher\EventDispatcherInterface;
33 
45 {
46  public function ‪__construct(
47  EventDispatcherInterface ‪$eventDispatcher,
48  ) {
49  $this->eventDispatcher = ‪$eventDispatcher;
50  }
51 
55  public function ‪build(‪LineStream $lineStream, ‪RootNode $ast, array ‪$flatConstants = []): ‪RootNode
56  {
57  $this->flatConstants = ‪$flatConstants;
58 
59  $currentObjectPath = new ‪CurrentObjectPath($ast);
60  $currentObjectPathStack = new ‪CurrentObjectPathStack();
61  $currentObjectPathStack->push($currentObjectPath);
62 
63  foreach ($lineStream->‪getNextLine() as $line) {
64  if ($line instanceof ‪IdentifierAssignmentLine) {
65  // "foo = bar" and "foo ( bar )": Single and multi line assignments
66  $this->‪handleIdentifierAssignmentLine($line, $currentObjectPath);
67  } elseif ($line instanceof ‪IdentifierBlockOpenLine) {
68  // "foo {": Opening a block - push to object path stack
69  $node = $this->‪getOrAddNodeFromIdentifierStream($currentObjectPath, $line->getIdentifierTokenStream());
70  $currentObjectPath = (new ‪CurrentObjectPath($node));
71  $currentObjectPathStack->push($currentObjectPath);
72  } elseif ($line instanceof ‪BlockCloseLine) {
73  // "}": Closing a block - pop from object path stack
74  $currentObjectPath = $currentObjectPathStack->pop();
75  } elseif ($line instanceof ‪IdentifierUnsetLine) {
76  // "foo >": Remove a path
77  $this->‪handleIdentifierUnsetLine($line, $currentObjectPath);
78  } elseif ($line instanceof ‪IdentifierCopyLine) {
79  // "foo < bar": Copy a node source path to a target path
80  $this->‪handleIdentifierCopyLine($line, $ast, $currentObjectPath);
81  } elseif ($line instanceof ‪IdentifierFunctionLine) {
82  // "foo := addToList(42)": Evaluate functions
83  $node = $this->‪getOrAddNodeFromIdentifierStream($currentObjectPath, $line->getIdentifierTokenStream());
84  $node->setValue($this->‪evaluateValueModifier($line->getFunctionNameToken(), $line->getFunctionValueToken(), $node->getValue()));
85  } elseif ($line instanceof ‪IdentifierReferenceLine) {
86  // "foo =< bar": Prepare a reference resolving
87  $this->‪handleIdentifierReferenceLine($line, $currentObjectPath);
88  }
89  }
90 
91  return $ast;
92  }
93 
95  {
96  $node = $this->‪getOrAddNodeFromIdentifierStream($currentObjectPath, $line->‪getIdentifierTokenStream());
97  $valueTokenStream = $line->‪getValueTokenStream();
98  if ($valueTokenStream instanceof ‪ConstantAwareTokenStream) {
99  $valueTokenStream->setFlatConstants($this->flatConstants);
100  $node->setValue((string)$valueTokenStream);
101  $valueTokenStream->setFlatConstants(null);
102  $node->setOriginalValueTokenStream($valueTokenStream);
103  return;
104  }
105  $node->setValue((string)$valueTokenStream);
106  }
107 }
‪TYPO3\CMS\Core\TypoScript\AST\AbstractAstBuilder\handleIdentifierReferenceLine
‪handleIdentifierReferenceLine(IdentifierReferenceLine $line, CurrentObjectPath $currentObjectPath)
Definition: AbstractAstBuilder.php:150
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\IdentifierUnsetLine
Definition: IdentifierUnsetLine.php:31
‪TYPO3\CMS\Core\TypoScript\AST\AbstractAstBuilder\getOrAddNodeFromIdentifierStream
‪getOrAddNodeFromIdentifierStream(CurrentObjectPath $currentObjectPath, IdentifierTokenStream $tokenStream)
Definition: AbstractAstBuilder.php:183
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\IdentifierAssignmentLine
Definition: IdentifierAssignmentLine.php:37
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\IdentifierCopyLine
Definition: IdentifierCopyLine.php:37
‪TYPO3\CMS\Core\TypoScript\AST\AbstractAstBuilder\$flatConstants
‪array $flatConstants
Definition: AbstractAstBuilder.php:45
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\BlockCloseLine
Definition: BlockCloseLine.php:25
‪TYPO3\CMS\Core\TypoScript\AST\AbstractAstBuilder\evaluateValueModifier
‪evaluateValueModifier(Token $functionNameToken, ?Token $functionArgumentToken, ?string $originalValue)
Definition: AbstractAstBuilder.php:203
‪TYPO3\CMS\Core\TypoScript\AST\AstBuilder\build
‪build(LineStream $lineStream, RootNode $ast, array $flatConstants=[])
Definition: AstBuilder.php:55
‪TYPO3\CMS\Core\TypoScript\AST\AstBuilder\__construct
‪__construct(EventDispatcherInterface $eventDispatcher,)
Definition: AstBuilder.php:46
‪TYPO3\CMS\Core\TypoScript\AST\AbstractAstBuilder
Definition: AbstractAstBuilder.php:41
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\IdentifierReferenceLine
Definition: IdentifierReferenceLine.php:35
‪TYPO3\CMS\Core\TypoScript\AST\CurrentObjectPath\CurrentObjectPathStack
Definition: CurrentObjectPathStack.php:28
‪TYPO3\CMS\Core\TypoScript\AST\AbstractAstBuilder\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: AbstractAstBuilder.php:46
‪TYPO3\CMS\Core\TypoScript\AST\AbstractAstBuilder\handleIdentifierUnsetLine
‪handleIdentifierUnsetLine(IdentifierUnsetLine $line, CurrentObjectPath $currentObjectPath)
Definition: AbstractAstBuilder.php:48
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\LineStream\getNextLine
‪iterable< LineInterface > getNextLine()
Definition: LineStream.php:85
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\IdentifierFunctionLine
Definition: IdentifierFunctionLine.php:34
‪TYPO3\CMS\Core\TypoScript\AST\CurrentObjectPath\CurrentObjectPath
Definition: CurrentObjectPath.php:32
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\IdentifierBlockOpenLine
Definition: IdentifierBlockOpenLine.php:31
‪TYPO3\CMS\Core\TypoScript\AST\AstBuilder
Definition: AstBuilder.php:45
‪TYPO3\CMS\Core\TypoScript\AST\AstBuilderInterface
Definition: AstBuilderInterface.php:34
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Token\ConstantAwareTokenStream
Definition: ConstantAwareTokenStream.php:29
‪TYPO3\CMS\Core\TypoScript\AST\AbstractAstBuilder\handleIdentifierCopyLine
‪handleIdentifierCopyLine(IdentifierCopyLine $line, RootNode $rootNode, CurrentObjectPath $currentObjectPath)
Definition: AbstractAstBuilder.php:66
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\IdentifierAssignmentLine\getValueTokenStream
‪getValueTokenStream()
Definition: IdentifierAssignmentLine.php:64
‪TYPO3\CMS\Core\TypoScript\AST\Node\RootNode
Definition: RootNode.php:26
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\IdentifierAssignmentLine\getIdentifierTokenStream
‪getIdentifierTokenStream()
Definition: IdentifierAssignmentLine.php:50
‪TYPO3\CMS\Core\TypoScript\AST
Definition: AbstractAstBuilder.php:18
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\LineStream
Definition: LineStream.php:29
‪TYPO3\CMS\Core\TypoScript\AST\AstBuilder\handleIdentifierAssignmentLine
‪handleIdentifierAssignmentLine(IdentifierAssignmentLine $line, CurrentObjectPath $currentObjectPath)
Definition: AstBuilder.php:94