diff options
author | Nadrieril Feneanar | 2019-12-19 21:33:26 +0000 |
---|---|---|
committer | GitHub | 2019-12-19 21:33:26 +0000 |
commit | 91ef0cf697d56c91a8d15937aa4669dc221cd6c1 (patch) | |
tree | d3f00cf31d4386b82c6fb09eda3f690415dd8902 /dhall/src/syntax | |
parent | 3f00e4ca3fe22f88a1d0633e254df0bff781c6d3 (diff) | |
parent | 1e4f15d1891b497ecf6632432bc9252dc6a4507d (diff) |
Merge pull request #117 from Nadrieril/merge-crates
Merge a bunch of sub-crates
Diffstat (limited to '')
-rw-r--r-- | dhall/src/syntax/binary/decode.rs (renamed from dhall/src/phase/binary.rs) | 329 | ||||
-rw-r--r-- | dhall/src/syntax/binary/encode.rs | 319 | ||||
-rw-r--r-- | dhall/src/syntax/binary/mod.rs | 4 | ||||
-rw-r--r-- | dhall/src/syntax/core/context.rs (renamed from dhall_syntax/src/core/context.rs) | 0 | ||||
-rw-r--r-- | dhall/src/syntax/core/expr.rs (renamed from dhall_syntax/src/core/expr.rs) | 8 | ||||
-rw-r--r-- | dhall/src/syntax/core/import.rs (renamed from dhall_syntax/src/core/import.rs) | 0 | ||||
-rw-r--r-- | dhall/src/syntax/core/label.rs (renamed from dhall_syntax/src/core/label.rs) | 0 | ||||
-rw-r--r-- | dhall/src/syntax/core/map.rs (renamed from dhall_syntax/src/core/map.rs) | 0 | ||||
-rw-r--r-- | dhall/src/syntax/core/mod.rs (renamed from dhall_syntax/src/core/mod.rs) | 0 | ||||
-rw-r--r-- | dhall/src/syntax/core/span.rs (renamed from dhall_syntax/src/core/span.rs) | 0 | ||||
-rw-r--r-- | dhall/src/syntax/core/text.rs (renamed from dhall_syntax/src/core/text.rs) | 0 | ||||
-rw-r--r-- | dhall/src/syntax/core/visitor.rs (renamed from dhall_syntax/src/core/visitor.rs) | 6 | ||||
-rw-r--r-- | dhall/src/syntax/mod.rs | 15 | ||||
-rw-r--r-- | dhall/src/syntax/text/mod.rs | 2 | ||||
-rw-r--r-- | dhall/src/syntax/text/parser.rs (renamed from dhall_syntax/src/parser.rs) | 180 | ||||
-rw-r--r-- | dhall/src/syntax/text/printer.rs (renamed from dhall_syntax/src/printer.rs) | 14 |
16 files changed, 442 insertions, 435 deletions
diff --git a/dhall/src/phase/binary.rs b/dhall/src/syntax/binary/decode.rs index 0639120..46c9921 100644 --- a/dhall/src/phase/binary.rs +++ b/dhall/src/syntax/binary/decode.rs @@ -1,18 +1,15 @@ use itertools::Itertools; use serde_cbor::value::value as cbor; use std::iter::FromIterator; -use std::vec; -use dhall_syntax::map::DupTreeMap; -use dhall_syntax::{ - Expr, ExprF, FilePath, FilePrefix, Hash, Import, ImportLocation, - ImportMode, Integer, InterpolatedText, Label, Natural, RawExpr, Scheme, - Span, URL, V, +use crate::semantics::error::DecodeError; +use crate::semantics::phase::DecodedExpr; +use crate::syntax; +use crate::syntax::{ + Expr, ExprF, FilePath, FilePrefix, Hash, ImportLocation, ImportMode, + Integer, InterpolatedText, Label, Natural, RawExpr, Scheme, Span, URL, V, }; -use crate::error::{DecodeError, EncodeError}; -use crate::phase::DecodedExpr; - pub(crate) fn decode(data: &[u8]) -> Result<DecodedExpr, DecodeError> { match serde_cbor::de::from_slice(data) { Ok(v) => cbor_value_to_dhall(&v), @@ -20,19 +17,14 @@ pub(crate) fn decode(data: &[u8]) -> Result<DecodedExpr, DecodeError> { } } -pub(crate) fn encode<E>(expr: &Expr<E>) -> Result<Vec<u8>, EncodeError> { - serde_cbor::ser::to_vec(&Serialize::Expr(expr)) - .map_err(|e| EncodeError::CBORError(e)) -} - // Should probably rename this -pub fn rc<E>(x: RawExpr<E>) -> Expr<E> { +fn rc<E>(x: RawExpr<E>) -> Expr<E> { Expr::new(x, Span::Decoded) } fn cbor_value_to_dhall(data: &cbor::Value) -> Result<DecodedExpr, DecodeError> { use cbor::Value::*; - use dhall_syntax::{BinOp, Builtin, Const}; + use syntax::{BinOp, Builtin, Const}; use ExprF::*; Ok(rc(match data { String(s) => match Builtin::parse(s) { @@ -350,7 +342,7 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result<DecodedExpr, DecodeError> { "import/type".to_owned(), ))?, }; - Import(dhall_syntax::Import { + Import(syntax::Import { mode, hash, location, @@ -443,306 +435,3 @@ where }) .collect::<Result<_, _>>() } - -enum Serialize<'a, E> { - Expr(&'a Expr<E>), - CBOR(cbor::Value), - RecordMap(&'a DupTreeMap<Label, Expr<E>>), - UnionMap(&'a DupTreeMap<Label, Option<Expr<E>>>), -} - -macro_rules! count { - (@replace_with $_t:tt $sub:expr) => { $sub }; - ($($tts:tt)*) => {0usize $(+ count!(@replace_with $tts 1usize))*}; -} - -macro_rules! ser_seq { - ($ser:expr; $($elt:expr),* $(,)?) => {{ - use serde::ser::SerializeSeq; - let count = count!($($elt)*); - let mut ser_seq = $ser.serialize_seq(Some(count))?; - $( - ser_seq.serialize_element(&$elt)?; - )* - ser_seq.end() - }}; -} - -fn serialize_subexpr<S, E>(ser: S, e: &Expr<E>) -> Result<S::Ok, S::Error> -where - S: serde::ser::Serializer, -{ - use cbor::Value::{String, I64, U64}; - use dhall_syntax::Builtin; - use dhall_syntax::ExprF::*; - use std::iter::once; - - use self::Serialize::{RecordMap, UnionMap}; - fn expr<E>(x: &Expr<E>) -> self::Serialize<'_, E> { - self::Serialize::Expr(x) - } - let cbor = - |v: cbor::Value| -> self::Serialize<'_, E> { self::Serialize::CBOR(v) }; - let tag = |x: u64| cbor(U64(x)); - let null = || cbor(cbor::Value::Null); - let label = |l: &Label| cbor(cbor::Value::String(l.into())); - - match e.as_ref() { - Const(c) => ser.serialize_str(&c.to_string()), - Builtin(b) => ser.serialize_str(&b.to_string()), - BoolLit(b) => ser.serialize_bool(*b), - NaturalLit(n) => ser_seq!(ser; tag(15), U64(*n as u64)), - IntegerLit(n) => ser_seq!(ser; tag(16), I64(*n as i64)), - DoubleLit(n) => { - let n: f64 = (*n).into(); - ser.serialize_f64(n) - } - BoolIf(x, y, z) => ser_seq!(ser; tag(14), expr(x), expr(y), expr(z)), - Var(V(l, n)) if l == &"_".into() => ser.serialize_u64(*n as u64), - Var(V(l, n)) => ser_seq!(ser; label(l), U64(*n as u64)), - Lam(l, x, y) if l == &"_".into() => { - ser_seq!(ser; tag(1), expr(x), expr(y)) - } - Lam(l, x, y) => ser_seq!(ser; tag(1), label(l), expr(x), expr(y)), - Pi(l, x, y) if l == &"_".into() => { - ser_seq!(ser; tag(2), expr(x), expr(y)) - } - Pi(l, x, y) => ser_seq!(ser; tag(2), label(l), expr(x), expr(y)), - Let(_, _, _, _) => { - let (bound_e, bindings) = collect_nested_lets(e); - let count = 1 + 3 * bindings.len() + 1; - - use serde::ser::SerializeSeq; - let mut ser_seq = ser.serialize_seq(Some(count))?; - ser_seq.serialize_element(&tag(25))?; - for (l, t, v) in bindings { - ser_seq.serialize_element(&label(l))?; - match t { - Some(t) => ser_seq.serialize_element(&expr(t))?, - None => ser_seq.serialize_element(&null())?, - } - ser_seq.serialize_element(&expr(v))?; - } - ser_seq.serialize_element(&expr(bound_e))?; - ser_seq.end() - } - App(_, _) => { - let (f, args) = collect_nested_applications(e); - ser.collect_seq( - once(tag(0)) - .chain(once(expr(f))) - .chain(args.into_iter().rev().map(expr)), - ) - } - Annot(x, y) => ser_seq!(ser; tag(26), expr(x), expr(y)), - Assert(x) => ser_seq!(ser; tag(19), expr(x)), - SomeLit(x) => ser_seq!(ser; tag(5), null(), expr(x)), - EmptyListLit(x) => match x.as_ref() { - App(f, a) => match f.as_ref() { - ExprF::Builtin(Builtin::List) => ser_seq!(ser; tag(4), expr(a)), - _ => ser_seq!(ser; tag(28), expr(x)), - }, - _ => ser_seq!(ser; tag(28), expr(x)), - }, - NEListLit(xs) => ser.collect_seq( - once(tag(4)).chain(once(null())).chain(xs.iter().map(expr)), - ), - TextLit(xs) => { - use dhall_syntax::InterpolatedTextContents::{Expr, Text}; - ser.collect_seq(once(tag(18)).chain(xs.iter().map(|x| match x { - Expr(x) => expr(x), - Text(x) => cbor(String(x.clone())), - }))) - } - RecordType(map) => ser_seq!(ser; tag(7), RecordMap(map)), - RecordLit(map) => ser_seq!(ser; tag(8), RecordMap(map)), - UnionType(map) => ser_seq!(ser; tag(11), UnionMap(map)), - Field(x, l) => ser_seq!(ser; tag(9), expr(x), label(l)), - BinOp(op, x, y) => { - use dhall_syntax::BinOp::*; - let op = match op { - BoolOr => 0, - BoolAnd => 1, - BoolEQ => 2, - BoolNE => 3, - NaturalPlus => 4, - NaturalTimes => 5, - TextAppend => 6, - ListAppend => 7, - RecursiveRecordMerge => 8, - RightBiasedRecordMerge => 9, - RecursiveRecordTypeMerge => 10, - ImportAlt => 11, - Equivalence => 12, - }; - ser_seq!(ser; tag(3), U64(op), expr(x), expr(y)) - } - Merge(x, y, None) => ser_seq!(ser; tag(6), expr(x), expr(y)), - Merge(x, y, Some(z)) => { - ser_seq!(ser; tag(6), expr(x), expr(y), expr(z)) - } - ToMap(x, None) => ser_seq!(ser; tag(27), expr(x)), - ToMap(x, Some(y)) => ser_seq!(ser; tag(27), expr(x), expr(y)), - Projection(x, ls) => ser.collect_seq( - once(tag(10)) - .chain(once(expr(x))) - .chain(ls.iter().map(label)), - ), - ProjectionByExpr(x, y) => { - ser_seq!(ser; tag(10), expr(x), vec![expr(y)]) - } - Import(import) => serialize_import(ser, import), - Embed(_) => unimplemented!( - "An expression with resolved imports cannot be binary-encoded" - ), - } -} - -fn serialize_import<S, E>( - ser: S, - import: &Import<Expr<E>>, -) -> Result<S::Ok, S::Error> -where - S: serde::ser::Serializer, -{ - use cbor::Value::{Bytes, Null, U64}; - use serde::ser::SerializeSeq; - - let count = 4 + match &import.location { - ImportLocation::Remote(url) => 3 + url.path.file_path.len(), - ImportLocation::Local(_, path) => path.file_path.len(), - ImportLocation::Env(_) => 1, - ImportLocation::Missing => 0, - }; - let mut ser_seq = ser.serialize_seq(Some(count))?; - - ser_seq.serialize_element(&U64(24))?; - - let hash = match &import.hash { - None => Null, - Some(Hash::SHA256(h)) => { - let mut bytes = vec![18, 32]; - bytes.extend_from_slice(h); - Bytes(bytes) - } - }; - ser_seq.serialize_element(&hash)?; - - let mode = match import.mode { - ImportMode::Code => 0, - ImportMode::RawText => 1, - ImportMode::Location => 2, - }; - ser_seq.serialize_element(&U64(mode))?; - - let scheme = match &import.location { - ImportLocation::Remote(url) => match url.scheme { - Scheme::HTTP => 0, - Scheme::HTTPS => 1, - }, - ImportLocation::Local(prefix, _) => match prefix { - FilePrefix::Absolute => 2, - FilePrefix::Here => 3, - FilePrefix::Parent => 4, - FilePrefix::Home => 5, - }, - ImportLocation::Env(_) => 6, - ImportLocation::Missing => 7, - }; - ser_seq.serialize_element(&U64(scheme))?; - - match &import.location { - ImportLocation::Remote(url) => { - match &url.headers { - None => ser_seq.serialize_element(&Null)?, - Some(e) => { - ser_seq.serialize_element(&self::Serialize::Expr(e))? - } - }; - ser_seq.serialize_element(&url.authority)?; - for p in url.path.file_path.iter() { - ser_seq.serialize_element(&p)?; - } - match &url.query { - None => ser_seq.serialize_element(&Null)?, - Some(x) => ser_seq.serialize_element(x)?, - }; - } - ImportLocation::Local(_, path) => { - for p in path.file_path.iter() { - ser_seq.serialize_element(&p)?; - } - } - ImportLocation::Env(env) => { - ser_seq.serialize_element(env)?; - } - ImportLocation::Missing => {} - } - - ser_seq.end() -} - -impl<'a, E> serde::ser::Serialize for Serialize<'a, E> { - fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error> - where - S: serde::ser::Serializer, - { - match self { - Serialize::Expr(e) => serialize_subexpr(ser, e), - Serialize::CBOR(v) => v.serialize(ser), - Serialize::RecordMap(map) => { - ser.collect_map(map.iter().map(|(k, v)| { - (cbor::Value::String(k.into()), Serialize::Expr(v)) - })) - } - Serialize::UnionMap(map) => { - ser.collect_map(map.iter().map(|(k, v)| { - let v = match v { - Some(x) => Serialize::Expr(x), - None => Serialize::CBOR(cbor::Value::Null), - }; - (cbor::Value::String(k.into()), v) - })) - } - } - } -} - -fn collect_nested_applications<'a, E>( - e: &'a Expr<E>, -) -> (&'a Expr<E>, Vec<&'a Expr<E>>) { - fn go<'a, E>(e: &'a Expr<E>, vec: &mut Vec<&'a Expr<E>>) -> &'a Expr<E> { - match e.as_ref() { - ExprF::App(f, a) => { - vec.push(a); - go(f, vec) - } - _ => e, - } - } - let mut vec = vec![]; - let e = go(e, &mut vec); - (e, vec) -} - -type LetBinding<'a, E> = (&'a Label, &'a Option<Expr<E>>, &'a Expr<E>); - -fn collect_nested_lets<'a, E>( - e: &'a Expr<E>, -) -> (&'a Expr<E>, Vec<LetBinding<'a, E>>) { - fn go<'a, E>( - e: &'a Expr<E>, - vec: &mut Vec<LetBinding<'a, E>>, - ) -> &'a Expr<E> { - match e.as_ref() { - ExprF::Let(l, t, v, e) => { - vec.push((l, t, v)); - go(e, vec) - } - _ => e, - } - } - let mut vec = vec![]; - let e = go(e, &mut vec); - (e, vec) -} diff --git a/dhall/src/syntax/binary/encode.rs b/dhall/src/syntax/binary/encode.rs new file mode 100644 index 0000000..8e13efd --- /dev/null +++ b/dhall/src/syntax/binary/encode.rs @@ -0,0 +1,319 @@ +use serde_cbor::value::value as cbor; +use std::vec; + +use crate::semantics::error::EncodeError; +use crate::syntax; +use crate::syntax::map::DupTreeMap; +use crate::syntax::{ + Expr, ExprF, FilePrefix, Hash, Import, ImportLocation, ImportMode, Label, + Scheme, V, +}; + +/// Warning: will fail if `expr` contains an `Embed` node. +pub(crate) fn encode<E>(expr: &Expr<E>) -> Result<Vec<u8>, EncodeError> { + serde_cbor::ser::to_vec(&Serialize::Expr(expr)) + .map_err(|e| EncodeError::CBORError(e)) +} + +enum Serialize<'a, E> { + Expr(&'a Expr<E>), + CBOR(cbor::Value), + RecordMap(&'a DupTreeMap<Label, Expr<E>>), + UnionMap(&'a DupTreeMap<Label, Option<Expr<E>>>), +} + +macro_rules! count { + (@replace_with $_t:tt $sub:expr) => { $sub }; + ($($tts:tt)*) => {0usize $(+ count!(@replace_with $tts 1usize))*}; +} + +macro_rules! ser_seq { + ($ser:expr; $($elt:expr),* $(,)?) => {{ + use serde::ser::SerializeSeq; + let count = count!($($elt)*); + let mut ser_seq = $ser.serialize_seq(Some(count))?; + $( + ser_seq.serialize_element(&$elt)?; + )* + ser_seq.end() + }}; +} + +fn serialize_subexpr<S, E>(ser: S, e: &Expr<E>) -> Result<S::Ok, S::Error> +where + S: serde::ser::Serializer, +{ + use cbor::Value::{String, I64, U64}; + use std::iter::once; + use syntax::Builtin; + use syntax::ExprF::*; + + use self::Serialize::{RecordMap, UnionMap}; + fn expr<E>(x: &Expr<E>) -> self::Serialize<'_, E> { + self::Serialize::Expr(x) + } + let cbor = + |v: cbor::Value| -> self::Serialize<'_, E> { self::Serialize::CBOR(v) }; + let tag = |x: u64| cbor(U64(x)); + let null = || cbor(cbor::Value::Null); + let label = |l: &Label| cbor(cbor::Value::String(l.into())); + + match e.as_ref() { + Const(c) => ser.serialize_str(&c.to_string()), + Builtin(b) => ser.serialize_str(&b.to_string()), + BoolLit(b) => ser.serialize_bool(*b), + NaturalLit(n) => ser_seq!(ser; tag(15), U64(*n as u64)), + IntegerLit(n) => ser_seq!(ser; tag(16), I64(*n as i64)), + DoubleLit(n) => { + let n: f64 = (*n).into(); + ser.serialize_f64(n) + } + BoolIf(x, y, z) => ser_seq!(ser; tag(14), expr(x), expr(y), expr(z)), + Var(V(l, n)) if l == &"_".into() => ser.serialize_u64(*n as u64), + Var(V(l, n)) => ser_seq!(ser; label(l), U64(*n as u64)), + Lam(l, x, y) if l == &"_".into() => { + ser_seq!(ser; tag(1), expr(x), expr(y)) + } + Lam(l, x, y) => ser_seq!(ser; tag(1), label(l), expr(x), expr(y)), + Pi(l, x, y) if l == &"_".into() => { + ser_seq!(ser; tag(2), expr(x), expr(y)) + } + Pi(l, x, y) => ser_seq!(ser; tag(2), label(l), expr(x), expr(y)), + Let(_, _, _, _) => { + let (bound_e, bindings) = collect_nested_lets(e); + let count = 1 + 3 * bindings.len() + 1; + + use serde::ser::SerializeSeq; + let mut ser_seq = ser.serialize_seq(Some(count))?; + ser_seq.serialize_element(&tag(25))?; + for (l, t, v) in bindings { + ser_seq.serialize_element(&label(l))?; + match t { + Some(t) => ser_seq.serialize_element(&expr(t))?, + None => ser_seq.serialize_element(&null())?, + } + ser_seq.serialize_element(&expr(v))?; + } + ser_seq.serialize_element(&expr(bound_e))?; + ser_seq.end() + } + App(_, _) => { + let (f, args) = collect_nested_applications(e); + ser.collect_seq( + once(tag(0)) + .chain(once(expr(f))) + .chain(args.into_iter().rev().map(expr)), + ) + } + Annot(x, y) => ser_seq!(ser; tag(26), expr(x), expr(y)), + Assert(x) => ser_seq!(ser; tag(19), expr(x)), + SomeLit(x) => ser_seq!(ser; tag(5), null(), expr(x)), + EmptyListLit(x) => match x.as_ref() { + App(f, a) => match f.as_ref() { + ExprF::Builtin(Builtin::List) => ser_seq!(ser; tag(4), expr(a)), + _ => ser_seq!(ser; tag(28), expr(x)), + }, + _ => ser_seq!(ser; tag(28), expr(x)), + }, + NEListLit(xs) => ser.collect_seq( + once(tag(4)).chain(once(null())).chain(xs.iter().map(expr)), + ), + TextLit(xs) => { + use syntax::InterpolatedTextContents::{Expr, Text}; + ser.collect_seq(once(tag(18)).chain(xs.iter().map(|x| match x { + Expr(x) => expr(x), + Text(x) => cbor(String(x.clone())), + }))) + } + RecordType(map) => ser_seq!(ser; tag(7), RecordMap(map)), + RecordLit(map) => ser_seq!(ser; tag(8), RecordMap(map)), + UnionType(map) => ser_seq!(ser; tag(11), UnionMap(map)), + Field(x, l) => ser_seq!(ser; tag(9), expr(x), label(l)), + BinOp(op, x, y) => { + use syntax::BinOp::*; + let op = match op { + BoolOr => 0, + BoolAnd => 1, + BoolEQ => 2, + BoolNE => 3, + NaturalPlus => 4, + NaturalTimes => 5, + TextAppend => 6, + ListAppend => 7, + RecursiveRecordMerge => 8, + RightBiasedRecordMerge => 9, + RecursiveRecordTypeMerge => 10, + ImportAlt => 11, + Equivalence => 12, + }; + ser_seq!(ser; tag(3), U64(op), expr(x), expr(y)) + } + Merge(x, y, None) => ser_seq!(ser; tag(6), expr(x), expr(y)), + Merge(x, y, Some(z)) => { + ser_seq!(ser; tag(6), expr(x), expr(y), expr(z)) + } + ToMap(x, None) => ser_seq!(ser; tag(27), expr(x)), + ToMap(x, Some(y)) => ser_seq!(ser; tag(27), expr(x), expr(y)), + Projection(x, ls) => ser.collect_seq( + once(tag(10)) + .chain(once(expr(x))) + .chain(ls.iter().map(label)), + ), + ProjectionByExpr(x, y) => { + ser_seq!(ser; tag(10), expr(x), vec![expr(y)]) + } + Import(import) => serialize_import(ser, import), + Embed(_) => unimplemented!( + "An expression with resolved imports cannot be binary-encoded" + ), + } +} + +fn serialize_import<S, E>( + ser: S, + import: &Import<Expr<E>>, +) -> Result<S::Ok, S::Error> +where + S: serde::ser::Serializer, +{ + use cbor::Value::{Bytes, Null, U64}; + use serde::ser::SerializeSeq; + + let count = 4 + match &import.location { + ImportLocation::Remote(url) => 3 + url.path.file_path.len(), + ImportLocation::Local(_, path) => path.file_path.len(), + ImportLocation::Env(_) => 1, + ImportLocation::Missing => 0, + }; + let mut ser_seq = ser.serialize_seq(Some(count))?; + + ser_seq.serialize_element(&U64(24))?; + + let hash = match &import.hash { + None => Null, + Some(Hash::SHA256(h)) => { + let mut bytes = vec![18, 32]; + bytes.extend_from_slice(h); + Bytes(bytes) + } + }; + ser_seq.serialize_element(&hash)?; + + let mode = match import.mode { + ImportMode::Code => 0, + ImportMode::RawText => 1, + ImportMode::Location => 2, + }; + ser_seq.serialize_element(&U64(mode))?; + + let scheme = match &import.location { + ImportLocation::Remote(url) => match url.scheme { + Scheme::HTTP => 0, + Scheme::HTTPS => 1, + }, + ImportLocation::Local(prefix, _) => match prefix { + FilePrefix::Absolute => 2, + FilePrefix::Here => 3, + FilePrefix::Parent => 4, + FilePrefix::Home => 5, + }, + ImportLocation::Env(_) => 6, + ImportLocation::Missing => 7, + }; + ser_seq.serialize_element(&U64(scheme))?; + + match &import.location { + ImportLocation::Remote(url) => { + match &url.headers { + None => ser_seq.serialize_element(&Null)?, + Some(e) => { + ser_seq.serialize_element(&self::Serialize::Expr(e))? + } + }; + ser_seq.serialize_element(&url.authority)?; + for p in url.path.file_path.iter() { + ser_seq.serialize_element(&p)?; + } + match &url.query { + None => ser_seq.serialize_element(&Null)?, + Some(x) => ser_seq.serialize_element(x)?, + }; + } + ImportLocation::Local(_, path) => { + for p in path.file_path.iter() { + ser_seq.serialize_element(&p)?; + } + } + ImportLocation::Env(env) => { + ser_seq.serialize_element(env)?; + } + ImportLocation::Missing => {} + } + + ser_seq.end() +} + +impl<'a, E> serde::ser::Serialize for Serialize<'a, E> { + fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error> + where + S: serde::ser::Serializer, + { + match self { + Serialize::Expr(e) => serialize_subexpr(ser, e), + Serialize::CBOR(v) => v.serialize(ser), + Serialize::RecordMap(map) => { + ser.collect_map(map.iter().map(|(k, v)| { + (cbor::Value::String(k.into()), Serialize::Expr(v)) + })) + } + Serialize::UnionMap(map) => { + ser.collect_map(map.iter().map(|(k, v)| { + let v = match v { + Some(x) => Serialize::Expr(x), + None => Serialize::CBOR(cbor::Value::Null), + }; + (cbor::Value::String(k.into()), v) + })) + } + } + } +} + +fn collect_nested_applications<'a, E>( + e: &'a Expr<E>, +) -> (&'a Expr<E>, Vec<&'a Expr<E>>) { + fn go<'a, E>(e: &'a Expr<E>, vec: &mut Vec<&'a Expr<E>>) -> &'a Expr<E> { + match e.as_ref() { + ExprF::App(f, a) => { + vec.push(a); + go(f, vec) + } + _ => e, + } + } + let mut vec = vec![]; + let e = go(e, &mut vec); + (e, vec) +} + +type LetBinding<'a, E> = (&'a Label, &'a Option<Expr<E>>, &'a Expr<E>); + +fn collect_nested_lets<'a, E>( + e: &'a Expr<E>, +) -> (&'a Expr<E>, Vec<LetBinding<'a, E>>) { + fn go<'a, E>( + e: &'a Expr<E>, + vec: &mut Vec<LetBinding<'a, E>>, + ) -> &'a Expr<E> { + match e.as_ref() { + ExprF::Let(l, t, v, e) => { + vec.push((l, t, v)); + go(e, vec) + } + _ => e, + } + } + let mut vec = vec![]; + let e = go(e, &mut vec); + (e, vec) +} diff --git a/dhall/src/syntax/binary/mod.rs b/dhall/src/syntax/binary/mod.rs new file mode 100644 index 0000000..7ed1f6e --- /dev/null +++ b/dhall/src/syntax/binary/mod.rs @@ -0,0 +1,4 @@ +mod decode; +mod encode; +pub(crate) use decode::decode; +pub(crate) use encode::encode; diff --git a/dhall_syntax/src/core/context.rs b/dhall/src/syntax/core/context.rs index 6844baa..6844baa 100644 --- a/dhall_syntax/src/core/context.rs +++ b/dhall/src/syntax/core/context.rs diff --git a/dhall_syntax/src/core/expr.rs b/dhall/src/syntax/core/expr.rs index 131f97e..5b9f401 100644 --- a/dhall_syntax/src/core/expr.rs +++ b/dhall/src/syntax/core/expr.rs @@ -1,6 +1,6 @@ -use crate::map::{DupTreeMap, DupTreeSet}; -use crate::visitor::{self, ExprFMutVisitor, ExprFVisitor}; -use crate::*; +use crate::syntax::map::{DupTreeMap, DupTreeSet}; +use crate::syntax::visitor::{self, ExprFMutVisitor, ExprFVisitor}; +use crate::syntax::*; pub type Integer = isize; pub type Natural = usize; @@ -351,7 +351,7 @@ impl<E> Expr<E> { } /// Add an isize to an usize -/// Panics on over/underflow +/// Returns `None` on over/underflow fn add_ui(u: usize, i: isize) -> Option<usize> { Some(if i < 0 { u.checked_sub(i.checked_neg()? as usize)? diff --git a/dhall_syntax/src/core/import.rs b/dhall/src/syntax/core/import.rs index da3e99b..da3e99b 100644 --- a/dhall_syntax/src/core/import.rs +++ b/dhall/src/syntax/core/import.rs diff --git a/dhall_syntax/src/core/label.rs b/dhall/src/syntax/core/label.rs index 43c3f53..43c3f53 100644 --- a/dhall_syntax/src/core/label.rs +++ b/dhall/src/syntax/core/label.rs diff --git a/dhall_syntax/src/core/map.rs b/dhall/src/syntax/core/map.rs index c4c6126..c4c6126 100644 --- a/dhall_syntax/src/core/map.rs +++ b/dhall/src/syntax/core/map.rs diff --git a/dhall_syntax/src/core/mod.rs b/dhall/src/syntax/core/mod.rs index 66bf229..66bf229 100644 --- a/dhall_syntax/src/core/mod.rs +++ b/dhall/src/syntax/core/mod.rs diff --git a/dhall_syntax/src/core/span.rs b/dhall/src/syntax/core/span.rs index f9c7008..f9c7008 100644 --- a/dhall_syntax/src/core/span.rs +++ b/dhall/src/syntax/core/span.rs diff --git a/dhall_syntax/src/core/text.rs b/dhall/src/syntax/core/text.rs index fb390ee..fb390ee 100644 --- a/dhall_syntax/src/core/text.rs +++ b/dhall/src/syntax/core/text.rs diff --git a/dhall_syntax/src/core/visitor.rs b/dhall/src/syntax/core/visitor.rs index 143e556..b76d037 100644 --- a/dhall_syntax/src/core/visitor.rs +++ b/dhall/src/syntax/core/visitor.rs @@ -1,4 +1,4 @@ -use crate::*; +use crate::syntax::*; use std::iter::FromIterator; /// A visitor trait that can be used to traverse `ExprF`s. We need this pattern so that Rust lets @@ -111,7 +111,7 @@ where .collect() } - use crate::ExprF::*; + use crate::syntax::ExprF::*; Ok(match input { Var(v) => Var(v.clone()), Lam(l, t, e) => { @@ -225,7 +225,7 @@ where Ok(()) } - use crate::ExprF::*; + use crate::syntax::ExprF::*; match input { Var(_) | Const(_) | Builtin(_) | BoolLit(_) | NaturalLit(_) | IntegerLit(_) | DoubleLit(_) => {} diff --git a/dhall/src/syntax/mod.rs b/dhall/src/syntax/mod.rs new file mode 100644 index 0000000..a82e827 --- /dev/null +++ b/dhall/src/syntax/mod.rs @@ -0,0 +1,15 @@ +#![allow( + clippy::many_single_char_names, + clippy::should_implement_trait, + clippy::new_without_default, + clippy::type_complexity +)] + +mod core; +pub use crate::syntax::core::context; +pub use crate::syntax::core::visitor; +pub use crate::syntax::core::*; +pub use crate::syntax::text::parser::*; +pub use crate::syntax::text::printer::*; +pub mod binary; +pub mod text; diff --git a/dhall/src/syntax/text/mod.rs b/dhall/src/syntax/text/mod.rs new file mode 100644 index 0000000..c868288 --- /dev/null +++ b/dhall/src/syntax/text/mod.rs @@ -0,0 +1,2 @@ +pub mod parser; +pub mod printer; diff --git a/dhall_syntax/src/parser.rs b/dhall/src/syntax/text/parser.rs index f5d161f..f6b6577 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall/src/syntax/text/parser.rs @@ -3,36 +3,42 @@ use pest::prec_climber as pcl; use pest::prec_climber::PrecClimber; use std::rc::Rc; -use dgp::Rule; -use dhall_generated_parser as dgp; use pest_consume::{match_nodes, Parser}; -use crate::map::{DupTreeMap, DupTreeSet}; -use crate::ExprF::*; -use crate::*; +use crate::semantics::phase::Normalized; +use crate::syntax; +use crate::syntax::core; +use crate::syntax::map::{DupTreeMap, DupTreeSet}; +use crate::syntax::ExprF::*; +use crate::syntax::{ + FilePath, FilePrefix, Hash, ImportLocation, ImportMode, InterpolatedText, + InterpolatedTextContents, Label, NaiveDouble, RawExpr, Scheme, Span, URL, + V, +}; // This file consumes the parse tree generated by pest and turns it into // our own AST. All those custom macros should eventually moved into // their own crate because they are quite general and useful. For now they // are here and hopefully you can figure out how they work. -type ParsedText<E> = InterpolatedText<Expr<E>>; -type ParsedTextContents<E> = InterpolatedTextContents<Expr<E>>; +type Expr = syntax::Expr<Normalized>; +type ParsedText = InterpolatedText<Expr>; +type ParsedTextContents = InterpolatedTextContents<Expr>; type ParseInput<'input> = pest_consume::Node<'input, Rule, Rc<str>>; pub type ParseError = pest::error::Error<Rule>; pub type ParseResult<T> = Result<T, ParseError>; #[derive(Debug)] -enum Selector<E> { +enum Selector { Field(Label), Projection(DupTreeSet<Label>), - ProjectionByExpr(Expr<E>), + ProjectionByExpr(Expr), } -impl crate::Builtin { +impl crate::syntax::Builtin { pub fn parse(s: &str) -> Option<Self> { - use crate::Builtin::*; + use crate::syntax::Builtin::*; match s { "Bool" => Some(Bool), "Natural" => Some(Natural), @@ -71,16 +77,16 @@ impl crate::Builtin { fn input_to_span(input: ParseInput) -> Span { Span::make(input.user_data().clone(), input.as_pair().as_span()) } -fn spanned<E>(input: ParseInput, x: RawExpr<E>) -> Expr<E> { +fn spanned(input: ParseInput, x: RawExpr<Normalized>) -> Expr { Expr::new(x, input_to_span(input)) } -fn spanned_union<E>(span1: Span, span2: Span, x: RawExpr<E>) -> Expr<E> { +fn spanned_union(span1: Span, span2: Span, x: RawExpr<Normalized>) -> Expr { Expr::new(x, span1.union(&span2)) } // Trim the shared indent off of a vec of lines, as defined by the Dhall semantics of multiline // literals. -fn trim_indent<E: Clone>(lines: &mut Vec<ParsedText<E>>) { +fn trim_indent(lines: &mut Vec<ParsedText>) { let is_indent = |c: char| c == ' ' || c == '\t'; // There is at least one line so this is safe @@ -147,9 +153,11 @@ lazy_static::lazy_static! { }; } +#[derive(Parser)] +#[grammar = "dhall.pest"] struct DhallParser; -#[pest_consume::parser(parser = dgp::DhallParser, rule = dgp::Rule)] +#[pest_consume::parser(parser = DhallParser, rule = Rule)] impl DhallParser { fn EOI(_input: ParseInput) -> ParseResult<()> { Ok(()) @@ -164,9 +172,7 @@ impl DhallParser { Ok(Label::from(input.as_str())) } - fn double_quote_literal<E: Clone>( - input: ParseInput, - ) -> ParseResult<ParsedText<E>> { + fn double_quote_literal(input: ParseInput) -> ParseResult<ParsedText> { Ok(match_nodes!(input.into_children(); [double_quote_chunk(chunks)..] => { chunks.collect() @@ -174,9 +180,9 @@ impl DhallParser { )) } - fn double_quote_chunk<E: Clone>( + fn double_quote_chunk( input: ParseInput, - ) -> ParseResult<ParsedTextContents<E>> { + ) -> ParseResult<ParsedTextContents> { Ok(match_nodes!(input.into_children(); [expression(e)] => { InterpolatedTextContents::Expr(e) @@ -261,18 +267,16 @@ impl DhallParser { Ok(input.as_str().to_owned()) } - fn single_quote_literal<E: Clone>( - input: ParseInput, - ) -> ParseResult<ParsedText<E>> { + fn single_quote_literal(input: ParseInput) -> ParseResult<ParsedText> { Ok(match_nodes!(input.into_children(); [single_quote_continue(lines)] => { - let newline: ParsedText<E> = "\n".to_string().into(); + let newline: ParsedText = "\n".to_string().into(); // Reverse lines and chars in each line - let mut lines: Vec<ParsedText<E>> = lines + let mut lines: Vec<ParsedText> = lines .into_iter() .rev() - .map(|l| l.into_iter().rev().collect::<ParsedText<E>>()) + .map(|l| l.into_iter().rev().collect::<ParsedText>()) .collect(); trim_indent(&mut lines); @@ -281,7 +285,7 @@ impl DhallParser { .into_iter() .intersperse(newline) .flat_map(InterpolatedText::into_iter) - .collect::<ParsedText<E>>() + .collect::<ParsedText>() } )) } @@ -298,9 +302,9 @@ impl DhallParser { } // Returns a vec of lines in reversed order, where each line is also in reversed order. - fn single_quote_continue<E: Clone>( + fn single_quote_continue( input: ParseInput, - ) -> ParseResult<Vec<Vec<ParsedTextContents<E>>>> { + ) -> ParseResult<Vec<Vec<ParsedTextContents>>> { Ok(match_nodes!(input.into_children(); [expression(e), single_quote_continue(lines)] => { let c = InterpolatedTextContents::Expr(e); @@ -326,16 +330,16 @@ impl DhallParser { } #[alias(expression)] - fn builtin<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { + fn builtin(input: ParseInput) -> ParseResult<Expr> { let s = input.as_str(); - let e = match crate::Builtin::parse(s) { + let e = match crate::syntax::Builtin::parse(s) { Some(b) => Builtin(b), None => match s { "True" => BoolLit(true), "False" => BoolLit(false), - "Type" => Const(crate::Const::Type), - "Kind" => Const(crate::Const::Kind), - "Sort" => Const(crate::Const::Sort), + "Type" => Const(crate::syntax::Const::Type), + "Kind" => Const(crate::syntax::Const::Kind), + "Sort" => Const(crate::syntax::Const::Sort), _ => { Err(input.error(format!("Unrecognized builtin: '{}'", s)))? } @@ -387,7 +391,7 @@ impl DhallParser { } #[alias(expression, shortcut = true)] - fn identifier<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { + fn identifier(input: ParseInput) -> ParseResult<Expr> { Ok(match_nodes!(input.children(); [variable(v)] => spanned(input, Var(v)), [expression(e)] => e, @@ -441,9 +445,7 @@ impl DhallParser { } #[alias(import_type)] - fn local<E: Clone>( - input: ParseInput, - ) -> ParseResult<ImportLocation<Expr<E>>> { + fn local(input: ParseInput) -> ParseResult<ImportLocation<Expr>> { Ok(match_nodes!(input.into_children(); [local_path((prefix, p))] => ImportLocation::Local(prefix, p), )) @@ -482,7 +484,7 @@ impl DhallParser { }) } - fn http_raw<E: Clone>(input: ParseInput) -> ParseResult<URL<Expr<E>>> { + fn http_raw(input: ParseInput) -> ParseResult<URL<Expr>> { Ok(match_nodes!(input.into_children(); [scheme(sch), authority(auth), path(p)] => URL { scheme: sch, @@ -510,9 +512,7 @@ impl DhallParser { } #[alias(import_type)] - fn http<E: Clone>( - input: ParseInput, - ) -> ParseResult<ImportLocation<Expr<E>>> { + fn http(input: ParseInput) -> ParseResult<ImportLocation<Expr>> { Ok(ImportLocation::Remote(match_nodes!(input.into_children(); [http_raw(url)] => url, [http_raw(url), expression(e)] => URL { headers: Some(e), ..url }, @@ -520,9 +520,7 @@ impl DhallParser { } #[alias(import_type)] - fn env<E: Clone>( - input: ParseInput, - ) -> ParseResult<ImportLocation<Expr<E>>> { + fn env(input: ParseInput) -> ParseResult<ImportLocation<Expr>> { Ok(match_nodes!(input.into_children(); [environment_variable(v)] => ImportLocation::Env(v), )) @@ -557,9 +555,7 @@ impl DhallParser { } #[alias(import_type)] - fn missing<E: Clone>( - _input: ParseInput, - ) -> ParseResult<ImportLocation<Expr<E>>> { + fn missing(_input: ParseInput) -> ParseResult<ImportLocation<Expr>> { Ok(ImportLocation::Missing) } @@ -573,10 +569,10 @@ impl DhallParser { Ok(Hash::SHA256(hex::decode(hash).unwrap())) } - fn import_hashed<E: Clone>( + fn import_hashed( input: ParseInput, - ) -> ParseResult<crate::Import<Expr<E>>> { - use crate::Import; + ) -> ParseResult<crate::syntax::Import<Expr>> { + use crate::syntax::Import; let mode = ImportMode::Code; Ok(match_nodes!(input.into_children(); [import_type(location)] => Import { mode, location, hash: None }, @@ -594,8 +590,8 @@ impl DhallParser { } #[alias(expression)] - fn import<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { - use crate::Import; + fn import(input: ParseInput) -> ParseResult<Expr> { + use crate::syntax::Import; let import = match_nodes!(input.children(); [import_hashed(imp)] => { Import { mode: ImportMode::Code, ..imp } @@ -630,13 +626,13 @@ impl DhallParser { } #[alias(expression)] - fn empty_list_literal<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { + fn empty_list_literal(input: ParseInput) -> ParseResult<Expr> { Ok(match_nodes!(input.children(); [expression(e)] => spanned(input, EmptyListLit(e)), )) } - fn expression<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { + fn expression(input: ParseInput) -> ParseResult<Expr> { Ok(match_nodes!(input.children(); [lambda(()), label(l), expression(typ), arrow(()), expression(body)] => { @@ -681,9 +677,9 @@ impl DhallParser { )) } - fn let_binding<E: Clone>( + fn let_binding( input: ParseInput, - ) -> ParseResult<(Label, Option<Expr<E>>, Expr<E>, Span)> { + ) -> ParseResult<(Label, Option<Expr>, Expr, Span)> { Ok(match_nodes!(input.children(); [label(name), expression(annot), expression(expr)] => (name, Some(annot), expr, input_to_span(input)), @@ -694,12 +690,12 @@ impl DhallParser { #[alias(expression, shortcut = true)] #[prec_climb(expression, PRECCLIMBER)] - fn operator_expression<E: Clone>( - l: Expr<E>, + fn operator_expression( + l: Expr, op: ParseInput, - r: Expr<E>, - ) -> ParseResult<Expr<E>> { - use crate::BinOp::*; + r: Expr, + ) -> ParseResult<Expr> { + use crate::syntax::BinOp::*; use Rule::*; let op = match op.as_rule() { import_alt => ImportAlt, @@ -726,9 +722,7 @@ impl DhallParser { } #[alias(expression, shortcut = true)] - fn application_expression<E: Clone>( - input: ParseInput, - ) -> ParseResult<Expr<E>> { + fn application_expression(input: ParseInput) -> ParseResult<Expr> { Ok(match_nodes!(input.children(); [expression(e)] => e, [expression(first), expression(rest)..] => { @@ -747,9 +741,7 @@ impl DhallParser { } #[alias(expression, shortcut = true)] - fn first_application_expression<E: Clone>( - input: ParseInput, - ) -> ParseResult<Expr<E>> { + fn first_application_expression(input: ParseInput) -> ParseResult<Expr> { Ok(match_nodes!(input.children(); [Some_(()), expression(e)] => { spanned(input, SomeLit(e)) @@ -765,9 +757,7 @@ impl DhallParser { } #[alias(expression, shortcut = true)] - fn selector_expression<E: Clone>( - input: ParseInput, - ) -> ParseResult<Expr<E>> { + fn selector_expression(input: ParseInput) -> ParseResult<Expr> { Ok(match_nodes!(input.children(); [expression(e)] => e, [expression(first), selector(rest)..] => { @@ -789,9 +779,7 @@ impl DhallParser { )) } - fn selector<E: Clone>( - input: ParseInput, - ) -> ParseResult<(Selector<E>, Span)> { + fn selector(input: ParseInput) -> ParseResult<(Selector, Span)> { let stor = match_nodes!(input.children(); [label(l)] => Selector::Field(l), [labels(ls)] => Selector::Projection(ls), @@ -807,9 +795,7 @@ impl DhallParser { } #[alias(expression, shortcut = true)] - fn primitive_expression<E: Clone>( - input: ParseInput, - ) -> ParseResult<Expr<E>> { + fn primitive_expression(input: ParseInput) -> ParseResult<Expr> { Ok(match_nodes!(input.children(); [double_literal(n)] => spanned(input, DoubleLit(n)), [natural_literal(n)] => spanned(input, NaturalLit(n)), @@ -821,21 +807,19 @@ impl DhallParser { } #[alias(expression)] - fn empty_record_literal<E: Clone>( - input: ParseInput, - ) -> ParseResult<Expr<E>> { + fn empty_record_literal(input: ParseInput) -> ParseResult<Expr> { Ok(spanned(input, RecordLit(Default::default()))) } #[alias(expression)] - fn empty_record_type<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { + fn empty_record_type(input: ParseInput) -> ParseResult<Expr> { Ok(spanned(input, RecordType(Default::default()))) } #[alias(expression)] - fn non_empty_record_type_or_literal<E: Clone>( + fn non_empty_record_type_or_literal( input: ParseInput, - ) -> ParseResult<Expr<E>> { + ) -> ParseResult<Expr> { let e = match_nodes!(input.children(); [label(first_label), non_empty_record_type(rest)] => { let (first_expr, mut map) = rest; @@ -851,9 +835,9 @@ impl DhallParser { Ok(spanned(input, e)) } - fn non_empty_record_type<E: Clone>( + fn non_empty_record_type( input: ParseInput, - ) -> ParseResult<(Expr<E>, DupTreeMap<Label, Expr<E>>)> { + ) -> ParseResult<(Expr, DupTreeMap<Label, Expr>)> { Ok(match_nodes!(input.into_children(); [expression(expr), record_type_entry(entries)..] => { (expr, entries.collect()) @@ -861,17 +845,15 @@ impl DhallParser { )) } - fn record_type_entry<E: Clone>( - input: ParseInput, - ) -> ParseResult<(Label, Expr<E>)> { + fn record_type_entry(input: ParseInput) -> ParseResult<(Label, Expr)> { Ok(match_nodes!(input.into_children(); [label(name), expression(expr)] => (name, expr) )) } - fn non_empty_record_literal<E: Clone>( + fn non_empty_record_literal( input: ParseInput, - ) -> ParseResult<(Expr<E>, DupTreeMap<Label, Expr<E>>)> { + ) -> ParseResult<(Expr, DupTreeMap<Label, Expr>)> { Ok(match_nodes!(input.into_children(); [expression(expr), record_literal_entry(entries)..] => { (expr, entries.collect()) @@ -879,16 +861,14 @@ impl DhallParser { )) } - fn record_literal_entry<E: Clone>( - input: ParseInput, - ) -> ParseResult<(Label, Expr<E>)> { + fn record_literal_entry(input: ParseInput) -> ParseResult<(Label, Expr)> { Ok(match_nodes!(input.into_children(); [label(name), expression(expr)] => (name, expr) )) } #[alias(expression)] - fn union_type<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { + fn union_type(input: ParseInput) -> ParseResult<Expr> { let map = match_nodes!(input.children(); [empty_union_type(_)] => Default::default(), [union_type_entry(entries)..] => entries.collect(), @@ -900,9 +880,9 @@ impl DhallParser { Ok(()) } - fn union_type_entry<E: Clone>( + fn union_type_entry( input: ParseInput, - ) -> ParseResult<(Label, Option<Expr<E>>)> { + ) -> ParseResult<(Label, Option<Expr>)> { Ok(match_nodes!(input.children(); [label(name), expression(expr)] => (name, Some(expr)), [label(name)] => (name, None), @@ -910,9 +890,7 @@ impl DhallParser { } #[alias(expression)] - fn non_empty_list_literal<E: Clone>( - input: ParseInput, - ) -> ParseResult<Expr<E>> { + fn non_empty_list_literal(input: ParseInput) -> ParseResult<Expr> { Ok(match_nodes!(input.children(); [expression(items)..] => spanned( input, @@ -922,14 +900,14 @@ impl DhallParser { } #[alias(expression)] - fn final_expression<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { + fn final_expression(input: ParseInput) -> ParseResult<Expr> { Ok(match_nodes!(input.into_children(); [expression(e), EOI(_)] => e )) } } -pub fn parse_expr<E: Clone>(input_str: &str) -> ParseResult<Expr<E>> { +pub fn parse_expr(input_str: &str) -> ParseResult<Expr> { let rc_input_str = input_str.to_string().into(); let inputs = DhallParser::parse_with_userdata( Rule::final_expression, diff --git a/dhall_syntax/src/printer.rs b/dhall/src/syntax/text/printer.rs index ce6ff97..8df456b 100644 --- a/dhall_syntax/src/printer.rs +++ b/dhall/src/syntax/text/printer.rs @@ -1,11 +1,11 @@ -use crate::*; +use crate::syntax::*; use itertools::Itertools; use std::fmt::{self, Display}; /// Generic instance that delegates to subexpressions impl<SE: Display + Clone, E: Display> Display for ExprF<SE, E> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - use crate::ExprF::*; + use crate::syntax::ExprF::*; match self { Lam(a, b, c) => { write!(f, "λ({} : {}) → {}", a, b, c)?; @@ -141,7 +141,7 @@ impl<A: Display + Clone> RawExpr<A> { f: &mut fmt::Formatter, phase: PrintPhase, ) -> Result<(), fmt::Error> { - use crate::ExprF::*; + use crate::syntax::ExprF::*; use PrintPhase::*; let needs_paren = match self { @@ -298,7 +298,7 @@ impl Display for Const { impl Display for BinOp { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - use crate::BinOp::*; + use crate::syntax::BinOp::*; f.write_str(match self { BoolOr => "||", TextAppend => "++", @@ -344,7 +344,7 @@ impl Display for Label { let is_reserved = match s.as_str() { "let" | "in" | "if" | "then" | "else" | "Type" | "Kind" | "Sort" | "True" | "False" => true, - _ => crate::Builtin::parse(&s).is_some(), + _ => crate::syntax::Builtin::parse(&s).is_some(), }; if !is_reserved && s.chars().all(|c| c.is_ascii_alphanumeric()) { write!(f, "{}", s) @@ -443,7 +443,7 @@ impl<SubExpr: Display> Display for Import<SubExpr> { impl Display for Builtin { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - use crate::Builtin::*; + use crate::syntax::Builtin::*; f.write_str(match *self { Bool => "Bool", Natural => "Natural", @@ -480,7 +480,7 @@ impl Display for Builtin { impl Display for Scheme { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - use crate::Scheme::*; + use crate::syntax::Scheme::*; f.write_str(match *self { HTTP => "http", HTTPS => "https", |