/** * Check if the token is operator * * @param Token $token * @return boolean */ public function isOperator(Token $token) { if (T_STRING == $token->getType()) { if (isset($this->byPass[$token->getValue()])) { return false; } return isset($this->operatorsStrings[$token->getValue()]); } return isset($this->operators[$token->getType()]); }
public function testGetters() { $token = new Token(Token::IDENTIFIER, 'foobar', 7); $this->assertNotEquals(Token::TEXT, $token->getType()); $this->assertEquals(Token::IDENTIFIER, $token->getType()); $this->assertNotEquals('foo', $token->getValue()); $this->assertEquals('foobar', $token->getValue()); $this->assertNotEquals(6, $token->getLine()); $this->assertEquals(7, $token->getLine()); }
public function testWithersReturnNewModifiedInstance() { $value = 'bob'; $newValue = 'alice'; $type = new TokenType(TokenType::DYNAMIC_ARRAY_TYPE); $token = new Token($value, $type); $newToken = $token->withValue($newValue); $this->assertEquals($value, $token->getValue()); $this->assertEquals($type->getValue(), $token->getType()); $this->assertInstanceOf(Token::class, $newToken); $this->assertEquals($newValue, $newToken->getValue()); $this->assertEquals($type->getValue(), $newToken->getType()); }
public function addToken(Token $token) { switch ($token->getType()) { // This block is basically a whitelist of token types permissible before // seeing a T_FUNCTION. If we hit anything else, the docblock isn't // attached to something we care about. case T_DOC_COMMENT: $this->docblock = $token; // fall through // fall through case T_WHITESPACE: case T_PUBLIC: case T_PROTECTED: case T_PRIVATE: case T_STATIC: case T_ABSTRACT: break; case Token::SINGLE_CHARACTER: if ($token->is('{')) { $this->has_started = true; $this->depth++; } elseif ($token->is('}')) { $this->depth--; } break; case T_FUNCTION: $this->track = false; break; case T_STRING: if (!$this->has_started) { $this->name = $token->getValue(); } // fall through // fall through default: if ($this->track) { $this->no_op = true; } break; } if ($this->has_started) { $this->body[] = $token; } else { $this->head[] = $token; } return $this; }
public function FromToken(Token $oldToken) { $text = $oldToken->getText(); $type = $oldToken->getType(); $line = $oldToken->getLine(); $index = $oldToken->getTokenIndex(); $charPositionInLine = $oldToken->getCharPositionInLine(); $channel = $oldToken->getChannel(); if ($oldToken instanceof CommonToken) { $start = $oldToken->start; $stop = $oldToken->stop; } $token = new CommonToken(null, $type, $channel, $start, $stop); $token->text = $text; $token->line = $line; $token->index = $index; $token->charPositionInLine = $charPositionInLine; return $token; }
public function addToken(Token $token) { switch ($token->getType()) { case T_WHITESPACE: break; case T_VARIABLE: if ($this->current_th) { } else { $this->addProvidedTypehint(); } break; default: $this->current_th .= $token; break; } $this->tokens[] = $token; if ($token->is(',')) { $this->argno++; $this->current_th = ''; } }
/** * Given the set of matches, nest them according to their types * * @param \ArrayIterator $result Iterator of match results * @param \SalesforceEng\Breakout\Token $latest Token instance * @return array Set of nested token instances (arrays) */ public function nest(&$result, $latest = null) { $container = []; if (!$result instanceof \ArrayIterator) { $result = new \ArrayIterator($result); } while ($result->valid()) { $current = new Token($result->current()); $result->next(); if ($current->getType() == 'block') { // see if we're closing it out $content = trim($current->getContent()); if ($content !== 'endif' && $content !== 'endfor' && $content !== 'else' && $content !== 'endraw') { $group = [$current]; $found = $this->nest($result, $current); $current = array_merge($group, $found); } else { if ($latest !== null) { $latestType = explode(' ', trim($latest->getContent()))[0]; if ($latestType == 'if' && trim($current->getContent()) == 'endif') { $container[] = $current; return $container; } elseif ($latestType == 'for' && trim($current->getContent()) == 'endfor') { $container[] = $current; return $container; } elseif ($latestType == 'raw' && trim($current->getContent()) == 'endraw') { $container[] = $current; return $container; } } } } $container[] = $current; } return $container; }
/** * Checks if the given token is a keyword in the current strict mode state * * @param Token $token Token to checks * * @return bool */ public function isStrictModeKeyword($token) { return $token->getType() === Token::TYPE_KEYWORD && (in_array($token->getValue(), $this->keywords) || $this->strictMode && in_array($token->getValue(), $this->strictModeKeywords)); }
protected function shouldSkipToken(Token $t) { return $t->getType() === 'e'; }
protected function procAssignment(Token $token) { $this->debug(__METHOD__, __LINE__, "identifier: {$token->getValue()}"); // Validate the leftside if ($token->getType() != TokenType::IDENTIFIER) { throw new SyntaxException($token->getLine(), $token->getColumn() - mb_strlen($token->getValue()) + 1, "Invalid assignment identifier: '{$token->getValue()}'", __FILE__, __LINE__); } $name = $token->getValue(); $token = $this->getToken(); // Validate the operator switch ($token->getType()) { case TokenType::SYMBOL: switch ($token->getValue()) { case ';': if (!$this->memory->hasVar($name)) { throw new SyntaxException($token->getLine(), $token->getColumn() - mb_strlen($name) + 1, "Unknown identifier: '{$name}'", __FILE__, __LINE__); } // Do nothing :\ return; break; case '=': // allow = break; case '++': case '--': // allow ++ and -- break; default: throw new SyntaxException($token->getLine(), $token->getColumn() - mb_strlen($token->getValue()) + 1, "Invalid assignment operator: '{$token->getValue()}'", __FILE__, __LINE__); } break; default: throw new SyntaxException($token->getLine(), $token->getColumn() - mb_strlen($token->getValue()) + 1, "Invalid assignment operator: '{$token->getValue()}'", __FILE__, __LINE__); } $operator = $token->getValue(); // Evaluate the rightside $type = $this->evalExp($value); // Perform action switch ($operator) { case '=': $this->memory->setVar($name, $value, $type); break; case '++': // number only // number only case '--': if (!$this->memory->hasVar($name)) { throw new SyntaxException($token->getLine(), $token->getColumn(), "Unknown identifier: '{$name}'", __FILE__, __LINE__); } $value = $this->memory->getVar($name); if ($operator == '++') { $newval = intval($value + 1); } else { $newval = intval($value - 1); } $this->memory->setVar($name, $newval, $type); break; default: throw new SyntaxException($token->getLine(), $token->getColumn() - mb_strlen($operator) + 1, "Unknown operator: '{$operator}'", __FILE__, __LINE__); break; } }
protected function buildNumberToken(Token $token, Character $char) { $token->setType(TokenType::INTEGER); $buffer = ''; do { $buffer .= $char->getValue(); try { $char = $this->getCharacter(); } catch (EofException $e) { throw new SyntaxException($char->getLine(), $char->getColumn() - mb_strlen($buffer) + 1, "Found EOF reading number: '{$buffer}'", __FILE__, __LINE__); } } while ($char->isNumber() || $char->getValue() == '.'); $this->ungetCharacter(); // If number contains a period, is a FLOAT if (mb_strpos($buffer, '.') !== FALSE) { $token->setType(TokenType::FLOAT); // If period is not followed by numbers if (mb_substr($buffer, -1) == '.') { throw new SyntaxException($token->getLine(), $token->getColumn(), "Invalid FLOAT value: '{$buffer}'", __FILE__, __LINE__); } } switch ($token->getType()) { case TokenType::FLOAT: $token->setValue(floatval($buffer)); break; case TokenType::INTEGER: $token->setValue(intval($buffer)); break; default: throw new SyntaxException($char->getLine(), $char->getColumn(), 'Unknown number token type: "' . $buffer . '"', __FILE__, __LINE__); break; } return $token; }
/** * Equals * * @param \PHP\Manipulator\Token $token * @return boolean */ public function equals(Token $token, $strict = false) { $match = false; if ($this->getType() === $token->getType() && $this->getValue() === $token->getValue()) { $match = true; } if (true === $strict && $this->_linenumber !== $token->getLinenumber()) { $match = false; } return $match; }