mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-16 12:26:30 +00:00
stack lexer states
This commit is contained in:
+21
-6
@@ -24,6 +24,7 @@ class Twig_Lexer implements Twig_LexerInterface
|
|||||||
protected $lineno;
|
protected $lineno;
|
||||||
protected $end;
|
protected $end;
|
||||||
protected $state;
|
protected $state;
|
||||||
|
protected $states;
|
||||||
protected $brackets;
|
protected $brackets;
|
||||||
|
|
||||||
protected $env;
|
protected $env;
|
||||||
@@ -82,6 +83,7 @@ class Twig_Lexer implements Twig_LexerInterface
|
|||||||
$this->end = strlen($this->code);
|
$this->end = strlen($this->code);
|
||||||
$this->tokens = array();
|
$this->tokens = array();
|
||||||
$this->state = self::STATE_DATA;
|
$this->state = self::STATE_DATA;
|
||||||
|
$this->states = array();
|
||||||
$this->brackets = array();
|
$this->brackets = array();
|
||||||
$this->position = -1;
|
$this->position = -1;
|
||||||
|
|
||||||
@@ -157,21 +159,19 @@ class Twig_Lexer implements Twig_LexerInterface
|
|||||||
if (preg_match($this->options['lex_block_raw_regex'], $this->code, $match, null, $this->cursor)) {
|
if (preg_match($this->options['lex_block_raw_regex'], $this->code, $match, null, $this->cursor)) {
|
||||||
$this->moveCursor($match[0]);
|
$this->moveCursor($match[0]);
|
||||||
$this->lexRawData();
|
$this->lexRawData();
|
||||||
$this->state = self::STATE_DATA;
|
|
||||||
// {% line \d+ %}
|
// {% line \d+ %}
|
||||||
} else if (preg_match($this->options['lex_block_line_regex'], $this->code, $match, null, $this->cursor)) {
|
} else if (preg_match($this->options['lex_block_line_regex'], $this->code, $match, null, $this->cursor)) {
|
||||||
$this->moveCursor($match[0]);
|
$this->moveCursor($match[0]);
|
||||||
$this->lineno = (int) $match[1];
|
$this->lineno = (int) $match[1];
|
||||||
$this->state = self::STATE_DATA;
|
|
||||||
} else {
|
} else {
|
||||||
$this->pushToken(Twig_Token::BLOCK_START_TYPE);
|
$this->pushToken(Twig_Token::BLOCK_START_TYPE);
|
||||||
$this->state = self::STATE_BLOCK;
|
$this->pushState(self::STATE_BLOCK);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case $this->options['tag_variable'][0]:
|
case $this->options['tag_variable'][0]:
|
||||||
$this->pushToken(Twig_Token::VAR_START_TYPE);
|
$this->pushToken(Twig_Token::VAR_START_TYPE);
|
||||||
$this->state = self::STATE_VAR;
|
$this->pushState(self::STATE_VAR);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -181,7 +181,7 @@ class Twig_Lexer implements Twig_LexerInterface
|
|||||||
if (empty($this->brackets) && preg_match($this->options['lex_block_regex'], $this->code, $match, null, $this->cursor)) {
|
if (empty($this->brackets) && preg_match($this->options['lex_block_regex'], $this->code, $match, null, $this->cursor)) {
|
||||||
$this->pushToken(Twig_Token::BLOCK_END_TYPE);
|
$this->pushToken(Twig_Token::BLOCK_END_TYPE);
|
||||||
$this->moveCursor($match[0]);
|
$this->moveCursor($match[0]);
|
||||||
$this->state = self::STATE_DATA;
|
$this->popState();
|
||||||
} else {
|
} else {
|
||||||
$this->lexExpression();
|
$this->lexExpression();
|
||||||
}
|
}
|
||||||
@@ -192,7 +192,7 @@ class Twig_Lexer implements Twig_LexerInterface
|
|||||||
if (empty($this->brackets) && preg_match($this->options['lex_var_regex'], $this->code, $match, null, $this->cursor)) {
|
if (empty($this->brackets) && preg_match($this->options['lex_var_regex'], $this->code, $match, null, $this->cursor)) {
|
||||||
$this->pushToken(Twig_Token::VAR_END_TYPE);
|
$this->pushToken(Twig_Token::VAR_END_TYPE);
|
||||||
$this->moveCursor($match[0]);
|
$this->moveCursor($match[0]);
|
||||||
$this->state = self::STATE_DATA;
|
$this->popState();
|
||||||
} else {
|
} else {
|
||||||
$this->lexExpression();
|
$this->lexExpression();
|
||||||
}
|
}
|
||||||
@@ -315,4 +315,19 @@ class Twig_Lexer implements Twig_LexerInterface
|
|||||||
|
|
||||||
return '/'.implode('|', $regex).'/A';
|
return '/'.implode('|', $regex).'/A';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function pushState($state)
|
||||||
|
{
|
||||||
|
$this->states[] = $this->state;
|
||||||
|
$this->state = $state;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function popState()
|
||||||
|
{
|
||||||
|
if (0 === count($this->states)) {
|
||||||
|
throw new Exception('Cannot pop state without a previous state');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->state = array_pop($this->states);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user