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()); }
protected function _getParseTokenFromToken(Token $token) { $code = $token->getCode(); if ($code === "WHILE_START") { $code = "WHILE"; } return new ParseToken($code, $token->getContent(), $token->getChar(), $token->getLine()); }
/** * Returns the line in the source file the message refers to. * If this error message does not refer to a token (lint error), * the line number is 0. * * @returns integer */ public function getLine() { // Explicitly set line overrides token start line if ($this->line !== null) { return $this->line; } if (!$this->token instanceof Token) { return 0; } return $this->token->getLine(); }
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; }
/** * @covers spriebsch\PHPca\Token::__construct * @covers spriebsch\PHPca\Token::getLine */ public function testGetLineReturnsCorrectLineWhenWhitespaceTokenHasNoLeadingNewline() { $t = new Token(T_WHITESPACE, " ", 3, 7); $this->assertEquals(3, $t->getLine()); }
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; }