From 775b26ee3e6dba4173d71ab5c15a0f11aceeef69 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 20 Mar 2019 21:42:32 +0100 Subject: Allow patterns in parser macros --- dhall_core/src/parser.rs | 52 +++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 31 deletions(-) (limited to 'dhall_core/src/parser.rs') diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs index 34b6a13..8b14da0 100644 --- a/dhall_core/src/parser.rs +++ b/dhall_core/src/parser.rs @@ -72,15 +72,15 @@ fn debug_pair(pair: Pair) -> String { } macro_rules! match_pair { - (@make_child_match, $pair:expr, ($($outer_acc:tt)*), ($($acc:tt)*), ($(,)* $ty:ident ($x:ident) $($rest_of_match:tt)*) => $body:expr, $($rest:tt)*) => { - match_pair!(@make_child_match, $pair, ($($outer_acc)*), ($($acc)*, ParsedValue::$ty($x)), ($($rest_of_match)*) => $body, $($rest)*) - }; (@make_child_match, $pair:expr, ($($outer_acc:tt)*), ($($acc:tt)*), ($(,)* $ty:ident ($x:ident..) $($rest_of_match:tt)*) => $body:expr, $($rest:tt)*) => { match_pair!(@make_child_match, $pair, ($($outer_acc)*), ($($acc)*, x..), ($($rest_of_match)*) => { let $x = x.map(|x| x.$ty()); $body }, $($rest)*) }; + (@make_child_match, $pair:expr, ($($outer_acc:tt)*), ($($acc:tt)*), ($(,)* $ty:ident ($x:pat) $($rest_of_match:tt)*) => $body:expr, $($rest:tt)*) => { + match_pair!(@make_child_match, $pair, ($($outer_acc)*), ($($acc)*, ParsedValue::$ty($x)), ($($rest_of_match)*) => $body, $($rest)*) + }; (@make_child_match, $pair:expr, ($($outer_acc:tt)*), (, $($acc:tt)*), ($(,)*) => $body:expr, $($rest:tt)*) => { match_pair!(@make_matches, $pair, ([$($acc)*] => { $body }, $($outer_acc)*), $($rest)*) }; @@ -261,8 +261,8 @@ rule!(double_quote_literal; children!( )); rule!(double_quote_chunk>; children!( - [interpolation(c)] => { - InterpolatedTextContents::Expr(c) + [interpolation(e)] => { + InterpolatedTextContents::Expr(e) }, [double_quote_escaped(s)] => { InterpolatedTextContents::Text(s) @@ -396,25 +396,21 @@ rule!(import_type_raw; children!( // [http(url)] => { // ImportLocation::Remote(url) // } - [local_raw(import)] => { - let (prefix, path) = import; + [local_raw((prefix, path))] => { ImportLocation::Local(prefix, path) } )); rule!(import_hashed_raw<(ImportLocation, Option<()>)>; children!( // TODO: handle hash - [import_type_raw(import)] => { - (import, None) - } + [import_type_raw(import)] => (import, None) )); rule_group!(expression); rule_in_group!(import_raw; expression; children!( // TODO: handle "as Text" - [import_hashed_raw(import)] => { - let (location, hash) = import; + [import_hashed_raw((location, hash))] => { bx(Expr::Embed(Import { mode: ImportMode::Code, hash, @@ -467,16 +463,16 @@ rule!(List<()>; raw_pair!(_) => ()); rule!(Optional<()>; raw_pair!(_) => ()); rule_in_group!(empty_collection; expression; children!( - [List(_x), expression(y)] => { + [List(_), expression(y)] => { bx(Expr::EmptyListLit(y)) }, - [Optional(_x), expression(y)] => { + [Optional(_), expression(y)] => { bx(Expr::OptionalLit(Some(y), None)) }, )); rule_in_group!(non_empty_optional; expression; children!( - [expression(x), Optional(_y), expression(z)] => { + [expression(x), Optional(_), expression(z)] => { bx(Expr::OptionalLit(Some(z), Some(x))) } )); @@ -544,9 +540,7 @@ rule_in_group!(selector_expression_raw; expression; children!( // TODO: handle record projection rule!(selector_raw