From 9f1c5fe4c5ea275f85d8920351591378dd87ab71 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 5 Sep 2019 22:42:36 +0200 Subject: Implement rule shortcutting, and cleanup make_parser code --- dhall_syntax/src/parser.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index bd29a27..3f961e8 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -60,6 +60,16 @@ impl<'input> ParseInput<'input, Rule> { original_input_str: self.original_input_str.clone(), } } + /// If the contained pair has exactly one child, return a new Self containing it. + fn single_child(&self) -> Option { + let mut children = self.pair.clone().into_inner(); + if let Some(child) = children.next() { + if children.next().is_none() { + return Some(self.with_pair(child)); + } + } + None + } fn as_span(&self) -> Span { Span::make(self.original_input_str.clone(), self.pair.as_span()) } @@ -75,6 +85,7 @@ impl<'input> ParseInput<'input, Rule> { trait PestConsumer { type Rule: pest::RuleType; fn rule_alias(rule: Self::Rule) -> String; + fn allows_shortcut(rule: Self::Rule) -> bool; } fn debug_pair(pair: Pair) -> String { @@ -482,7 +493,7 @@ impl Parsers { .map_err(|e| input.error(format!("{}", e))) } - #[alias(expression)] + #[alias(expression, shortcut = true)] fn identifier(input: ParseInput) -> ParseResult> { Ok(parse_children!(input; [variable(v)] => { @@ -809,7 +820,7 @@ impl Parsers { )) } - #[alias(expression)] + #[alias(expression, shortcut = true)] #[prec_climb(expression, PRECCLIMBER)] fn operator_expression( input: ParseInput, @@ -843,7 +854,7 @@ impl Parsers { Ok(()) } - #[alias(expression)] + #[alias(expression, shortcut = true)] fn application_expression( input: ParseInput, ) -> ParseResult> { @@ -855,7 +866,7 @@ impl Parsers { )) } - #[alias(expression)] + #[alias(expression, shortcut = true)] fn first_application_expression( input: ParseInput, ) -> ParseResult> { @@ -874,7 +885,7 @@ impl Parsers { )) } - #[alias(expression)] + #[alias(expression, shortcut = true)] fn selector_expression( input: ParseInput, ) -> ParseResult> { @@ -905,7 +916,7 @@ impl Parsers { )) } - #[alias(expression)] + #[alias(expression, shortcut = true)] fn primitive_expression( input: ParseInput, ) -> ParseResult> { -- cgit v1.2.3