From 8eb169713ea1220f2d515391c08d34509d75ab73 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 16 Aug 2019 10:34:10 +0200 Subject: Match on rules in parser instead of on ParsedValues --- dhall_syntax/src/lib.rs | 1 + dhall_syntax/src/parser.rs | 153 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 129 insertions(+), 25 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/src/lib.rs b/dhall_syntax/src/lib.rs index 4566e64..e4a6077 100644 --- a/dhall_syntax/src/lib.rs +++ b/dhall_syntax/src/lib.rs @@ -2,6 +2,7 @@ #![feature(slice_patterns)] #![feature(try_blocks)] #![feature(never_type)] +#![feature(bind_by_move_pattern_guards)] #![allow( clippy::many_single_char_names, clippy::should_implement_trait, diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index bcaa00e..366103e 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -127,17 +127,75 @@ macro_rules! make_parser { (@filter, token_rule) => (true); (@filter, rule_group) => (false); - (@construct_climber, - ($map:expr), + (@child_pattern, + $varpat:ident, + ($($acc:tt)*), + [$variant:ident ($x:pat), $($rest:tt)*] + ) => ( + make_parser!(@child_pattern, + $varpat, + ($($acc)* , Rule::$variant), + [$($rest)*] + ) + ); + (@child_pattern, + $varpat:ident, + ($($acc:tt)*), + [$variant:ident ($x:ident).., $($rest:tt)*] + ) => ( + make_parser!(@child_pattern, + $varpat, + ($($acc)* , $varpat..), + [$($rest)*] + ) + ); + (@child_pattern, + $varpat:ident, + (, $($acc:tt)*), [$(,)*] + ) => ([$($acc)*]); + (@child_pattern, + $varpat:ident, + ($($acc:tt)*), [$(,)*] + ) => ([$($acc)*]); + + (@child_filter, + $varpat:ident, + [$variant:ident ($x:pat), $($rest:tt)*] + ) => ( + true && + make_parser!(@child_filter, $varpat, [$($rest)*]) + ); + (@child_filter, + $varpat:ident, + [$variant:ident ($x:ident).., $($rest:tt)*] + ) => ( + $varpat.iter().all(|r| r == &Rule::$variant) && + make_parser!(@child_filter, $varpat, [$($rest)*]) + ); + (@child_filter, $varpat:ident, [$(,)*]) => (true); + + (@rule_alias, + rule!( $name:ident<$o:ty>; $($args:tt)* ) + ) => ( + Rule::$name + ); + (@rule_alias, rule!( $name:ident<$o:ty> as $group:ident; - prec_climb!( $climber:expr, $($_rest:tt)* ) + $($args:tt)* ) ) => ({ - $map.insert(Rule::$name, $climber) + Rule::$group }); - (@construct_climber, ($($things:tt)*), $($args:tt)*) => (()); + (@rule_alias, + token_rule!($name:ident<$o:ty>) + ) => ({ + Rule::$name + }); + (@rule_alias, rule_group!( $name:ident<$o:ty> )) => ( + unreachable!() + ); (@body, ($climbers:expr, $input:expr, $pair:expr), @@ -169,19 +227,43 @@ macro_rules! make_parser { .into_inner() .map(|p| parse_any($climbers, $input.clone(), p)) .collect::>()?; + let children_rules: Vec = $pair + .clone() + .into_inner() + .map(|p| p.as_rule()) + .map(rule_alias) + .collect(); #[allow(unreachable_code)] let res: Result<_, String> = try { #[allow(unused_imports)] use ParsedValue::*; let $span = Span::make($input, $pair.as_span()); + #[allow(unused_mut)] + let mut iter = children.into_iter(); - improved_slice_patterns::match_vec!(children; - $( [$($args)*] => $body, )* - [x..] => Err( - format!("Unexpected children: {:?}", x.collect::>()) - )?, - ).map_err(|_| -> String { unreachable!() })? + match children_rules.as_slice() { + $( + make_parser!(@child_pattern, x, (), [$($args)*,]) + if make_parser!(@child_filter, x, [$($args)*,]) + => { + let ret = + improved_slice_patterns::destructure_iter!(iter; + [$($args)*] => $body + ); + match ret { + Some(x) => x, + None => Err({ + format!("Unexpected children: {:?}", children_rules) + })? + } + } + , + )* + [..] => Err({ + format!("Unexpected children: {:?}", children_rules) + })?, + } }; let res = res.map_err(|msg| custom_parse_error(&$pair, msg))?; @@ -252,6 +334,18 @@ macro_rules! make_parser { unreachable!() ); + (@construct_climber, + ($map:expr), + rule!( + $name:ident<$o:ty> + as $group:ident; + prec_climb!( $climber:expr, $($_rest:tt)* ) + ) + ) => ({ + $map.insert(Rule::$name, $climber) + }); + (@construct_climber, ($($things:tt)*), $($args:tt)*) => (()); + ($( $submac:ident!( $name:ident<$o:ty> $($args:tt)* ); )*) => ( #[allow(non_camel_case_types, dead_code, clippy::large_enum_variant)] #[derive(Debug)] @@ -284,6 +378,19 @@ macro_rules! make_parser { )* } + fn rule_alias(r: Rule) -> Rule { + match r { + $( + make_parser!(@pattern, $submac, $name) + if make_parser!(@filter, $submac) + => make_parser!(@rule_alias, + $submac!( $name<$o> $($args)* )) + , + )* + r => r, + } + } + fn parse_any<'a>( climbers: &HashMap>, input: Rc, @@ -520,21 +627,15 @@ make_parser! { lines.last_mut().unwrap().push(c); lines }, - [single_quote_char("\n"), single_quote_continue(lines)] => { - let mut lines = lines; - lines.push(vec![]); - lines - }, - [single_quote_char("\r\n"), single_quote_continue(lines)] => { - let mut lines = lines; - lines.push(vec![]); - lines - }, [single_quote_char(c), single_quote_continue(lines)] => { - // TODO: don't allocate for every char - let c = InterpolatedTextContents::Text(c.to_owned()); let mut lines = lines; - lines.last_mut().unwrap().push(c); + if c == "\n" || c == "\r\n" { + lines.push(vec![]); + } else { + // TODO: don't allocate for every char + let c = InterpolatedTextContents::Text(c.to_owned()); + lines.last_mut().unwrap().push(c); + } lines }, [] => { @@ -631,7 +732,9 @@ make_parser! { } )); - rule_group!(local<(FilePrefix, Vec)>); + rule!(local<(FilePrefix, Vec)>; children!( + [local(l)] => l + )); rule!(parent_path<(FilePrefix, Vec)> as local; children!( [path(p)] => (FilePrefix::Parent, p) -- cgit v1.2.3 From 0fa94400492c08c3a38ae6798a0caeab23da5287 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 16 Aug 2019 10:39:15 +0200 Subject: Remove rule_group macro --- dhall_syntax/src/parser.rs | 26 ++++---------------------- 1 file changed, 4 insertions(+), 22 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index 366103e..b443656 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -120,13 +120,6 @@ fn debug_pair(pair: Pair) -> String { } macro_rules! make_parser { - (@pattern, rule, $name:ident) => (Rule::$name); - (@pattern, token_rule, $name:ident) => (Rule::$name); - (@pattern, rule_group, $name:ident) => (_); - (@filter, rule) => (true); - (@filter, token_rule) => (true); - (@filter, rule_group) => (false); - (@child_pattern, $varpat:ident, ($($acc:tt)*), @@ -193,9 +186,6 @@ macro_rules! make_parser { ) => ({ Rule::$name }); - (@rule_alias, rule_group!( $name:ident<$o:ty> )) => ( - unreachable!() - ); (@body, ($climbers:expr, $input:expr, $pair:expr), @@ -330,9 +320,6 @@ macro_rules! make_parser { ) => ({ Ok(ParsedValue::$name(())) }); - (@body, ($($things:tt)*), rule_group!( $name:ident<$o:ty> )) => ( - unreachable!() - ); (@construct_climber, ($map:expr), @@ -381,11 +368,9 @@ macro_rules! make_parser { fn rule_alias(r: Rule) -> Rule { match r { $( - make_parser!(@pattern, $submac, $name) - if make_parser!(@filter, $submac) - => make_parser!(@rule_alias, - $submac!( $name<$o> $($args)* )) - , + Rule::$name => { + make_parser!(@rule_alias, $submac!($name<$o> $($args)*)) + } )* r => r, } @@ -417,10 +402,7 @@ macro_rules! make_parser { match pair.as_rule() { $( - make_parser!(@pattern, $submac, $name) - if make_parser!(@filter, $submac) - => Parsers::$name(climbers, input, pair) - , + Rule::$name => Parsers::$name(climbers, input, pair), )* r => Err(custom_parse_error(&pair, format!("Unexpected {:?}", r))), } -- cgit v1.2.3 From f3a66c3603b01cb61334caee4a4749cac166b675 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 16 Aug 2019 11:05:44 +0200 Subject: Wording --- dhall_syntax/src/parser.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index b443656..d11eb18 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -120,52 +120,52 @@ fn debug_pair(pair: Pair) -> String { } macro_rules! make_parser { - (@child_pattern, + (@children_pattern, $varpat:ident, ($($acc:tt)*), [$variant:ident ($x:pat), $($rest:tt)*] ) => ( - make_parser!(@child_pattern, + make_parser!(@children_pattern, $varpat, ($($acc)* , Rule::$variant), [$($rest)*] ) ); - (@child_pattern, + (@children_pattern, $varpat:ident, ($($acc:tt)*), [$variant:ident ($x:ident).., $($rest:tt)*] ) => ( - make_parser!(@child_pattern, + make_parser!(@children_pattern, $varpat, ($($acc)* , $varpat..), [$($rest)*] ) ); - (@child_pattern, + (@children_pattern, $varpat:ident, (, $($acc:tt)*), [$(,)*] ) => ([$($acc)*]); - (@child_pattern, + (@children_pattern, $varpat:ident, ($($acc:tt)*), [$(,)*] ) => ([$($acc)*]); - (@child_filter, + (@children_filter, $varpat:ident, [$variant:ident ($x:pat), $($rest:tt)*] ) => ( true && - make_parser!(@child_filter, $varpat, [$($rest)*]) + make_parser!(@children_filter, $varpat, [$($rest)*]) ); - (@child_filter, + (@children_filter, $varpat:ident, [$variant:ident ($x:ident).., $($rest:tt)*] ) => ( $varpat.iter().all(|r| r == &Rule::$variant) && - make_parser!(@child_filter, $varpat, [$($rest)*]) + make_parser!(@children_filter, $varpat, [$($rest)*]) ); - (@child_filter, $varpat:ident, [$(,)*]) => (true); + (@children_filter, $varpat:ident, [$(,)*]) => (true); (@rule_alias, rule!( $name:ident<$o:ty>; $($args:tt)* ) @@ -234,8 +234,8 @@ macro_rules! make_parser { match children_rules.as_slice() { $( - make_parser!(@child_pattern, x, (), [$($args)*,]) - if make_parser!(@child_filter, x, [$($args)*,]) + make_parser!(@children_pattern, x, (), [$($args)*,]) + if make_parser!(@children_filter, x, [$($args)*,]) => { let ret = improved_slice_patterns::destructure_iter!(iter; -- cgit v1.2.3 From c13a100c36c3b54d76dba2207aa75fa6736ebadc Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 26 Aug 2019 23:18:11 +0200 Subject: Remove rule aliasing --- dhall_syntax/src/parser.rs | 200 +++++++++++++++++++++------------------------ 1 file changed, 93 insertions(+), 107 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index d11eb18..2debc8b 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -174,12 +174,11 @@ macro_rules! make_parser { ); (@rule_alias, rule!( - $name:ident<$o:ty> - as $group:ident; + $name:ident<$o:ty>; $($args:tt)* ) ) => ({ - Rule::$group + Rule::$name }); (@rule_alias, token_rule!($name:ident<$o:ty>) @@ -190,8 +189,7 @@ macro_rules! make_parser { (@body, ($climbers:expr, $input:expr, $pair:expr), rule!( - $name:ident<$o:ty> - as $group:ident; + $name:ident<$o:ty>; $span:ident; captured_str!($x:pat) => $body:expr ) @@ -199,15 +197,14 @@ macro_rules! make_parser { let res: Result<_, String> = try { let $span = Span::make($input, $pair.as_span()); let $x = $pair.as_str(); - ParsedValue::$group($body) + ParsedValue::$name($body) }; res.map_err(|msg| custom_parse_error(&$pair, msg)) }); (@body, ($climbers:expr, $input:expr, $pair:expr), rule!( - $name:ident<$o:ty> - as $group:ident; + $name:ident<$o:ty>; $span:ident; children!( $( [$($args:tt)*] => $body:expr ),* $(,)* ) ) @@ -257,27 +254,33 @@ macro_rules! make_parser { }; let res = res.map_err(|msg| custom_parse_error(&$pair, msg))?; - Ok(ParsedValue::$group(res)) + Ok(ParsedValue::$name(res)) }); (@body, ($climbers:expr, $input:expr, $pair:expr), rule!( - $name:ident<$o:ty> - as $group:ident; - prec_climb!( $_climber:expr, $args:pat => $body:expr $(,)* ) + $name:ident<$o:ty>; + prec_climb!($other_name:ident, $_climber:expr, $args:pat => $body:expr $(,)* ) ) ) => ({ - let climber = $climbers.get(&Rule::$name).unwrap(); + let climber = $climbers.get(&Rule::$name).expect(&format!("UUUU: {:?}", $climbers)); climber.climb( $pair.clone().into_inner(), - |p| parse_any($climbers, $input.clone(), p), + |p| { + let parsed = parse_any($climbers, $input.clone(), p)?; + if let ParsedValue::$other_name(x) = parsed { + Ok(ParsedValue::$name(x)) + } else { + unreachable!() + } + }, |l, op, r| { let (l, r) = (l?, r?); let res: Result<_, String> = try { #[allow(unused_imports)] use ParsedValue::*; match (l, op, r) { - $args => ParsedValue::$group($body), + $args => ParsedValue::$name($body), children => Err( format!("Unexpected children: {:?}", children) )?, @@ -289,26 +292,13 @@ macro_rules! make_parser { }); (@body, ($($things:tt)*), - rule!( $name:ident<$o:ty>; $($args:tt)* ) - ) => ( - make_parser!(@body, - ($($things)*), - rule!( $name<$o> as $name; $($args)* ) - ) - ); - (@body, - ($($things:tt)*), - rule!( - $name:ident<$o:ty> - as $group:ident; - $($args:tt)* + rule!( $name:ident<$o:ty>; $($args:tt)* ) ) => ({ make_parser!(@body, ($($things)*), rule!( - $name<$o> - as $group; + $name<$o>; _span; $($args)* ) @@ -324,9 +314,8 @@ macro_rules! make_parser { (@construct_climber, ($map:expr), rule!( - $name:ident<$o:ty> - as $group:ident; - prec_climb!( $climber:expr, $($_rest:tt)* ) + $name:ident<$o:ty>; + prec_climb!( $other_name:ident, $climber:expr, $($_rest:tt)* ) ) ) => ({ $map.insert(Rule::$name, $climber) @@ -379,27 +368,8 @@ macro_rules! make_parser { fn parse_any<'a>( climbers: &HashMap>, input: Rc, - mut pair: Pair<'a, Rule>, + pair: Pair<'a, Rule>, ) -> ParseResult> { - // Avoid parsing while the pair has exactly one child that can be returned as-is - loop { - if can_be_shortcutted(pair.as_rule()) { - let mut i = pair.clone().into_inner(); - let first = i.next(); - let second = i.next(); - match (first, second) { - // If pair has exactly one child, just go on parsing that child - (Some(p), None) => { - pair = p; - continue; - } - // Otherwise parse normally - _ => break, - } - } - break; - } - match pair.as_rule() { $( Rule::$name => Parsers::$name(climbers, input, pair), @@ -418,20 +388,6 @@ fn do_parse<'a>( parse_any(&climbers, input, pair) } -// List of rules that can be shortcutted if they have a single child -fn can_be_shortcutted(rule: Rule) -> bool { - use Rule::*; - match rule { - expression - | operator_expression - | application_expression - | first_application_expression - | selector_expression - | annotated_expression => true, - _ => false, - } -} - // Trim the shared indent off of a vec of lines, as defined by the Dhall semantics of multiline // literals. fn trim_indent(lines: &mut Vec) { @@ -591,10 +547,10 @@ make_parser! { rule!(single_quote_char<&'a str>; captured_str!(s) => s ); - rule!(escaped_quote_pair<&'a str> as single_quote_char; + rule!(escaped_quote_pair<&'a str>; captured_str!(_) => "''" ); - rule!(escaped_interpolation<&'a str> as single_quote_char; + rule!(escaped_interpolation<&'a str>; captured_str!(_) => "${" ); rule!(interpolation; children!( @@ -609,6 +565,20 @@ make_parser! { lines.last_mut().unwrap().push(c); lines }, + [escaped_quote_pair(c), single_quote_continue(lines)] => { + let mut lines = lines; + // TODO: don't allocate for every char + let c = InterpolatedTextContents::Text(c.to_owned()); + lines.last_mut().unwrap().push(c); + lines + }, + [escaped_interpolation(c), single_quote_continue(lines)] => { + let mut lines = lines; + // TODO: don't allocate for every char + let c = InterpolatedTextContents::Text(c.to_owned()); + lines.last_mut().unwrap().push(c); + lines + }, [single_quote_char(c), single_quote_continue(lines)] => { let mut lines = lines; if c == "\n" || c == "\r\n" { @@ -682,7 +652,7 @@ make_parser! { } ); - rule!(identifier as expression; span; children!( + rule!(identifier; span; children!( [variable(v)] => { spanned(span, Var(v)) }, @@ -715,19 +685,22 @@ make_parser! { )); rule!(local<(FilePrefix, Vec)>; children!( - [local(l)] => l + [parent_path(l)] => l, + [here_path(l)] => l, + [home_path(l)] => l, + [absolute_path(l)] => l, )); - rule!(parent_path<(FilePrefix, Vec)> as local; children!( + rule!(parent_path<(FilePrefix, Vec)>; children!( [path(p)] => (FilePrefix::Parent, p) )); - rule!(here_path<(FilePrefix, Vec)> as local; children!( + rule!(here_path<(FilePrefix, Vec)>; children!( [path(p)] => (FilePrefix::Here, p) )); - rule!(home_path<(FilePrefix, Vec)> as local; children!( + rule!(home_path<(FilePrefix, Vec)>; children!( [path(p)] => (FilePrefix::Home, p) )); - rule!(absolute_path<(FilePrefix, Vec)> as local; children!( + rule!(absolute_path<(FilePrefix, Vec)>; children!( [path(p)] => (FilePrefix::Absolute, p) )); @@ -760,7 +733,7 @@ make_parser! { rule!(http>; children!( [http_raw(url)] => url, - [http_raw(url), expression(e)] => + [http_raw(url), import_expression(e)] => URL { headers: Some(e), ..url }, )); @@ -828,7 +801,7 @@ make_parser! { token_rule!(Text<()>); token_rule!(Location<()>); - rule!(import as expression; span; children!( + rule!(import; span; children!( [import_hashed(imp)] => { spanned(span, Import(crate::Import { mode: ImportMode::Code, @@ -857,13 +830,13 @@ make_parser! { token_rule!(if_<()>); token_rule!(in_<()>); - rule!(empty_list_literal as expression; span; children!( - [expression(e)] => { + rule!(empty_list_literal; span; children!( + [application_expression(e)] => { spanned(span, EmptyListLit(e)) }, )); - rule!(expression as expression; span; children!( + rule!(expression; span; children!( [lambda(()), label(l), expression(typ), arrow(()), expression(body)] => { spanned(span, Lam(l, typ, body)) @@ -881,16 +854,17 @@ make_parser! { arrow(()), expression(body)] => { spanned(span, Pi(l, typ, body)) }, - [expression(typ), arrow(()), expression(body)] => { + [operator_expression(typ), arrow(()), expression(body)] => { spanned(span, Pi("_".into(), typ, body)) }, - [merge(()), expression(x), expression(y), expression(z)] => { + [merge(()), import_expression(x), import_expression(y), application_expression(z)] => { spanned(span, Merge(x, y, Some(z))) }, + [empty_list_literal(e)] => e, [assert(()), expression(x)] => { spanned(span, Assert(x)) }, - [expression(e)] => e, + [annotated_expression(e)] => e, )); rule!(let_binding<(Label, Option, ParsedSubExpr)>; @@ -904,14 +878,14 @@ make_parser! { token_rule!(List<()>); token_rule!(Optional<()>); - rule!(annotated_expression as expression; span; children!( - [expression(e)] => e, - [expression(e), expression(annot)] => { + rule!(annotated_expression; span; children!( + [operator_expression(e)] => e, + [operator_expression(e), expression(annot)] => { spanned(span, Annot(e, annot)) }, )); - rule!(operator_expression as expression; prec_climb!( + rule!(operator_expression; prec_climb!(application_expression, { use Rule::*; // In order of precedence @@ -937,7 +911,7 @@ make_parser! { .collect(), ) }, - (expression(l), op, expression(r)) => { + (operator_expression(l), op, operator_expression(r)) => { use crate::BinOp::*; use Rule::*; let op = match op.as_rule() { @@ -966,32 +940,38 @@ make_parser! { token_rule!(Some_<()>); token_rule!(toMap<()>); - rule!(application_expression as expression; children!( - [expression(e)] => e, - [expression(first), expression(rest)..] => { + rule!(application_expression; children!( + [first_application_expression(e)] => e, + [first_application_expression(first), import_expression(rest)..] => { rest.fold(first, |acc, e| unspanned(App(acc, e))) }, )); - rule!(first_application_expression as expression; span; + rule!(first_application_expression; span; children!( - [expression(e)] => e, - [Some_(()), expression(e)] => { + [Some_(()), import_expression(e)] => { spanned(span, SomeLit(e)) }, - [merge(()), expression(x), expression(y)] => { + [merge(()), import_expression(x), import_expression(y)] => { spanned(span, Merge(x, y, None)) }, + [import_expression(e)] => e, )); - rule!(selector_expression as expression; children!( - [expression(e)] => e, - [expression(first), selector(rest)..] => { + rule!(import_expression; span; + children!( + [selector_expression(e)] => e, + [import(e)] => e, + )); + + rule!(selector_expression; children!( + [primitive_expression(e)] => e, + [primitive_expression(first), selector(rest)..] => { rest.fold(first, |acc, e| unspanned(match e { Either::Left(l) => Field(acc, l), Either::Right(ls) => Projection(acc, ls), })) - } + }, )); rule!(selector>>; children!( @@ -1004,24 +984,30 @@ make_parser! { [label(ls)..] => ls.collect(), )); - rule!(primitive_expression as expression; span; children!( + rule!(primitive_expression; span; children!( [double_literal(n)] => spanned(span, DoubleLit(n)), [natural_literal(n)] => spanned(span, NaturalLit(n)), [integer_literal(n)] => spanned(span, IntegerLit(n)), [double_quote_literal(s)] => spanned(span, TextLit(s)), [single_quote_literal(s)] => spanned(span, TextLit(s)), + [empty_record_type(e)] => e, + [empty_record_literal(e)] => e, + [non_empty_record_type_or_literal(e)] => e, + [union_type(e)] => e, + [non_empty_list_literal(e)] => e, + [identifier(e)] => e, [expression(e)] => e, )); - rule!(empty_record_literal as expression; span; + rule!(empty_record_literal; span; captured_str!(_) => spanned(span, RecordLit(Default::default())) ); - rule!(empty_record_type as expression; span; + rule!(empty_record_type; span; captured_str!(_) => spanned(span, RecordType(Default::default())) ); - rule!(non_empty_record_type_or_literal as expression; span; + rule!(non_empty_record_type_or_literal; span; children!( [label(first_label), non_empty_record_type(rest)] => { let (first_expr, mut map) = rest; @@ -1057,7 +1043,7 @@ make_parser! { [label(name), expression(expr)] => (name, expr) )); - rule!(union_type as expression; span; children!( + rule!(union_type; span; children!( [empty_union_type(_)] => { spanned(span, UnionType(Default::default())) }, @@ -1073,7 +1059,7 @@ make_parser! { [label(name)] => (name, None), )); - rule!(non_empty_list_literal as expression; span; + rule!(non_empty_list_literal; span; children!( [expression(items)..] => spanned( span, @@ -1081,7 +1067,7 @@ make_parser! { ) )); - rule!(final_expression as expression; children!( + rule!(final_expression; children!( [expression(e), EOI(_eoi)] => e )); } @@ -1092,7 +1078,7 @@ pub fn parse_expr(s: &str) -> ParseResult { let expr = do_parse(rc_input, pairs.next().unwrap())?; assert_eq!(pairs.next(), None); match expr { - ParsedValue::expression(e) => Ok(e), + ParsedValue::final_expression(e) => Ok(e), _ => unreachable!(), } } -- cgit v1.2.3 From 7b891800afa932660bc889e43c41c5d8f805cdc2 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 27 Aug 2019 13:42:53 +0200 Subject: Rework parse macros to avoid the large ParsedValue enum --- dhall_syntax/Cargo.toml | 1 - dhall_syntax/src/parser.rs | 285 ++++++++++++++++++++++----------------------- 2 files changed, 139 insertions(+), 147 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/Cargo.toml b/dhall_syntax/Cargo.toml index 6a61b09..08d0195 100644 --- a/dhall_syntax/Cargo.toml +++ b/dhall_syntax/Cargo.toml @@ -16,4 +16,3 @@ either = "1.5.2" take_mut = "0.2.2" hex = "0.3.2" dhall_generated_parser = { path = "../dhall_generated_parser" } -improved_slice_patterns = { version = "2.0.0", path = "../improved_slice_patterns" } diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index 2debc8b..8facb3e 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -119,6 +119,76 @@ fn debug_pair(pair: Pair) -> String { s } +macro_rules! parse_children { + // Variable length pattern with a common unary variant + (@match_forwards, + $parse_args:expr, + $iter:expr, + ($body:expr), + $variant:ident ($x:ident).., + $($rest:tt)* + ) => { + parse_children!(@match_backwards, + $parse_args, $iter, + ({ + let $x = $iter + .map(|x| Parsers::$variant($parse_args, x)) + .collect::, _>>()? + .into_iter(); + $body + }), + $($rest)* + ) + }; + // Single item pattern + (@match_forwards, + $parse_args:expr, + $iter:expr, + ($body:expr), + $variant:ident ($x:pat), + $($rest:tt)* + ) => {{ + let p = $iter.next().unwrap(); + let $x = Parsers::$variant($parse_args, p)?; + parse_children!(@match_forwards, + $parse_args, $iter, + ($body), + $($rest)* + ) + }}; + // Single item pattern after a variable length one: declare reversed and take from the end + (@match_backwards, + $parse_args:expr, + $iter:expr, + ($body:expr), + $variant:ident ($x:pat), + $($rest:tt)* + ) => { + parse_children!(@match_backwards, $parse_args, $iter, ({ + let p = $iter.next_back().unwrap(); + let $x = Parsers::$variant($parse_args, p)?; + $body + }), $($rest)*) + }; + + // Check no elements remain + (@match_forwards, $parse_args:expr, $iter:expr, ($body:expr) $(,)*) => { + $body + }; + // After a variable length pattern, everything has already been consumed + (@match_backwards, $parse_args:expr, $iter:expr, ($body:expr) $(,)*) => { + $body + }; + + ($parse_args:expr, $iter:expr; [$($args:tt)*] => $body:expr) => { + parse_children!(@match_forwards, + $parse_args, $iter, + ($body), + $($args)*, + ) + }; +} + macro_rules! make_parser { (@children_pattern, $varpat:ident, @@ -155,7 +225,6 @@ macro_rules! make_parser { $varpat:ident, [$variant:ident ($x:pat), $($rest:tt)*] ) => ( - true && make_parser!(@children_filter, $varpat, [$($rest)*]) ); (@children_filter, @@ -167,25 +236,6 @@ macro_rules! make_parser { ); (@children_filter, $varpat:ident, [$(,)*]) => (true); - (@rule_alias, - rule!( $name:ident<$o:ty>; $($args:tt)* ) - ) => ( - Rule::$name - ); - (@rule_alias, - rule!( - $name:ident<$o:ty>; - $($args:tt)* - ) - ) => ({ - Rule::$name - }); - (@rule_alias, - token_rule!($name:ident<$o:ty>) - ) => ({ - Rule::$name - }); - (@body, ($climbers:expr, $input:expr, $pair:expr), rule!( @@ -194,11 +244,9 @@ macro_rules! make_parser { captured_str!($x:pat) => $body:expr ) ) => ({ - let res: Result<_, String> = try { - let $span = Span::make($input, $pair.as_span()); - let $x = $pair.as_str(); - ParsedValue::$name($body) - }; + let $span = Span::make($input.clone(), $pair.as_span()); + let $x = $pair.as_str(); + let res: Result<_, String> = try { $body }; res.map_err(|msg| custom_parse_error(&$pair, msg)) }); (@body, @@ -209,90 +257,64 @@ macro_rules! make_parser { children!( $( [$($args:tt)*] => $body:expr ),* $(,)* ) ) ) => ({ - let children: Vec<_> = $pair - .clone() - .into_inner() - .map(|p| parse_any($climbers, $input.clone(), p)) - .collect::>()?; let children_rules: Vec = $pair .clone() .into_inner() .map(|p| p.as_rule()) - .map(rule_alias) .collect(); + let $span = Span::make($input.clone(), $pair.as_span()); + #[allow(unused_mut)] + let mut iter = $pair.clone().into_inner(); + #[allow(unreachable_code)] - let res: Result<_, String> = try { - #[allow(unused_imports)] - use ParsedValue::*; - let $span = Span::make($input, $pair.as_span()); - #[allow(unused_mut)] - let mut iter = children.into_iter(); - - match children_rules.as_slice() { - $( - make_parser!(@children_pattern, x, (), [$($args)*,]) - if make_parser!(@children_filter, x, [$($args)*,]) - => { - let ret = - improved_slice_patterns::destructure_iter!(iter; - [$($args)*] => $body - ); - match ret { - Some(x) => x, - None => Err({ - format!("Unexpected children: {:?}", children_rules) - })? + match children_rules.as_slice() { + $( + make_parser!(@children_pattern, x, (), [$($args)*,]) + if make_parser!(@children_filter, x, [$($args)*,]) + => { + parse_children!(($climbers, $input.clone()), iter; + [$($args)*] => { + let res: Result<_, String> = try { $body }; + res.map_err(|msg| custom_parse_error(&$pair, msg)) } - } - , - )* - [..] => Err({ - format!("Unexpected children: {:?}", children_rules) - })?, - } - }; - - let res = res.map_err(|msg| custom_parse_error(&$pair, msg))?; - Ok(ParsedValue::$name(res)) + ) + } + , + )* + [..] => Err(custom_parse_error( + &$pair, + format!("Unexpected children: {:?}", children_rules) + )), + } }); (@body, ($climbers:expr, $input:expr, $pair:expr), rule!( $name:ident<$o:ty>; - prec_climb!($other_name:ident, $_climber:expr, $args:pat => $body:expr $(,)* ) + prec_climb!( + $other_rule:ident, + $_climber:expr, + $args:pat => $body:expr $(,)* + ) ) ) => ({ - let climber = $climbers.get(&Rule::$name).expect(&format!("UUUU: {:?}", $climbers)); + let climber = $climbers.get(&Rule::$name).unwrap(); climber.climb( $pair.clone().into_inner(), - |p| { - let parsed = parse_any($climbers, $input.clone(), p)?; - if let ParsedValue::$other_name(x) = parsed { - Ok(ParsedValue::$name(x)) - } else { - unreachable!() - } - }, + |p| Parsers::$other_rule(($climbers, $input.clone()), p), |l, op, r| { - let (l, r) = (l?, r?); - let res: Result<_, String> = try { - #[allow(unused_imports)] - use ParsedValue::*; - match (l, op, r) { - $args => ParsedValue::$name($body), - children => Err( - format!("Unexpected children: {:?}", children) - )?, - } - }; + let $args = (l?, op, r?); + let res: Result<_, String> = try { $body }; res.map_err(|msg| custom_parse_error(&$pair, msg)) }, ) }); (@body, ($($things:tt)*), - rule!( $name:ident<$o:ty>; $($args:tt)* + rule!( + $name:ident<$o:ty>; + $($args:tt)* ) ) => ({ make_parser!(@body, @@ -308,14 +330,14 @@ macro_rules! make_parser { ($($things:tt)*), token_rule!($name:ident<$o:ty>) ) => ({ - Ok(ParsedValue::$name(())) + Ok(()) }); (@construct_climber, ($map:expr), rule!( $name:ident<$o:ty>; - prec_climb!( $other_name:ident, $climber:expr, $($_rest:tt)* ) + prec_climb!($other_rule:ident, $climber:expr, $($_rest:tt)* ) ) ) => ({ $map.insert(Rule::$name, $climber) @@ -323,10 +345,19 @@ macro_rules! make_parser { (@construct_climber, ($($things:tt)*), $($args:tt)*) => (()); ($( $submac:ident!( $name:ident<$o:ty> $($args:tt)* ); )*) => ( - #[allow(non_camel_case_types, dead_code, clippy::large_enum_variant)] - #[derive(Debug)] - enum ParsedValue<'a> { - $( $name($o), )* + struct Parsers; + + impl Parsers { + $( + #[allow(non_snake_case, unused_variables)] + fn $name<'a>( + (climbers, input): (&HashMap>, Rc), + pair: Pair<'a, Rule>, + ) -> ParseResult<$o> { + make_parser!(@body, (climbers, input, pair), + $submac!( $name<$o> $($args)* )) + } + )* } fn construct_precclimbers() -> HashMap> { @@ -338,56 +369,23 @@ macro_rules! make_parser { map } - struct Parsers; + struct EntryPoint; - impl Parsers { + impl EntryPoint { $( - #[allow(non_snake_case, unused_variables)] + #[allow(non_snake_case, dead_code)] fn $name<'a>( - climbers: &HashMap>, input: Rc, pair: Pair<'a, Rule>, - ) -> ParseResult> { - make_parser!(@body, (climbers, input, pair), - $submac!( $name<$o> $($args)* )) + ) -> ParseResult<$o> { + let climbers = construct_precclimbers(); + Parsers::$name((&climbers, input), pair) } )* } - - fn rule_alias(r: Rule) -> Rule { - match r { - $( - Rule::$name => { - make_parser!(@rule_alias, $submac!($name<$o> $($args)*)) - } - )* - r => r, - } - } - - fn parse_any<'a>( - climbers: &HashMap>, - input: Rc, - pair: Pair<'a, Rule>, - ) -> ParseResult> { - match pair.as_rule() { - $( - Rule::$name => Parsers::$name(climbers, input, pair), - )* - r => Err(custom_parse_error(&pair, format!("Unexpected {:?}", r))), - } - } ); } -fn do_parse<'a>( - input: Rc, - pair: Pair<'a, Rule>, -) -> ParseResult> { - let climbers = construct_precclimbers(); - parse_any(&climbers, input, pair) -} - // Trim the shared indent off of a vec of lines, as defined by the Dhall semantics of multiline // literals. fn trim_indent(lines: &mut Vec) { @@ -857,14 +855,18 @@ make_parser! { [operator_expression(typ), arrow(()), expression(body)] => { spanned(span, Pi("_".into(), typ, body)) }, - [merge(()), import_expression(x), import_expression(y), application_expression(z)] => { + [merge(()), import_expression(x), import_expression(y), + application_expression(z)] => { spanned(span, Merge(x, y, Some(z))) }, [empty_list_literal(e)] => e, [assert(()), expression(x)] => { spanned(span, Assert(x)) }, - [annotated_expression(e)] => e, + [operator_expression(e)] => e, + [operator_expression(e), expression(annot)] => { + spanned(span, Annot(e, annot)) + }, )); rule!(let_binding<(Label, Option, ParsedSubExpr)>; @@ -878,14 +880,8 @@ make_parser! { token_rule!(List<()>); token_rule!(Optional<()>); - rule!(annotated_expression; span; children!( - [operator_expression(e)] => e, - [operator_expression(e), expression(annot)] => { - spanned(span, Annot(e, annot)) - }, - )); - - rule!(operator_expression; prec_climb!(application_expression, + rule!(operator_expression; prec_climb!( + application_expression, { use Rule::*; // In order of precedence @@ -911,7 +907,7 @@ make_parser! { .collect(), ) }, - (operator_expression(l), op, operator_expression(r)) => { + (l, op, r) => { use crate::BinOp::*; use Rule::*; let op = match op.as_rule() { @@ -1075,10 +1071,7 @@ make_parser! { pub fn parse_expr(s: &str) -> ParseResult { let mut pairs = DhallParser::parse(Rule::final_expression, s)?; let rc_input = s.to_string().into(); - let expr = do_parse(rc_input, pairs.next().unwrap())?; + let expr = EntryPoint::final_expression(rc_input, pairs.next().unwrap())?; assert_eq!(pairs.next(), None); - match expr { - ParsedValue::final_expression(e) => Ok(e), - _ => unreachable!(), - } + Ok(expr) } -- cgit v1.2.3 From cddb3b0a0270cf6d41bbaa1a0e01708e91a9673c Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 27 Aug 2019 22:35:33 +0200 Subject: s/token_rule/rule/ --- dhall_syntax/src/parser.rs | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index 8facb3e..4da1dfc 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -328,7 +328,7 @@ macro_rules! make_parser { }); (@body, ($($things:tt)*), - token_rule!($name:ident<$o:ty>) + rule!($name:ident<$o:ty>) ) => ({ Ok(()) }); @@ -428,7 +428,7 @@ fn trim_indent(lines: &mut Vec) { } make_parser! { - token_rule!(EOI<()>); + rule!(EOI<()>); rule!(simple_label