mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-23 15:42:36 +00:00
This works on multiple statements to make them better for the bracketing model. Stub nodes are added in more cases of invalid syntax, simply so that the semantics has reliably structured input. Comments in parse_node_kind.def now try to show the expected parse tree structure in postorder form. This labels If, While, and For a little differently in parse nodes so that at the start of the postorder traversal, it'll already be available to semantics which structure is being processed. I need to do a little more with If in particular, but this felt like a reasonable stopping point. While this makes significant parser changes, the changes to parser_state.def are minimal, mostly naming-related. The actual flow isn't substantively changed, just a couple minor names and the new As(If|While) state which allows distinguishing IfCondition and WhileCondition. Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
213 lines
7.6 KiB
Modula-2
213 lines
7.6 KiB
Modula-2
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
// Note that this is an X-macro header.
|
|
//
|
|
// It does not use `#include` guards, and instead is designed to be `#include`ed
|
|
// after the x-macro is defined in order for its inclusion to expand to the
|
|
// desired output.
|
|
//
|
|
// x-macros come in three forms:
|
|
// CARBON_PARSE_NODE_KIND(Name)
|
|
// Used as a fallback if other macros are missing.
|
|
// CARBON_PARSE_NODE_KIND_BRACKET(Name, BracketName)
|
|
// Defines a bracketed node kind. BracketName should refer to the node kind
|
|
// that is the _start_ of the bracketed range.
|
|
// CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ChildCount)
|
|
// Defines a parse node with a set number of children, often 0. This count
|
|
// must be correct even when the node contains errors.
|
|
//
|
|
// Macro definitions will be removed at the end of this file to clean up.
|
|
//
|
|
// TODO: Using CHILD_COUNT(..., TodoFixParseNode) to indicate nodes which need to be fixed,
|
|
// sometimes due to inconsistent output in error modes. This is also handled in
|
|
// ParseNodeKind::child_count() and ParseTree::Verify(). All of that can be
|
|
// cleaned up once remainining nodes are fixed.
|
|
|
|
#if !(defined(CARBON_PARSE_NODE_KIND) || \
|
|
(defined(CARBON_PARSE_NODE_KIND_BRACKET) && \
|
|
defined(CARBON_PARSE_NODE_KIND_CHILD_COUNT)))
|
|
#error "Must define CARBON_PARSE_NODE_KIND family x-macros to use this file."
|
|
#endif
|
|
|
|
// The BRACKET and CHILD_COUNT macros will use CARBON_PARSE_NODE_KIND by default
|
|
// when undefined.
|
|
#ifndef CARBON_PARSE_NODE_KIND_BRACKET
|
|
#define CARBON_PARSE_NODE_KIND_BRACKET(Name, ...) CARBON_PARSE_NODE_KIND(Name)
|
|
#endif
|
|
#ifndef CARBON_PARSE_NODE_KIND_CHILD_COUNT
|
|
#define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ...) \
|
|
CARBON_PARSE_NODE_KIND(Name)
|
|
#endif
|
|
|
|
// The end of the file.
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(FileEnd, 0)
|
|
|
|
// An empty declaration, such as `;`.
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(EmptyDeclaration, 0)
|
|
|
|
// A name.
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(DeclaredName, 0)
|
|
|
|
// `package`:
|
|
// PackageIntroducer
|
|
// (name)
|
|
// (library)
|
|
// PackageLibrary
|
|
// PackageApi or PackageImpl
|
|
// PackageDirective
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageIntroducer, 0)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageApi, 0)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageImpl, 0)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(PackageLibrary, 1)
|
|
CARBON_PARSE_NODE_KIND_BRACKET(PackageDirective, PackageIntroducer)
|
|
|
|
// A code block:
|
|
// CodeBlockStart
|
|
// (statements)
|
|
// CodeBlock
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(CodeBlockStart, 0)
|
|
CARBON_PARSE_NODE_KIND_BRACKET(CodeBlock, CodeBlockStart)
|
|
|
|
|
|
// `fn`:
|
|
// FunctionIntroducer
|
|
// DeclaredName
|
|
// (parameter list)
|
|
// (expression)
|
|
// ReturnType
|
|
// FunctionDefinitionStart
|
|
// (statements)
|
|
// FunctionDefinition
|
|
//
|
|
// Or, for a declaration, FunctionDeclaration will end it where
|
|
// FunctionDefinitionStart is for a definition.
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(FunctionIntroducer, 0)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnType, 1)
|
|
CARBON_PARSE_NODE_KIND_BRACKET(FunctionDefinitionStart, FunctionIntroducer)
|
|
CARBON_PARSE_NODE_KIND_BRACKET(FunctionDefinition, FunctionDefinitionStart)
|
|
CARBON_PARSE_NODE_KIND_BRACKET(FunctionDeclaration, FunctionIntroducer)
|
|
|
|
// A parameter list:
|
|
// ParamertListStart
|
|
// (expressions separated by ParameterListComma)
|
|
// ParameterList
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParameterListStart, 0)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParameterListComma, 0)
|
|
CARBON_PARSE_NODE_KIND_BRACKET(ParameterList, ParameterListStart)
|
|
|
|
// A pattern binding, such as `name: Type`:
|
|
// DeclaredName
|
|
// (type expression)
|
|
// PatternBinding
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(PatternBinding, 2)
|
|
|
|
// `var`:
|
|
// VariableIntroducer
|
|
// (pattern)
|
|
// (expression)
|
|
// optional VariableInitializer
|
|
// VariableDeclaration
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableIntroducer, 0)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(VariableInitializer, 1)
|
|
CARBON_PARSE_NODE_KIND_BRACKET(VariableDeclaration, VariableIntroducer)
|
|
|
|
// An expression statement:
|
|
// (expression)
|
|
// Statement
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(ExpressionStatement, 1)
|
|
|
|
// `break`:
|
|
// BreakStatementStart
|
|
// BreakStatement
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatement, 1)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(BreakStatementStart, 0)
|
|
|
|
// `continue`:
|
|
// ContinueStatementStart
|
|
// ContinueStatement
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatement, 1)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(ContinueStatementStart, 0)
|
|
|
|
// `return`:
|
|
// ReturnStatementStart
|
|
// (optional expression)
|
|
// ReturnStatement
|
|
CARBON_PARSE_NODE_KIND_BRACKET(ReturnStatement, ReturnStatementStart)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(ReturnStatementStart, 0)
|
|
|
|
// `for`:
|
|
// ForHeaderStart
|
|
// (var)
|
|
// ForIn (versus a normal `var`, replaces VariableDeclaration)
|
|
// (expression)
|
|
// ForHeader
|
|
// CodeBlockStart
|
|
// (statements)
|
|
// CodeBlock
|
|
// ForStatement
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForHeaderStart, 0)
|
|
CARBON_PARSE_NODE_KIND_BRACKET(ForIn, VariableIntroducer)
|
|
CARBON_PARSE_NODE_KIND_BRACKET(ForHeader, ForHeaderStart)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(ForStatement, 2)
|
|
|
|
// `if`:
|
|
// TODO: Still thinking about structure.
|
|
// IfConditionStart
|
|
// (expression)
|
|
// IfCondition
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfConditionStart, 0)
|
|
CARBON_PARSE_NODE_KIND_BRACKET(IfCondition, IfConditionStart)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfStatement, TodoFixParseNode)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(IfStatementElse, 0)
|
|
|
|
// `while`:
|
|
// WhileConditionStart
|
|
// (expression)
|
|
// WhileCondition
|
|
// CodeBlockStart
|
|
// (statements)
|
|
// CodeBlock
|
|
// WhileStatement
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileConditionStart, 0)
|
|
CARBON_PARSE_NODE_KIND_BRACKET(WhileCondition, WhileConditionStart)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(WhileStatement, 2)
|
|
|
|
// Expressions.
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(Literal, 0)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(TupleLiteral, TodoFixParseNode)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(TupleLiteralComma, 0)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(TupleLiteralEnd, 0)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(NameReference, 0)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParenExpression, TodoFixParseNode)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(ParenExpressionEnd, 0)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(DesignatorExpression, TodoFixParseNode)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(DesignatedName, 0)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExpression, TodoFixParseNode)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExpressionComma, 0)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(CallExpressionEnd, 0)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(PrefixOperator, 1)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(InfixOperator, 2)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(PostfixOperator, 1)
|
|
|
|
// Struct literals.
|
|
CARBON_PARSE_NODE_KIND_BRACKET(StructLiteral,
|
|
StructLiteralOrStructTypeLiteralStart)
|
|
CARBON_PARSE_NODE_KIND_BRACKET(StructTypeLiteral,
|
|
StructLiteralOrStructTypeLiteralStart)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructLiteralOrStructTypeLiteralStart, 0)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldDesignator, TodoFixParseNode)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldValue, TodoFixParseNode)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructFieldType, TodoFixParseNode)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(StructComma, 0)
|
|
|
|
// Interfaces
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(InterfaceDefinition, TodoFixParseNode)
|
|
CARBON_PARSE_NODE_KIND_CHILD_COUNT(InterfaceBodyStart, 0)
|
|
CARBON_PARSE_NODE_KIND_BRACKET(InterfaceBody, InterfaceBodyStart)
|
|
|
|
#undef CARBON_PARSE_NODE_KIND
|
|
#undef CARBON_PARSE_NODE_KIND_BRACKET
|
|
#undef CARBON_PARSE_NODE_KIND_CHILD_COUNT
|