forked from twigphp/Twig
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move parsing of
{name: 'string'}
mappings to ExpressionParser
This also adds support for optional keys as discussed in twigphp#4165. I really don't like using a static method, but I couldn't figure out how else to call the method in tests. See the notes on the method itself. Any help is appreciated!
- Loading branch information
Showing
3 changed files
with
183 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,8 +25,15 @@ | |
/** | ||
* Declare variable types. | ||
* | ||
* {% types {foo: 'int', bar: 'string'} %} | ||
* | ||
* The mapping must use names as keys (not strings) and type strings as values. | ||
* | ||
* Names may be suffixed with `?` to mark them as optional (i.e., the context may not | ||
* have that variable defined). | ||
* | ||
* Example: | ||
* ``` | ||
* {% types {foo: 'int', bar?: 'string'} %} | ||
* ``` | ||
* @author Jeroen Versteeg <[email protected]> | ||
* @see https://github.com/twigphp/Twig/issues/4165 | ||
* @internal | ||
|
@@ -37,50 +44,13 @@ public function parse(Token $token): Node | |
{ | ||
$stream = $this->parser->getStream(); | ||
|
||
$expression = $this->parseSimpleMappingExpression($stream); | ||
$expression = ExpressionParser::parseNameToStringMapping($stream, true); | ||
|
||
$stream->expect(Token::BLOCK_END_TYPE); | ||
|
||
return new TypesNode($expression, $token->getLine(), $this->getTag()); | ||
} | ||
|
||
/** | ||
* @throws SyntaxError | ||
* @see ExpressionParser::parseMappingExpression() | ||
*/ | ||
private function parseSimpleMappingExpression(TokenStream $stream): ArrayExpression | ||
{ | ||
$stream->expect(Token::PUNCTUATION_TYPE, '{', 'A mapping element was expected'); | ||
|
||
$node = new ArrayExpression([], $stream->getCurrent()->getLine()); | ||
|
||
$first = true; | ||
while (!$stream->test(Token::PUNCTUATION_TYPE, '}')) { | ||
if (!$first) { | ||
$stream->expect(Token::PUNCTUATION_TYPE, ',', 'A mapping value must be followed by a comma'); | ||
|
||
// trailing ,? | ||
if ($stream->test(Token::PUNCTUATION_TYPE, '}')) { | ||
break; | ||
} | ||
} | ||
$first = false; | ||
|
||
$nameToken = $stream->expect(Token::NAME_TYPE); | ||
$nameExpression = new NameExpression($nameToken->getValue(), $nameToken->getLine()); | ||
|
||
$stream->expect(Token::PUNCTUATION_TYPE, ':', 'A mapping key must be followed by a colon (:)'); | ||
|
||
$valueToken = $stream->expect(Token::STRING_TYPE); | ||
$valueExpression = new ConstantExpression($valueToken->getValue(), $valueToken->getLine()); | ||
|
||
$node->addElement($valueExpression, $nameExpression); | ||
} | ||
$stream->expect(Token::PUNCTUATION_TYPE, '}', 'An opened mapping is not properly closed'); | ||
|
||
return $node; | ||
} | ||
|
||
public function getTag(): string | ||
{ | ||
return 'types'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters