Pelzini

This is the code documentation for the Pelzini project

source of /processor/javascript_analyser.php

Contains the JavascriptAnalyser 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 JavascriptAnalyser} class
  24.  *
  25.  * @package Parsers
  26.  * @author Josh
  27.  * @since 0.2
  28.  **/
  29.  
  30. /**
  31.  * Analyses the javascript tokens, and creates a set of ParserItem objects.
  32.  **/
  33. class JavascriptAnalyser extends Analyser {
  34. /**
  35.   * Resets any state variables used by this class back to their initial state
  36.   **/
  37. public function resetState()
  38. {
  39. parent::resetState();
  40. }
  41.  
  42.  
  43. /**
  44.   * Should create ParserItem objects that represent the provided tokens
  45.   * and apply those objects to the ParserFile specified.
  46.   * @return boolean True on success, false on failure
  47.   **/
  48. public function process($tokens, $parser_file)
  49. {
  50. $this->setTokens($tokens);
  51.  
  52. // File docblock
  53. $this->setPos(0);
  54. while ($token = $this->getToken()) {
  55. switch ($token->getType()) {
  56. case TOKEN_COMMENT:
  57. break;
  58.  
  59. case TOKEN_DOCBLOCK:
  60. $parser_file->applyComment($token->getValue());
  61. break 2;
  62.  
  63. default:
  64. break 2;
  65. }
  66. $this->movePosForward();
  67. }
  68.  
  69. // Process functions
  70. $this->setPos(0);
  71. while ($function = $this->findTokenForwards(TOKEN_FUNCTION)) {
  72. $this->setPos($this->getTokenPos());
  73.  
  74. $parser_function = new ParserFunction();
  75. $parser_file->functions[] = $parser_function;
  76.  
  77. // Find the name
  78. $name = $this->findTokenForwards(TOKEN_IDENTIFIER);
  79. if ($name == null) return false;
  80. $parser_function->name = $name->getValue();
  81. $parser_function->linenum = $name->getLineNum();
  82.  
  83. // Look for a docblock
  84. $docblock = $this->findTokenBackwards(TOKEN_DOCBLOCK, array(TOKEN_CLOSE_CURLY_BRACKET));
  85. if ($docblock != null) {
  86. $parser_function->applyComment($docblock->getValue());
  87. }
  88.  
  89. // Find the end of the arguments by counting regular brackets
  90. $depth = 0;
  91. $find_types = array(
  92. TOKEN_OPEN_NORMAL_BRACKET,
  93. TOKEN_CLOSE_NORMAL_BRACKET,
  94. TOKEN_IDENTIFIER
  95. );
  96. $token = $this->findTokenForwards(TOKEN_OPEN_NORMAL_BRACKET);
  97. $this->setPos($this->getTokenPos());
  98. while ($token) {
  99. switch ($token->getType()) {
  100. case TOKEN_OPEN_NORMAL_BRACKET:
  101. $depth++;
  102. break;
  103.  
  104. case TOKEN_CLOSE_NORMAL_BRACKET:
  105. $depth--;
  106. break;
  107.  
  108. case TOKEN_IDENTIFIER:
  109. $arg = new ParserArgument();
  110. $arg->name = $token->getValue();
  111. $parser_function->args[] = $arg;
  112. break;
  113.  
  114. }
  115.  
  116. if ($depth == 0) break;
  117.  
  118. $token = $this->findTokenForwards($find_types);
  119. $this->setPos($this->getTokenPos());
  120. }
  121.  
  122. // Find the end of the function by counting curly brackets
  123. $depth = 0;
  124. $token = $this->findTokenForwards(TOKEN_OPEN_CURLY_BRACKET);
  125. $this->setPos($this->getTokenPos());
  126. while ($token) {
  127. if ($token->getType() == TOKEN_OPEN_CURLY_BRACKET) $depth++;
  128. if ($token->getType() == TOKEN_CLOSE_CURLY_BRACKET) $depth--;
  129.  
  130. if ($depth == 0) break;
  131.  
  132. $token = $this->findTokenForwards(array(TOKEN_OPEN_CURLY_BRACKET, TOKEN_CLOSE_CURLY_BRACKET));
  133. $this->setPos($this->getTokenPos());
  134. }
  135.  
  136. $parser_function->post_load();
  137. }
  138. // End of function processing
  139.  
  140. }
  141.  
  142.  
  143. }
  144.  
  145.  
  146. ?>
  147.