From bb93a54010c3517f5b023cbb0ab5ea8e950e72e6 Mon Sep 17 00:00:00 2001 From: Igor Matuszewski Date: Wed, 6 Dec 2023 19:22:59 +0100 Subject: [PATCH] Add some comments to the precedence parser codegen --- .../src/precedence_parser_definition.rs | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/crates/codegen/parser/generator/src/precedence_parser_definition.rs b/crates/codegen/parser/generator/src/precedence_parser_definition.rs index 83372fc65c..a359c5e18e 100644 --- a/crates/codegen/parser/generator/src/precedence_parser_definition.rs +++ b/crates/codegen/parser/generator/src/precedence_parser_definition.rs @@ -148,6 +148,7 @@ impl PrecedenceParserDefinitionNodeExtensions for PrecedenceParserDefinitionNode let mut binary_operand_terms = vec![]; + // First, establish the binary operand parser `BinaryOperand ::= PrefixOperator* PrimaryExpression PostfixOperator*` if !prefix_operator_parsers.is_empty() { let prefix_operator_parser = make_choice(prefix_operator_parsers); operator_closures.push(quote! { let prefix_operator_parser = |input: &mut ParserContext| #prefix_operator_parser; }); @@ -168,25 +169,28 @@ impl PrecedenceParserDefinitionNodeExtensions for PrecedenceParserDefinitionNode let binary_operand_parser = make_sequence(binary_operand_terms); - if binary_operator_parsers.is_empty() { - operator_closures.push(quote! { let linear_expression_parser = |input: &mut ParserContext| #binary_operand_parser; }); + // Now, establish the linear expression parser `Expression ::= BinaryOperand ( BinaryOperator BinaryOperand )*` + let linear_expression_parser = if binary_operator_parsers.is_empty() { + // No binary operators, so the expression is simply `BinaryOperand` + binary_operand_parser } else { operator_closures.push(quote! { let binary_operand_parser = |input: &mut ParserContext| #binary_operand_parser; }); let binary_operator_parser = make_choice(binary_operator_parsers); operator_closures.push(quote! { let binary_operator_parser = |input: &mut ParserContext| #binary_operator_parser; }); - let linear_expression_parser = - make_sequence(vec![quote! { binary_operand_parser(input) }, { - let pairs = make_sequence(vec![ - quote! { binary_operator_parser(input) }, - quote! { binary_operand_parser(input) }, - ]); - quote! { ZeroOrMoreHelper::run(input, |input| #pairs) } - }]); - operator_closures + // `BinaryOperand ( BinaryOperator BinaryOperand )*` + make_sequence(vec![quote! { binary_operand_parser(input) }, { + let pairs = make_sequence(vec![ + quote! { binary_operator_parser(input) }, + quote! { binary_operand_parser(input) }, + ]); + quote! { ZeroOrMoreHelper::run(input, |input| #pairs) } + }]) + }; + + operator_closures .push(quote! { let linear_expression_parser = |input: &mut ParserContext| #linear_expression_parser; }); - } quote! { #(