From 8489a9f3922ab17709f28adddda851dfae2c02e5 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 7 Mar 2019 00:37:18 +0100 Subject: Revert "Start moving strs in the AST" This reverts commit c2965244babe7b391fc6a72c015db6720ceb853f. --- dhall_core/src/parser.rs | 96 ++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 49 deletions(-) (limited to 'dhall_core/src/parser.rs') diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs index ad9e52b..34327b1 100644 --- a/dhall_core/src/parser.rs +++ b/dhall_core/src/parser.rs @@ -46,7 +46,7 @@ fn debug_pair(pair: Pair) -> String { .unwrap(); } } - aux(s, indent + 1, "".to_owned(), p); + aux(s, indent + 1, "".into(), p); } if first { writeln!( @@ -57,7 +57,7 @@ fn debug_pair(pair: Pair) -> String { .unwrap(); } } - aux(&mut s, 0, "".to_owned(), pair); + aux(&mut s, 0, "".into(), pair); s } @@ -416,8 +416,6 @@ rule!(EOI<()>; children!() => ()); named!(str<&'a str>; captured_str!(s) => s.trim()); -named!(string; captured_str!(s) => s.trim().to_owned()); - named!(raw_str<&'a str>; captured_str!(s) => s); // TODO: parse escapes and interpolation @@ -537,7 +535,7 @@ rule!(import_hashed_raw<(ImportLocation, Option<()>)>; } ); -rule!(import_raw; +rule!(import_raw>; // TODO: handle "as Text" children!(import: import_hashed_raw) => { let (location, hash) = import; @@ -549,7 +547,7 @@ rule!(import_raw; } ); -rule_group!(expression; +rule_group!(expression>; identifier_raw, lambda_expression, ifthenelse_expression, @@ -586,47 +584,47 @@ rule_group!(expression; final_expression ); -rule!(lambda_expression; - children!(label: string, typ: expression, body: expression) => { +rule!(lambda_expression>; + children!(label: str, typ: expression, body: expression) => { bx(Expr::Lam(label, typ, body)) } ); -rule!(ifthenelse_expression; +rule!(ifthenelse_expression>; children!(cond: expression, left: expression, right: expression) => { bx(Expr::BoolIf(cond, left, right)) } ); -rule!(let_expression; +rule!(let_expression>; children!(bindings*: let_binding, final_expr: expression) => { bindings.fold(final_expr, |acc, x| bx(Expr::Let(x.0, x.1, x.2, acc))) } ); -rule!(let_binding<(String, Option, BoxExpr)>; - children!(name: string, annot?: expression, expr: expression) => (name, annot, expr) +rule!(let_binding<(&'a str, Option>, BoxExpr<'a>)>; + children!(name: str, annot?: expression, expr: expression) => (name, annot, expr) ); -rule!(forall_expression; - children!(label: string, typ: expression, body: expression) => { +rule!(forall_expression>; + children!(label: str, typ: expression, body: expression) => { bx(Expr::Pi(label, typ, body)) } ); -rule!(arrow_expression; +rule!(arrow_expression>; children!(typ: expression, body: expression) => { - bx(Expr::Pi("_".to_owned(), typ, body)) + bx(Expr::Pi("_", typ, body)) } ); -rule!(merge_expression; +rule!(merge_expression>; children!(x: expression, y: expression, z?: expression) => { bx(Expr::Merge(x, y, z)) } ); -rule!(empty_collection; +rule!(empty_collection>; children!(x: str, y: expression) => { match x { "Optional" => bx(Expr::OptionalLit(Some(y), vec![])), @@ -636,7 +634,7 @@ rule!(empty_collection; } ); -rule!(non_empty_optional; +rule!(non_empty_optional>; children!(x: expression, _y: str, z: expression) => { bx(Expr::OptionalLit(Some(z), vec![*x])) } @@ -644,7 +642,7 @@ rule!(non_empty_optional; macro_rules! binop { ($rule:ident, $op:ident) => { - rule!($rule; + rule!($rule>; children!(first: expression, rest*: expression) => { rest.fold(first, |acc, e| bx(Expr::BinOp(BinOp::$op, acc, e))) } @@ -652,7 +650,7 @@ macro_rules! binop { }; } -rule!(annotated_expression; +rule!(annotated_expression>; children!(e: expression, annot: expression) => { bx(Expr::Annot(e, annot)) }, @@ -672,19 +670,19 @@ binop!(times_expression, NaturalTimes); binop!(equal_expression, BoolEQ); binop!(not_equal_expression, BoolNE); -rule!(application_expression; +rule!(application_expression>; children!(first: expression, rest*: expression) => { rest.fold(first, |acc, e| bx(Expr::App(acc, e))) } ); -rule!(selector_expression_raw; - children!(first: expression, rest*: string) => { +rule!(selector_expression_raw>; + children!(first: expression, rest*: str) => { rest.fold(first, |acc, e| bx(Expr::Field(acc, e))) } ); -rule!(literal_expression_raw; +rule!(literal_expression_raw>; children!(n: double_literal_raw) => bx(Expr::DoubleLit(n)), children!(n: minus_infinity_literal) => bx(Expr::DoubleLit(std::f64::NEG_INFINITY)), children!(n: plus_infinity_literal) => bx(Expr::DoubleLit(std::f64::INFINITY)), @@ -696,7 +694,7 @@ rule!(literal_expression_raw; children!(e: expression) => e, ); -rule!(identifier_raw; +rule!(identifier_raw>; children!(name: str, idx?: natural_literal_raw) => { match Builtin::parse(name) { Some(b) => bx(Expr::Builtin(b)), @@ -705,40 +703,40 @@ rule!(identifier_raw; "False" => bx(Expr::BoolLit(false)), "Type" => bx(Expr::Const(Const::Type)), "Kind" => bx(Expr::Const(Const::Kind)), - name => bx(Expr::Var(V(name.to_owned(), idx.unwrap_or(0)))), + name => bx(Expr::Var(V(name, idx.unwrap_or(0)))), } } } ); -rule!(empty_record_literal; +rule!(empty_record_literal>; children!() => bx(Expr::RecordLit(BTreeMap::new())) ); -rule!(empty_record_type; +rule!(empty_record_type>; children!() => bx(Expr::Record(BTreeMap::new())) ); -rule!(non_empty_record_type_or_literal; - children!(first_label: string, rest: non_empty_record_type) => { +rule!(non_empty_record_type_or_literal>; + children!(first_label: str, rest: non_empty_record_type) => { let (first_expr, mut map) = rest; map.insert(first_label, *first_expr); bx(Expr::Record(map)) }, - children!(first_label: string, rest: non_empty_record_literal) => { + children!(first_label: str, rest: non_empty_record_literal) => { let (first_expr, mut map) = rest; map.insert(first_label, *first_expr); bx(Expr::RecordLit(map)) }, ); -rule!(non_empty_record_type<(BoxExpr, BTreeMap)>; +rule!(non_empty_record_type<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>; self!(x: partial_record_entries) => x ); -named!(partial_record_entries<(BoxExpr, BTreeMap)>; +named!(partial_record_entries<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>; children!(expr: expression, entries*: record_entry) => { - let mut map: BTreeMap = BTreeMap::new(); + let mut map: BTreeMap<&str, ParsedExpr> = BTreeMap::new(); for (n, e) in entries { map.insert(n, *e); } @@ -746,15 +744,15 @@ named!(partial_record_entries<(BoxExpr, BTreeMap)>; } ); -named!(record_entry<(String, BoxExpr)>; - children!(name: string, expr: expression) => (name, expr) +named!(record_entry<(&'a str, BoxExpr<'a>)>; + children!(name: str, expr: expression) => (name, expr) ); -rule!(non_empty_record_literal<(BoxExpr, BTreeMap)>; +rule!(non_empty_record_literal<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>; self!(x: partial_record_entries) => x ); -rule!(union_type_or_literal; +rule!(union_type_or_literal>; children!(_e: empty_union_type) => { bx(Expr::Union(BTreeMap::new())) }, @@ -769,25 +767,25 @@ rule!(union_type_or_literal; rule!(empty_union_type<()>; children!() => ()); rule!(non_empty_union_type_or_literal - <(Option<(String, BoxExpr)>, BTreeMap)>; - children!(label: string, e: expression, entries: union_type_entries) => { + <(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: string, e: expression, rest: non_empty_union_type_or_literal) => { + 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: string, e: expression) => { + children!(l: str, e: expression) => { let mut entries = BTreeMap::new(); entries.insert(l, *e); (None, entries) }, ); -rule!(union_type_entries>; +rule!(union_type_entries>>; children!(entries*: union_type_entry) => { - let mut map: BTreeMap = BTreeMap::new(); + let mut map: BTreeMap<&str, ParsedExpr> = BTreeMap::new(); for (n, e) in entries { map.insert(n, *e); } @@ -795,17 +793,17 @@ rule!(union_type_entries>; } ); -rule!(union_type_entry<(String, BoxExpr)>; - children!(name: string, expr: expression) => (name, expr) +rule!(union_type_entry<(&'a str, BoxExpr<'a>)>; + children!(name: str, expr: expression) => (name, expr) ); -rule!(non_empty_list_literal_raw; +rule!(non_empty_list_literal_raw>; children!(items*: expression) => { bx(Expr::ListLit(None, items.map(|x| *x).collect())) } ); -rule!(final_expression; +rule!(final_expression>; children!(e: expression, _eoi: EOI) => e ); -- cgit v1.2.3