Token.php 6.2KB

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