From f71a73d996fa16a3b1c58f9c0bd87824ac11f14c Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 20 Mar 2019 21:15:04 +0100 Subject: Remove binop special case in parser; cleanup --- dhall_core/src/parser.rs | 116 +++++++++++++++++++++++++++-------------------- 1 file changed, 66 insertions(+), 50 deletions(-) (limited to 'dhall_core/src') diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs index cbba8ca..52e8c64 100644 --- a/dhall_core/src/parser.rs +++ b/dhall_core/src/parser.rs @@ -91,18 +91,6 @@ macro_rules! match_pair { (@make_matches, $pair:expr, ($($acc:tt)*), children!($($args:tt)*) => $body:expr, $($rest:tt)*) => { match_pair!(@make_child_match, $pair, ($($acc)*), (), ($($args)*) => $body, $($rest)*) }; - (@make_matches, $pair:expr, ($($acc:tt)*), raw_pair!($x:ident) => $body:expr, $($rest:tt)*) => { - match_pair!(@make_matches, $pair, ([..] => { - let $x = $pair.clone(); - $body - }, $($acc)*), $($rest)*) - }; - (@make_matches, $pair:expr, ($($acc:tt)*), captured_str!($x:ident) => $body:expr, $($rest:tt)*) => { - match_pair!(@make_matches, $pair, ([..] => { - let $x = $pair.as_str(); - $body - }, $($acc)*), $($rest)*) - }; (@make_matches, $pair:expr, ($($acc:tt)*) $(,)*) => { { let pair = $pair.clone(); @@ -122,23 +110,28 @@ macro_rules! match_pair { } macro_rules! make_parser { - // Filter out definitions that should not be matched on + // Filter out definitions that should not be matched on (i.e. rule_group) (@filter, rule) => (true); (@filter, rule_in_group) => (true); - (@filter, binop) => (true); (@filter, rule_group) => (false); - (@body, $pair:expr, rule!( $name:ident<$o:ty>; $($args:tt)* )) => ( { - let res: $o = match_pair!($pair; $($args)*)?; - Ok(ParsedValue::$name(res)) + (@body, $pair:expr, rule!( $name:ident<$o:ty>; $($args:tt)* )) => ( + make_parser!(@body, $pair, rule_in_group!( $name<$o>; $name; $($args)* )) + ); + (@body, $pair:expr, rule_in_group!( $name:ident<$o:ty>; $group:ident; raw_pair!($x:pat) => $body:expr )) => ( { + let $x = $pair.clone(); + let res: $o = $body; + Ok(ParsedValue::$group(res)) + }); + (@body, $pair:expr, rule_in_group!( $name:ident<$o:ty>; $group:ident; captured_str!($x:ident) => $body:expr )) => ( { + let $x = $pair.as_str(); + let res: $o = $body; + Ok(ParsedValue::$group(res)) }); (@body, $pair:expr, rule_in_group!( $name:ident<$o:ty>; $group:ident; $($args:tt)* )) => ( { let res: $o = match_pair!($pair; $($args)*)?; Ok(ParsedValue::$group(res)) }); - (@body, $pair:expr, binop!( $name:ident<$o:ty>; $op:ident )) => ( { - parse_binop($pair, BinOp::$op) - }); (@body, $pair:expr, rule_group!( $name:ident<$o:ty> )) => ( unreachable!() ); @@ -224,7 +217,7 @@ fn can_be_shortcutted(rule: Rule) -> bool { } } -fn parse_binop(pair: Pair, o: BinOp) -> ParseResult { +fn parse_binop(pair: Pair, o: BinOp) -> ParseResult { // This all could be a trivial fold, but to avoid stack explosion // we try to cut down on the recursion level here, by consuming // chains of blah_expression > ... > blih_expression in one go. @@ -237,10 +230,9 @@ fn parse_binop(pair: Pair, o: BinOp) -> ParseResult { if !rest.is_empty() { // If there is more than one subexpression, handle it normally let first = parse_any(first)?.expression(); - Ok(ParsedValue::expression( - rest.into_iter() - .fold(first, |acc, e| bx(Expr::BinOp(o, acc, e))), - )) + Ok(rest + .into_iter() + .fold(first, |acc, e| bx(Expr::BinOp(o, acc, e)))) } else { // Otherwise, consume short-cuttable rules as long as they contain only one subexpression. pair = first; @@ -253,12 +245,12 @@ fn parse_binop(pair: Pair, o: BinOp) -> ParseResult { } pair = first; } - parse_any(pair) + Ok(parse_any(pair)?.expression()) } } make_parser! { -rule!(EOI<()>; children!() => ()); +rule!(EOI<()>; raw_pair!(_) => ()); rule!(label_raw