PageRenderTime 78ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ChartDown/Token.php

https://github.com/bshaffer/ChartDown
PHP | 224 lines | 138 code | 15 blank | 71 comment | 8 complexity | 3347bf658f375e48ec9e7d1252135f9d MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of ChartDown.
  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 chartdown
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. */
  17. class ChartDown_Token
  18. {
  19. protected $value;
  20. protected $type;
  21. protected $lineno;
  22. const EOF_TYPE = -1;
  23. const CHORD_TYPE = 1;
  24. const CHORD_GROUP_START_TYPE = 8;
  25. const CHORD_GROUP_END_TYPE = 9;
  26. const LINE_START = 10;
  27. const LINE_END = 11;
  28. const TEXT_TYPE = 2;
  29. const BAR_LINE = 3;
  30. const METADATA_KEY_TYPE = 4;
  31. const METADATA_VALUE_TYPE = 5;
  32. const EXPRESSION_TYPE = 12;
  33. const RHYTHM_TYPE = 13;
  34. const END_ROW_TYPE = 7;
  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), self::valueToString($this->type, $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. static public function valueToString($type, $value)
  110. {
  111. switch ($type) {
  112. case self::LINE_START:
  113. return ChartDown_Lexer::stateToString($value);
  114. }
  115. return (string) $value;
  116. }
  117. /**
  118. * Returns the constant representation (internal) of a given type.
  119. *
  120. * @param integer $type The type as an integer
  121. * @param Boolean $short Whether to return a short representation or not
  122. *
  123. * @return string The string representation
  124. */
  125. static public function typeToString($type, $short = false, $line = -1)
  126. {
  127. switch ($type) {
  128. case self::EOF_TYPE:
  129. $name = 'EOF_TYPE';
  130. break;
  131. case self::CHORD_TYPE:
  132. $name = 'CHORD_TYPE';
  133. break;
  134. case self::CHORD_GROUP_START_TYPE:
  135. $name = 'CHORD_GROUP_START_TYPE';
  136. break;
  137. case self::CHORD_GROUP_END_TYPE:
  138. $name = 'CHORD_GROUP_END_TYPE';
  139. break;
  140. case self::RHYTHM_TYPE:
  141. $name = 'RHYTHM_TYPE';
  142. break;
  143. case self::EXPRESSION_TYPE:
  144. $name = 'EXPRESSION_TYPE';
  145. break;
  146. case self::TEXT_TYPE:
  147. $name = 'TEXT_TYPE';
  148. break;
  149. case self::METADATA_KEY_TYPE:
  150. $name = 'METADATA_KEY_TYPE';
  151. break;
  152. case self::METADATA_VALUE_TYPE:
  153. $name = 'METADATA_VALUE_TYPE';
  154. break;
  155. case self::BAR_LINE:
  156. $name = 'BAR_LINE';
  157. break;
  158. case self::LINE_START:
  159. $name = 'LINE_START';
  160. break;
  161. case self::LINE_END:
  162. $name = 'LINE_END';
  163. break;
  164. case self::END_ROW_TYPE:
  165. $name = 'END_ROW_TYPE';
  166. break;
  167. default:
  168. throw new ChartDown_Error_Syntax(sprintf('Token of type "%s" does not exist.', $type), $line);
  169. }
  170. return $short ? $name : 'ChartDown_Token::'.$name;
  171. }
  172. /**
  173. * Returns the english representation of a given type.
  174. *
  175. * @param integer $type The type as an integer
  176. * @param Boolean $short Whether to return a short representation or not
  177. *
  178. * @return string The string representation
  179. */
  180. static public function typeToEnglish($type, $line = -1)
  181. {
  182. switch ($type) {
  183. case self::EOF_TYPE:
  184. return 'end of template';
  185. case self::CHORD_TYPE:
  186. return 'chord';
  187. case self::CHORD_GROUP_START_TYPE:
  188. return 'chord group start';
  189. case self::CHORD_GROUP_END_TYPE:
  190. return 'chord group end';
  191. case self::RHYTHM_TYPE:
  192. return 'rhythm';
  193. case self::EXPRESSION_TYPE:
  194. return 'expression';
  195. case self::TEXT_TYPE:
  196. return 'text';
  197. case self::METADATA_KEY_TYPE:
  198. return 'key for metadata';
  199. case self::METADATA_VALUE_TYPE:
  200. return 'value for metadata';
  201. case self::BAR_LINE:
  202. return 'bar line';
  203. case self::END_ROW_TYPE:
  204. return 'end of row';
  205. default:
  206. throw new ChartDown_Error_Syntax(sprintf('Token of type "%s" does not exist.', $type), $line);
  207. }
  208. }
  209. }