45 const REGEX_NAME =
'/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/A';
47 const REGEX_STRING =
'/"([^#"\\\\]*(?:\\\\.[^#"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'/As';
56 $this->options = array_merge(
array(
57 'tag_comment' =>
array(
'{#',
'#}'),
58 'tag_block' =>
array(
'{%',
'%}'),
59 'tag_variable' =>
array(
'{{',
'}}'),
60 'whitespace_trim' =>
'-',
61 'interpolation' =>
array(
'#{',
'}'),
64 $this->regexes =
array(
65 'lex_var' =>
'/\s*'.preg_quote($this->options[
'whitespace_trim'].$this->options[
'tag_variable'][1],
'/').
'\s*|\s*'.preg_quote($this->options[
'tag_variable'][1],
'/').
'/A',
66 'lex_block' =>
'/\s*(?:'.preg_quote($this->options[
'whitespace_trim'].$this->options[
'tag_block'][1],
'/').
'\s*|\s*'.preg_quote($this->options[
'tag_block'][1],
'/').
')\n?/A',
67 'lex_raw_data' =>
'/('.preg_quote($this->options[
'tag_block'][0].$this->options[
'whitespace_trim'],
'/').
'|'.preg_quote($this->options[
'tag_block'][0],
'/').
')\s*(?:end%s)\s*(?:'.preg_quote($this->options[
'whitespace_trim'].$this->options[
'tag_block'][1],
'/').
'\s*|\s*'.preg_quote($this->options[
'tag_block'][1],
'/').
')/s',
69 'lex_comment' =>
'/(?:'.preg_quote($this->options[
'whitespace_trim'],
'/').preg_quote($this->options[
'tag_comment'][1],
'/').
'\s*|'.preg_quote($this->options[
'tag_comment'][1],
'/').
')\n?/s',
70 'lex_block_raw' =>
'/\s*(raw|verbatim)\s*(?:'.preg_quote($this->options[
'whitespace_trim'].$this->options[
'tag_block'][1],
'/').
'\s*|\s*'.preg_quote($this->options[
'tag_block'][1],
'/').
')/As',
71 'lex_block_line' =>
'/\s*line\s+(\d+)\s*'.preg_quote($this->options[
'tag_block'][1],
'/').
'/As',
72 'lex_tokens_start' =>
'/('.preg_quote($this->options[
'tag_variable'][0],
'/').
'|'.preg_quote($this->options[
'tag_block'][0],
'/').
'|'.preg_quote($this->options[
'tag_comment'][0],
'/').
')('.preg_quote($this->options[
'whitespace_trim'],
'/').
')?/s',
73 'interpolation_start' =>
'/'.preg_quote($this->options[
'interpolation'][0],
'/').
'\s*/A',
74 'interpolation_end' =>
'/\s*'.preg_quote($this->options[
'interpolation'][1],
'/').
'/A',
81 @trigger_error(sprintf(
'Passing a string as the $code argument of %s() is deprecated since version 1.27 and will be removed in 2.0. Pass a Twig_Source instance instead.', __METHOD__), E_USER_DEPRECATED);
84 $this->source =
$code;
87 if (((
int) ini_get(
'mbstring.func_overload')) & 2) {
88 @trigger_error(
'Support for having "mbstring.func_overload" different from 0 is deprecated version 1.29 and will be removed in 2.0.', E_USER_DEPRECATED);
91 if (function_exists(
'mb_internal_encoding') && ((
int) ini_get(
'mbstring.func_overload')) & 2) {
92 $mbEncoding = mb_internal_encoding();
93 mb_internal_encoding(
'ASCII');
98 $this->code = str_replace(
array(
"\r\n",
"\r"),
"\n", $this->source->getCode());
99 $this->filename = $this->source->getName();
102 $this->end = strlen($this->code);
103 $this->tokens =
array();
104 $this->state = self::STATE_DATA;
105 $this->states =
array();
106 $this->brackets =
array();
107 $this->position = -1;
110 preg_match_all($this->regexes[
'lex_tokens_start'], $this->code, $matches, PREG_OFFSET_CAPTURE);
111 $this->positions = $matches;
113 while ($this->cursor < $this->end) {
116 switch ($this->state) {
117 case self::STATE_DATA:
121 case self::STATE_BLOCK:
125 case self::STATE_VAR:
129 case self::STATE_STRING:
133 case self::STATE_INTERPOLATION:
141 if (!empty($this->brackets)) {
142 list($expect,
$lineno) = array_pop($this->brackets);
147 mb_internal_encoding($mbEncoding);
156 if ($this->position == count($this->positions[0]) - 1) {
166 if ($this->position == count($this->positions[0]) - 1) {
173 $text = $textContent = substr($this->code, $this->cursor,
$position[1] - $this->cursor);
174 if (isset($this->positions[2][$this->position][0])) {
180 switch ($this->positions[1][$this->position][0]) {
181 case $this->options[
'tag_comment'][0]:
185 case $this->options[
'tag_block'][0]:
187 if (preg_match($this->regexes[
'lex_block_raw'], $this->code, $match, null, $this->cursor)) {
191 } elseif (preg_match($this->regexes[
'lex_block_line'], $this->code, $match, null, $this->cursor)) {
193 $this->lineno = (int) $match[1];
201 case $this->options[
'tag_variable'][0]:
211 if (empty($this->brackets) && preg_match($this->regexes[
'lex_block'], $this->code, $match, null, $this->cursor)) {
222 if (empty($this->brackets) && preg_match($this->regexes[
'lex_var'], $this->code, $match, null, $this->cursor)) {
234 if (preg_match(
'/\s+/A', $this->code, $match, null, $this->cursor)) {
237 if ($this->cursor >= $this->end) {
238 throw new Twig_Error_Syntax(sprintf(
'Unclosed "%s".', self::STATE_BLOCK === $this->state ?
'block' :
'variable'), $this->currentVarBlockLine, $this->source);
243 if (preg_match($this->regexes[
'operator'], $this->code, $match, null, $this->cursor)) {
248 elseif (preg_match(self::REGEX_NAME, $this->code, $match, null, $this->cursor)) {
253 elseif (preg_match(self::REGEX_NUMBER, $this->code, $match, null, $this->cursor)) {
254 $number = (float) $match[0];
255 if (ctype_digit($match[0]) && $number <= PHP_INT_MAX) {
256 $number = (int) $match[0];
262 elseif (
false !== strpos(self::PUNCTUATION, $this->code[$this->cursor])) {
264 if (
false !== strpos(
'([{', $this->code[$this->cursor])) {
265 $this->brackets[] =
array($this->code[$this->cursor], $this->lineno);
268 elseif (
false !== strpos(
')]}', $this->code[$this->cursor])) {
269 if (empty($this->brackets)) {
270 throw new Twig_Error_Syntax(sprintf(
'Unexpected "%s".', $this->code[$this->cursor]), $this->lineno, $this->source);
273 list($expect,
$lineno) = array_pop($this->brackets);
274 if ($this->code[$this->cursor] != strtr($expect,
'([{',
')]}')) {
283 elseif (preg_match(self::REGEX_STRING, $this->code, $match, null, $this->cursor)) {
288 elseif (preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, null, $this->cursor)) {
289 $this->brackets[] =
array(
'"', $this->lineno);
295 throw new Twig_Error_Syntax(sprintf(
'Unexpected character "%s".', $this->code[$this->cursor]), $this->lineno, $this->source);
301 if (
'raw' ===
$tag) {
302 @trigger_error(sprintf(
'Twig Tag "raw" is deprecated since version 1.21. Use "verbatim" instead in %s at line %d.', $this->filename, $this->lineno), E_USER_DEPRECATED);
305 if (!preg_match(str_replace(
'%s',
$tag, $this->regexes[
'lex_raw_data']), $this->code, $match, PREG_OFFSET_CAPTURE, $this->cursor)) {
306 throw new Twig_Error_Syntax(sprintf(
'Unexpected end of file: Unclosed "%s" block.',
$tag), $this->lineno, $this->source);
309 $text = substr($this->code, $this->cursor, $match[0][1] - $this->cursor);
312 if (
false !== strpos($match[1][0], $this->options[
'whitespace_trim'])) {
321 if (!preg_match($this->regexes[
'lex_comment'], $this->code, $match, PREG_OFFSET_CAPTURE, $this->cursor)) {
325 $this->
moveCursor(substr($this->code, $this->cursor, $match[0][1] - $this->cursor).$match[0][0]);
330 if (preg_match($this->regexes[
'interpolation_start'], $this->code, $match, null, $this->cursor)) {
331 $this->brackets[] =
array($this->options[
'interpolation'][0], $this->lineno);
334 $this->
pushState(self::STATE_INTERPOLATION);
335 } elseif (preg_match(self::REGEX_DQ_STRING_PART, $this->code, $match, null, $this->cursor) && strlen($match[0]) > 0) {
338 } elseif (preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, null, $this->cursor)) {
339 list($expect,
$lineno) = array_pop($this->brackets);
340 if (
'"' != $this->code[$this->cursor]) {
348 throw new Twig_Error_Syntax(sprintf(
'Unexpected character "%s".', $this->code[$this->cursor]), $this->lineno, $this->source);
354 $bracket = end($this->brackets);
355 if ($this->options[
'interpolation'][0] === $bracket[0] && preg_match($this->regexes[
'interpolation_end'], $this->code, $match, null, $this->cursor)) {
356 array_pop($this->brackets);
377 $this->cursor += strlen(
$text);
378 $this->lineno += substr_count(
$text,
"\n");
383 $operators = array_merge(
385 array_keys($this->env->getUnaryOperators()),
386 array_keys($this->env->getBinaryOperators())
389 $operators = array_combine($operators, array_map(
'strlen', $operators));
393 foreach ($operators as $operator => $length) {
396 if (ctype_alpha($operator[$length - 1])) {
397 $r = preg_quote($operator,
'/').
'(?=[\s()])';
399 $r = preg_quote($operator,
'/');
403 $r = preg_replace(
'/\s+/',
'\s+',
$r);
408 return '/'.implode(
'|', $regex).
'/A';
419 if (0 === count($this->states)) {
420 throw new Exception(
'Cannot pop state without a previous state.');
423 $this->state = array_pop($this->states);
427 class_alias(
'Twig_Lexer',
'Twig\Lexer',
false);
Represents a token stream.
Exception thrown when a syntax error occurs during lexing or parsing of a template.
const REGEX_DQ_STRING_DELIM
const REGEX_DQ_STRING_PART
Interface implemented by lexer classes.
Create styles array
The data for the language used.
tokenize($code, $name=null)
Tokenizes a source code.
const INTERPOLATION_END_TYPE
Holds information about a non-compiled Twig template.
__construct(Twig_Environment $env, array $options=array())
Stores the Twig configuration.
const STATE_INTERPOLATION
pushToken($type, $value='')
const INTERPOLATION_START_TYPE
if(function_exists('posix_getuid') &&posix_getuid()===0) if(!array_key_exists('t', $options)) $tag