mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-02 15:25:56 +01:00
[parse] Implement initial parsing support for Lambda expressions (#6583)
This adds the necessary parser infrastructure to recognize and parse lambda expressions in Carbon. Key changes: - Added and Parse Node Kinds. - Updated to use to accommodate the growing number of node kinds. - Implemented parser states and handlers for lambda syntax ( or ). - Added structure to . - Added diagnostics for missing lambda bodies. - Added a stub in phase to defer semantic analysis using . - Added parser tests for lambdas.
This commit is contained in:
@@ -51,6 +51,13 @@ auto HandleFunctionSignatureFinish(Context& context) -> void {
|
||||
context.PushState(StateKind::StatementScopeLoop);
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::EqualGreater: {
|
||||
context.AddFunctionDefinitionStart(context.Consume(), state.has_error);
|
||||
context.AddLeafNode(NodeKind::TerseBodyArrow, *(context.position() - 1));
|
||||
context.PushState(state, StateKind::FunctionTerseBodyFinish);
|
||||
context.PushStateForExpr(PrecedenceGroup::ForTopLevelExpr());
|
||||
break;
|
||||
}
|
||||
case Lex::TokenKind::Equal: {
|
||||
context.AddNode(NodeKind::BuiltinFunctionDefinitionStart,
|
||||
context.Consume(), state.has_error);
|
||||
@@ -95,4 +102,16 @@ auto HandleFunctionDefinitionFinish(Context& context) -> void {
|
||||
context.AddFunctionDefinition(context.Consume(), state.has_error);
|
||||
}
|
||||
|
||||
auto HandleFunctionTerseBodyFinish(Context& context) -> void {
|
||||
auto state = context.PopState();
|
||||
|
||||
auto semi = context.ConsumeIf(Lex::TokenKind::Semi);
|
||||
if (!semi && !state.has_error) {
|
||||
context.DiagnoseExpectedDeclSemi(Lex::TokenKind::Fn);
|
||||
state.has_error = true;
|
||||
}
|
||||
context.AddFunctionTerseDefinition(semi ? *semi : *(context.position() - 1),
|
||||
state.has_error);
|
||||
}
|
||||
|
||||
} // namespace Carbon::Parse
|
||||
|
||||
Reference in New Issue
Block a user