Skip to content
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

BUGFIX: edgecasy parenthesis in expressions #18

Merged
merged 4 commits into from
Aug 5, 2023

Conversation

mhsdesign
Copy link
Contributor

@mhsdesign mhsdesign commented Apr 23, 2023

previously ExpressionNode::fromString("(foo)") couldnt be parsed (it worked though when used in return statement of a module)

but the real fix, is that you couldnt write return ((foo)) as $tokens is reassigned but not passed by ref

(foo === ((null))) ? 1 : (((0))) this seemed to worked previously but not this: (((foo)) === ((null))) ? 1 : (((0)))

previously `ExpressionNode::fromString("(foo)")` couldnt be parsed (it worked though when used in return statement of a module)

but the real fix, is that you couldnt write `return ((foo))`
as $tokens is reassigned but not passed by ref
@mhsdesign mhsdesign changed the title BUGFIX: parenthesis in expressions BUGFIX: edgecasy parenthesis in expressions Apr 23, 2023
for example ExpressionNode::fromString('(true) ? 42 : "foo"')
would previously only tokenize `(true)` due to the brace

this is not an issue, when used in the return statement
Copy link
Member

@grebaldi grebaldi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @mhsdesign,

sorry that I'm taking so long to review your changes. Please know, that I really appreciate the hard work you're putting into this. I only have trouble keeping up with your pace :D

For this particular fix, I think I understand the problem, you're trying to solve. Yet, there's a couple of questions that arose while I was reading the code. But I think, it'll be easy to clear those up and get this fix on the road :)

EDIT: I may stand corrected on my comments below, because as pointed out in #10 (comment), I have been running the wrong tests. I'll have another look and keep you posted.

Comment on lines +174 to +178
$tokens = (function(): \Generator {
throw new \Exception('Once debugged, $tokens is empty.');
// @phpstan-ignore-next-line
yield;
})();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should rather be removed, right?

src/Parser/Tokenizer/Tokenizer.php Show resolved Hide resolved
src/Parser/Tokenizer/LookAhead.php Show resolved Hide resolved
Comment on lines +61 to +63
if (!Scanner::isEnd($this->tokens)) {
yield from $this->tokens;
}
Copy link
Member

@grebaldi grebaldi Jun 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!Scanner::isEnd($this->tokens)) {
yield from $this->tokens;
}
yield from $this->tokens;

Scanner::isEnd only checks whether $this->tokens is still valid. This is implied in yield from - the extra check is not needed.

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.

src/Parser/Ast/ExpressionNode.php Show resolved Hide resolved
Copy link
Member

@grebaldi grebaldi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will move those test cases regarding parentheses into the Parser realm in the scope of #31, once it's rebased on this.

@grebaldi grebaldi merged commit 279738d into main Aug 5, 2023
5 checks passed
@grebaldi grebaldi deleted the bugfix/parenthesisInExpressions branch August 5, 2023 16:37
@mhsdesign
Copy link
Contributor Author

Thanks for taking care @grebaldi and sorry for not responding ^^ its hard to have so many side projects xD

Comment on lines +126 to +133
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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

mhsdesign added a commit that referenced this pull request Aug 6, 2023
The additions that here from #18 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, thats why i removed the duplicate logic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants