From 108c35242884b9cb5e3988e406f7983404cdf315 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 27 Mar 2019 00:22:39 +0100 Subject: Manually rustfmt like a nazi --- dhall_core/src/parser.rs | 303 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 213 insertions(+), 90 deletions(-) (limited to 'dhall_core') diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs index fb119b1..99e1883 100644 --- a/dhall_core/src/parser.rs +++ b/dhall_core/src/parser.rs @@ -113,28 +113,83 @@ fn debug_pair(pair: Pair) -> String { } macro_rules! match_pair { - (@make_child_match, ($pair:expr, $($vars:tt)*), ($($outer_acc:tt)*), ($($acc:tt)*), ($(,)* $ty:ident ($x:ident..) $($rest_of_match:tt)*) => $body:expr, $($rest:tt)*) => { - match_pair!(@make_child_match, ($pair, $($vars)*), ($($outer_acc)*), ($($acc)*, xs..), ($($rest_of_match)*) => { - let xs = xs.map(|x| match x { - ParsedValue::$ty(y) => Ok(y), - x => Err(format!("Unexpected child: {:?}", x)), - }).collect::, _>>()?; - let $x = xs.into_iter(); - $body - }, $($rest)*) + (@make_child_match, + ($pair:expr, $($vars:tt)*), + ($($outer_acc:tt)*), + ($($acc:tt)*), + ($(,)* $ty:ident ($x:ident..) $($rest_of_match:tt)*) => $body:expr, + $($rest:tt)* + ) => { + match_pair!(@make_child_match, + ($pair, $($vars)*), + ($($outer_acc)*), + ($($acc)*, xs..), + ($($rest_of_match)*) => { + let xs = xs.map(|x| match x { + ParsedValue::$ty(y) => Ok(y), + x => Err(format!("Unexpected child: {:?}", x)), + }).collect::, _>>()?; + let $x = xs.into_iter(); + $body + }, + $($rest)* + ) }; - (@make_child_match, ($($vars:tt)*), ($($outer_acc:tt)*), ($($acc:tt)*), ($(,)* $ty:ident ($x:pat) $($rest_of_match:tt)*) => $body:expr, $($rest:tt)*) => { - match_pair!(@make_child_match, ($($vars)*), ($($outer_acc)*), ($($acc)*, ParsedValue::$ty($x)), ($($rest_of_match)*) => $body, $($rest)*) + (@make_child_match, + ($($vars:tt)*), + ($($outer_acc:tt)*), + ($($acc:tt)*), + ($(,)* $ty:ident ($x:pat) $($rest_of_match:tt)*) => $body:expr, + $($rest:tt)* + ) => { + match_pair!(@make_child_match, + ($($vars)*), + ($($outer_acc)*), + ($($acc)*, ParsedValue::$ty($x)), + ($($rest_of_match)*) => $body, + $($rest)* + ) }; - (@make_child_match, ($($vars:tt)*), ($($outer_acc:tt)*), (, $($acc:tt)*), ($(,)*) => $body:expr, $($rest:tt)*) => { - match_pair!(@make_matches, ($($vars)*), ($($outer_acc)* [$($acc)*] => { $body },), $($rest)*) + (@make_child_match, + ($($vars:tt)*), + ($($outer_acc:tt)*), + (, $($acc:tt)*), + ($(,)*) => $body:expr, + $($rest:tt)* + ) => { + match_pair!(@make_matches, + ($($vars)*), + ($($outer_acc)* [$($acc)*] => { $body },), + $($rest)* + ) }; - (@make_child_match, ($($vars:tt)*), ($($outer_acc:tt)*), (), ($(,)*) => $body:expr, $($rest:tt)*) => { - match_pair!(@make_matches, ($($vars)*), ($($outer_acc)* [] => { $body },), $($rest)*) + (@make_child_match, + ($($vars:tt)*), + ($($outer_acc:tt)*), + (), + ($(,)*) => $body:expr, + $($rest:tt)* + ) => { + match_pair!(@make_matches, + ($($vars)*), + ($($outer_acc)* [] => { $body },), + $($rest)* + ) }; - (@make_matches, ($($vars:tt)*), ($($acc:tt)*), [$($args:tt)*] => $body:expr, $($rest:tt)*) => { - match_pair!(@make_child_match, ($($vars)*), ($($acc)*), (), ($($args)*) => $body, $($rest)*) + (@make_matches, + ($($vars:tt)*), + ($($acc:tt)*), + [$($args:tt)*] => $body:expr, + $($rest:tt)* + ) => { + match_pair!(@make_child_match, + ($($vars)*), + ($($acc)*), + (), + ($($args)*) => $body, + $($rest)* + ) }; (@make_matches, ($pair:expr, $children:expr), ($($acc:tt)*) $(,)*) => { { @@ -149,7 +204,11 @@ macro_rules! match_pair { }; (($($vars:tt)*); $( [$($args:tt)*] => $body:expr ),* $(,)*) => { - match_pair!(@make_matches, ($($vars)*), (), $( [$($args)*] => $body ),* ,) + match_pair!(@make_matches, + ($($vars)*), + (), + $( [$($args)*] => $body ),* , + ) }; } @@ -159,15 +218,39 @@ macro_rules! make_parser { (@filter, rule) => (true); (@filter, rule_group) => (false); - (@body, $pair:expr, $children:expr, rule!( $name:ident<$o:ty>; $($args:tt)* )) => ( - make_parser!(@body, $pair, $children, rule!( $name<$o> as $name; $($args)* )) + (@body, + $pair:expr, + $children:expr, + rule!( $name:ident<$o:ty>; $($args:tt)* ) + ) => ( + make_parser!(@body, + $pair, + $children, + rule!( $name<$o> as $name; $($args)* ) + ) ); - (@body, $pair:expr, $children:expr, rule!( $name:ident<$o:ty> as $group:ident; captured_str!($x:pat) => $body:expr )) => ( { + (@body, + $pair:expr, + $children:expr, + rule!( + $name:ident<$o:ty> + as $group:ident; + captured_str!($x:pat) => $body:expr + ) + ) => ({ let $x = $pair.as_str(); let res: $o = $body; Ok(ParsedValue::$group(res)) }); - (@body, $pair:expr, $children:expr, rule!( $name:ident<$o:ty> as $group:ident; children!( $($args:tt)* ) )) => ( { + (@body, + $pair:expr, + $children:expr, + rule!( + $name:ident<$o:ty> + as $group:ident; + children!( $($args:tt)* ) + ) + ) => ({ let res: $o = match_pair!(($pair, $children); $($args)*)?; Ok(ParsedValue::$group(res)) }); @@ -182,57 +265,60 @@ macro_rules! make_parser { $( $name($o), )* } - fn parse_any<'a>(pair: Pair<'a, Rule>, children: Vec>) -> Result, String> { + fn parse_any<'a>(pair: Pair<'a, Rule>, children: Vec>) + -> Result, String> { match pair.as_rule() { $( make_parser!(@pattern, $submac, $name) if make_parser!(@filter, $submac) - => make_parser!(@body, pair, children, $submac!( $name<$o> $($args)* )) + => make_parser!(@body, pair, children, + $submac!( $name<$o> $($args)* )) , )* r => Err(format!("Unexpected {:?}", r)), } } + ); +} - // Non-recursive implementation to avoid stack overflows - fn do_parse<'a>(initial_pair: Pair<'a, Rule>) -> ParseResult> { - enum StackFrame<'a> { - Unprocessed(Pair<'a, Rule>), - Processed(Pair<'a, Rule>, usize), - } - use StackFrame::*; - let mut pairs_stack: Vec = vec![Unprocessed(initial_pair.clone())]; - let mut values_stack: Vec = vec![]; - while let Some(p) = pairs_stack.pop() { - match p { - Unprocessed(mut pair) => { - loop { - let mut pairs: Vec<_> = pair.clone().into_inner().collect(); - let n_children = pairs.len(); - if n_children == 1 && can_be_shortcutted(pair.as_rule()) { - pair = pairs.pop().unwrap(); - continue - } else { - pairs_stack.push(Processed(pair, n_children)); - pairs_stack.extend(pairs.into_iter().map(StackFrame::Unprocessed)); - break - } - } - } - Processed(pair, n) => { - let mut children: Vec<_> = values_stack.split_off(values_stack.len() - n); - children.reverse(); - let val = match parse_any(pair.clone(), children) { - Ok(v) => v, - Err(msg) => Err(custom_parse_error(&pair, msg))?, - }; - values_stack.push(val); - } +// Non-recursive implementation to avoid stack overflows +fn do_parse<'a>(initial_pair: Pair<'a, Rule>) -> ParseResult> { + enum StackFrame<'a> { + Unprocessed(Pair<'a, Rule>), + Processed(Pair<'a, Rule>, usize), + } + use StackFrame::*; + let mut pairs_stack: Vec = + vec![Unprocessed(initial_pair.clone())]; + let mut values_stack: Vec = vec![]; + while let Some(p) = pairs_stack.pop() { + match p { + Unprocessed(mut pair) => loop { + let mut pairs: Vec<_> = pair.clone().into_inner().collect(); + let n_children = pairs.len(); + if n_children == 1 && can_be_shortcutted(pair.as_rule()) { + pair = pairs.pop().unwrap(); + continue; + } else { + pairs_stack.push(Processed(pair, n_children)); + pairs_stack + .extend(pairs.into_iter().map(StackFrame::Unprocessed)); + break; } + }, + Processed(pair, n) => { + let mut children: Vec<_> = + values_stack.split_off(values_stack.len() - n); + children.reverse(); + let val = match parse_any(pair.clone(), children) { + Ok(v) => v, + Err(msg) => Err(custom_parse_error(&pair, msg))?, + }; + values_stack.push(val); } - Ok(values_stack.pop().unwrap()) } - ); + } + Ok(values_stack.pop().unwrap()) } // List of rules that can be shortcutted if they have a single child @@ -261,8 +347,12 @@ fn can_be_shortcutted(rule: Rule) -> bool { make_parser! { rule!(EOI<()>; captured_str!(_) => ()); - rule!(simple_label