Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

stack lexer states

  • Loading branch information...
commit 0bd63dd3075e43229fe5fece65166220a3ae5a34 1 parent 6849705
@arnaud-lb arnaud-lb authored
Showing with 21 additions and 6 deletions.
  1. +21 −6 lib/Twig/Lexer.php
View
27 lib/Twig/Lexer.php
@@ -24,6 +24,7 @@ class Twig_Lexer implements Twig_LexerInterface
protected $lineno;
protected $end;
protected $state;
+ protected $states;
protected $brackets;
protected $env;
@@ -82,6 +83,7 @@ public function tokenize($code, $filename = null)
$this->end = strlen($this->code);
$this->tokens = array();
$this->state = self::STATE_DATA;
+ $this->states = array();
$this->brackets = array();
$this->position = -1;
@@ -157,21 +159,19 @@ protected function lexData()
if (preg_match($this->options['lex_block_raw_regex'], $this->code, $match, null, $this->cursor)) {
$this->moveCursor($match[0]);
$this->lexRawData();
- $this->state = self::STATE_DATA;
// {% line \d+ %}
} else if (preg_match($this->options['lex_block_line_regex'], $this->code, $match, null, $this->cursor)) {
$this->moveCursor($match[0]);
$this->lineno = (int) $match[1];
- $this->state = self::STATE_DATA;
} else {
$this->pushToken(Twig_Token::BLOCK_START_TYPE);
- $this->state = self::STATE_BLOCK;
+ $this->pushState(self::STATE_BLOCK);
}
break;
case $this->options['tag_variable'][0]:
$this->pushToken(Twig_Token::VAR_START_TYPE);
- $this->state = self::STATE_VAR;
+ $this->pushState(self::STATE_VAR);
break;
}
}
@@ -181,7 +181,7 @@ protected function lexBlock()
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->moveCursor($match[0]);
- $this->state = self::STATE_DATA;
+ $this->popState();
} else {
$this->lexExpression();
}
@@ -192,7 +192,7 @@ protected function lexVar()
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->moveCursor($match[0]);
- $this->state = self::STATE_DATA;
+ $this->popState();
} else {
$this->lexExpression();
}
@@ -315,4 +315,19 @@ protected function getOperatorRegex()
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);
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.