ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Twig_TokenStream Class Reference

Represents a token stream. More...

+ Collaboration diagram for Twig_TokenStream:

Public Member Functions

 __construct (array $tokens, $name=null, $source=null)
 
 __toString ()
 
 injectTokens (array $tokens)
 
 next ()
 Sets the pointer to the next token and returns the old one. More...
 
 nextIf ($primary, $secondary=null)
 Tests a token, sets the pointer to the next one and returns it or throws a syntax error. More...
 
 expect ($type, $value=null, $message=null)
 Tests a token and returns it or throws a syntax error. More...
 
 look ($number=1)
 Looks at the next token. More...
 
 test ($primary, $secondary=null)
 Tests the current token. More...
 
 isEOF ()
 Checks if end of stream was reached. More...
 
 getCurrent ()
 
 getFilename ()
 Gets the name associated with this stream (null if not defined). More...
 
 getSource ()
 Gets the source code associated with this stream. More...
 
 getSourceContext ()
 Gets the source associated with this stream. More...
 

Protected Attributes

 $tokens
 
 $current = 0
 
 $filename
 

Private Attributes

 $source
 

Detailed Description

Represents a token stream.

Author
Fabien Potencier fabie.nosp@m.n@sy.nosp@m.mfony.nosp@m..com

Definition at line 20 of file TokenStream.php.

Constructor & Destructor Documentation

◆ __construct()

Twig_TokenStream::__construct ( array  $tokens,
  $name = null,
  $source = null 
)
Parameters
array$tokensAn array of tokens
string | null$nameThe name of the template which tokens are associated with
string | null$sourceThe source code associated with the tokens

Definition at line 33 of file TokenStream.php.

References $name, $source, and $tokens.

34  {
35  if (!$name instanceof Twig_Source) {
36  if (null !== $name || null !== $source) {
37  @trigger_error(sprintf('Passing a string as the $name argument of %s() is deprecated since version 1.27. Pass a Twig_Source instance instead.', __METHOD__), E_USER_DEPRECATED);
38  }
39  $this->source = new Twig_Source($source, $name);
40  } else {
41  $this->source = $name;
42  }
43 
44  $this->tokens = $tokens;
45 
46  // deprecated, not used anymore, to be removed in 2.0
47  $this->filename = $this->source->getName();
48  }
if($format !==null) $name
Definition: metadata.php:146
Holds information about a non-compiled Twig template.
Definition: Source.php:19

Member Function Documentation

◆ __toString()

Twig_TokenStream::__toString ( )

Definition at line 50 of file TokenStream.php.

51  {
52  return implode("\n", $this->tokens);
53  }

◆ expect()

Twig_TokenStream::expect (   $type,
  $value = null,
  $message = null 
)

Tests a token and returns it or throws a syntax error.

Returns
Twig_Token

Definition at line 91 of file TokenStream.php.

References $current, $message, $type, next(), and Twig_Token\typeToEnglish().

92  {
93  $token = $this->tokens[$this->current];
94  if (!$token->test($type, $value)) {
95  $line = $token->getLine();
96  throw new Twig_Error_Syntax(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s).',
97  $message ? $message.'. ' : '',
98  Twig_Token::typeToEnglish($token->getType()), $token->getValue(),
99  Twig_Token::typeToEnglish($type), $value ? sprintf(' with value "%s"', $value) : ''),
100  $line,
101  $this->source
102  );
103  }
104  $this->next();
105 
106  return $token;
107  }
$type
Exception thrown when a syntax error occurs during lexing or parsing of a template.
Definition: Syntax.php:18
next()
Sets the pointer to the next token and returns the old one.
Definition: TokenStream.php:65
catch(Exception $e) $message
static typeToEnglish($type)
Returns the English representation of a given type.
Definition: Token.php:172
+ Here is the call graph for this function:

◆ getCurrent()

Twig_TokenStream::getCurrent ( )
Returns
Twig_Token

Definition at line 148 of file TokenStream.php.

References $current.

149  {
150  return $this->tokens[$this->current];
151  }

◆ getFilename()

Twig_TokenStream::getFilename ( )

Gets the name associated with this stream (null if not defined).

Returns
string|null
Deprecated:
since 1.27 (to be removed in 2.0)

Definition at line 160 of file TokenStream.php.

161  {
162  @trigger_error(sprintf('The %s() method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', __METHOD__), E_USER_DEPRECATED);
163 
164  return $this->source->getName();
165  }

◆ getSource()

Twig_TokenStream::getSource ( )

Gets the source code associated with this stream.

Returns
string

Definition at line 176 of file TokenStream.php.

177  {
178  @trigger_error(sprintf('The %s() method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', __METHOD__), E_USER_DEPRECATED);
179 
180  return $this->source->getCode();
181  }

◆ getSourceContext()

Twig_TokenStream::getSourceContext ( )

Gets the source associated with this stream.

Returns
Twig_Source

Definition at line 190 of file TokenStream.php.

References $source.

Referenced by Twig_TokenParser_For\checkLoopUsageBody(), Twig_TokenParser_For\checkLoopUsageCondition(), and Twig_Parser\parse().

191  {
192  return $this->source;
193  }
+ Here is the caller graph for this function:

◆ injectTokens()

Twig_TokenStream::injectTokens ( array  $tokens)

Definition at line 55 of file TokenStream.php.

56  {
57  $this->tokens = array_merge(array_slice($this->tokens, 0, $this->current), $tokens, array_slice($this->tokens, $this->current));
58  }

◆ isEOF()

Twig_TokenStream::isEOF ( )

Checks if end of stream was reached.

Returns
bool

Definition at line 140 of file TokenStream.php.

References $current, and Twig_Token\EOF_TYPE.

141  {
142  return Twig_Token::EOF_TYPE === $this->tokens[$this->current]->getType();
143  }
const EOF_TYPE
Definition: Token.php:26

◆ look()

Twig_TokenStream::look (   $number = 1)

Looks at the next token.

Parameters
int$number
Returns
Twig_Token

Definition at line 116 of file TokenStream.php.

117  {
118  if (!isset($this->tokens[$this->current + $number])) {
119  throw new Twig_Error_Syntax('Unexpected end of template.', $this->tokens[$this->current + $number - 1]->getLine(), $this->source);
120  }
121 
122  return $this->tokens[$this->current + $number];
123  }
Exception thrown when a syntax error occurs during lexing or parsing of a template.
Definition: Syntax.php:18

◆ next()

Twig_TokenStream::next ( )

Sets the pointer to the next token and returns the old one.

Returns
Twig_Token

Definition at line 65 of file TokenStream.php.

Referenced by expect(), and nextIf().

66  {
67  if (!isset($this->tokens[++$this->current])) {
68  throw new Twig_Error_Syntax('Unexpected end of template.', $this->tokens[$this->current - 1]->getLine(), $this->source);
69  }
70 
71  return $this->tokens[$this->current - 1];
72  }
Exception thrown when a syntax error occurs during lexing or parsing of a template.
Definition: Syntax.php:18
+ Here is the caller graph for this function:

◆ nextIf()

Twig_TokenStream::nextIf (   $primary,
  $secondary = null 
)

Tests a token, sets the pointer to the next one and returns it or throws a syntax error.

Returns
Twig_Token|null The next token if the condition is true, null otherwise

Definition at line 79 of file TokenStream.php.

References next(), and test().

80  {
81  if ($this->tokens[$this->current]->test($primary, $secondary)) {
82  return $this->next();
83  }
84  }
test($primary, $secondary=null)
Tests the current token.
next()
Sets the pointer to the next token and returns the old one.
Definition: TokenStream.php:65
+ Here is the call graph for this function:

◆ test()

Twig_TokenStream::test (   $primary,
  $secondary = null 
)

Tests the current token.

Returns
bool

Definition at line 130 of file TokenStream.php.

References $current.

Referenced by nextIf().

131  {
132  return $this->tokens[$this->current]->test($primary, $secondary);
133  }
+ Here is the caller graph for this function:

Field Documentation

◆ $current

Twig_TokenStream::$current = 0
protected

Definition at line 23 of file TokenStream.php.

Referenced by expect(), getCurrent(), isEOF(), and test().

◆ $filename

Twig_TokenStream::$filename
protected

Definition at line 24 of file TokenStream.php.

◆ $source

Twig_TokenStream::$source
private

Definition at line 26 of file TokenStream.php.

Referenced by __construct(), and getSourceContext().

◆ $tokens

Twig_TokenStream::$tokens
protected

Definition at line 22 of file TokenStream.php.

Referenced by __construct().


The documentation for this class was generated from the following file: