analyser.php | ||||
|
||||
Coverage | ||||||||||
Classes | Functions / Methods | Lines | ||||||||
Total |
|
0.00% | 0 / 1 |
|
70.00% | 7 / 10 | CRAP |
|
91.11% | 41 / 45 |
Analyser |
|
0.00% | 0 / 1 |
|
72.73% | 8 / 11 | 25.44 |
|
91.11% | 41 / 45 |
resetState() |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 4 / 4 | |||
process($tokens, $parser_file) |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 0 / 0 | |||
setTokens($tokens) |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 2 / 2 | |||
setPos($pos) |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 2 / 2 | |||
movePosForward($num = 1) |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 2 / 2 | |||
movePosBackward($num = 1) |
|
0.00% | 0 / 1 | 2 |
|
0.00% | 0 / 2 | |||
getToken($pos = null) |
|
100.00% | 1 / 1 | 2 |
|
100.00% | 2 / 2 | |||
getPos() |
|
0.00% | 0 / 1 | 2 |
|
0.00% | 0 / 1 | |||
findTokenForwards($token_types, $stop_list = null) |
|
0.00% | 0 / 1 | 8.02 |
|
93.33% | 14 / 15 | |||
findTokenBackwards($token_types, $stop_list = null) |
|
100.00% | 1 / 1 | 7 |
|
100.00% | 14 / 14 | |||
getTokenPos() |
|
100.00% | 1 / 1 | 1 |
|
100.00% | 1 / 1 |
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 3 : $this->tokens = array(); 45 3 : $this->pos = 0; 46 3 : $this->token_pos = 0; 47 3 : } 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 6 : $this->tokens = $tokens; 62 6 : } 63 : 64 : 65 : /** 66 : * Sets the current position 67 : **/ 68 : final protected function setPos($pos) 69 : { 70 6 : $this->pos = $pos; 71 6 : } 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 1 : $this->pos += $num; 82 1 : } 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 0 : $this->pos -= $num; 93 0 : } 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 6 : if ($pos === null) $pos = $this->pos; 103 6 : return $this->tokens[$pos]; 104 : } 105 : 106 : 107 : /** 108 : * Returns the current position 109 : **/ 110 : final protected function getPos() 111 : { 112 0 : 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 6 : if (! is_array($token_types)) $token_types = array($token_types); 128 6 : if (! is_array($stop_list)) $stop_list = array($stop_list); 129 : 130 6 : $pos = $this->pos; 131 6 : if ($pos != 0) $pos++; 132 : 133 6 : while (true) { 134 6 : if (empty($this->tokens[$pos])) break; 135 : 136 6 : $tok = $this->tokens[$pos]; 137 6 : if (in_array($tok->getType(), $stop_list)) { 138 0 : break; 139 : } 140 : 141 6 : if (in_array($tok->getType(), $token_types)) { 142 6 : $this->token_pos = $pos; 143 6 : return $tok; 144 : } 145 : 146 6 : ++$pos; 147 6 : } 148 : 149 6 : 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 6 : if (! is_array($token_types)) $token_types = array($token_types); 165 6 : if (! is_array($stop_list)) $stop_list = array($stop_list); 166 : 167 6 : $pos = $this->pos - 1; 168 6 : while (true) { 169 6 : if (empty($this->tokens[$pos])) break; 170 : 171 5 : $tok = $this->tokens[$pos]; 172 5 : if (in_array($tok->getType(), $stop_list)) { 173 1 : break; 174 : } 175 : 176 5 : if (in_array($tok->getType(), $token_types)) { 177 5 : $this->token_pos = $pos; 178 5 : return $tok; 179 : } 180 : 181 1 : --$pos; 182 1 : } 183 : 184 5 : 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 6 : return $this->token_pos; 194 : } 195 : 196 : 197 : } 198 : 199 : 200 : ?> |
Generated by PHP_CodeCoverage 1.1.2 using PHP 5.4.39-0+deb7u2 and PHPUnit 3.6.10 at Fri Sep 11 11:35:19 WIT 2015. |