Add macro for postfix operators. (#3504)

Per request on #3481, for consistency with prefix/infix.
This commit is contained in:
Jon Ross-Perkins
2023-12-13 23:54:27 +00:00
committed by GitHub
parent add31eb4e3
commit e343ea593c
3 changed files with 42 additions and 17 deletions
+3 -1
View File
@@ -333,7 +333,6 @@ class NodeStack {
case Parse::NodeKind::MemberAccessExpr:
case Parse::NodeKind::PackageExpr:
case Parse::NodeKind::ParenExpr:
case Parse::NodeKind::PostfixOperatorStar:
case Parse::NodeKind::ReturnType:
case Parse::NodeKind::SelfTypeNameExpr:
case Parse::NodeKind::SelfValueNameExpr:
@@ -387,6 +386,9 @@ class NodeStack {
#define CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Name, ...) \
case Parse::NodeKind::InfixOperator##Name: \
return IdKind::InstId;
#define CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR(Name, ...) \
case Parse::NodeKind::PostfixOperator##Name: \
return IdKind::InstId;
#define CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Name, ...) \
case Parse::NodeKind::PrefixOperator##Name: \
return IdKind::InstId;
+16 -2
View File
@@ -287,8 +287,22 @@ auto HandleExprLoop(Context& context) -> void {
context.PushState(state);
context.PushStateForExpr(operator_precedence);
} else {
context.AddNode(NodeKind::PostfixOperatorStar, state.token,
state.subtree_start, state.has_error);
NodeKind node_kind;
switch (operator_kind) {
#define CARBON_PARSE_NODE_KIND(...)
#define CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR(Name, ...) \
case Lex::TokenKind::Name: \
node_kind = NodeKind::PostfixOperator##Name; \
break;
#include "toolchain/parse/node_kind.def"
default:
CARBON_FATAL() << "Unexpected token kind for postfix operator: "
<< operator_kind;
}
context.AddNode(node_kind, state.token, state.subtree_start,
state.has_error);
state.has_error = false;
context.PushState(state);
}
+23 -14
View File
@@ -16,10 +16,12 @@
// - CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ChildCount, LexTokenKind)
// Defines a parse node with a set number of children, often 0. This count
// must be correct even when the node contains errors.
// - CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Name)
// Defines a parse node for an infix operator, with the Name as token.
// - CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Name)
// Defines a parse node for a prefix operator, with the Name as token.
// - CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Name)
// Defines a parse node for an infix operator, with the Name as token.
// - CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR(Name)
// Defines a parse node for a postfix operator, with the Name as token.
// - CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(Name, LexTokenKind)
// Defines a parse node that corresponds to a token that is a single-token
// literal. The token is wrapped for LexTokenKinds.
@@ -56,6 +58,16 @@
CARBON_PARSE_NODE_KIND(Name)
#endif
// This is expected to be used with something like:
//
// // Use x-macros to handle modifier cases.
// #define CARBON_PARSE_NODE_KIND(...)
// #define CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Name, ...) <code>
#ifndef CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR
#define CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Name) \
CARBON_PARSE_NODE_KIND_CHILD_COUNT(PrefixOperator##Name, 1, Name)
#endif
// This is expected to be used with something like:
//
// // Use x-macros to handle modifier cases.
@@ -70,10 +82,10 @@
//
// // Use x-macros to handle modifier cases.
// #define CARBON_PARSE_NODE_KIND(...)
// #define CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Name, ...) <code>
#ifndef CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR
#define CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR(Name) \
CARBON_PARSE_NODE_KIND_CHILD_COUNT(PrefixOperator##Name, 1, Name)
// #define CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR(Name, ...) <code>
#ifndef CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR
#define CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR(Name) \
CARBON_PARSE_NODE_KIND_CHILD_COUNT(PostfixOperator##Name, 1, Name)
#endif
// This is expected to be used with something like:
@@ -472,8 +484,6 @@ CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(FloatTypeLiteral, FloatTypeLiteral)
CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(StringTypeLiteral, StringTypeLiteral)
CARBON_PARSE_NODE_KIND_TOKEN_LITERAL(TypeTypeLiteral, Type)
// clang-format off
// A prefix operator, such as `not`:
// _external_: expression
// PrefixOperator<name>
@@ -520,7 +530,10 @@ CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(SlashEqual)
CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(Star)
CARBON_PARSE_NODE_KIND_INFIX_OPERATOR(StarEqual)
// clang-format on
// A postfix operator, currently only `*`:
// _external_: expression
// PostfixOperator<name>
CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR(Star)
// A short-circuiting infix operator, such as `and`:
// _external_: expression
@@ -532,11 +545,6 @@ CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperandOr, 1, Or)
CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperatorAnd, 2, And)
CARBON_PARSE_NODE_KIND_CHILD_COUNT(ShortCircuitOperatorOr, 2, Or)
// A postfix operator, currently only `*`:
// _external_: expression
// PostfixOperatorStar
CARBON_PARSE_NODE_KIND_CHILD_COUNT(PostfixOperatorStar, 1, Star)
// `if` expression + `then` + `else`:
// _external_: expression
// IfExprIf
@@ -696,6 +704,7 @@ CARBON_PARSE_NODE_KIND_BRACKET(NamedConstraintDecl, NamedConstraintIntroducer,
#undef CARBON_PARSE_NODE_KIND_BRACKET
#undef CARBON_PARSE_NODE_KIND_CHILD_COUNT
#undef CARBON_PARSE_NODE_KIND_INFIX_OPERATOR
#undef CARBON_PARSE_NODE_KIND_POSTFIX_OPERATOR
#undef CARBON_PARSE_NODE_KIND_PREFIX_OPERATOR
#undef CARBON_PARSE_NODE_KIND_TOKEN_LITERAL
#undef CARBON_PARSE_NODE_KIND_TOKEN_MODIFIER