Skip to content

Bug fixes for parsing associativity #268

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

Merged
merged 1 commit into from
Dec 29, 2018

Conversation

TysonAndre
Copy link
Contributor

This fixes the most common cases for #19
(there may be other edge cases I haven't considered yet)

This also fixes incorrect precedence for <=>.
It has the same precedence as == -
https://secure.php.net/manual/en/language.operators.precedence.php
has correct information for the <=> operator.

@TysonAndre
Copy link
Contributor Author

TysonAndre commented Dec 5, 2018

I ran a test of all valid combinations of a subset of binary operators (according to what php-ast said was valid) to check for false positives. (not including assignment by ref)

(The 4 remaining edge cases include $x ?? $y ?: $z and $x ?: $y ?: $z, which were pre-existing. The other two were nonsense with instanceof that shouldn't occur in valid code.)

The number of errors in the validation tests is still 3, and I think those were pre-existing (for the symfony edge cases)

This fixes the most common cases for microsoft#19
(there may be other edge cases I haven't considered yet)

This also fixes incorrect precedence for `<=>`.
It has the same precedence as `==` -
https://secure.php.net/manual/en/language.operators.precedence.php
has correct information for the `<=>` operator.
@TysonAndre TysonAndre force-pushed the parsing-precedence-fixes branch from ceef8c3 to c64dbb2 Compare December 5, 2018 02:46
@TysonAndre
Copy link
Contributor Author

Is there anything else that should be done in this PR?

@roblourens
Copy link
Member

Sorry, I will look at it soon. Been busy/on vacation.

Copy link
Member

@roblourens roblourens left a comment

Choose a reason for hiding this comment

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

This looks good but I don't understand why $x == $y <=> $z; is a syntax error but $x < $a <=> $b; is not? I must be missing something.

@TysonAndre
Copy link
Contributor Author

TysonAndre commented Dec 28, 2018

This is part of the standard that most programmers normally don't need to think about.

The PHP manual mentions this (replace == with <=> in that sentence). See http://php.net/manual/en/language.operators.precedence.php

Operators of equal precedence that are non-associative cannot be used next to each other, for example 1 < 2 > 1 is illegal in PHP. The expression 1 <= 1 == 1 on the other hand is legal, because the == operator has lesser precedence than the <= operator.

The below two groups have the same precedences within the groups:

  • (non-associative) < <= > >=
  • (non-associative) == != === !== <> <=>

I'm assuming this is so that expressions such as X < Y == EXPECTED_BOOL_VAL will work, but ambiguous expressions would be detected as a parse error (e.g. X == Y == Z)

php > var_export(0 < 3 == true);
true

@roblourens
Copy link
Member

That's kind of crazy but makes sense, thanks!

@roblourens roblourens merged commit b662587 into microsoft:master Dec 29, 2018
@TysonAndre TysonAndre deleted the parsing-precedence-fixes branch March 27, 2021 16:50
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