mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
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:
@@ -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.
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user