mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +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:
@@ -313,6 +313,11 @@ auto ParserContext::IsTrailingOperatorInfix() -> bool {
|
||||
}
|
||||
|
||||
auto ParserContext::DiagnoseOperatorFixity(OperatorFixity fixity) -> void {
|
||||
if (!PositionKind().is_symbol()) {
|
||||
// Whitespace-based fixity rules only apply to symbolic operators.
|
||||
return;
|
||||
}
|
||||
|
||||
if (fixity == OperatorFixity::Infix) {
|
||||
// Infix operators must satisfy the infix operator rules.
|
||||
if (!IsLexicallyValidInfixOperator()) {
|
||||
@@ -331,8 +336,7 @@ auto ParserContext::DiagnoseOperatorFixity(OperatorFixity fixity) -> void {
|
||||
|
||||
// Whitespace is not permitted between a symbolic pre/postfix operator and
|
||||
// its operand.
|
||||
if (PositionKind().is_symbol() &&
|
||||
(prefix ? tokens().HasTrailingWhitespace(*position_)
|
||||
if ((prefix ? tokens().HasTrailingWhitespace(*position_)
|
||||
: tokens().HasLeadingWhitespace(*position_))) {
|
||||
CARBON_DIAGNOSTIC(UnaryOperatorHasWhitespace, Error,
|
||||
"Whitespace is not allowed {0} this unary operator.",
|
||||
@@ -340,9 +344,8 @@ auto ParserContext::DiagnoseOperatorFixity(OperatorFixity fixity) -> void {
|
||||
emitter_->Emit(
|
||||
*position_, UnaryOperatorHasWhitespace,
|
||||
prefix ? RelativeLocation::After : RelativeLocation::Before);
|
||||
}
|
||||
// Pre/postfix operators must not satisfy the infix operator rules.
|
||||
if (IsLexicallyValidInfixOperator()) {
|
||||
} else if (IsLexicallyValidInfixOperator()) {
|
||||
// Pre/postfix operators must not satisfy the infix operator rules.
|
||||
CARBON_DIAGNOSTIC(UnaryOperatorRequiresWhitespace, Error,
|
||||
"Whitespace is required {0} this unary operator.",
|
||||
RelativeLocation);
|
||||
|
||||
Reference in New Issue
Block a user