diff options
author | Nadrieril | 2019-03-07 00:37:13 +0100 |
---|---|---|
committer | Nadrieril | 2019-03-07 00:37:13 +0100 |
commit | c2965244babe7b391fc6a72c015db6720ceb853f (patch) | |
tree | 9e11af30fb75a62861975fe68fcca4d45e332492 /dhall | |
parent | 474744de28034e9558b1a3ff4f9e21ec1425d794 (diff) |
Start moving strs in the AST
Diffstat (limited to '')
-rw-r--r-- | dhall_core/src/core.rs | 236 | ||||
-rw-r--r-- | dhall_core/src/grammar_util.rs | 4 | ||||
-rw-r--r-- | dhall_core/src/parser.rs | 96 |
3 files changed, 170 insertions, 166 deletions
diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index f2177b1..fcbc813 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -103,8 +103,8 @@ pub struct Import { /// Zero indices are omitted when pretty-printing `Var`s and non-zero indices /// appear as a numeric suffix. /// -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub struct V<'i>(pub &'i str, pub usize); +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct V(pub String, pub usize); #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum BinOp { @@ -136,40 +136,40 @@ pub enum BinOp { /// Syntax tree for expressions #[derive(Debug, Clone, PartialEq)] -pub enum Expr<'i, S, A> { +pub enum Expr<S, A> { /// `Const c ~ c` Const(Const), /// `Var (V x 0) ~ x`<br> /// `Var (V x n) ~ x@n` - Var(V<'i>), + Var(V), /// `Lam x A b ~ λ(x : A) -> b` - Lam(&'i str, Box<Expr<'i, S, A>>, Box<Expr<'i, S, A>>), + Lam(String, Box<Expr<S, A>>, Box<Expr<S, A>>), /// `Pi "_" A B ~ A -> B` /// `Pi x A B ~ ∀(x : A) -> B` - Pi(&'i str, Box<Expr<'i, S, A>>, Box<Expr<'i, S, A>>), + Pi(String, Box<Expr<S, A>>, Box<Expr<S, A>>), /// `App f A ~ f A` - App(Box<Expr<'i, S, A>>, Box<Expr<'i, S, A>>), + App(Box<Expr<S, A>>, Box<Expr<S, A>>), /// `Let x Nothing r e ~ let x = r in e` /// `Let x (Just t) r e ~ let x : t = r in e` Let( - &'i str, - Option<Box<Expr<'i, S, A>>>, - Box<Expr<'i, S, A>>, - Box<Expr<'i, S, A>>, + String, + Option<Box<Expr<S, A>>>, + Box<Expr<S, A>>, + Box<Expr<S, A>>, ), /// `Annot x t ~ x : t` - Annot(Box<Expr<'i, S, A>>, Box<Expr<'i, S, A>>), + Annot(Box<Expr<S, A>>, Box<Expr<S, A>>), /// Built-in values Builtin(Builtin), // Binary operations - BinOp(BinOp, Box<Expr<'i, S, A>>, Box<Expr<'i, S, A>>), + BinOp(BinOp, Box<Expr<S, A>>, Box<Expr<S, A>>), /// `BoolLit b ~ b` BoolLit(bool), /// `BoolIf x y z ~ if x then y else z` BoolIf( - Box<Expr<'i, S, A>>, - Box<Expr<'i, S, A>>, - Box<Expr<'i, S, A>>, + Box<Expr<S, A>>, + Box<Expr<S, A>>, + Box<Expr<S, A>>, ), /// `NaturalLit n ~ +n` NaturalLit(Natural), @@ -180,32 +180,32 @@ pub enum Expr<'i, S, A> { /// `TextLit t ~ t` TextLit(Builder), /// `ListLit t [x, y, z] ~ [x, y, z] : List t` - ListLit(Option<Box<Expr<'i, S, A>>>, Vec<Expr<'i, S, A>>), + ListLit(Option<Box<Expr<S, A>>>, Vec<Expr<S, A>>), /// `OptionalLit t [e] ~ [e] : Optional t` /// `OptionalLit t [] ~ [] : Optional t` - OptionalLit(Option<Box<Expr<'i, S, A>>>, Vec<Expr<'i, S, A>>), + OptionalLit(Option<Box<Expr<S, A>>>, Vec<Expr<S, A>>), /// `Record [(k1, t1), (k2, t2)] ~ { k1 : t1, k2 : t1 }` - Record(BTreeMap<&'i str, Expr<'i, S, A>>), + Record(BTreeMap<String, Expr<S, A>>), /// `RecordLit [(k1, v1), (k2, v2)] ~ { k1 = v1, k2 = v2 }` - RecordLit(BTreeMap<&'i str, Expr<'i, S, A>>), + RecordLit(BTreeMap<String, Expr<S, A>>), /// `Union [(k1, t1), (k2, t2)] ~ < k1 : t1, k2 : t2 >` - Union(BTreeMap<&'i str, Expr<'i, S, A>>), + Union(BTreeMap<String, Expr<S, A>>), /// `UnionLit (k1, v1) [(k2, t2), (k3, t3)] ~ < k1 = t1, k2 : t2, k3 : t3 >` UnionLit( - &'i str, - Box<Expr<'i, S, A>>, - BTreeMap<&'i str, Expr<'i, S, A>>, + String, + Box<Expr<S, A>>, + BTreeMap<String, Expr<S, A>>, ), /// `Merge x y t ~ merge x y : t` Merge( - Box<Expr<'i, S, A>>, - Box<Expr<'i, S, A>>, - Option<Box<Expr<'i, S, A>>>, + Box<Expr<S, A>>, + Box<Expr<S, A>>, + Option<Box<Expr<S, A>>>, ), /// `Field e x ~ e.x` - Field(Box<Expr<'i, S, A>>, &'i str), + Field(Box<Expr<S, A>>, String), /// `Note S x ~ e` - Note(S, Box<Expr<'i, S, A>>), + Note(S, Box<Expr<S, A>>), /// `Embed path ~ path` Embed(A), } @@ -242,50 +242,52 @@ pub enum Builtin { TextShow, } -impl<'i> From<&'i str> for V<'i> { - fn from(s: &'i str) -> Self { +impl<'i> From<String> for V { + fn from(s: String) -> Self { V(s, 0) } } -impl<'i, S, A> From<&'i str> for Expr<'i, S, A> { - fn from(s: &'i str) -> Self { +impl<'i, S, A> From<String> for Expr<S, A> { + fn from(s: String) -> Self { Expr::Var(s.into()) } } -impl<'i, S, A> From<Builtin> for Expr<'i, S, A> { +impl<'i, S, A> From<Builtin> for Expr<S, A> { fn from(t: Builtin) -> Self { Expr::Builtin(t) } } -impl<'i, S, A> Expr<'i, S, A> { +impl<'i, S, A> Expr<S, A> { pub fn map_shallow<T, B, F1, F2, F3>( &self, map_expr: F1, map_note: F2, map_embed: F3, - ) -> Expr<'i, T, B> + ) -> Expr<T, B> where A: Clone, T: Clone, S: Clone, - F1: Fn(&Self) -> Expr<'i, T, B>, + F1: Fn(&Self) -> Expr<T, B>, F2: FnOnce(&S) -> T, F3: FnOnce(&A) -> B, { map_shallow(self, map_expr, map_note, map_embed) } - pub fn map_embed<B, F>(&self, map_embed: &F) -> Expr<'i, S, B> + pub fn map_embed<B, F>( + &self, + map_embed: &F, + ) -> Expr<S, B> where A: Clone, S: Clone, F: Fn(&A) -> B, { - let recurse = - |e: &Expr<'i, S, A>| -> Expr<'i, S, B> { e.map_embed(map_embed) }; + let recurse = |e: &Expr<S, A>| -> Expr<S, B> { e.map_embed(map_embed) }; map_shallow(self, recurse, |x| x.clone(), map_embed) } @@ -321,7 +323,7 @@ impl<'i, S, A> Expr<'i, S, A> { // you add a new constructor to the syntax tree without adding a matching // case the corresponding builder. -impl<'i, S, A: Display> Display for Expr<'i, S, A> { +impl<'i, S, A: Display> Display for Expr<S, A> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { // buildExprA use crate::Expr::*; @@ -337,11 +339,11 @@ impl<'i, S, A: Display> Display for Expr<'i, S, A> { } } -impl<'i, S, A: Display> Expr<'i, S, A> { +impl<'i, S, A: Display> Expr<S, A> { fn fmt_b(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { use crate::Expr::*; match self { - &Lam(a, ref b, ref c) => { + &Lam(ref a, ref b, ref c) => { write!(f, "λ({} : ", a)?; b.fmt(f)?; write!(f, ") → ")?; @@ -355,24 +357,24 @@ impl<'i, S, A: Display> Expr<'i, S, A> { write!(f, " else ")?; c.fmt_c(f) } - &Pi("_", ref b, ref c) => { + &Pi(ref a, ref b, ref c) if a == "_" => { b.fmt_c(f)?; write!(f, " → ")?; c.fmt_b(f) } - &Pi(a, ref b, ref c) => { + &Pi(ref a, ref b, ref c) => { write!(f, "∀({} : ", a)?; b.fmt(f)?; write!(f, ") → ")?; c.fmt_b(f) } - &Let(a, None, ref c, ref d) => { + &Let(ref a, None, ref c, ref d) => { write!(f, "let {} = ", a)?; c.fmt(f)?; write!(f, ") → ")?; d.fmt_b(f) } - &Let(a, Some(ref b), ref c, ref d) => { + &Let(ref a, Some(ref b), ref c, ref d) => { write!(f, "let {} : ", a)?; b.fmt(f)?; write!(f, " = ")?; @@ -487,7 +489,7 @@ impl<'i, S, A: Display> Expr<'i, S, A> { fn fmt_e(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { use crate::Expr::*; match self { - &Field(ref a, b) => { + &Field(ref a, ref b) => { a.fmt_e(f)?; write!(f, ".{}", b) } @@ -520,7 +522,7 @@ impl<'i, S, A: Display> Expr<'i, S, A> { write!(f, "{} = {}", k, v) }), &Union(ref _a) => f.write_str("Union"), - &UnionLit(_a, ref _b, ref _c) => f.write_str("UnionLit"), + &UnionLit(ref _a, ref _b, ref _c) => f.write_str("UnionLit"), &Embed(ref a) => a.fmt(f), &Note(_, ref b) => b.fmt_f(f), a => write!(f, "({})", a), @@ -632,10 +634,10 @@ impl Builtin { } } -impl<'i> Display for V<'i> { +impl<'i> Display for V { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { let V(x, n) = *self; - f.write_str(x)?; + f.write_str(&x)?; if n != 0 { write!(f, "@{}", n)?; } @@ -647,19 +649,19 @@ pub fn pi<'i, S, A, Name, Et, Ev>( var: Name, ty: Et, value: Ev, -) -> Expr<'i, S, A> +) -> Expr<S, A> where - Name: Into<&'i str>, - Et: Into<Expr<'i, S, A>>, - Ev: Into<Expr<'i, S, A>>, + Name: Into<String>, + Et: Into<Expr<S, A>>, + Ev: Into<Expr<S, A>>, { Expr::Pi(var.into(), bx(ty.into()), bx(value.into())) } -pub fn app<'i, S, A, Ef, Ex>(f: Ef, x: Ex) -> Expr<'i, S, A> +pub fn app<'i, S, A, Ef, Ex>(f: Ef, x: Ex) -> Expr<S, A> where - Ef: Into<Expr<'i, S, A>>, - Ex: Into<Expr<'i, S, A>>, + Ef: Into<Expr<S, A>>, + Ex: Into<Expr<S, A>>, { Expr::App(bx(f.into()), bx(x.into())) } @@ -693,29 +695,29 @@ fn add_ui(u: usize, i: isize) -> usize { } pub fn map_shallow<'i, S, T, A, B, F1, F2, F3>( - e: &Expr<'i, S, A>, + e: &Expr<S, A>, map: F1, map_note: F2, map_embed: F3, -) -> Expr<'i, T, B> +) -> Expr<T, B> where A: Clone, S: Clone, T: Clone, - F1: Fn(&Expr<'i, S, A>) -> Expr<'i, T, B>, + F1: Fn(&Expr<S, A>) -> Expr<T, B>, F2: FnOnce(&S) -> T, F3: FnOnce(&A) -> B, { use crate::Expr::*; - let bxmap = |x: &Expr<'i, S, A>| -> Box<Expr<'i, T, B>> { bx(map(x)) }; + let bxmap = |x: &Expr<S, A>| -> Box<Expr<T, B>> { bx(map(x)) }; let opt = |x| map_opt_box(x, &map); match *e { Const(k) => Const(k), Var(v) => Var(v), - Lam(x, ref t, ref b) => Lam(x, bxmap(t), bxmap(b)), - Pi(x, ref t, ref b) => Pi(x, bxmap(t), bxmap(b)), + Lam(ref x, ref t, ref b) => Lam(x.clone(), bxmap(t), bxmap(b)), + Pi(ref x, ref t, ref b) => Pi(x.clone(), bxmap(t), bxmap(b)), App(ref f, ref a) => App(bxmap(f), bxmap(a)), - Let(l, ref t, ref a, ref b) => Let(l, opt(t), bxmap(a), bxmap(b)), + Let(ref l, ref t, ref a, ref b) => Let(l.clone(), opt(t), bxmap(a), bxmap(b)), Annot(ref x, ref t) => Annot(bxmap(x), bxmap(t)), Builtin(v) => Builtin(v), BoolLit(b) => BoolLit(b), @@ -736,11 +738,11 @@ where Record(ref kts) => Record(map_record_value(kts, map)), RecordLit(ref kvs) => RecordLit(map_record_value(kvs, map)), Union(ref kts) => Union(map_record_value(kts, map)), - UnionLit(k, ref v, ref kvs) => { - UnionLit(k, bxmap(v), map_record_value(kvs, map)) + UnionLit(ref k, ref v, ref kvs) => { + UnionLit(k.clone(), bxmap(v), map_record_value(kvs, map)) } Merge(ref x, ref y, ref t) => Merge(bxmap(x), bxmap(y), opt(t)), - Field(ref r, x) => Field(bxmap(r), x), + Field(ref r, ref x) => Field(bxmap(r), x.clone()), Note(ref n, ref e) => Note(map_note(n), bxmap(e)), Embed(ref a) => Embed(map_embed(a)), } @@ -749,7 +751,7 @@ where pub fn map_record_value<'a, I, K, V, U, F>(it: I, mut f: F) -> BTreeMap<K, U> where I: IntoIterator<Item = (&'a K, &'a V)>, - K: Eq + Ord + Copy + 'a, + K: Eq + Ord + 'a, V: 'a, F: FnMut(&V) -> U, { @@ -845,8 +847,8 @@ where pub fn shift<'i, S, T, A: Clone>( d: isize, v: V, - e: &Expr<'i, S, A>, -) -> Expr<'i, T, A> { + e: &Expr<S, A>, +) -> Expr<T, A> { use crate::Expr::*; let V(x, n) = v; match *e { @@ -857,27 +859,27 @@ pub fn shift<'i, S, T, A: Clone>( } else { n2 }; - Var(V(x2, n3)) + Var(V(x2.clone(), n3)) } - Lam(x2, ref tA, ref b) => { - let n2 = if x == x2 { n + 1 } else { n }; + Lam(ref x2, ref tA, ref b) => { + let n2 = if x == *x2 { n + 1 } else { n }; let tA2 = shift(d, V(x, n), tA); let b2 = shift(d, V(x, n2), b); - Lam(x2, bx(tA2), bx(b2)) + Lam(x2.clone(), bx(tA2), bx(b2)) } - Pi(x2, ref tA, ref tB) => { - let n2 = if x == x2 { n + 1 } else { n }; + Pi(ref x2, ref tA, ref tB) => { + let n2 = if x == *x2 { n + 1 } else { n }; let tA2 = shift(d, V(x, n), tA); let tB2 = shift(d, V(x, n2), tB); - pi(x2, tA2, tB2) + pi(x2.clone(), tA2, tB2) } App(ref f, ref a) => app(shift(d, v, f), shift(d, v, a)), - Let(f, ref mt, ref r, ref e) => { - let n2 = if x == f { n + 1 } else { n }; + Let(ref f, ref mt, ref r, ref e) => { + let n2 = if x == *f { n + 1 } else { n }; let e2 = shift(d, V(x, n2), e); let mt2 = mt.as_ref().map(|t| bx(shift(d, V(x, n), t))); let r2 = shift(d, V(x, n), r); - Let(f, mt2, bx(r2), bx(e2)) + Let(f.clone(), mt2, bx(r2), bx(e2)) } Annot(ref a, ref b) => shift_op2(Annot, d, v, a, b), Builtin(v) => Builtin(v), @@ -903,8 +905,8 @@ pub fn shift<'i, S, T, A: Clone>( RecordLit(map_record_value(a, |val| shift(d, v, val))) } Union(ref a) => Union(map_record_value(a, |val| shift(d, v, val))), - UnionLit(k, ref uv, ref a) => UnionLit( - k, + UnionLit(ref k, ref uv, ref a) => UnionLit( + k.clone(), bx(shift(d, v, uv)), map_record_value(a, |val| shift(d, v, val)), ), @@ -913,7 +915,7 @@ pub fn shift<'i, S, T, A: Clone>( bx(shift(d, v, b)), c.as_ref().map(|c| bx(shift(d, v, c))), ), - Field(ref a, b) => Field(bx(shift(d, v, a)), b), + Field(ref a, ref b) => Field(bx(shift(d, v, a)), b.clone()), Note(_, ref b) => shift(d, v, b), // The Dhall compiler enforces that all embedded values are closed expressions // and `shift` does nothing to a closed expression @@ -925,11 +927,11 @@ fn shift_op2<'i, S, T, A, F>( f: F, d: isize, v: V, - a: &Expr<'i, S, A>, - b: &Expr<'i, S, A>, -) -> Expr<'i, T, A> + a: &Expr<S, A>, + b: &Expr<S, A>, +) -> Expr<T, A> where - F: FnOnce(Box<Expr<'i, T, A>>, Box<Expr<'i, T, A>>) -> Expr<'i, T, A>, + F: FnOnce(Box<Expr<T, A>>, Box<Expr<T, A>>) -> Expr<T, A>, A: Clone, { map_op2(f, |x| bx(shift(d, v, x)), a, b) @@ -942,10 +944,10 @@ where /// ``` /// pub fn subst<'i, S, T, A>( - v: V<'i>, - e: &Expr<'i, S, A>, - b: &Expr<'i, T, A>, -) -> Expr<'i, S, A> + v: &V, + e: &Expr<S, A>, + b: &Expr<T, A>, +) -> Expr<S, A> where S: Clone, A: Clone, @@ -954,17 +956,17 @@ where let V(x, n) = v; match *b { Const(a) => Const(a), - Lam(y, ref tA, ref b) => { - let n2 = if x == y { n + 1 } else { n }; - let b2 = subst(V(x, n2), &shift(1, V(y, 0), e), b); - let tA2 = subst(V(x, n), e, tA); - Lam(y, bx(tA2), bx(b2)) + Lam(ref y, ref tA, ref b) => { + let n2 = if x == *y { n + 1 } else { n }; + let b2 = subst(&V(x.clone(), n2), &shift(1, &V(y.clone(), 0), e), b); + let tA2 = subst(v, e, tA); + Lam(y.clone(), bx(tA2), bx(b2)) } - Pi(y, ref tA, ref tB) => { - let n2 = if x == y { n + 1 } else { n }; - let tB2 = subst(V(x, n2), &shift(1, V(y, 0), e), tB); - let tA2 = subst(V(x, n), e, tA); - pi(y, tA2, tB2) + Pi(ref y, ref tA, ref tB) => { + let n2 = if x == *y { n + 1 } else { n }; + let tB2 = subst(&V(x.clone(), n2), &shift(1, &V(y.clone(), 0), e), tB); + let tA2 = subst(v, e, tA); + pi(y.clone(), tA2, tB2) } App(ref f, ref a) => { let f2 = subst(v, e, f); @@ -978,12 +980,12 @@ where Var(v2) } } - Let(f, ref mt, ref r, ref b) => { - let n2 = if x == f { n + 1 } else { n }; - let b2 = subst(V(x, n2), &shift(1, V(f, 0), e), b); - let mt2 = mt.as_ref().map(|t| bx(subst(V(x, n), e, t))); - let r2 = subst(V(x, n), e, r); - Let(f, mt2, bx(r2), bx(b2)) + Let(ref f, ref mt, ref r, ref b) => { + let n2 = if x == *f { n + 1 } else { n }; + let b2 = subst(&V(x.clone(), n2), &shift(1, &V(f.clone(), 0), e), b); + let mt2 = mt.as_ref().map(|t| bx(subst(v, e, t))); + let r2 = subst(v, e, r); + Let(f.clone(), mt2, bx(r2), bx(b2)) } Annot(ref a, ref b) => subst_op2(Annot, v, e, a, b), Builtin(v) => Builtin(v), @@ -1011,8 +1013,8 @@ where RecordLit(map_record_value(kvs, |val| subst(v, e, val))) } Union(ref kts) => Union(map_record_value(kts, |t| subst(v, e, t))), - UnionLit(k, ref uv, ref kvs) => UnionLit( - k, + UnionLit(ref k, ref uv, ref kvs) => UnionLit( + k.clone(), bx(subst(v, e, uv)), map_record_value(kvs, |val| subst(v, e, val)), ), @@ -1021,7 +1023,7 @@ where bx(subst(v, e, b)), c.as_ref().map(|c| bx(subst(v, e, c))), ), - Field(ref a, b) => Field(bx(subst(v, e, a)), b), + Field(ref a, ref b) => Field(bx(subst(v, e, a)), b.clone()), Note(_, ref b) => subst(v, e, b), Embed(ref p) => Embed(p.clone()), } @@ -1029,13 +1031,13 @@ where fn subst_op2<'i, S, T, A, F>( f: F, - v: V<'i>, - e: &Expr<'i, S, A>, - a: &Expr<'i, T, A>, - b: &Expr<'i, T, A>, -) -> Expr<'i, S, A> + v: &V, + e: &Expr<S, A>, + a: &Expr<T, A>, + b: &Expr<T, A>, +) -> Expr<S, A> where - F: FnOnce(Box<Expr<'i, S, A>>, Box<Expr<'i, S, A>>) -> Expr<'i, S, A>, + F: FnOnce(Box<Expr<S, A>>, Box<Expr<S, A>>) -> Expr<S, A>, S: Clone, A: Clone, { diff --git a/dhall_core/src/grammar_util.rs b/dhall_core/src/grammar_util.rs index 73f935f..fa3d007 100644 --- a/dhall_core/src/grammar_util.rs +++ b/dhall_core/src/grammar_util.rs @@ -1,4 +1,4 @@ use crate::core::{Expr, Import, X}; -pub type ParsedExpr<'i> = Expr<'i, X, Import>; -pub type BoxExpr<'i> = Box<ParsedExpr<'i>>; +pub type ParsedExpr = Expr<X, Import>; +pub type BoxExpr = Box<ParsedExpr>; diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs index 34327b1..ad9e52b 100644 --- a/dhall_core/src/parser.rs +++ b/dhall_core/src/parser.rs @@ -46,7 +46,7 @@ fn debug_pair(pair: Pair<Rule>) -> String { .unwrap(); } } - aux(s, indent + 1, "".into(), p); + aux(s, indent + 1, "".to_owned(), p); } if first { writeln!( @@ -57,7 +57,7 @@ fn debug_pair(pair: Pair<Rule>) -> String { .unwrap(); } } - aux(&mut s, 0, "".into(), pair); + aux(&mut s, 0, "".to_owned(), pair); s } @@ -416,6 +416,8 @@ rule!(EOI<()>; children!() => ()); named!(str<&'a str>; captured_str!(s) => s.trim()); +named!(string<String>; captured_str!(s) => s.trim().to_owned()); + named!(raw_str<&'a str>; captured_str!(s) => s); // TODO: parse escapes and interpolation @@ -535,7 +537,7 @@ rule!(import_hashed_raw<(ImportLocation, Option<()>)>; } ); -rule!(import_raw<BoxExpr<'a>>; +rule!(import_raw<BoxExpr>; // TODO: handle "as Text" children!(import: import_hashed_raw) => { let (location, hash) = import; @@ -547,7 +549,7 @@ rule!(import_raw<BoxExpr<'a>>; } ); -rule_group!(expression<BoxExpr<'a>>; +rule_group!(expression<BoxExpr>; identifier_raw, lambda_expression, ifthenelse_expression, @@ -584,47 +586,47 @@ rule_group!(expression<BoxExpr<'a>>; final_expression ); -rule!(lambda_expression<BoxExpr<'a>>; - children!(label: str, typ: expression, body: expression) => { +rule!(lambda_expression<BoxExpr>; + children!(label: string, typ: expression, body: expression) => { bx(Expr::Lam(label, typ, body)) } ); -rule!(ifthenelse_expression<BoxExpr<'a>>; +rule!(ifthenelse_expression<BoxExpr>; children!(cond: expression, left: expression, right: expression) => { bx(Expr::BoolIf(cond, left, right)) } ); -rule!(let_expression<BoxExpr<'a>>; +rule!(let_expression<BoxExpr>; 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<(&'a str, Option<BoxExpr<'a>>, BoxExpr<'a>)>; - children!(name: str, annot?: expression, expr: expression) => (name, annot, expr) +rule!(let_binding<(String, Option<BoxExpr>, BoxExpr)>; + children!(name: string, annot?: expression, expr: expression) => (name, annot, expr) ); -rule!(forall_expression<BoxExpr<'a>>; - children!(label: str, typ: expression, body: expression) => { +rule!(forall_expression<BoxExpr>; + children!(label: string, typ: expression, body: expression) => { bx(Expr::Pi(label, typ, body)) } ); -rule!(arrow_expression<BoxExpr<'a>>; +rule!(arrow_expression<BoxExpr>; children!(typ: expression, body: expression) => { - bx(Expr::Pi("_", typ, body)) + bx(Expr::Pi("_".to_owned(), typ, body)) } ); -rule!(merge_expression<BoxExpr<'a>>; +rule!(merge_expression<BoxExpr>; children!(x: expression, y: expression, z?: expression) => { bx(Expr::Merge(x, y, z)) } ); -rule!(empty_collection<BoxExpr<'a>>; +rule!(empty_collection<BoxExpr>; children!(x: str, y: expression) => { match x { "Optional" => bx(Expr::OptionalLit(Some(y), vec![])), @@ -634,7 +636,7 @@ rule!(empty_collection<BoxExpr<'a>>; } ); -rule!(non_empty_optional<BoxExpr<'a>>; +rule!(non_empty_optional<BoxExpr>; children!(x: expression, _y: str, z: expression) => { bx(Expr::OptionalLit(Some(z), vec![*x])) } @@ -642,7 +644,7 @@ rule!(non_empty_optional<BoxExpr<'a>>; macro_rules! binop { ($rule:ident, $op:ident) => { - rule!($rule<BoxExpr<'a>>; + rule!($rule<BoxExpr>; children!(first: expression, rest*: expression) => { rest.fold(first, |acc, e| bx(Expr::BinOp(BinOp::$op, acc, e))) } @@ -650,7 +652,7 @@ macro_rules! binop { }; } -rule!(annotated_expression<BoxExpr<'a>>; +rule!(annotated_expression<BoxExpr>; children!(e: expression, annot: expression) => { bx(Expr::Annot(e, annot)) }, @@ -670,19 +672,19 @@ binop!(times_expression, NaturalTimes); binop!(equal_expression, BoolEQ); binop!(not_equal_expression, BoolNE); -rule!(application_expression<BoxExpr<'a>>; +rule!(application_expression<BoxExpr>; children!(first: expression, rest*: expression) => { rest.fold(first, |acc, e| bx(Expr::App(acc, e))) } ); -rule!(selector_expression_raw<BoxExpr<'a>>; - children!(first: expression, rest*: str) => { +rule!(selector_expression_raw<BoxExpr>; + children!(first: expression, rest*: string) => { rest.fold(first, |acc, e| bx(Expr::Field(acc, e))) } ); -rule!(literal_expression_raw<BoxExpr<'a>>; +rule!(literal_expression_raw<BoxExpr>; 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)), @@ -694,7 +696,7 @@ rule!(literal_expression_raw<BoxExpr<'a>>; children!(e: expression) => e, ); -rule!(identifier_raw<BoxExpr<'a>>; +rule!(identifier_raw<BoxExpr>; children!(name: str, idx?: natural_literal_raw) => { match Builtin::parse(name) { Some(b) => bx(Expr::Builtin(b)), @@ -703,40 +705,40 @@ rule!(identifier_raw<BoxExpr<'a>>; "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)))), + name => bx(Expr::Var(V(name.to_owned(), idx.unwrap_or(0)))), } } } ); -rule!(empty_record_literal<BoxExpr<'a>>; +rule!(empty_record_literal<BoxExpr>; children!() => bx(Expr::RecordLit(BTreeMap::new())) ); -rule!(empty_record_type<BoxExpr<'a>>; +rule!(empty_record_type<BoxExpr>; children!() => bx(Expr::Record(BTreeMap::new())) ); -rule!(non_empty_record_type_or_literal<BoxExpr<'a>>; - children!(first_label: str, rest: non_empty_record_type) => { +rule!(non_empty_record_type_or_literal<BoxExpr>; + children!(first_label: string, rest: non_empty_record_type) => { let (first_expr, mut map) = rest; map.insert(first_label, *first_expr); bx(Expr::Record(map)) }, - children!(first_label: str, rest: non_empty_record_literal) => { + children!(first_label: string, 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<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>; +rule!(non_empty_record_type<(BoxExpr, BTreeMap<String, ParsedExpr>)>; self!(x: partial_record_entries) => x ); -named!(partial_record_entries<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>; +named!(partial_record_entries<(BoxExpr, BTreeMap<String, ParsedExpr>)>; children!(expr: expression, entries*: record_entry) => { - let mut map: BTreeMap<&str, ParsedExpr> = BTreeMap::new(); + let mut map: BTreeMap<String, ParsedExpr> = BTreeMap::new(); for (n, e) in entries { map.insert(n, *e); } @@ -744,15 +746,15 @@ named!(partial_record_entries<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>; } ); -named!(record_entry<(&'a str, BoxExpr<'a>)>; - children!(name: str, expr: expression) => (name, expr) +named!(record_entry<(String, BoxExpr)>; + children!(name: string, expr: expression) => (name, expr) ); -rule!(non_empty_record_literal<(BoxExpr<'a>, BTreeMap<&'a str, ParsedExpr<'a>>)>; +rule!(non_empty_record_literal<(BoxExpr, BTreeMap<String, ParsedExpr>)>; self!(x: partial_record_entries) => x ); -rule!(union_type_or_literal<BoxExpr<'a>>; +rule!(union_type_or_literal<BoxExpr>; children!(_e: empty_union_type) => { bx(Expr::Union(BTreeMap::new())) }, @@ -767,25 +769,25 @@ rule!(union_type_or_literal<BoxExpr<'a>>; 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) => { + <(Option<(String, BoxExpr)>, BTreeMap<String, ParsedExpr>)>; + children!(label: string, e: expression, entries: union_type_entries) => { (Some((label, e)), entries) }, - children!(l: str, e: expression, rest: non_empty_union_type_or_literal) => { + children!(l: string, 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) => { + children!(l: string, e: expression) => { let mut entries = BTreeMap::new(); entries.insert(l, *e); (None, entries) }, ); -rule!(union_type_entries<BTreeMap<&'a str, ParsedExpr<'a>>>; +rule!(union_type_entries<BTreeMap<String, ParsedExpr>>; children!(entries*: union_type_entry) => { - let mut map: BTreeMap<&str, ParsedExpr> = BTreeMap::new(); + let mut map: BTreeMap<String, ParsedExpr> = BTreeMap::new(); for (n, e) in entries { map.insert(n, *e); } @@ -793,17 +795,17 @@ rule!(union_type_entries<BTreeMap<&'a str, ParsedExpr<'a>>>; } ); -rule!(union_type_entry<(&'a str, BoxExpr<'a>)>; - children!(name: str, expr: expression) => (name, expr) +rule!(union_type_entry<(String, BoxExpr)>; + children!(name: string, expr: expression) => (name, expr) ); -rule!(non_empty_list_literal_raw<BoxExpr<'a>>; +rule!(non_empty_list_literal_raw<BoxExpr>; children!(items*: expression) => { bx(Expr::ListLit(None, items.map(|x| *x).collect())) } ); -rule!(final_expression<BoxExpr<'a>>; +rule!(final_expression<BoxExpr>; children!(e: expression, _eoi: EOI) => e ); |