Skip to content

parser: replace the ':' conversion operator with ':>' - #13

Merged
rameel merged 1 commit into
mainfrom
parser/conversion-operator
Nov 17, 2026
Merged

rameel merged 1 commit into
mainfrom
parser/conversion-operator

Conversation

@rameel

@rameel rameel commented Nov 17, 2026

Copy link
Copy Markdown
Owner

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 : count for a conversion to the type count, so expressions such as true ? Math.PI : Math.E, true ? "a" : null, and a > b ? a : b failed with Expected ':'.

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:int syntax no longer parse. Expr.Unary.ToString prints conversions in the new syntax, and the README describes conversions.

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.
@rameel
rameel merged commit dd6e087 into main Nov 17, 2026
1 check passed
@rameel
rameel deleted the parser/conversion-operator branch November 17, 2026 15:07
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.

1 participant