-
Notifications
You must be signed in to change notification settings - Fork 1
BUGFIX: edgecasy parenthesis in expressions #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
ac90fc2
e678017
734aa46
8556d4e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,10 +39,11 @@ private function __construct( | |
|
||
public static function fromString(string $expressionAsString): self | ||
{ | ||
$tokens = Tokenizer::fromSource( | ||
Source::fromString($expressionAsString) | ||
)->getIterator(); | ||
return self::fromTokens( | ||
Tokenizer::fromSource( | ||
Source::fromString($expressionAsString) | ||
)->getIterator() | ||
$tokens | ||
); | ||
} | ||
|
||
|
@@ -51,7 +52,7 @@ public static function fromString(string $expressionAsString): self | |
* @param Precedence $precedence | ||
* @return self | ||
*/ | ||
public static function fromTokens(\Iterator $tokens, Precedence $precedence = Precedence::SEQUENCE): self | ||
public static function fromTokens(\Iterator &$tokens, Precedence $precedence = Precedence::SEQUENCE): self | ||
{ | ||
Scanner::skipSpaceAndComments($tokens); | ||
|
||
|
@@ -122,8 +123,14 @@ public static function fromTokens(\Iterator $tokens, Precedence $precedence = Pr | |
} | ||
|
||
Scanner::skipSpaceAndComments($tokens); | ||
if (Scanner::isEnd($tokens) || $precedence->mustStopAt(Scanner::type($tokens))) { | ||
return new self( | ||
root: $root | ||
); | ||
} | ||
|
||
while (!Scanner::isEnd($tokens) && !$precedence->mustStopAt(Scanner::type($tokens))) { | ||
Scanner::skipSpaceAndComments($tokens); | ||
Comment on lines
+126
to
+133
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The additions that are here are not necessary. The condition is also handled by the "while" loop and later comes the return. I think i messed stuff up because the partial fix belongs to #10 either way, it works as it is, just not super clean. |
||
switch (Scanner::type($tokens)) { | ||
case TokenType::OPERATOR_BOOLEAN_AND: | ||
case TokenType::OPERATOR_BOOLEAN_OR: | ||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -58,7 +58,9 @@ public function getIterator(): \Iterator | |||||||||
yield $token; | ||||||||||
} | ||||||||||
|
||||||||||
yield from $this->tokens; | ||||||||||
if (!Scanner::isEnd($this->tokens)) { | ||||||||||
yield from $this->tokens; | ||||||||||
} | ||||||||||
Comment on lines
+61
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
EDIT: Oh my god! Not true at all. Indeed if I remove this check, the unit tests fail... This is highly unexpected. I honestly don't know what's going on here. |
||||||||||
} | ||||||||||
|
||||||||||
public function shift(): void | ||||||||||
|
@@ -68,8 +70,11 @@ public function shift(): void | |||||||||
Scanner::skipOne($this->tokens); | ||||||||||
} | ||||||||||
|
||||||||||
public function type(): TokenType | ||||||||||
public function type(): ?TokenType | ||||||||||
{ | ||||||||||
if (Scanner::isEnd($this->tokens)) { | ||||||||||
return null; | ||||||||||
} | ||||||||||
return Scanner::type($this->tokens); | ||||||||||
} | ||||||||||
grebaldi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,4 +165,26 @@ public static function isEnd(\Iterator $tokens): bool | |
{ | ||
return !$tokens->valid(); | ||
} | ||
|
||
/** | ||
* @param \Iterator<mixed,Token> $tokens | ||
*/ | ||
public static function debugPrint(\Iterator &$tokens): string | ||
{ | ||
$tokens = (function(): \Generator { | ||
throw new \Exception('Once debugged, $tokens is empty.'); | ||
// @phpstan-ignore-next-line | ||
yield; | ||
})(); | ||
Comment on lines
+174
to
+178
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should rather be removed, right? |
||
|
||
$tokensAsArray = []; | ||
while ($tokens->valid()) { | ||
$tokensAsArray[] = [ | ||
"type" => $tokens->current()->type, | ||
"value" => $tokens->current()->value | ||
]; | ||
$tokens->next(); | ||
} | ||
return json_encode($tokensAsArray, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.