From 092b7208ffd22f8facc7af387ac307e59208d52c Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 7 Apr 2020 11:19:36 +0100 Subject: Move duplicate field checking to parser --- dhall/src/syntax/ast/expr.rs | 5 +- dhall/src/syntax/ast/map.rs | 282 -------------------------------------- dhall/src/syntax/ast/mod.rs | 1 - dhall/src/syntax/binary/encode.rs | 13 +- dhall/src/syntax/text/parser.rs | 64 +++++++-- 5 files changed, 59 insertions(+), 306 deletions(-) delete mode 100644 dhall/src/syntax/ast/map.rs (limited to 'dhall/src/syntax') diff --git a/dhall/src/syntax/ast/expr.rs b/dhall/src/syntax/ast/expr.rs index 8f55540..9d216a7 100644 --- a/dhall/src/syntax/ast/expr.rs +++ b/dhall/src/syntax/ast/expr.rs @@ -4,7 +4,6 @@ use crate::builtins::Builtin; use crate::error::Error; use crate::operations::OpKind; use crate::semantics::Universe; -use crate::syntax::map::DupTreeMap; use crate::syntax::visitor; use crate::syntax::*; @@ -81,11 +80,11 @@ pub enum ExprKind { /// `[x, y, z]` NEListLit(Vec), /// `{ k1 : t1, k2 : t1 }` - RecordType(DupTreeMap), + RecordType(BTreeMap), /// `{ k1 = v1, k2 = v2 }` RecordLit(BTreeMap), /// `< k1 : t1, k2 >` - UnionType(DupTreeMap>), + UnionType(BTreeMap>), /// `x`, `x@n` Var(V), diff --git a/dhall/src/syntax/ast/map.rs b/dhall/src/syntax/ast/map.rs deleted file mode 100644 index 7a88204..0000000 --- a/dhall/src/syntax/ast/map.rs +++ /dev/null @@ -1,282 +0,0 @@ -/// A sorted map that allows multiple values for each key. -pub use dup_tree_map::DupTreeMap; -pub use dup_tree_set::DupTreeSet; - -mod known_size_iter { - pub struct KnownSizeIterator { - pub iter: I, - pub size: usize, - } - - impl Iterator for KnownSizeIterator { - type Item = I::Item; - - fn next(&mut self) -> Option { - let next = self.iter.next(); - if next.is_some() { - self.size -= 1; - } - next - } - - fn size_hint(&self) -> (usize, Option) { - (self.size, Some(self.size)) - } - } - - // unsafe impl iter::TrustedLen for KnownSizeIterator {} -} - -mod tuple { - mod sealed { - pub trait Sealed {} - } - pub trait Tuple: sealed::Sealed { - type First; - type Second; - } - impl sealed::Sealed for (A, B) {} - impl Tuple for (A, B) { - type First = A; - type Second = B; - } -} - -mod dup_tree_map { - use super::known_size_iter::KnownSizeIterator; - use super::tuple::Tuple; - use smallvec::SmallVec; - use std::collections::BTreeMap; - use std::iter; - - type OneOrMore = SmallVec<[V; 1]>; - type DupTreeMapInternal = BTreeMap>; - - #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] - pub struct DupTreeMap { - map: DupTreeMapInternal, - size: usize, - } - - // Generic types and functions to construct the iterators for this struct. - type ZipRepeatIter = iter::Zip< - iter::Repeat<::First>, - <::Second as IntoIterator>::IntoIter, - >; - type DupTreeMapIter = KnownSizeIterator< - iter::FlatMap< - ::IntoIter, - ZipRepeatIter<::Item>, - fn( - ::Item, - ) -> ZipRepeatIter<::Item>, - >, - >; - - fn zip_repeat((k, iter): (K, I)) -> ZipRepeatIter<(K, I)> - where - K: Clone, - I: IntoIterator, - { - iter::repeat(k).zip(iter.into_iter()) - } - - fn make_map_iter(map: M, size: usize) -> DupTreeMapIter - where - M: IntoIterator, - K: Clone, - I: IntoIterator, - { - KnownSizeIterator { - iter: map.into_iter().flat_map(zip_repeat), - size, - } - } - - pub type IterMut<'a, K, V> = - DupTreeMapIter<&'a mut DupTreeMapInternal>; - pub type Iter<'a, K, V> = DupTreeMapIter<&'a DupTreeMapInternal>; - pub type IntoIter = DupTreeMapIter>; - - impl DupTreeMap { - pub fn new() -> Self { - DupTreeMap { - map: BTreeMap::new(), - size: 0, - } - } - - pub fn insert(&mut self, key: K, value: V) { - self.map.entry(key).or_default().push(value); - self.size += 1; - } - - pub fn len(&self) -> usize { - self.size - } - pub fn is_empty(&self) -> bool { - self.size == 0 - } - - pub fn iter(&self) -> Iter<'_, K, V> { - make_map_iter(&self.map, self.size) - } - - pub fn iter_mut(&mut self) -> IterMut<'_, K, V> { - make_map_iter(&mut self.map, self.size) - } - } - - impl Default for DupTreeMap - where - K: Ord, - { - fn default() -> Self { - Self::new() - } - } - - impl IntoIterator for DupTreeMap - where - K: Ord + Clone, - { - type Item = (K, V); - type IntoIter = IntoIter; - - fn into_iter(self) -> Self::IntoIter { - make_map_iter(self.map, self.size) - } - } - - impl<'a, K, V> IntoIterator for &'a DupTreeMap - where - K: Ord + 'a, - V: 'a, - { - type Item = (&'a K, &'a V); - type IntoIter = Iter<'a, K, V>; - - fn into_iter(self) -> Self::IntoIter { - self.iter() - } - } - - impl<'a, K, V> IntoIterator for &'a mut DupTreeMap - where - K: Ord + 'a, - V: 'a, - { - type Item = (&'a K, &'a mut V); - type IntoIter = IterMut<'a, K, V>; - - fn into_iter(self) -> Self::IntoIter { - self.iter_mut() - } - } - - impl iter::FromIterator<(K, V)> for DupTreeMap - where - K: Ord, - { - fn from_iter(iter: T) -> Self - where - T: IntoIterator, - { - let mut map = DupTreeMap::new(); - for (k, v) in iter { - map.insert(k, v); - } - map - } - } -} - -mod dup_tree_set { - use super::tuple::Tuple; - use super::DupTreeMap; - use std::iter; - - #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] - pub struct DupTreeSet { - map: DupTreeMap, - } - - type DupTreeSetIter = iter::Map< - ::IntoIter, - fn( - ::Item, - ) -> <::Item as Tuple>::First, - >; - - pub type Iter<'a, K> = DupTreeSetIter<&'a DupTreeMap>; - pub type IntoIter = DupTreeSetIter>; - - fn drop_second((a, _): (A, B)) -> A { - a - } - - impl DupTreeSet { - pub fn new() -> Self { - DupTreeSet { - map: DupTreeMap::new(), - } - } - - pub fn len(&self) -> usize { - self.map.len() - } - pub fn is_empty(&self) -> bool { - self.map.is_empty() - } - - pub fn iter(&self) -> Iter<'_, K> { - self.map.iter().map(drop_second) - } - } - - impl Default for DupTreeSet - where - K: Ord, - { - fn default() -> Self { - Self::new() - } - } - - impl IntoIterator for DupTreeSet - where - K: Ord + Clone, - { - type Item = K; - type IntoIter = IntoIter; - - fn into_iter(self) -> Self::IntoIter { - self.map.into_iter().map(drop_second) - } - } - - impl<'a, K> IntoIterator for &'a DupTreeSet - where - K: Ord + 'a, - { - type Item = &'a K; - type IntoIter = Iter<'a, K>; - - fn into_iter(self) -> Self::IntoIter { - self.iter() - } - } - - impl iter::FromIterator for DupTreeSet - where - K: Ord, - { - fn from_iter(iter: T) -> Self - where - T: IntoIterator, - { - let map = iter.into_iter().map(|k| (k, ())).collect(); - DupTreeSet { map } - } - } -} diff --git a/dhall/src/syntax/ast/mod.rs b/dhall/src/syntax/ast/mod.rs index 1950154..c341af7 100644 --- a/dhall/src/syntax/ast/mod.rs +++ b/dhall/src/syntax/ast/mod.rs @@ -8,5 +8,4 @@ mod span; pub use span::*; mod text; pub use text::*; -pub mod map; pub mod visitor; diff --git a/dhall/src/syntax/binary/encode.rs b/dhall/src/syntax/binary/encode.rs index 913dd67..b6bbabe 100644 --- a/dhall/src/syntax/binary/encode.rs +++ b/dhall/src/syntax/binary/encode.rs @@ -6,7 +6,6 @@ use crate::builtins::Builtin; use crate::error::EncodeError; use crate::operations::{BinOp, OpKind}; use crate::syntax; -use crate::syntax::map::DupTreeMap; use crate::syntax::{ Expr, ExprKind, FilePrefix, Hash, Import, ImportMode, ImportTarget, Label, Scheme, V, @@ -21,8 +20,7 @@ enum Serialize<'a> { Expr(&'a Expr), CBOR(cbor::Value), RecordMap(&'a BTreeMap), - RecordDupMap(&'a DupTreeMap), - UnionMap(&'a DupTreeMap>), + UnionMap(&'a BTreeMap>), } macro_rules! count { @@ -52,7 +50,7 @@ where use syntax::NumKind::*; use OpKind::*; - use self::Serialize::{RecordDupMap, RecordMap, UnionMap}; + use self::Serialize::{RecordMap, UnionMap}; fn expr(x: &Expr) -> self::Serialize<'_> { self::Serialize::Expr(x) } @@ -133,7 +131,7 @@ where Text(x) => cbor(String(x)), }))) } - RecordType(map) => ser_seq!(ser; tag(7), RecordDupMap(map)), + 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)), Op(Field(x, l)) => ser_seq!(ser; tag(9), expr(x), label(l)), @@ -266,11 +264,6 @@ impl<'a> serde::ser::Serialize for Serialize<'a> { match self { Serialize::Expr(e) => serialize_subexpr(ser, e), Serialize::CBOR(v) => v.serialize(ser), - Serialize::RecordDupMap(map) => { - ser.collect_map(map.iter().map(|(k, v)| { - (cbor::Value::String(k.into()), Serialize::Expr(v)) - })) - } Serialize::RecordMap(map) => { ser.collect_map(map.iter().map(|(k, v)| { (cbor::Value::String(k.into()), Serialize::Expr(v)) diff --git a/dhall/src/syntax/text/parser.rs b/dhall/src/syntax/text/parser.rs index dbf5c75..dcaf5e4 100644 --- a/dhall/src/syntax/text/parser.rs +++ b/dhall/src/syntax/text/parser.rs @@ -1,14 +1,13 @@ use itertools::Itertools; use pest::prec_climber as pcl; use pest::prec_climber::PrecClimber; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, BTreeSet}; use std::iter::once; use std::rc::Rc; use pest_consume::{match_nodes, Parser}; use crate::operations::OpKind::*; -use crate::syntax::map::{DupTreeMap, DupTreeSet}; use crate::syntax::ExprKind::*; use crate::syntax::NumKind::*; use crate::syntax::{ @@ -32,7 +31,7 @@ pub type ParseResult = Result; #[derive(Debug)] enum Selector { Field(Label), - Projection(DupTreeSet