Repository navigation
parser: replace the ':' conversion operator with ':>' - #13
Merged
Merged
Conversation
The conversion suffix ':' conflicted with the conditional operator. In 'flag ? 0 : count', ': count' was taken for a conversion to the type 'count', so parsing failed with "Expected ':'" whenever the false branch started with an identifier or a keyword, as in 'true ? "a" : null'. Keeping ':' would require either backtracking, which can take exponential time on nested conditional expressions, or parentheses around conversions in the true branch. Write conversions as 'x :> int'. An expression cannot start with '>', so the new operator never conflicts with the branch separator, and conversions work in either branch without parentheses. The former 'x:int' syntax is no longer supported and fails to parse, which makes this a breaking change. Expr.Unary.ToString prints conversions in the new syntax.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A conditional expression failed to parse when its false branch started with an identifier or a keyword. In
flag ? 0 : count, the conversion suffix took: countfor a conversion to the typecount, so expressions such astrue ? Math.PI : Math.E,true ? "a" : null, anda > b ? a : bfailed withExpected ':'.This PR writes conversions as
x :> int. An expression cannot start with>, so the new operator never conflicts with the branch separator, and conversions work in either branch without parentheses. Keeping:would require either backtracking, which can take exponential time on nested conditional expressions, or parentheses around conversions in the true branch.This is a breaking change: expressions in the former
x:intsyntax no longer parse.Expr.Unary.ToStringprints conversions in the new syntax, and the README describes conversions.