Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
getExpressionParser example
final
class
MacroTokenParser
extends
AbstractTokenParser
{
public
function
parse
(
Token
$token
)
: Node
{
$lineno
=
$token
->
getLine
(
)
;
$stream
=
$this
->parser->
getStream
(
)
;
$name
=
$stream
->
expect
(
/* Token::NAME_TYPE */ 5
)
->
getValue
(
)
;
$arguments
=
$this
->parser->
getExpressionParser
(
)
->
parseArguments
(
true, true
)
;
$stream
->
expect
(
/* Token::BLOCK_END_TYPE */ 3
)
;
$this
->parser->
pushLocalScope
(
)
;
$body
=
$this
->parser->
subparse
(
[
$this
, 'decideBlockEnd'
]
, true
)
;
if
(
$token
=
$stream
->
nextIf
(
/* Token::NAME_TYPE */ 5
)
)
{
$value
=
$token
->
getValue
(
)
;
if
(
$value
!=
$name
)
{
throw
new
SyntaxError
(
sprintf
(
'Expected endmacro for macro "%s" (but "%s" given).',
$name
,
$value
)
,
$stream
->
getCurrent
(
)
->
getLine
(
)
,
$stream
->
getSourceContext
(
)
)
;
}
}
$stream
=
$this
->parser->
getStream
(
)
;
if
(
$this
->parser->
peekBlockStack
(
)
)
{
throw
new
SyntaxError
(
'Cannot use "extend" in a block.',
$token
->
getLine
(
)
,
$stream
->
getSourceContext
(
)
)
;
}
elseif
(
!
$this
->parser->
isMainScope
(
)
)
{
throw
new
SyntaxError
(
'Cannot use "extend" in a macro.',
$token
->
getLine
(
)
,
$stream
->
getSourceContext
(
)
)
;
}
if
(
null !==
$this
->parser->
getParent
(
)
)
{
throw
new
SyntaxError
(
'Multiple extends tags are forbidden.',
$token
->
getLine
(
)
,
$stream
->
getSourceContext
(
)
)
;
}
$this
->parser->
setParent
(
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
)
;
$stream
->
expect
(
/* Token::BLOCK_END_TYPE */ 3
)
;
return
new
Node
(
)
;
}
public
function
getTag
(
)
: string
{
return
'extends';
}
}
use
Twig\TokenParser\AbstractTokenParser;
#[Package('core')]
final
class
ReturnNodeTokenParser
extends
AbstractTokenParser
{
public
function
parse
(
Token
$token
)
: ReturnNode
{
$stream
=
$this
->parser->
getStream
(
)
;
$nodes
=
[
]
;
if
(
!
$stream
->
test
(
Token::BLOCK_END_TYPE
)
)
{
$nodes
[
'expr'
]
=
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
;
}
$stream
->
expect
(
Token::BLOCK_END_TYPE
)
;
return
new
ReturnNode
(
$nodes
,
[
]
,
$token
->
getLine
(
)
,
$this
->
getTag
(
)
)
;
}
public
function
getTag
(
)
: string
{
return
'return';
}
}
public
function
parse
(
Token
$token
)
{
$lineno
=
$token
->
getLine
(
)
;
$stream
=
$this
->parser->
getStream
(
)
;
$body
= NULL;
$options
= NULL;
$count
= NULL;
$plural
= NULL;
if
(
!
$stream
->
test
(
Token::BLOCK_END_TYPE
)
&&
$stream
->
test
(
Token::STRING_TYPE
)
)
{
$body
=
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
;
}
if
(
!
$stream
->
test
(
Token::BLOCK_END_TYPE
)
&&
$stream
->
test
(
Token::NAME_TYPE, 'with'
)
)
{
$stream
->
next
(
)
;
$options
=
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
;
}
if
(
!
$body
)
{
$stream
->
expect
(
Token::BLOCK_END_TYPE
)
;
$body
=
$this
->parser->
subparse
(
[
$this
, 'decideForFork'
]
)
;
if
(
'plural' ===
$stream
->
next
(
)
->
getValue
(
)
)
{
$count
=
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
;
$stream
->
expect
(
Token::BLOCK_END_TYPE
)
;
final
class
ForTokenParser
extends
AbstractTokenParser
{
public
function
parse
(
Token
$token
)
: Node
{
$lineno
=
$token
->
getLine
(
)
;
$stream
=
$this
->parser->
getStream
(
)
;
$targets
=
$this
->parser->
getExpressionParser
(
)
->
parseAssignmentExpression
(
)
;
$stream
->
expect
(
/* Token::OPERATOR_TYPE */ 8, 'in'
)
;
$seq
=
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
;
$stream
->
expect
(
/* Token::BLOCK_END_TYPE */ 3
)
;
$body
=
$this
->parser->
subparse
(
[
$this
, 'decideForFork'
]
)
;
if
(
'else' ==
$stream
->
next
(
)
->
getValue
(
)
)
{
$stream
->
expect
(
/* Token::BLOCK_END_TYPE */ 3
)
;
$else
=
$this
->parser->
subparse
(
[
$this
, 'decideForEnd'
]
, true
)
;
}
else
{
$else
= null;
}
final
class
DeprecatedTokenParser
extends
AbstractTokenParser
{
public
function
parse
(
Token
$token
)
: Node
{
$expr
=
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
;
$this
->parser->
getStream
(
)
->
expect
(
Token::BLOCK_END_TYPE
)
;
return
new
DeprecatedNode
(
$expr
,
$token
->
getLine
(
)
,
$this
->
getTag
(
)
)
;
}
public
function
getTag
(
)
: string
{
return
'deprecated';
}
}
use
Twig\Token;
/** * Evaluates an expression, discarding the returned value. * * @internal */
final
class
DoTokenParser
extends
AbstractTokenParser
{
public
function
parse
(
Token
$token
)
: Node
{
$expr
=
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
;
$this
->parser->
getStream
(
)
->
expect
(
/* Token::BLOCK_END_TYPE */ 3
)
;
return
new
DoNode
(
$expr
,
$token
->
getLine
(
)
,
$this
->
getTag
(
)
)
;
}
public
function
getTag
(
)
: string
{
return
'do';
}
}
use
Twig\TokenParser\AbstractTokenParser;
/** * Token Parser for the 'trans_default_domain' tag. * * @author Fabien Potencier <fabien@symfony.com> */
final
class
TransDefaultDomainTokenParser
extends
AbstractTokenParser
{
public
function
parse
(
Token
$token
)
: Node
{
$expr
=
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
;
$this
->parser->
getStream
(
)
->
expect
(
Token::BLOCK_END_TYPE
)
;
return
new
TransDefaultDomainNode
(
$expr
,
$token
->
getLine
(
)
,
$this
->
getTag
(
)
)
;
}
public
function
getTag
(
)
: string
{
return
'trans_default_domain';
}
}
$lineno
=
$token
->
getLine
(
)
;
$stream
=
$this
->parser->
getStream
(
)
;
$count
= null;
$vars
=
new
ArrayExpression
(
[
]
,
$lineno
)
;
$domain
= null;
$locale
= null;
if
(
!
$stream
->
test
(
Token::BLOCK_END_TYPE
)
)
{
if
(
$stream
->
test
(
'count'
)
)
{
// {% trans count 5 %}
$stream
->
next
(
)
;
$count
=
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
;
}
if
(
$stream
->
test
(
'with'
)
)
{
// {% trans with vars %}
$stream
->
next
(
)
;
$vars
=
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
;
}
if
(
$stream
->
test
(
'from'
)
)
{
// {% trans from "messages" %}
$stream
->
next
(
)
;
#[Package('storefront')]
final
class
IconTokenParser
extends
AbstractTokenParser
{
/** * @var Parser */
protected
$parser
;
public
function
parse
(
Token
$token
)
: SwInclude
{
$expr
=
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
;
$icon
=
$expr
->
getAttribute
(
'value'
)
;
$expr
->
setAttribute
(
'value', '@Storefront/storefront/utilities/icon.html.twig'
)
;
$stream
=
$this
->parser->
getStream
(
)
;
$variables
=
new
ArrayExpression
(
[
]
,
$token
->
getLine
(
)
)
;
if
(
$stream
->
nextIf
(
Token::NAME_TYPE, 'style'
)
)
{
/** @var ArrayExpression $variables */
if
(
$stream
->
nextIf
(
/* Token::BLOCK_END_TYPE */ 3
)
)
{
$body
=
$this
->parser->
subparse
(
[
$this
, 'decideBlockEnd'
]
, true
)
;
if
(
$token
=
$stream
->
nextIf
(
/* Token::NAME_TYPE */ 5
)
)
{
$value
=
$token
->
getValue
(
)
;
if
(
$value
!=
$name
)
{
throw
new
SyntaxError
(
sprintf
(
'Expected endblock for block "%s" (but "%s" given).',
$name
,
$value
)
,
$stream
->
getCurrent
(
)
->
getLine
(
)
,
$stream
->
getSourceContext
(
)
)
;
}
}
}
else
{
$body
=
new
Node
(
[
new
PrintNode
(
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
,
$lineno
)
,
]
)
;
}
$stream
->
expect
(
/* Token::BLOCK_END_TYPE */ 3
)
;
$block
->
setNode
(
'body',
$body
)
;
$this
->parser->
popBlockStack
(
)
;
$this
->parser->
popLocalScope
(
)
;
return
new
BlockReferenceNode
(
$name
,
$lineno
,
$this
->
getTag
(
)
)
;
}
final
class
IfTokenParser
extends
AbstractTokenParser
{
public
function
parse
(
Token
$token
)
: Node
{
$lineno
=
$token
->
getLine
(
)
;
$expr
=
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
;
$stream
=
$this
->parser->
getStream
(
)
;
$stream
->
expect
(
/* Token::BLOCK_END_TYPE */ 3
)
;
$body
=
$this
->parser->
subparse
(
[
$this
, 'decideIfFork'
]
)
;
$tests
=
[
$expr
,
$body
]
;
$else
= null;
$end
= false;
while
(
!
$end
)
{
switch
(
$stream
->
next
(
)
->
getValue
(
)
)
{
case
'else':
$stream
->
expect
(
/* Token::BLOCK_END_TYPE */ 3
)
;
/** * Imports macros. * * {% import 'forms.html' as forms %} * * @internal */
final
class
ImportTokenParser
extends
AbstractTokenParser
{
public
function
parse
(
Token
$token
)
: Node
{
$macro
=
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
;
$this
->parser->
getStream
(
)
->
expect
(
/* Token::NAME_TYPE */ 5, 'as'
)
;
$var
=
new
AssignNameExpression
(
$this
->parser->
getStream
(
)
->
expect
(
/* Token::NAME_TYPE */ 5
)
->
getValue
(
)
,
$token
->
getLine
(
)
)
;
$this
->parser->
getStream
(
)
->
expect
(
/* Token::BLOCK_END_TYPE */ 3
)
;
$this
->parser->
addImportedSymbol
(
'template',
$var
->
getAttribute
(
'name'
)
)
;
return
new
ImportNode
(
$macro
,
$var
,
$token
->
getLine
(
)
,
$this
->
getTag
(
)
,
$this
->parser->
isMainScope
(
)
)
;
}
public
function
getTag
(
)
: string
{
final
class
UseTokenParser
extends
AbstractTokenParser
{
public
function
parse
(
Token
$token
)
: Node
{
$template
=
$this
->parser->
getExpressionParser
(
)
->
parseExpression
(
)
;
$stream
=
$this
->parser->
getStream
(
)
;
if
(
!
$template
instanceof ConstantExpression
)
{
throw
new
SyntaxError
(
'The template references in a "use" statement must be a string.',
$stream
->
getCurrent
(
)
->
getLine
(
)
,
$stream
->
getSourceContext
(
)
)
;
}
$targets
=
[
]
;
if
(
$stream
->
nextIf
(
'with'
)
)
{
do
{
$name
=
$stream
->
expect
(
/* Token::NAME_TYPE */ 5
)
->
getValue
(
)
;
final
class
ApplyTokenParser
extends
AbstractTokenParser
{
public
function
parse
(
Token
$token
)
: Node
{
$lineno
=
$token
->
getLine
(
)
;
$name
=
$this
->parser->
getVarName
(
)
;
$ref
=
new
TempNameExpression
(
$name
,
$lineno
)
;
$ref
->
setAttribute
(
'always_defined', true
)
;
$filter
=
$this
->parser->
getExpressionParser
(
)
->
parseFilterExpressionRaw
(
$ref
,
$this
->
getTag
(
)
)
;
$this
->parser->
getStream
(
)
->
expect
(
Token::BLOCK_END_TYPE
)
;
$body
=
$this
->parser->
subparse
(
[
$this
, 'decideApplyEnd'
]
, true
)
;
$this
->parser->
getStream
(
)
->
expect
(
Token::BLOCK_END_TYPE
)
;
return
new
Node
(
[
new
SetNode
(
true,
$ref
,
$body
,
$lineno
,
$this
->
getTag
(
)
)
,
new
PrintNode
(
$filter
,
$lineno
,
$this
->
getTag
(
)
)
,
]
)
;
}