Pelzini

This is the code documentation for the Pelzini project

source of /processor/analyser.php

Contains the Analyser class
  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.  * Contains the {@link Analyser} class
  24.  *
  25.  * @package Parsers
  26.  * @author Josh
  27.  * @since 0.2
  28.  **/
  29.  
  30. /**
  31.  * Generic language analyser. Analysers are used to tranform the language-specific tokens into a set of {@link CodeParserItem ParserItems}
  32.  **/
  33. abstract class Analyser
  34. {
  35. private $tokens;
  36. private $pos;
  37. private $token_pos;
  38.  
  39. /**
  40.   * Resets the analyser ready for more parsing work
  41.   **/
  42. public function resetState()
  43. {
  44. $this->tokens = array();
  45. $this->pos = 0;
  46. $this->token_pos = 0;
  47. }
  48.  
  49.  
  50. /**
  51.   * Processes a set of token and populates a {@link ParserFile}
  52.   **/
  53. abstract public function process($tokens, $parser_file);
  54.  
  55.  
  56. /**
  57.   * Tells the analyser what tokens it should use
  58.   **/
  59. final protected function setTokens($tokens)
  60. {
  61. $this->tokens = $tokens;
  62. }
  63.  
  64.  
  65. /**
  66.   * Sets the current position
  67.   **/
  68. final protected function setPos($pos)
  69. {
  70. $this->pos = $pos;
  71. }
  72.  
  73.  
  74. /**
  75.   * Moves the internal token pointer forwards
  76.   *
  77.   * @param $num integer The number of positions to move the pointer forwards
  78.   **/
  79. final protected function movePosForward($num = 1)
  80. {
  81. $this->pos += $num;
  82. }
  83.  
  84.  
  85. /**
  86.   * Moves the internal token pointer backwards
  87.   *
  88.   * @param $num integer The number of positions to move the pointer backwards
  89.   **/
  90. final protected function movePosBackward($num = 1)
  91. {
  92. $this->pos -= $num;
  93. }
  94.  
  95.  
  96. /**
  97.   * Returns a token at a specific position
  98.   * If no position is specified, uses the current position
  99.   **/
  100. final protected function getToken($pos = null)
  101. {
  102. if ($pos === null) $pos = $this->pos;
  103. return $this->tokens[$pos];
  104. }
  105.  
  106.  
  107. /**
  108.   * Returns the current position
  109.   **/
  110. final protected function getPos()
  111. {
  112. return $this->pos;
  113. }
  114.  
  115.  
  116. /**
  117.   * Finds a token looking forward from the current position.
  118.   * Searching starts after the current token.
  119.   * The token must be of the type specified
  120.   *
  121.   * @param mixed $token_types A token type constant, or an array of token type constants
  122.   * @param mixed $stop_list Token(s) that should stop the search process
  123.   * @return Token The found token, or null if nothing was found
  124.   **/
  125. final protected function findTokenForwards($token_types, $stop_list = null)
  126. {
  127. if (! is_array($token_types)) $token_types = array($token_types);
  128. if (! is_array($stop_list)) $stop_list = array($stop_list);
  129.  
  130. $pos = $this->pos;
  131. if ($pos != 0) $pos++;
  132.  
  133. while (true) {
  134. if (empty($this->tokens[$pos])) break;
  135.  
  136. $tok = $this->tokens[$pos];
  137. if (in_array($tok->getType(), $stop_list)) {
  138. break;
  139. }
  140.  
  141. if (in_array($tok->getType(), $token_types)) {
  142. $this->token_pos = $pos;
  143. return $tok;
  144. }
  145.  
  146. ++$pos;
  147. }
  148.  
  149. return null;
  150. }
  151.  
  152.  
  153. /**
  154.   * Finds a token looking backwards from the current position.
  155.   * Searching starts before the current token.
  156.   * The token must be of the type specified
  157.   *
  158.   * @param mixed $token_types A token type constant, or an array of token type constants
  159.   * @param mixed $stop_list Token(s) that should stop the search process
  160.   * @return Token The found token, or null if nothing was found
  161.   **/
  162. final protected function findTokenBackwards($token_types, $stop_list = null)
  163. {
  164. if (! is_array($token_types)) $token_types = array($token_types);
  165. if (! is_array($stop_list)) $stop_list = array($stop_list);
  166.  
  167. $pos = $this->pos - 1;
  168. while (true) {
  169. if (empty($this->tokens[$pos])) break;
  170.  
  171. $tok = $this->tokens[$pos];
  172. if (in_array($tok->getType(), $stop_list)) {
  173. break;
  174. }
  175.  
  176. if (in_array($tok->getType(), $token_types)) {
  177. $this->token_pos = $pos;
  178. return $tok;
  179. }
  180.  
  181. --$pos;
  182. }
  183.  
  184. return null;
  185. }
  186.  
  187.  
  188. /**
  189.   * Gets the position of the last token found using one of the search functions
  190.   **/
  191. final protected function getTokenPos()
  192. {
  193. return $this->token_pos;
  194. }
  195.  
  196.  
  197. }
  198.  
  199.  
  200. ?>
  201.