‪TYPO3CMS  ‪main
CommentAwareAstBuilder.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;
36 
55 {
56  public function ‪__construct(
57  EventDispatcherInterface ‪$eventDispatcher,
58  ) {
59  $this->eventDispatcher = ‪$eventDispatcher;
60  }
61 
65  public function ‪build(‪LineStream $lineStream, ‪RootNode $ast, array ‪$flatConstants = []): ‪RootNode
66  {
67  $this->flatConstants = ‪$flatConstants;
68 
69  $currentObjectPath = new ‪CurrentObjectPath($ast);
70  $currentObjectPathStack = new ‪CurrentObjectPathStack();
71  $currentObjectPathStack->push($currentObjectPath);
72 
73  $previousLineComments = [];
74  while ($line = $lineStream->‪getNext()) {
75  $node = null;
76  if ($line instanceof ‪IdentifierAssignmentLine) {
77  // "foo = bar" and "foo ( bar )": Single and multi line assignments
78  $node = $this->‪handleIdentifierAssignmentLine($line, $currentObjectPath);
79  if ($previousLineComments) {
80  foreach ($previousLineComments as $previousLineComment) {
81  $node->addComment($previousLineComment);
82  }
83  $previousLineComments = [];
84  }
85  } elseif ($line instanceof ‪IdentifierBlockOpenLine) {
86  // "foo {": Opening a block - push to object path stack
87  $node = $this->‪getOrAddNodeFromIdentifierStream($currentObjectPath, $line->getIdentifierTokenStream());
88  if ($previousLineComments) {
89  foreach ($previousLineComments as $previousLineComment) {
90  $node->addComment($previousLineComment);
91  }
92  $previousLineComments = [];
93  }
94  $currentObjectPath = (new ‪CurrentObjectPath($node));
95  $currentObjectPathStack->push($currentObjectPath);
96  } elseif ($line instanceof ‪BlockCloseLine) {
97  // "}": Closing a block - pop from object path stack
98  $currentObjectPath = $currentObjectPathStack->pop();
99  } elseif ($line instanceof ‪IdentifierUnsetLine) {
100  // "foo >": Remove a path
101  $this->‪handleIdentifierUnsetLine($line, $currentObjectPath);
102  } elseif ($line instanceof ‪IdentifierCopyLine) {
103  // "foo < bar": Copy a node source path to a target path
104  $node = $this->‪handleIdentifierCopyLine($line, $ast, $currentObjectPath);
105  if ($node && $previousLineComments) {
106  foreach ($previousLineComments as $previousLineComment) {
107  $node->addComment($previousLineComment);
108  }
109  $previousLineComments = [];
110  }
111  } elseif ($line instanceof ‪IdentifierFunctionLine) {
112  // "foo := addToList(42)": Evaluate functions
113  $node = $this->‪getOrAddNodeFromIdentifierStream($currentObjectPath, $line->getIdentifierTokenStream());
114  $node->setValue($this->‪evaluateValueModifier($line->getFunctionNameToken(), $line->getFunctionValueToken(), $node->getValue()));
115  if ($previousLineComments) {
116  foreach ($previousLineComments as $previousLineComment) {
117  $node->addComment($previousLineComment);
118  }
119  $previousLineComments = [];
120  }
121  } elseif ($line instanceof ‪IdentifierReferenceLine) {
122  // "foo =< bar": Prepare a reference resolving
123  $node = $this->‪handleIdentifierReferenceLine($line, $currentObjectPath);
124  if ($previousLineComments) {
125  foreach ($previousLineComments as $previousLineComment) {
126  $node->addComment($previousLineComment);
127  }
128  $previousLineComments = [];
129  }
130  } elseif ($line instanceof ‪CommentLine) {
131  $nextLine = $lineStream->‪peekNext();
132  if ($currentObjectPath->getLast() instanceof ‪RootNode && ($nextLine === null || $nextLine instanceof ‪EmptyLine)) {
133  $previousLineComments[] = $line->getTokenStream();
134  foreach ($previousLineComments as $commentLineTokenStream) {
135  $ast->‪addComment($commentLineTokenStream);
136  }
137  $previousLineComments = [];
138  }
139  if ($nextLine instanceof ‪CommentLine) {
140  $previousLineComments[] = $line->getTokenStream();
141  }
142  if ($nextLine instanceof ‪IdentifierAssignmentLine
143  || $nextLine instanceof ‪IdentifierBlockOpenLine
144  || $nextLine instanceof ‪IdentifierCopyLine
145  || $nextLine instanceof ‪IdentifierFunctionLine
146  || $nextLine instanceof ‪IdentifierReferenceLine
147  ) {
148  $previousLineComments[] = $line->getTokenStream();
149  }
150  }
151  }
152 
153  return $ast;
154  }
155 
160  {
161  $node = $this->‪getOrAddNodeFromIdentifierStream($currentObjectPath, $line->‪getIdentifierTokenStream());
162  $valueTokenStream = $line->‪getValueTokenStream();
163  if ($valueTokenStream instanceof ‪ConstantAwareTokenStream) {
164  $valueTokenStream->setFlatConstants($this->flatConstants);
165  $node->setPreviousValue($node->getValue());
166  $node->setValue((string)$valueTokenStream);
167  $valueTokenStream->setFlatConstants(null);
168  $node->setOriginalValueTokenStream($valueTokenStream);
169  return $node;
170  }
171  $node->setPreviousValue($node->getValue());
172  $node->setValue((string)$valueTokenStream);
173  return $node;
174  }
175 }
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\CommentLine
Definition: CommentLine.php:29
‪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\Node\NodeInterface
Definition: NodeInterface.php:35
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\LineStream\peekNext
‪peekNext()
Definition: LineStream.php:120
‪TYPO3\CMS\Core\TypoScript\AST\AbstractAstBuilder\evaluateValueModifier
‪evaluateValueModifier(Token $functionNameToken, ?Token $functionArgumentToken, ?string $originalValue)
Definition: AbstractAstBuilder.php:203
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\LineStream\getNext
‪getNext()
Definition: LineStream.php:111
‪TYPO3\CMS\Core\TypoScript\AST\CommentAwareAstBuilder\build
‪build(LineStream $lineStream, RootNode $ast, array $flatConstants=[])
Definition: CommentAwareAstBuilder.php:65
‪TYPO3\CMS\Core\TypoScript\AST\AbstractAstBuilder
Definition: AbstractAstBuilder.php:41
‪TYPO3\CMS\Core\TypoScript\AST\CommentAwareAstBuilder\__construct
‪__construct(EventDispatcherInterface $eventDispatcher,)
Definition: CommentAwareAstBuilder.php:56
‪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\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\CommentAwareAstBuilder\handleIdentifierAssignmentLine
‪handleIdentifierAssignmentLine(IdentifierAssignmentLine $line, CurrentObjectPath $currentObjectPath)
Definition: CommentAwareAstBuilder.php:159
‪TYPO3\CMS\Core\TypoScript\AST\AstBuilderInterface
Definition: AstBuilderInterface.php:34
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\EmptyLine
Definition: EmptyLine.php:32
‪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\AST\Node\AbstractNode\addComment
‪addComment(TokenStreamInterface $tokenStream)
Definition: AbstractNode.php:173
‪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\AST\CommentAwareAstBuilder
Definition: CommentAwareAstBuilder.php:55
‪TYPO3\CMS\Core\TypoScript\Tokenizer\Line\LineStream
Definition: LineStream.php:29