1<?php 2 3/** 4 * Hoa 5 * 6 * 7 * @license 8 * 9 * New BSD License 10 * 11 * Copyright © 2007-2017, Hoa community. All rights reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions are met: 15 * * Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * * Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * * Neither the name of the Hoa nor the names of its contributors may be 21 * used to endorse or promote products derived from this software without 22 * specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE 28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34 * POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37namespace Hoa\Compiler\Llk\Rule; 38 39use Hoa\Compiler; 40use Hoa\File; 41 42/** 43 * Class \Hoa\Compiler\Llk\Rule\Token. 44 * 45 * The token rule. 46 * 47 * @copyright Copyright © 2007-2017 Hoa community 48 * @license New BSD License 49 */ 50class Token extends Rule 51{ 52 /** 53 * LL(k) compiler of hoa://Library/Regex/Grammar.pp. 54 * 55 * @var \Hoa\Compiler\Llk\Parser 56 */ 57 protected static $_regexCompiler = null; 58 59 /** 60 * Token name. 61 * 62 * @var string 63 */ 64 protected $_tokenName = null; 65 66 /** 67 * Namespace. 68 * 69 * @var string 70 */ 71 protected $_namespace = null; 72 73 /** 74 * Token representation. 75 * 76 * @var string 77 */ 78 protected $_regex = null; 79 80 /** 81 * AST of the regex. 82 * 83 * @var \Hoa\Compiler\Llk\TreeNode 84 */ 85 protected $_ast = null; 86 87 /** 88 * Token value. 89 * 90 * @var string 91 */ 92 protected $_value = null; 93 94 /** 95 * Whether the token is kept or not in the AST. 96 * 97 * @var bool 98 */ 99 protected $_kept = false; 100 101 /** 102 * Unification index. 103 * 104 * @var int 105 */ 106 protected $_unification = -1; 107 108 109 110 /** 111 * Constructor. 112 * 113 * @param string $name Name. 114 * @param string $tokenName Token name. 115 * @param string $nodeId Node ID. 116 * @param int $unification Unification index. 117 * @param bool $kept Whether the token is kept or not in the AST. 118 */ 119 public function __construct( 120 $name, 121 $tokenName, 122 $nodeId, 123 $unification, 124 $kept = false 125 ) { 126 parent::__construct($name, null, $nodeId); 127 128 $this->_tokenName = $tokenName; 129 $this->_unification = $unification; 130 $this->setKept($kept); 131 132 return; 133 } 134 135 /** 136 * Get token name. 137 * 138 * @return string 139 */ 140 public function getTokenName() 141 { 142 return $this->_tokenName; 143 } 144 145 /** 146 * Set token namespace. 147 * 148 * @param string $namespace Namespace. 149 * @return string 150 */ 151 public function setNamespace($namespace) 152 { 153 $old = $this->_namespace; 154 $this->_namespace = $namespace; 155 156 return $old; 157 } 158 159 /** 160 * Get token namespace. 161 * 162 * @return string 163 */ 164 public function getNamespace() 165 { 166 return $this->_namespace; 167 } 168 169 /** 170 * Set representation. 171 * 172 * @param string $regex Representation. 173 * @return string 174 */ 175 public function setRepresentation($regex) 176 { 177 $old = $this->_regex; 178 $this->_regex = $regex; 179 180 return $old; 181 } 182 183 /** 184 * Get token representation. 185 * 186 * @return string 187 */ 188 public function getRepresentation() 189 { 190 return $this->_regex; 191 } 192 193 /** 194 * Get AST of the token representation. 195 * 196 * @return \Hoa\Compiler\Llk\TreeNode 197 */ 198 public function getAST() 199 { 200 if (null === static::$_regexCompiler) { 201 $stream = new File\Read('hoa://Library/Regex/Grammar.pp'); 202 $stream->rewind(); 203 204 static::$_regexCompiler = Compiler\Llk::load($stream); 205 } 206 207 if (null === $this->_ast) { 208 $this->_ast = static::$_regexCompiler->parse( 209 $this->getRepresentation() 210 ); 211 } 212 213 return $this->_ast; 214 } 215 216 /** 217 * Set token value. 218 * 219 * @param string $value Value. 220 * @return string 221 */ 222 public function setValue($value) 223 { 224 $old = $this->_value; 225 $this->_value = $value; 226 227 return $old; 228 } 229 230 /** 231 * Get token value. 232 * 233 * @return string 234 */ 235 public function getValue() 236 { 237 return $this->_value; 238 } 239 240 /** 241 * Set whether the token is kept or not in the AST. 242 * 243 * @param bool $kept Kept. 244 * @return bool 245 */ 246 public function setKept($kept) 247 { 248 $old = $this->_kept; 249 $this->_kept = $kept; 250 251 return $old; 252 } 253 254 /** 255 * Check whether the token is kept in the AST or not. 256 * 257 * @return bool 258 */ 259 public function isKept() 260 { 261 return $this->_kept; 262 } 263 264 /** 265 * Get unification index. 266 * 267 * @return int 268 */ 269 public function getUnificationIndex() 270 { 271 return $this->_unification; 272 } 273} 274