From 39fc0af2548127a7c2ee1345d4ea63d473e0ab5a Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 6 Mar 2019 22:08:27 +0100 Subject: Reorder rule in parser to match grammar --- dhall_core/src/parser.rs | 218 ++++++++++++++++++++++++----------------------- 1 file changed, 110 insertions(+), 108 deletions(-) (limited to 'dhall_core') diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs index 6b20c01..f5a9129 100644 --- a/dhall_core/src/parser.rs +++ b/dhall_core/src/parser.rs @@ -428,6 +428,19 @@ named!(str<&'a str>; captured_str!(s) => s.trim()); named!(raw_str<&'a str>; captured_str!(s) => s); +// TODO: parse escapes and interpolation +rule!(double_quote_literal; + children!(strs*: raw_str) => { + strs.collect() + } +); + +rule!(single_quote_literal; + children!(eol: raw_str, contents: single_quote_continue) => { + contents.push(eol); + contents.into_iter().rev().collect() + } +); rule!(escaped_quote_pair<&'a str>; children!() => "''" ); @@ -455,65 +468,34 @@ rule!(single_quote_continue>; }, ); -rule!(let_binding<(&'a str, Option>, BoxExpr<'a>)>; - children!(name: str, annot?: expression, expr: expression) => (name, annot, expr) -); - -named!(record_entry<(&'a str, BoxExpr<'a>)>; - children!(name: str, expr: expression) => (name, expr) -); +rule!(NaN_raw<()>; children!() => ()); +rule!(minus_infinity_literal<()>; children!() => ()); +rule!(plus_infinity_literal<()>; children!() => ()); -named!(partial_record_entries<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>; - children!(expr: expression, entries*: record_entry) => { - let mut map: BTreeMap<&str, ParsedExpr> = BTreeMap::new(); - for (n, e) in entries { - map.insert(n, *e); - } - (expr, map) +rule!(double_literal_raw; + raw_pair!(pair) => { + pair.as_str().trim() + .parse() + .map_err(|e: std::num::ParseFloatError| custom_parse_error(&pair, format!("{}", e)))? } ); -rule!(non_empty_record_literal<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>; - self!(x: partial_record_entries) => x -); - -rule!(non_empty_record_type<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>; - self!(x: partial_record_entries) => x -); - -rule!(union_type_entry<(&'a str, BoxExpr<'a>)>; - children!(name: str, expr: expression) => (name, expr) -); - -rule!(union_type_entries>>; - children!(entries*: union_type_entry) => { - let mut map: BTreeMap<&str, ParsedExpr> = BTreeMap::new(); - for (n, e) in entries { - map.insert(n, *e); - } - map +rule!(natural_literal_raw; + raw_pair!(pair) => { + pair.as_str().trim() + .parse() + .map_err(|e: std::num::ParseIntError| custom_parse_error(&pair, format!("{}", e)))? } ); -rule!(non_empty_union_type_or_literal - <(Option<(&'a str, BoxExpr<'a>)>, BTreeMap<&'a str, ParsedExpr<'a>>)>; - children!(label: str, e: expression, entries: union_type_entries) => { - (Some((label, e)), entries) - }, - children!(l: str, e: expression, rest: non_empty_union_type_or_literal) => { - let (x, mut entries) = rest; - entries.insert(l, *e); - (x, entries) - }, - children!(l: str, e: expression) => { - let mut entries = BTreeMap::new(); - entries.insert(l, *e); - (None, entries) - }, +rule!(integer_literal_raw; + raw_pair!(pair) => { + pair.as_str().trim() + .parse() + .map_err(|e: std::num::ParseIntError| custom_parse_error(&pair, format!("{}", e)))? + } ); -rule!(empty_union_type<()>; children!() => ()); - rule_group!(expression>; identifier_raw, lambda_expression, @@ -550,62 +532,6 @@ rule_group!(expression>; final_expression ); -// TODO: parse escapes and interpolation -rule!(double_quote_literal; - children!(strs*: raw_str) => { - strs.collect() - } -); -rule!(single_quote_literal; - children!(eol: raw_str, contents: single_quote_continue) => { - contents.push(eol); - contents.into_iter().rev().collect() - } -); - -rule!(double_literal_raw; - raw_pair!(pair) => { - pair.as_str().trim() - .parse() - .map_err(|e: std::num::ParseFloatError| custom_parse_error(&pair, format!("{}", e)))? - } -); - -rule!(minus_infinity_literal<()>; children!() => ()); -rule!(plus_infinity_literal<()>; children!() => ()); -rule!(NaN_raw<()>; children!() => ()); - -rule!(natural_literal_raw; - raw_pair!(pair) => { - pair.as_str().trim() - .parse() - .map_err(|e: std::num::ParseIntError| custom_parse_error(&pair, format!("{}", e)))? - } -); - -rule!(integer_literal_raw; - raw_pair!(pair) => { - pair.as_str().trim() - .parse() - .map_err(|e: std::num::ParseIntError| custom_parse_error(&pair, format!("{}", e)))? - } -); - -rule!(identifier_raw>; - children!(name: str, idx?: natural_literal_raw) => { - match Builtin::parse(name) { - Some(b) => bx(Expr::Builtin(b)), - None => match name { - "True" => bx(Expr::BoolLit(true)), - "False" => bx(Expr::BoolLit(false)), - "Type" => bx(Expr::Const(Const::Type)), - "Kind" => bx(Expr::Const(Const::Kind)), - name => bx(Expr::Var(V(name, idx.unwrap_or(0)))), - } - } - } -); - rule!(lambda_expression>; children!(label: str, typ: expression, body: expression) => { bx(Expr::Lam(label, typ, body)) @@ -624,6 +550,10 @@ rule!(let_expression>; } ); +rule!(let_binding<(&'a str, Option>, BoxExpr<'a>)>; + children!(name: str, annot?: expression, expr: expression) => (name, annot, expr) +); + rule!(forall_expression>; children!(label: str, typ: expression, body: expression) => { bx(Expr::Pi(label, typ, body)) @@ -712,12 +642,29 @@ rule!(literal_expression_raw>; children!(e: expression) => e, ); -rule!(empty_record_type>; - children!() => bx(Expr::Record(BTreeMap::new())) +rule!(identifier_raw>; + children!(name: str, idx?: natural_literal_raw) => { + match Builtin::parse(name) { + Some(b) => bx(Expr::Builtin(b)), + None => match name { + "True" => bx(Expr::BoolLit(true)), + "False" => bx(Expr::BoolLit(false)), + "Type" => bx(Expr::Const(Const::Type)), + "Kind" => bx(Expr::Const(Const::Kind)), + name => bx(Expr::Var(V(name, idx.unwrap_or(0)))), + } + } + } ); + rule!(empty_record_literal>; children!() => bx(Expr::RecordLit(BTreeMap::new())) ); + +rule!(empty_record_type>; + children!() => bx(Expr::Record(BTreeMap::new())) +); + rule!(non_empty_record_type_or_literal>; children!(first_label: str, rest: non_empty_record_type) => { let (first_expr, mut map) = rest; @@ -731,6 +678,28 @@ rule!(non_empty_record_type_or_literal>; }, ); +rule!(non_empty_record_type<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>; + self!(x: partial_record_entries) => x +); + +named!(partial_record_entries<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>; + children!(expr: expression, entries*: record_entry) => { + let mut map: BTreeMap<&str, ParsedExpr> = BTreeMap::new(); + for (n, e) in entries { + map.insert(n, *e); + } + (expr, map) + } +); + +named!(record_entry<(&'a str, BoxExpr<'a>)>; + children!(name: str, expr: expression) => (name, expr) +); + +rule!(non_empty_record_literal<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>; + self!(x: partial_record_entries) => x +); + rule!(union_type_or_literal>; children!(_e: empty_union_type) => { bx(Expr::Union(BTreeMap::new())) @@ -743,6 +712,39 @@ rule!(union_type_or_literal>; }, ); +rule!(empty_union_type<()>; children!() => ()); + +rule!(non_empty_union_type_or_literal + <(Option<(&'a str, BoxExpr<'a>)>, BTreeMap<&'a str, ParsedExpr<'a>>)>; + children!(label: str, e: expression, entries: union_type_entries) => { + (Some((label, e)), entries) + }, + children!(l: str, e: expression, rest: non_empty_union_type_or_literal) => { + let (x, mut entries) = rest; + entries.insert(l, *e); + (x, entries) + }, + children!(l: str, e: expression) => { + let mut entries = BTreeMap::new(); + entries.insert(l, *e); + (None, entries) + }, +); + +rule!(union_type_entries>>; + children!(entries*: union_type_entry) => { + let mut map: BTreeMap<&str, ParsedExpr> = BTreeMap::new(); + for (n, e) in entries { + map.insert(n, *e); + } + map + } +); + +rule!(union_type_entry<(&'a str, BoxExpr<'a>)>; + children!(name: str, expr: expression) => (name, expr) +); + rule!(non_empty_list_literal_raw>; children!(items*: expression) => { bx(Expr::ListLit(None, items.map(|x| *x).collect())) -- cgit v1.2.3