mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 19:31:08 +01:00
Use abbreviation "expr" instead of "expression" (#3375)
Part of switching to the [abbreviations we've decided to use](https://docs.google.com/document/d/1RRYMm42osyqhI2LyjrjockYCutQ5dOf8Abu50kTrkX0/edit?resourcekey=0-kHyqOESbOHmzZphUbtLrTw#heading=h.pph7i5m5un7q). I will rename files in a follow-up PR.
This commit is contained in:
@@ -6,15 +6,15 @@
|
||||
|
||||
namespace Carbon::Parse {
|
||||
|
||||
static auto DiagnoseStatementOperatorAsSubexpression(Context& context) -> void {
|
||||
CARBON_DIAGNOSTIC(StatementOperatorAsSubexpression, Error,
|
||||
static auto DiagnoseStatementOperatorAsSubexpr(Context& context) -> void {
|
||||
CARBON_DIAGNOSTIC(StatementOperatorAsSubexpr, Error,
|
||||
"Operator `{0}` can only be used as a complete statement.",
|
||||
Lex::TokenKind);
|
||||
context.emitter().Emit(*context.position(), StatementOperatorAsSubexpression,
|
||||
context.emitter().Emit(*context.position(), StatementOperatorAsSubexpr,
|
||||
context.PositionKind());
|
||||
}
|
||||
|
||||
auto HandleExpression(Context& context) -> void {
|
||||
auto HandleExpr(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
// Check for a prefix operator.
|
||||
@@ -25,7 +25,7 @@ auto HandleExpression(Context& context) -> void {
|
||||
OperatorPriority::RightFirst) {
|
||||
// The precedence rules don't permit this prefix operator in this
|
||||
// context. Diagnose this, but carry on and parse it anyway.
|
||||
if (PrecedenceGroup::GetPriority(PrecedenceGroup::ForTopLevelExpression(),
|
||||
if (PrecedenceGroup::GetPriority(PrecedenceGroup::ForTopLevelExpr(),
|
||||
*operator_precedence) ==
|
||||
OperatorPriority::RightFirst) {
|
||||
CARBON_DIAGNOSTIC(
|
||||
@@ -37,7 +37,7 @@ auto HandleExpression(Context& context) -> void {
|
||||
context.PositionKind());
|
||||
} else {
|
||||
// This operator wouldn't be allowed even if parenthesized.
|
||||
DiagnoseStatementOperatorAsSubexpression(context);
|
||||
DiagnoseStatementOperatorAsSubexpr(context);
|
||||
}
|
||||
} else {
|
||||
// Check that this operator follows the proper whitespace rules.
|
||||
@@ -45,36 +45,35 @@ auto HandleExpression(Context& context) -> void {
|
||||
}
|
||||
|
||||
if (context.PositionIs(Lex::TokenKind::If)) {
|
||||
context.PushState(State::IfExpressionFinish);
|
||||
context.PushState(State::IfExpressionFinishCondition);
|
||||
context.PushState(State::IfExprFinish);
|
||||
context.PushState(State::IfExprFinishCondition);
|
||||
} else {
|
||||
context.PushStateForExpressionLoop(State::ExpressionLoopForPrefix,
|
||||
state.ambient_precedence,
|
||||
*operator_precedence);
|
||||
context.PushStateForExprLoop(State::ExprLoopForPrefix,
|
||||
state.ambient_precedence,
|
||||
*operator_precedence);
|
||||
}
|
||||
|
||||
++context.position();
|
||||
context.PushStateForExpression(*operator_precedence);
|
||||
context.PushStateForExpr(*operator_precedence);
|
||||
} else {
|
||||
context.PushStateForExpressionLoop(State::ExpressionLoop,
|
||||
state.ambient_precedence,
|
||||
PrecedenceGroup::ForPostfixExpression());
|
||||
context.PushState(State::ExpressionInPostfix);
|
||||
context.PushStateForExprLoop(State::ExprLoop, state.ambient_precedence,
|
||||
PrecedenceGroup::ForPostfixExpr());
|
||||
context.PushState(State::ExprInPostfix);
|
||||
}
|
||||
}
|
||||
|
||||
auto HandleExpressionInPostfix(Context& context) -> void {
|
||||
auto HandleExprInPostfix(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
// Continue to the loop state.
|
||||
state.state = State::ExpressionInPostfixLoop;
|
||||
state.state = State::ExprInPostfixLoop;
|
||||
|
||||
// Parses a primary expression, which is either a terminal portion of an
|
||||
// expression tree, such as an identifier or literal, or a parenthesized
|
||||
// expression.
|
||||
switch (context.PositionKind()) {
|
||||
case Lex::TokenKind::Identifier: {
|
||||
context.AddLeafNode(NodeKind::NameExpression, context.Consume());
|
||||
context.AddLeafNode(NodeKind::NameExpr, context.Consume());
|
||||
context.PushState(state);
|
||||
break;
|
||||
}
|
||||
@@ -95,26 +94,26 @@ auto HandleExpressionInPostfix(Context& context) -> void {
|
||||
}
|
||||
case Lex::TokenKind::OpenCurlyBrace: {
|
||||
context.PushState(state);
|
||||
context.PushState(State::BraceExpression);
|
||||
context.PushState(State::BraceExpr);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::OpenParen: {
|
||||
context.PushState(state);
|
||||
context.PushState(State::ParenExpression);
|
||||
context.PushState(State::ParenExpr);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::OpenSquareBracket: {
|
||||
context.PushState(state);
|
||||
context.PushState(State::ArrayExpression);
|
||||
context.PushState(State::ArrayExpr);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::SelfValueIdentifier: {
|
||||
context.AddLeafNode(NodeKind::SelfValueNameExpression, context.Consume());
|
||||
context.AddLeafNode(NodeKind::SelfValueNameExpr, context.Consume());
|
||||
context.PushState(state);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::SelfTypeIdentifier: {
|
||||
context.AddLeafNode(NodeKind::SelfTypeNameExpression, context.Consume());
|
||||
context.AddLeafNode(NodeKind::SelfTypeNameExpr, context.Consume());
|
||||
context.PushState(state);
|
||||
break;
|
||||
}
|
||||
@@ -122,15 +121,15 @@ auto HandleExpressionInPostfix(Context& context) -> void {
|
||||
// Add a node to keep the parse tree balanced.
|
||||
context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
|
||||
/*has_error=*/true);
|
||||
CARBON_DIAGNOSTIC(ExpectedExpression, Error, "Expected expression.");
|
||||
context.emitter().Emit(*context.position(), ExpectedExpression);
|
||||
CARBON_DIAGNOSTIC(ExpectedExpr, Error, "Expected expression.");
|
||||
context.emitter().Emit(*context.position(), ExpectedExpr);
|
||||
context.ReturnErrorOnState();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto HandleExpressionInPostfixLoop(Context& context) -> void {
|
||||
auto HandleExprInPostfixLoop(Context& context) -> void {
|
||||
// This is a cyclic state that repeats, so this state is typically pushed back
|
||||
// on.
|
||||
auto state = context.PopState();
|
||||
@@ -138,25 +137,25 @@ auto HandleExpressionInPostfixLoop(Context& context) -> void {
|
||||
switch (context.PositionKind()) {
|
||||
case Lex::TokenKind::Period: {
|
||||
context.PushState(state);
|
||||
state.state = State::PeriodAsExpression;
|
||||
state.state = State::PeriodAsExpr;
|
||||
context.PushState(state);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::MinusGreater: {
|
||||
context.PushState(state);
|
||||
state.state = State::ArrowExpression;
|
||||
state.state = State::ArrowExpr;
|
||||
context.PushState(state);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::OpenParen: {
|
||||
context.PushState(state);
|
||||
state.state = State::CallExpression;
|
||||
state.state = State::CallExpr;
|
||||
context.PushState(state);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::OpenSquareBracket: {
|
||||
context.PushState(state);
|
||||
state.state = State::IndexExpression;
|
||||
state.state = State::IndexExpr;
|
||||
context.PushState(state);
|
||||
break;
|
||||
}
|
||||
@@ -169,7 +168,7 @@ auto HandleExpressionInPostfixLoop(Context& context) -> void {
|
||||
}
|
||||
}
|
||||
|
||||
auto HandleExpressionLoop(Context& context) -> void {
|
||||
auto HandleExprLoop(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
auto operator_kind = context.PositionKind();
|
||||
@@ -202,7 +201,7 @@ auto HandleExpressionLoop(Context& context) -> void {
|
||||
// Either the LHS operator and this operator are ambiguous, or the
|
||||
// LHS operator is a unary operator that can't be nested within
|
||||
// this operator. Either way, parentheses are required.
|
||||
if (PrecedenceGroup::GetPriority(PrecedenceGroup::ForTopLevelExpression(),
|
||||
if (PrecedenceGroup::GetPriority(PrecedenceGroup::ForTopLevelExpr(),
|
||||
operator_precedence) ==
|
||||
OperatorPriority::RightFirst) {
|
||||
CARBON_DIAGNOSTIC(
|
||||
@@ -211,7 +210,7 @@ auto HandleExpressionLoop(Context& context) -> void {
|
||||
context.emitter().Emit(*context.position(), OperatorRequiresParentheses);
|
||||
} else {
|
||||
// This operator wouldn't be allowed even if parenthesized.
|
||||
DiagnoseStatementOperatorAsSubexpression(context);
|
||||
DiagnoseStatementOperatorAsSubexpr(context);
|
||||
}
|
||||
state.has_error = true;
|
||||
} else {
|
||||
@@ -232,9 +231,9 @@ auto HandleExpressionLoop(Context& context) -> void {
|
||||
state.subtree_start, state.has_error);
|
||||
}
|
||||
|
||||
state.state = State::ExpressionLoopForBinary;
|
||||
state.state = State::ExprLoopForBinary;
|
||||
context.PushState(state);
|
||||
context.PushStateForExpression(operator_precedence);
|
||||
context.PushStateForExpr(operator_precedence);
|
||||
} else {
|
||||
context.AddNode(NodeKind::PostfixOperator, state.token, state.subtree_start,
|
||||
state.has_error);
|
||||
@@ -243,37 +242,36 @@ auto HandleExpressionLoop(Context& context) -> void {
|
||||
}
|
||||
}
|
||||
|
||||
auto HandleExpressionLoopForBinary(Context& context) -> void {
|
||||
auto HandleExprLoopForBinary(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
context.AddNode(NodeKind::InfixOperator, state.token, state.subtree_start,
|
||||
state.has_error);
|
||||
state.state = State::ExpressionLoop;
|
||||
state.state = State::ExprLoop;
|
||||
state.has_error = false;
|
||||
context.PushState(state);
|
||||
}
|
||||
|
||||
auto HandleExpressionLoopForPrefix(Context& context) -> void {
|
||||
auto HandleExprLoopForPrefix(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
context.AddNode(NodeKind::PrefixOperator, state.token, state.subtree_start,
|
||||
state.has_error);
|
||||
state.state = State::ExpressionLoop;
|
||||
state.state = State::ExprLoop;
|
||||
state.has_error = false;
|
||||
context.PushState(state);
|
||||
}
|
||||
|
||||
auto HandleIfExpressionFinishCondition(Context& context) -> void {
|
||||
auto HandleIfExprFinishCondition(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
context.AddNode(NodeKind::IfExpressionIf, state.token, state.subtree_start,
|
||||
context.AddNode(NodeKind::IfExprIf, state.token, state.subtree_start,
|
||||
state.has_error);
|
||||
|
||||
if (context.PositionIs(Lex::TokenKind::Then)) {
|
||||
context.PushState(State::IfExpressionFinishThen);
|
||||
context.PushState(State::IfExprFinishThen);
|
||||
context.ConsumeChecked(Lex::TokenKind::Then);
|
||||
context.PushStateForExpression(
|
||||
*PrecedenceGroup::ForLeading(Lex::TokenKind::If));
|
||||
context.PushStateForExpr(*PrecedenceGroup::ForLeading(Lex::TokenKind::If));
|
||||
} else {
|
||||
// TODO: Include the location of the `if` token.
|
||||
CARBON_DIAGNOSTIC(ExpectedThenAfterIf, Error,
|
||||
@@ -281,7 +279,7 @@ auto HandleIfExpressionFinishCondition(Context& context) -> void {
|
||||
if (!state.has_error) {
|
||||
context.emitter().Emit(*context.position(), ExpectedThenAfterIf);
|
||||
}
|
||||
// Add placeholders for `IfExpressionThen` and final `Expression`.
|
||||
// Add placeholders for `IfExprThen` and final `Expr`.
|
||||
context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
|
||||
/*has_error=*/true);
|
||||
context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
|
||||
@@ -290,17 +288,16 @@ auto HandleIfExpressionFinishCondition(Context& context) -> void {
|
||||
}
|
||||
}
|
||||
|
||||
auto HandleIfExpressionFinishThen(Context& context) -> void {
|
||||
auto HandleIfExprFinishThen(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
context.AddNode(NodeKind::IfExpressionThen, state.token, state.subtree_start,
|
||||
context.AddNode(NodeKind::IfExprThen, state.token, state.subtree_start,
|
||||
state.has_error);
|
||||
|
||||
if (context.PositionIs(Lex::TokenKind::Else)) {
|
||||
context.PushState(State::IfExpressionFinishElse);
|
||||
context.PushState(State::IfExprFinishElse);
|
||||
context.ConsumeChecked(Lex::TokenKind::Else);
|
||||
context.PushStateForExpression(
|
||||
*PrecedenceGroup::ForLeading(Lex::TokenKind::If));
|
||||
context.PushStateForExpr(*PrecedenceGroup::ForLeading(Lex::TokenKind::If));
|
||||
} else {
|
||||
// TODO: Include the location of the `if` token.
|
||||
CARBON_DIAGNOSTIC(ExpectedElseAfterIf, Error,
|
||||
@@ -308,14 +305,14 @@ auto HandleIfExpressionFinishThen(Context& context) -> void {
|
||||
if (!state.has_error) {
|
||||
context.emitter().Emit(*context.position(), ExpectedElseAfterIf);
|
||||
}
|
||||
// Add placeholder for the final `Expression`.
|
||||
// Add placeholder for the final `Expr`.
|
||||
context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
|
||||
/*has_error=*/true);
|
||||
context.ReturnErrorOnState();
|
||||
}
|
||||
}
|
||||
|
||||
auto HandleIfExpressionFinishElse(Context& context) -> void {
|
||||
auto HandleIfExprFinishElse(Context& context) -> void {
|
||||
auto else_state = context.PopState();
|
||||
|
||||
// Propagate the location of `else`.
|
||||
@@ -325,31 +322,30 @@ auto HandleIfExpressionFinishElse(Context& context) -> void {
|
||||
context.PushState(if_state);
|
||||
}
|
||||
|
||||
auto HandleIfExpressionFinish(Context& context) -> void {
|
||||
auto HandleIfExprFinish(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
context.AddNode(NodeKind::IfExpressionElse, state.token, state.subtree_start,
|
||||
context.AddNode(NodeKind::IfExprElse, state.token, state.subtree_start,
|
||||
state.has_error);
|
||||
}
|
||||
|
||||
auto HandleExpressionStatementFinish(Context& context) -> void {
|
||||
auto HandleExprStatementFinish(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
if (auto semi = context.ConsumeIf(Lex::TokenKind::Semi)) {
|
||||
context.AddNode(NodeKind::ExpressionStatement, *semi, state.subtree_start,
|
||||
context.AddNode(NodeKind::ExprStatement, *semi, state.subtree_start,
|
||||
state.has_error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!state.has_error) {
|
||||
CARBON_DIAGNOSTIC(ExpectedExpressionSemi, Error,
|
||||
CARBON_DIAGNOSTIC(ExpectedExprSemi, Error,
|
||||
"Expected `;` after expression statement.");
|
||||
context.emitter().Emit(*context.position(), ExpectedExpressionSemi);
|
||||
context.emitter().Emit(*context.position(), ExpectedExprSemi);
|
||||
}
|
||||
|
||||
if (auto semi_token = context.SkipPastLikelyEnd(state.token)) {
|
||||
context.AddNode(NodeKind::ExpressionStatement, *semi_token,
|
||||
state.subtree_start,
|
||||
context.AddNode(NodeKind::ExprStatement, *semi_token, state.subtree_start,
|
||||
/*has_error=*/true);
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user