/** * Seeks to the matching curly brace. * * @param Token $token */ public function seekMatchingCurlyBrace(Token $token) { $id = $token->getId(); $level = $token->getBlockLevel(); if ($id != T_OPEN_CURLY && $id != T_CLOSE_CURLY) { throw new Exception($token->getName() . ' is not a curly brace'); } // Forward search if ($id == T_OPEN_CURLY) { $this->next(); while ($this->valid()) { $token = $this->current()->getId(); $closeLevel = $this->current()->getBlockLevel(); if ($token == T_CLOSE_CURLY && $closeLevel == $level) { return; } $this->next(); } } // Backward search if ($id == T_CLOSE_CURLY) { $this->prev(); while ($this->valid()) { $token = $this->current()->getId(); $openLevel = $this->current()->getBlockLevel(); if ($token == T_OPEN_CURLY && $level == $openLevel) { return; } $this->prev(); } } // This should be impossible since in a syntactically valid // PHP file, every opened curly brace must be closed. throw new Exception('No matching curly brace found'); }
public static function tokenize($string) { $len = strlen($string); $tokens = array(); $current_token = new Token(Token::T_NONE, 0); $i = 0; while ($i < $len) { $c = $string[$i]; switch ($c) { case '\\': // Escape character $current_token->addData($string[++$i]); break; case ' ': self::push($tokens, $current_token, $i); break; case ':': if ($current_token->getData() == null) { throw new ParseException('Expected T_FIELD_NAME, got nothing', $string, $i); } if (!$current_token->isTypeNoneOr(Token::T_FIELD_NAME)) { throw new ParseException('Expected T_FIELD_NAME, got ' . Token::getName($current_token->getType()), $string, $i); } $current_token->setType(Token::T_FIELD_NAME); self::push($tokens, $current_token, $i); $current_token->setType(Token::T_FIELD_VALUE); break; case '^': if ($current_token->getData() == null) { throw new ParseException('Expected T_FIELD_NAME, got nothing', $string, $i); } if (!$current_token->isTypeNoneOr(Token::T_FIELD_NAME)) { throw new ParseException('Expected T_FIELD_NAME, got ' . Token::getName($current_token->getType()), $string, $i); } $current_token->setType(Token::T_FIELD_NAME); $field_token = $current_token; self::push($tokens, $current_token, $i); $current_token->setType(Token::T_FIELD_WEIGHT); self::readInt($current_token, $string, $i); self::push($tokens, $current_token, $i); if ($i + 1 < $len && $string[$i + 1] == ':') { // Peek one ahead. Duplicate T_FIELD_NAME token if a T_FIELD_VALUE follows. $current_token = $field_token; } break; case '@': if ($current_token->getData() != null) { throw new ParseException('Expected nothing, got ' . Token::getName($current_token->getType()), $string, $i); } $current_token->setType(Token::T_FIELD_SEARCH); break; case '"': if ($current_token->getData() == null) { $current_token->setTypeIfNone(Token::T_STRING); self::readEncString($current_token, $string, $i); if ($i + 1 < $len && $string[$i + 1] != ' ') { // Peek one ahead. Should be empty throw new ParseException('Unexpected T_STRING', $string, $i + 1); } } else { throw new ParseException('Unexpected T_STRING', $string, $i); } break; default: $current_token->addData($c); } $i++; } self::push($tokens, $current_token, $i); return $tokens; }
/** * @covers spriebsch\PHPca\Token::__construct * @covers spriebsch\PHPca\Token::getName */ public function testGetName() { $t = new Token(T_OPEN_TAG, '<?php'); $this->assertEquals('T_OPEN_TAG', $t->getName()); }
public function addColumnSqlToken(Token $token, $alias = null) { if (is_null($alias)) { $i = 0; $tokenName = $token->getName(); $tokenName = substr($tokenName, 2); do { $i++; $alias = "{$tokenName}#{$i}"; } while (isset($this->columns[$alias])); } if (isset($this->columns[$alias])) { throw new MalformedSqlException("Column alias '{$alias}' already defined!"); } $this->columns[$alias] = $token; }