*/
class Twig_Grammar_Constant extends Twig_Grammar
{
+ protected $type;
+
+ public function __construct($name, $type = null)
+ {
+ $this->name = $name;
+ $this->type = null === $type ? Twig_Token::NAME_TYPE : $type;
+ }
+
public function __toString()
{
return $this->name;
public function parse(Twig_Token $token)
{
- $this->parser->getStream()->expect($this->name);
+ $this->parser->getStream()->expect($this->type, $this->name);
return $this->name;
}
+
+ public function getType()
+ {
+ return $this->type;
+ }
}
{
// test if we have the optional element before consuming it
if ($this->grammar[0] instanceof Twig_Grammar_Constant) {
- if (!$this->parser->getStream()->test($this->grammar[0]->getName())) {
+ if (!$this->parser->getStream()->test($this->grammar[0]->getType(), $this->grammar[0]->getName())) {
return array();
}
} elseif ($this->grammar[0] instanceof Twig_Grammar_Name) {
foreach ($this->grammar as $grammar) {
$grammar->setParser($this->parser);
- $elements[$grammar->getName()] = $grammar->parse($token);
+ $element = $grammar->parse($token);
+ if (is_array($element)) {
+ $elements = array_merge($elements, $element);
+ } else {
+ $elements[$grammar->getName()] = $element;
+ }
}
return $elements;
}
$grammar->addGrammar(new $class($match[1]));
$cursor += strlen($match[0]);
- } elseif (preg_match('/(\w+|,)/A', $str, $match, null, $cursor)) {
- $grammar->addGrammar(new Twig_Grammar_Constant($match[1]));
+ } elseif (preg_match('/\w+/A', $str, $match, null, $cursor)) {
+ $grammar->addGrammar(new Twig_Grammar_Constant($match[0]));
+ $cursor += strlen($match[0]);
+ } elseif (preg_match('/,/A', $str, $match, null, $cursor)) {
+ $grammar->addGrammar(new Twig_Grammar_Constant($match[0], Twig_Token::PUNCTUATION_TYPE));
$cursor += strlen($match[0]);
} elseif (preg_match('/\[/A', $str, $match, null, $cursor)) {
$cursor += strlen($match[0]);
new Twig_Grammar_Constant('with'),
new Twig_Grammar_Array('arguments'),
new Twig_Grammar_Optional(
- new Twig_Grammar_Constant(','),
+ new Twig_Grammar_Constant(',', Twig_Token::PUNCTUATION_TYPE),
new Twig_Grammar_Expression('optional')
)
)