Skip to content

Commit

Permalink
Add some comments to the precedence parser codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
Xanewok committed Dec 6, 2023
1 parent e87811b commit bb93a54
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions crates/codegen/parser/generator/src/precedence_parser_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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; });
Expand All @@ -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! {
#(
Expand Down

0 comments on commit bb93a54

Please sign in to comment.