Parsing support for if expressions. (#2883)

We model `if a then b else` as a prefix operator for parsing precedence purposes. The rule that a statement starting with `if` is never an `if` expression is handled implicitly because the statement parser never invokes the expression parser for a statement starting with `if`.

This exposed a bug in our diagnosis of the whitespace rule for prefix operators, which was incorrectly being applied to non-symbolic operators in some cases, and was producing a bogus second diagnostic in some cases, which is also fixed here.
This commit is contained in:
Richard Smith authored and GitHub committed 2023-06-09 16:41:10 -07:00
1 parent 4e1adcf4c7
commit 2c45cb3be8
18 files changed
+400 -22

No files matched your search

+28 -1
View File
@@ -181,7 +181,11 @@ CARBON_PARSER_STATE_VARIANTS2(Designator, Expression, Struct)
// Handles processing of an expression.
//
// If valid prefix operator:
// If `If`:
// 1. Expression
// 2. ExpressionThen
// 3. ExpressionIfFinish
// Else if valid prefix operator:
// 1. Expression
// 2. ExpressionLoopForPrefix
// Else:
@@ -242,6 +246,29 @@ CARBON_PARSER_STATE(ExpressionLoopForBinary)
// 1. ExpressionLoop
CARBON_PARSER_STATE(ExpressionLoopForPrefix)
// Completes an IfExpression.
//
// Always:
// (state done)
CARBON_PARSER_STATE(ExpressionIfFinish)
// Handles the `then` token in an `if` expression.
//
// If `Then`:
// 1. Expression
// 2. ExpressionElse
// Else:
// (state done)
CARBON_PARSER_STATE(ExpressionThen)
// Handles the `else` token in an `if` expression.
//
// If `Else`:
// 1. Expression
// Else:
// (state done)
CARBON_PARSER_STATE(ExpressionElse)
// Handles the `;` for an expression statement, which is different from most
// keyword statements.
//