PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Flitch/File/Token.php

http://github.com/DASPRiD/Flitch
PHP | 207 lines | 74 code | 22 blank | 111 comment | 1 complexity | bc4fde5c7b51a4526675e7b32a833108 MD5 | raw file
Possible License(s): BSD-2-Clause
  1. <?php
  2. /**
  3. * Flitch
  4. *
  5. * @link http://github.com/DASPRiD/Flitch For the canonical source repository
  6. * @copyright 2011-2012 Ben Scholzen 'DASPRiD'
  7. * @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
  8. */
  9. namespace Flitch\File;
  10. /**
  11. * Token representation.
  12. */
  13. class Token
  14. {
  15. /**
  16. * Token type.
  17. *
  18. * @var mixed
  19. */
  20. protected $type;
  21. /**
  22. * Token lexeme.
  23. *
  24. * @var string
  25. */
  26. protected $lexeme;
  27. /**
  28. * Token line.
  29. *
  30. * @var integer
  31. */
  32. protected $line;
  33. /**
  34. * Token column
  35. *
  36. * @var integer
  37. */
  38. protected $column;
  39. /**
  40. * Code block level.
  41. *
  42. * @var integer
  43. */
  44. protected $level;
  45. /**
  46. * Namespace the token is in.
  47. *
  48. * @var string
  49. */
  50. protected $namespace;
  51. /**
  52. * Create a new token.
  53. *
  54. * @param mixed $type
  55. * @param string $lexeme
  56. * @param integer $line
  57. * @param integer $column
  58. * @return void
  59. */
  60. public function __construct($type, $lexeme, $line, $column)
  61. {
  62. $this->type = $type;
  63. $this->lexeme = $lexeme;
  64. $this->line = $line;
  65. $this->column = $column;
  66. }
  67. /**
  68. * Get token type.
  69. *
  70. * @return mixed
  71. */
  72. public function getType()
  73. {
  74. return $this->type;
  75. }
  76. /**
  77. * Get token lexeme.
  78. *
  79. * @return string
  80. */
  81. public function getLexeme()
  82. {
  83. return $this->lexeme;
  84. }
  85. /**
  86. * Get token line.
  87. *
  88. * @return integer
  89. */
  90. public function getLine()
  91. {
  92. return $this->line;
  93. }
  94. /**
  95. * Get token column.
  96. *
  97. * @return integer
  98. */
  99. public function getColumn()
  100. {
  101. return $this->column;
  102. }
  103. /**
  104. * Set block level.
  105. *
  106. * @param integer $level
  107. * @return Token
  108. */
  109. public function setLevel($level)
  110. {
  111. $this->level = $level;
  112. return $this;
  113. }
  114. /**
  115. * Get block level.
  116. *
  117. * @return integer
  118. */
  119. public function getLevel()
  120. {
  121. return $this->level;
  122. }
  123. /**
  124. * Set namespace.
  125. *
  126. * @param string $namespace
  127. * @return Token
  128. */
  129. public function setNamespace($namespace)
  130. {
  131. $this->namespace = $namespace;
  132. }
  133. /**
  134. * Get namespace.
  135. *
  136. * @return string|null
  137. */
  138. public function getNamespace()
  139. {
  140. return $this->namespace;
  141. }
  142. /**
  143. * Check if the token contains newline characters.
  144. *
  145. * @return boolean
  146. */
  147. public function hasNewline()
  148. {
  149. if (preg_match('([\r\n])', $this->lexeme)) {
  150. return true;
  151. }
  152. return false;
  153. }
  154. /**
  155. * Get number of newlines.
  156. *
  157. * @return integer
  158. */
  159. public function getNewlineCount()
  160. {
  161. preg_match_all('(\n|\r\n?)', $this->lexeme, $matches, PREG_SET_ORDER);
  162. return count($matches);
  163. }
  164. /**
  165. * Get length of the last line.
  166. *
  167. * @return integer
  168. */
  169. public function getTrailingLineLength()
  170. {
  171. return iconv_strlen(
  172. substr(strrchr($this->lexeme, "\n") ?: strrchr($this->lexeme, "\r"), 1),
  173. 'utf-8'
  174. );
  175. }
  176. /**
  177. * Get length of the entire lexeme.
  178. *
  179. * @return integer
  180. */
  181. public function getLength()
  182. {
  183. return iconv_strlen($this->lexeme, 'utf-8');
  184. }
  185. }