TokenStream.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. * (c) 2009 Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Represents a token stream.
  13. *
  14. * @package twig
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class Twig_TokenStream
  18. {
  19. protected $tokens;
  20. protected $current;
  21. protected $filename;
  22. /**
  23. * Constructor.
  24. *
  25. * @param array $tokens An array of tokens
  26. * @param string $filename The name of the filename which tokens are associated with
  27. */
  28. public function __construct(array $tokens, $filename = null)
  29. {
  30. $this->tokens = $tokens;
  31. $this->current = 0;
  32. $this->filename = $filename;
  33. }
  34. /**
  35. * Returns a string representation of the token stream.
  36. *
  37. * @return string
  38. */
  39. public function __toString()
  40. {
  41. return implode("\n", $this->tokens);
  42. }
  43. /**
  44. * Sets the pointer to the next token and returns the old one.
  45. *
  46. * @return Twig_Token
  47. */
  48. public function next()
  49. {
  50. if (!isset($this->tokens[++$this->current])) {
  51. throw new Twig_Error_Syntax('Unexpected end of template', -1, $this->filename);
  52. }
  53. return $this->tokens[$this->current - 1];
  54. }
  55. /**
  56. * Tests a token and returns it or throws a syntax error.
  57. *
  58. * @return Twig_Token
  59. */
  60. public function expect($type, $value = null, $message = null)
  61. {
  62. $token = $this->tokens[$this->current];
  63. if (!$token->test($type, $value)) {
  64. $line = $token->getLine();
  65. throw new Twig_Error_Syntax(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s)',
  66. $message ? $message.'. ' : '',
  67. Twig_Token::typeToEnglish($token->getType(), $line), $token->getValue(),
  68. Twig_Token::typeToEnglish($type, $line), $value ? sprintf(' with value "%s"', $value) : ''),
  69. $line,
  70. $this->filename
  71. );
  72. }
  73. $this->next();
  74. return $token;
  75. }
  76. /**
  77. * Looks at the next token.
  78. *
  79. * @param integer $number
  80. *
  81. * @return Twig_Token
  82. */
  83. public function look($number = 1)
  84. {
  85. if (!isset($this->tokens[$this->current + $number])) {
  86. throw new Twig_Error_Syntax('Unexpected end of template', -1, $this->filename);
  87. }
  88. return $this->tokens[$this->current + $number];
  89. }
  90. /**
  91. * Tests the current token
  92. *
  93. * @return bool
  94. */
  95. public function test($primary, $secondary = null)
  96. {
  97. return $this->tokens[$this->current]->test($primary, $secondary);
  98. }
  99. /**
  100. * Checks if end of stream was reached
  101. *
  102. * @return bool
  103. */
  104. public function isEOF()
  105. {
  106. return $this->tokens[$this->current]->getType() === Twig_Token::EOF_TYPE;
  107. }
  108. /**
  109. * Gets the current token
  110. *
  111. * @return Twig_Token
  112. */
  113. public function getCurrent()
  114. {
  115. return $this->tokens[$this->current];
  116. }
  117. /**
  118. * Gets the filename associated with this stream
  119. *
  120. * @return string
  121. */
  122. public function getFilename()
  123. {
  124. return $this->filename;
  125. }
  126. }