summaryrefslogtreecommitdiff
path: root/dhall_syntax
diff options
context:
space:
mode:
authorNadrieril2019-09-05 22:42:36 +0200
committerNadrieril2019-09-05 22:42:36 +0200
commit9f1c5fe4c5ea275f85d8920351591378dd87ab71 (patch)
tree8b7c85fba6099f44b40d1b86486fa986718d472d /dhall_syntax
parentd3fcc3a5ef93d4e7f539954f1c77af58685c65b9 (diff)
Implement rule shortcutting, and cleanup make_parser code
Diffstat (limited to '')
-rw-r--r--dhall_syntax/src/parser.rs23
1 files changed, 17 insertions, 6 deletions
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<Self> {
+ 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<Rule>) -> String {
@@ -482,7 +493,7 @@ impl Parsers {
.map_err(|e| input.error(format!("{}", e)))
}
- #[alias(expression)]
+ #[alias(expression, shortcut = true)]
fn identifier<E: Clone>(input: ParseInput<Rule>) -> ParseResult<Expr<E>> {
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<E: Clone>(
input: ParseInput<Rule>,
@@ -843,7 +854,7 @@ impl Parsers {
Ok(())
}
- #[alias(expression)]
+ #[alias(expression, shortcut = true)]
fn application_expression<E: Clone>(
input: ParseInput<Rule>,
) -> ParseResult<Expr<E>> {
@@ -855,7 +866,7 @@ impl Parsers {
))
}
- #[alias(expression)]
+ #[alias(expression, shortcut = true)]
fn first_application_expression<E: Clone>(
input: ParseInput<Rule>,
) -> ParseResult<Expr<E>> {
@@ -874,7 +885,7 @@ impl Parsers {
))
}
- #[alias(expression)]
+ #[alias(expression, shortcut = true)]
fn selector_expression<E: Clone>(
input: ParseInput<Rule>,
) -> ParseResult<Expr<E>> {
@@ -905,7 +916,7 @@ impl Parsers {
))
}
- #[alias(expression)]
+ #[alias(expression, shortcut = true)]
fn primitive_expression<E: Clone>(
input: ParseInput<Rule>,
) -> ParseResult<Expr<E>> {