Token.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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.
  13. *
  14. * @package twig
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class Twig_Token
  18. {
  19. protected $value;
  20. protected $type;
  21. protected $lineno;
  22. const EOF_TYPE = -1;
  23. const TEXT_TYPE = 0;
  24. const BLOCK_START_TYPE = 1;
  25. const VAR_START_TYPE = 2;
  26. const BLOCK_END_TYPE = 3;
  27. const VAR_END_TYPE = 4;
  28. const NAME_TYPE = 5;
  29. const NUMBER_TYPE = 6;
  30. const STRING_TYPE = 7;
  31. const OPERATOR_TYPE = 8;
  32. const PUNCTUATION_TYPE = 9;
  33. /**
  34. * Constructor.
  35. *
  36. * @param integer $type The type of the token
  37. * @param string $value The token value
  38. * @param integer $lineno The line position in the source
  39. */
  40. public function __construct($type, $value, $lineno)
  41. {
  42. $this->type = $type;
  43. $this->value = $value;
  44. $this->lineno = $lineno;
  45. }
  46. /**
  47. * Returns a string representation of the token.
  48. *
  49. * @return string A string representation of the token
  50. */
  51. public function __toString()
  52. {
  53. return sprintf('%s(%s)', self::typeToString($this->type, true, $this->lineno), $this->value);
  54. }
  55. /**
  56. * Tests the current token for a type and/or a value.
  57. *
  58. * Parameters may be:
  59. * * just type
  60. * * type and value (or array of possible values)
  61. * * just value (or array of possible values) (NAME_TYPE is used as type)
  62. *
  63. * @param array|integer $type The type to test
  64. * @param array|string|null $values The token value
  65. *
  66. * @return Boolean
  67. */
  68. public function test($type, $values = null)
  69. {
  70. if (null === $values && !is_int($type)) {
  71. $values = $type;
  72. $type = self::NAME_TYPE;
  73. }
  74. return ($this->type === $type) && (
  75. null === $values ||
  76. (is_array($values) && in_array($this->value, $values)) ||
  77. $this->value == $values
  78. );
  79. }
  80. /**
  81. * Gets the line.
  82. *
  83. * @return integer The source line
  84. */
  85. public function getLine()
  86. {
  87. return $this->lineno;
  88. }
  89. /**
  90. * Gets the token type.
  91. *
  92. * @return integer The token type
  93. */
  94. public function getType()
  95. {
  96. return $this->type;
  97. }
  98. /**
  99. * Gets the token value.
  100. *
  101. * @return string The token value
  102. */
  103. public function getValue()
  104. {
  105. return $this->value;
  106. }
  107. /**
  108. * Returns the constant representation (internal) of a given type.
  109. *
  110. * @param integer $type The type as an integer
  111. * @param Boolean $short Whether to return a short representation or not
  112. *
  113. * @return string The string representation
  114. */
  115. static public function typeToString($type, $short = false, $line = -1)
  116. {
  117. switch ($type) {
  118. case self::EOF_TYPE:
  119. $name = 'EOF_TYPE';
  120. break;
  121. case self::TEXT_TYPE:
  122. $name = 'TEXT_TYPE';
  123. break;
  124. case self::BLOCK_START_TYPE:
  125. $name = 'BLOCK_START_TYPE';
  126. break;
  127. case self::VAR_START_TYPE:
  128. $name = 'VAR_START_TYPE';
  129. break;
  130. case self::BLOCK_END_TYPE:
  131. $name = 'BLOCK_END_TYPE';
  132. break;
  133. case self::VAR_END_TYPE:
  134. $name = 'VAR_END_TYPE';
  135. break;
  136. case self::NAME_TYPE:
  137. $name = 'NAME_TYPE';
  138. break;
  139. case self::NUMBER_TYPE:
  140. $name = 'NUMBER_TYPE';
  141. break;
  142. case self::STRING_TYPE:
  143. $name = 'STRING_TYPE';
  144. break;
  145. case self::OPERATOR_TYPE:
  146. $name = 'OPERATOR_TYPE';
  147. break;
  148. case self::PUNCTUATION_TYPE:
  149. $name = 'PUNCTUATION_TYPE';
  150. break;
  151. default:
  152. throw new Twig_Error_Syntax(sprintf('Token of type "%s" does not exist.', $type), $line);
  153. }
  154. return $short ? $name : 'Twig_Token::'.$name;
  155. }
  156. /**
  157. * Returns the english representation of a given type.
  158. *
  159. * @param integer $type The type as an integer
  160. * @param Boolean $short Whether to return a short representation or not
  161. *
  162. * @return string The string representation
  163. */
  164. static public function typeToEnglish($type, $line = -1)
  165. {
  166. switch ($type) {
  167. case self::EOF_TYPE:
  168. return 'end of template';
  169. case self::TEXT_TYPE:
  170. return 'text';
  171. case self::BLOCK_START_TYPE:
  172. return 'begin of statement block';
  173. case self::VAR_START_TYPE:
  174. return 'begin of print statement';
  175. case self::BLOCK_END_TYPE:
  176. return 'end of statement block';
  177. case self::VAR_END_TYPE:
  178. return 'end of print statement';
  179. case self::NAME_TYPE:
  180. return 'name';
  181. case self::NUMBER_TYPE:
  182. return 'number';
  183. case self::STRING_TYPE:
  184. return 'string';
  185. case self::OPERATOR_TYPE:
  186. return 'operator';
  187. case self::PUNCTUATION_TYPE:
  188. return 'punctuation';
  189. default:
  190. throw new Twig_Error_Syntax(sprintf('Token of type "%s" does not exist.', $type), $line);
  191. }
  192. }
  193. }