Token.php 5.9 KB

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