Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Commit

Permalink
Merge pull request #38 from Kdyby/tokenizer-update
Browse files Browse the repository at this point in the history
Replace deprecated TokenIterator with Stream and Nette\Utils\Tokenizer with Nette\Tokenizer\Tokenizer
  • Loading branch information
dakorpar authored Feb 11, 2019
2 parents bb711a3 + 881c0d9 commit 6f2e3e2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,22 @@ cache:
php:
- 7.1
- 7.2
- 7.3
matrix:
include:
- php: 7.1
env: COVERAGE="--coverage ./coverage.xml --coverage-src ./src" TESTER_RUNTIME="phpdbg"
- php: 7.2
env: COVERAGE="--coverage ./coverage.xml --coverage-src ./src" TESTER_RUNTIME="phpdbg"
- php: 7.3
env: COVERAGE="--coverage ./coverage.xml --coverage-src ./src" TESTER_RUNTIME="phpdbg"
allow_failures:
- php: 7.1
env: COVERAGE="--coverage ./coverage.xml --coverage-src ./src" TESTER_RUNTIME="phpdbg"
- php: 7.2
env: COVERAGE="--coverage ./coverage.xml --coverage-src ./src" TESTER_RUNTIME="phpdbg"
- php: 7.3
env: COVERAGE="--coverage ./coverage.xml --coverage-src ./src" TESTER_RUNTIME="phpdbg"
before_install:
- travis_retry composer self-update

Expand Down
47 changes: 25 additions & 22 deletions src/Kdyby/Aop/Pointcut/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use Kdyby;
use Nette;
use Nette\PhpGenerator\PhpLiteral;
use Nette\Utils\TokenIterator;
use Nette\Utils\Tokenizer;
use Nette\Tokenizer\Stream;
use Nette\Tokenizer\Tokenizer;



Expand Down Expand Up @@ -73,10 +73,10 @@ public function __construct(MatcherFactory $matcherFactory)
public function parse($input)
{
try {
$tokens = new TokenIterator($this->tokenizer->tokenize($input));
$tokens = $this->tokenizer->tokenize($input);
$tokens->ignored = [self::TOK_WHITESPACE];

} catch (Nette\Utils\TokenizerException $e) {
} catch (Nette\Tokenizer\Exception $e) {
throw new Kdyby\Aop\ParserException("Input contains unexpected expressions", 0, $e);
}

Expand All @@ -90,14 +90,14 @@ public function parse($input)
* @return Rules|mixed
* @throws \Kdyby\Aop\ParserException
*/
protected function doParse(TokenIterator $tokens)
protected function doParse(Stream $tokens)
{
$inverseNext = FALSE;
$operator = NULL;
$rules = [];
while ($token = $tokens->nextToken()) {
if ($tokens->isCurrent(self::TOK_KEYWORD)) {
$rule = $this->{'parse' . $token[0]}($tokens);
$rule = $this->{'parse' . $token->value}($tokens);
if ($inverseNext) {
$rule = new Matcher\Inverse($rule);
$inverseNext = FALSE;
Expand Down Expand Up @@ -145,7 +145,7 @@ protected function doParse(TokenIterator $tokens)



protected function parseClass(TokenIterator $tokens)
protected function parseClass(Stream $tokens)
{
$tokens->nextUntil(self::TOK_IDENTIFIER);
$className = $tokens->nextValue();
Expand All @@ -156,7 +156,7 @@ protected function parseClass(TokenIterator $tokens)



protected function parseMethod(TokenIterator $tokens)
protected function parseMethod(Stream $tokens)
{
$visibility = NULL;
$arguments = [];
Expand Down Expand Up @@ -201,7 +201,7 @@ protected function parseMethod(TokenIterator $tokens)



protected function parseWithin(TokenIterator $tokens)
protected function parseWithin(Stream $tokens)
{
$tokens->nextUntil(self::TOK_IDENTIFIER);
$within = $tokens->nextValue();
Expand All @@ -212,7 +212,7 @@ protected function parseWithin(TokenIterator $tokens)



protected function parseFilter(TokenIterator $tokens)
protected function parseFilter(Stream $tokens)
{
$tokens->nextUntil(self::TOK_IDENTIFIER);
$filter = $tokens->nextValue();
Expand All @@ -223,7 +223,7 @@ protected function parseFilter(TokenIterator $tokens)



protected function parseSetting(TokenIterator $tokens)
protected function parseSetting(Stream $tokens)
{
$tokens->nextUntil('(');
if (!$criteria = $this->parseArguments($tokens)) {
Expand All @@ -235,7 +235,7 @@ protected function parseSetting(TokenIterator $tokens)



protected function parseEvaluate(TokenIterator $tokens)
protected function parseEvaluate(Stream $tokens)
{
$tokens->nextUntil('(');
if (!$criteria = $this->parseArguments($tokens)) {
Expand All @@ -247,7 +247,7 @@ protected function parseEvaluate(TokenIterator $tokens)



protected function parseClassAnnotatedWith(TokenIterator $tokens)
protected function parseClassAnnotatedWith(Stream $tokens)
{
$tokens->nextUntil(self::TOK_IDENTIFIER);
$annotation = $tokens->nextValue();
Expand All @@ -258,7 +258,7 @@ protected function parseClassAnnotatedWith(TokenIterator $tokens)



protected function parseMethodAnnotatedWith(TokenIterator $tokens)
protected function parseMethodAnnotatedWith(Stream $tokens)
{
$tokens->nextUntil(self::TOK_IDENTIFIER);
$annotation = $tokens->nextValue();
Expand All @@ -269,7 +269,7 @@ protected function parseMethodAnnotatedWith(TokenIterator $tokens)



protected function parseArguments(TokenIterator $tokens)
protected function parseArguments(Stream $tokens)
{
$operator = NULL;
$conditions = [];
Expand Down Expand Up @@ -323,7 +323,7 @@ protected function parseArguments(TokenIterator $tokens)
$right[] = self::sanitizeArgumentExpression($tokens->currentValue(), $token);

} elseif (!$tokens->isCurrent(',', self::TOK_WHITESPACE)) {
throw new Kdyby\Aop\ParserException('Unexpected token ' . $token[Tokenizer::TYPE]);
throw new Kdyby\Aop\ParserException('Unexpected token ' . $token->type);
}
}

Expand Down Expand Up @@ -380,11 +380,14 @@ protected function parseArguments(TokenIterator $tokens)
return $criteria;
}



/**
* @param mixed $value
* @param Nette\Tokenizer\Token $token
* @return PhpLiteral
*/
protected static function sanitizeArgumentExpression($value, $token)
{
if ($token[Tokenizer::TYPE] === self::TOK_STRING || is_numeric($value) || preg_match('~^(TRUE|FALSE)\z~i', $value)) {
if ($token->type === self::TOK_STRING || is_numeric($value) || preg_match('~^(TRUE|FALSE)\z~i', $value)) {
return new PhpLiteral($value);
}

Expand All @@ -394,13 +397,13 @@ protected static function sanitizeArgumentExpression($value, $token)


/**
* @param TokenIterator $tokens
* @param Stream $tokens
* @param array|string $types
* @param array|string $allowedToSkip
* @throws \Kdyby\Aop\ParserException
* @return NULL|string
*/
protected static function nextValue(TokenIterator $tokens, $types, $allowedToSkip = [])
protected static function nextValue(Stream $tokens, $types, $allowedToSkip = [])
{
do {
if (call_user_func_array([$tokens, 'isCurrent'], (array)$types)) {
Expand All @@ -409,7 +412,7 @@ protected static function nextValue(TokenIterator $tokens, $types, $allowedToSki

if (!$allowedToSkip || !call_user_func_array([$tokens, 'isCurrent'], (array)$allowedToSkip)) {
$type = $tokens->currentToken();
throw new Kdyby\Aop\ParserException('Unexpected token ' . $type[Tokenizer::TYPE] . ' at offset ' . $type[Tokenizer::OFFSET]);
throw new Kdyby\Aop\ParserException('Unexpected token ' . $type->type . ' at offset ' . $type->offset);
}

} while ($token = $tokens->nextToken());
Expand Down

0 comments on commit 6f2e3e2

Please sign in to comment.