Pelzini

This is the code documentation for the Pelzini project

source of /processor/token.php

  1. <?php
  2. /*
  3. Copyright 2008 Josh Heidenreich
  4.  
  5. This file is part of Pelzini.
  6.  
  7. Pelzini is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 3 of the License, or
  10. (at your option) any later version.
  11.  
  12. Pelzini is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Pelzini. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20.  
  21.  
  22. /**
  23.  * This class is used to represent a token that has been tokenised using a Lexer. (e.g. the JavascriptLexer)
  24.  * These tokens are used to create various ParserItems, by passing them to an Analyser (e.g. the JavascriptAnalyser)
  25.  **/
  26. class Token
  27. {
  28. private $type;
  29. private $value;
  30. private $linenum;
  31.  
  32. static $curr_linenum = 1;
  33.  
  34. public function __construct($type, $value = null)
  35. {
  36. $this->linenum = self::$curr_linenum;
  37. $this->type = $type;
  38. $this->value = $value;
  39. }
  40.  
  41.  
  42. /**
  43.   * Gets the type of this token
  44.   **/
  45. public function gettype()
  46. {
  47. return $this->type;
  48. }
  49.  
  50.  
  51. /**
  52.   * Gets the value of this token
  53.   **/
  54. public function getValue()
  55. {
  56. return $this->value;
  57. }
  58.  
  59.  
  60. /**
  61.   * Set the "current" line number. New tokens have a line number set to this figure.
  62.   *
  63.   * @param int $line
  64.   **/
  65. public static function setCurrLineNum($line)
  66. {
  67. self::$curr_linenum = $line;
  68. }
  69.  
  70.  
  71. /**
  72.   * Increment the "current" line number. New tokens have a line number set to this figure.
  73.   **/
  74. public static function setIncrLineNum($incr = 1)
  75. {
  76. self::$curr_linenum += $incr;
  77. }
  78.  
  79.  
  80. /**
  81.   * Gets the line number this toekn
  82.   **/
  83. public function getLineNum()
  84. {
  85. return $this->linenum;
  86. }
  87.  
  88.  
  89. /**
  90.   * Uses some PHP cleverness to get the name of the constant
  91.   * that this token referres to.
  92.   * Good for debugging
  93.   **/
  94. public function getTypeName()
  95. {
  96. $constants = get_defined_constants();
  97. foreach ($constants as $name => $val) {
  98. if (strncmp($name, 'TOKEN_', 6) === 0 and $val == $this->type) {
  99. return $name;
  100. }
  101. }
  102.  
  103. return null;
  104. }
  105.  
  106. }
  107.