From 5895c3aa6552f75d7e5202be561f9734fe8945e7 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2019 19:31:23 +0200 Subject: No need to track the absence of `Span`s at the type level --- dhall_syntax/src/core/expr.rs | 131 +++++++++++++++++++-------------------- dhall_syntax/src/core/visitor.rs | 40 ++++-------- dhall_syntax/src/parser.rs | 39 ++---------- dhall_syntax/src/printer.rs | 12 ++-- 4 files changed, 84 insertions(+), 138 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/src/core/expr.rs b/dhall_syntax/src/core/expr.rs index 6522cb1..0cbece3 100644 --- a/dhall_syntax/src/core/expr.rs +++ b/dhall_syntax/src/core/expr.rs @@ -19,6 +19,30 @@ pub fn trivial_result(x: Result) -> T { } } +/// A location in the source text +#[derive(Debug, Clone)] +pub struct Span { + input: Rc, + /// # Safety + /// + /// Must be a valid character boundary index into `input`. + start: usize, + /// # Safety + /// + /// Must be a valid character boundary index into `input`. + end: usize, +} + +impl Span { + pub(crate) fn make(input: Rc, sp: pest::Span) -> Self { + Span { + input, + start: sp.start(), + end: sp.end(), + } + } +} + /// Double with bitwise equality #[derive(Debug, Copy, Clone)] pub struct NaiveDouble(f64); @@ -137,23 +161,19 @@ pub enum Builtin { TextShow, } -pub type ParsedExpr = SubExpr; -pub type ResolvedExpr = SubExpr; -pub type DhallExpr = ResolvedExpr; - // Each node carries an annotation. In practice it's either X (no annotation) or a Span. #[derive(Debug)] -pub struct SubExpr(Rc<(Expr, Option)>); +pub struct SubExpr(Rc<(Expr, Option)>); -impl std::cmp::PartialEq for SubExpr { +impl std::cmp::PartialEq for SubExpr { fn eq(&self, other: &Self) -> bool { self.0.as_ref().0 == other.0.as_ref().0 } } -impl std::cmp::Eq for SubExpr {} +impl std::cmp::Eq for SubExpr {} -pub type Expr = ExprF, Embed>; +pub type Expr = ExprF, Embed>; /// Syntax tree for expressions // Having the recursion out of the enum definition enables writing @@ -293,31 +313,22 @@ impl ExprF { } } -impl Expr { +impl Expr { fn traverse_embed( &self, visit_embed: impl FnMut(&E) -> Result, - ) -> Result, Err> - where - N: Clone, - { + ) -> Result, Err> { self.visit(&mut visitor::TraverseEmbedVisitor(visit_embed)) } - fn map_embed(&self, mut map_embed: impl FnMut(&E) -> E2) -> Expr - where - N: Clone, - { + fn map_embed(&self, mut map_embed: impl FnMut(&E) -> E2) -> Expr { trivial_result(self.traverse_embed(|x| Ok(map_embed(x)))) } pub fn traverse_resolve( &self, visit_embed: impl FnMut(&E) -> Result, - ) -> Result, Err> - where - N: Clone, - { + ) -> Result, Err> { self.traverse_resolve_with_visitor(&mut visitor::ResolveVisitor( visit_embed, )) @@ -326,9 +337,8 @@ impl Expr { pub(crate) fn traverse_resolve_with_visitor( &self, visitor: &mut visitor::ResolveVisitor, - ) -> Result, Err> + ) -> Result, Err> where - N: Clone, F1: FnMut(&E) -> Result, { match self { @@ -341,59 +351,44 @@ impl Expr { } } -impl Expr { - pub fn absurd(&self) -> Expr { +impl Expr { + pub fn absurd(&self) -> Expr { self.visit(&mut visitor::AbsurdVisitor) } } -impl Expr { - pub fn note_absurd(&self) -> Expr { - self.visit(&mut visitor::NoteAbsurdVisitor) - } -} - -impl SubExpr { - pub fn as_ref(&self) -> &Expr { +impl SubExpr { + pub fn as_ref(&self) -> &Expr { &self.0.as_ref().0 } - pub fn new(x: Expr, n: N) -> Self { + pub fn new(x: Expr, n: Span) -> Self { SubExpr(Rc::new((x, Some(n)))) } - pub fn from_expr_no_note(x: Expr) -> Self { + pub fn from_expr_no_span(x: Expr) -> Self { SubExpr(Rc::new((x, None))) } pub fn from_builtin(b: Builtin) -> Self { - SubExpr::from_expr_no_note(ExprF::Builtin(b)) + SubExpr::from_expr_no_span(ExprF::Builtin(b)) } - pub fn rewrap(&self, x: Expr) -> SubExpr - where - N: Clone, - { + pub fn rewrap(&self, x: Expr) -> SubExpr { SubExpr(Rc::new((x, (self.0).1.clone()))) } pub fn traverse_embed( &self, visit_embed: impl FnMut(&E) -> Result, - ) -> Result, Err> - where - N: Clone, - { + ) -> Result, Err> { Ok(self.rewrap(self.as_ref().traverse_embed(visit_embed)?)) } pub fn map_embed( &self, map_embed: impl FnMut(&E) -> E2, - ) -> SubExpr - where - N: Clone, - { + ) -> SubExpr { self.rewrap(self.as_ref().map_embed(map_embed)) } @@ -401,10 +396,7 @@ impl SubExpr { &'a self, map_expr: impl FnMut(&'a Self) -> Self, map_under_binder: impl FnMut(&'a Label, &'a Self) -> Self, - ) -> Self - where - N: Clone, - { + ) -> Self { match self.as_ref() { ExprF::Embed(_) => SubExpr::clone(self), // This calls ExprF::map_ref @@ -419,35 +411,38 @@ impl SubExpr { pub fn traverse_resolve( &self, visit_embed: impl FnMut(&E) -> Result, - ) -> Result, Err> - where - N: Clone, - { + ) -> Result, Err> { Ok(self.rewrap(self.as_ref().traverse_resolve(visit_embed)?)) } } -impl SubExpr { - pub fn absurd(&self) -> SubExpr { - SubExpr::from_expr_no_note(self.as_ref().absurd()) - } -} - -impl SubExpr { - pub fn note_absurd(&self) -> SubExpr { - SubExpr::from_expr_no_note(self.as_ref().note_absurd()) +impl SubExpr { + pub fn absurd(&self) -> SubExpr { + SubExpr::from_expr_no_span(self.as_ref().absurd()) } } -impl Clone for SubExpr { +impl Clone for SubExpr { fn clone(&self) -> Self { SubExpr(Rc::clone(&self.0)) } } // Should probably rename this -pub fn rc(x: Expr) -> SubExpr { - SubExpr::from_expr_no_note(x) +pub fn rc(x: Expr) -> SubExpr { + SubExpr::from_expr_no_span(x) +} + +pub(crate) fn spanned( + span: Span, + x: crate::parser::ParsedExpr, +) -> crate::parser::ParsedSubExpr { + SubExpr::new(x, span) +} +pub(crate) fn unspanned( + x: crate::parser::ParsedExpr, +) -> crate::parser::ParsedSubExpr { + SubExpr::from_expr_no_span(x) } /// Add an isize to an usize diff --git a/dhall_syntax/src/core/visitor.rs b/dhall_syntax/src/core/visitor.rs index 18f76d9..50ec68a 100644 --- a/dhall_syntax/src/core/visitor.rs +++ b/dhall_syntax/src/core/visitor.rs @@ -336,19 +336,18 @@ where pub struct TraverseEmbedVisitor(pub F1); -impl<'a, 'b, N, E, E2, Err, F1> - ExprFFallibleVisitor<'a, SubExpr, SubExpr, E, E2> +impl<'a, 'b, E, E2, Err, F1> + ExprFFallibleVisitor<'a, SubExpr, SubExpr, E, E2> for &'b mut TraverseEmbedVisitor where - N: Clone + 'a, F1: FnMut(&E) -> Result, { type Error = Err; fn visit_subexpr( &mut self, - subexpr: &'a SubExpr, - ) -> Result, Self::Error> { + subexpr: &'a SubExpr, + ) -> Result, Self::Error> { Ok(subexpr.rewrap(subexpr.as_ref().visit(&mut **self)?)) } fn visit_embed(self, embed: &'a E) -> Result { @@ -358,19 +357,18 @@ where pub struct ResolveVisitor(pub F1); -impl<'a, 'b, N, E, E2, Err, F1> - ExprFFallibleVisitor<'a, SubExpr, SubExpr, E, E2> +impl<'a, 'b, E, E2, Err, F1> + ExprFFallibleVisitor<'a, SubExpr, SubExpr, E, E2> for &'b mut ResolveVisitor where - N: Clone + 'a, F1: FnMut(&E) -> Result, { type Error = Err; fn visit_subexpr( &mut self, - subexpr: &'a SubExpr, - ) -> Result, Self::Error> { + subexpr: &'a SubExpr, + ) -> Result, Self::Error> { Ok(subexpr.rewrap( subexpr .as_ref() @@ -381,30 +379,14 @@ where (self.0)(embed) } } -pub struct NoteAbsurdVisitor; - -impl<'a, 'b, N, E> - ExprFInFallibleVisitor<'a, SubExpr, SubExpr, E, E> - for &'b mut NoteAbsurdVisitor -where - E: Clone + 'a, -{ - fn visit_subexpr(&mut self, subexpr: &'a SubExpr) -> SubExpr { - SubExpr::from_expr_no_note(subexpr.as_ref().visit(&mut **self)) - } - fn visit_embed(self, embed: &'a E) -> E { - E::clone(embed) - } -} pub struct AbsurdVisitor; -impl<'a, 'b, N, E> - ExprFInFallibleVisitor<'a, SubExpr, SubExpr, X, E> +impl<'a, 'b, E> ExprFInFallibleVisitor<'a, SubExpr, SubExpr, X, E> for &'b mut AbsurdVisitor { - fn visit_subexpr(&mut self, subexpr: &'a SubExpr) -> SubExpr { - SubExpr::from_expr_no_note(subexpr.as_ref().visit(&mut **self)) + fn visit_subexpr(&mut self, subexpr: &'a SubExpr) -> SubExpr { + SubExpr::from_expr_no_span(subexpr.as_ref().visit(&mut **self)) } fn visit_embed(self, embed: &'a X) -> E { match *embed {} diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index 9ae6dc8..1ab6e6d 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -15,46 +15,15 @@ use crate::*; // 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 ParsedExpr = Expr; -type ParsedSubExpr = SubExpr; -type ParsedText = InterpolatedText>; -type ParsedTextContents = InterpolatedTextContents>; +pub(crate) type ParsedExpr = Expr; +pub(crate) type ParsedSubExpr = SubExpr; +type ParsedText = InterpolatedText>; +type ParsedTextContents = InterpolatedTextContents>; pub type ParseError = pest::error::Error; pub type ParseResult = Result; -fn unspanned(x: ParsedExpr) -> ParsedSubExpr { - SubExpr::from_expr_no_note(x) -} - -#[derive(Debug, Clone)] -pub struct Span { - input: Rc, - /// # Safety - /// - /// Must be a valid character boundary index into `input`. - start: usize, - /// # Safety - /// - /// Must be a valid character boundary index into `input`. - end: usize, -} - -impl Span { - fn make(input: Rc, sp: pest::Span) -> Self { - Span { - input, - start: sp.start(), - end: sp.end(), - } - } -} - -fn spanned(span: Span, x: ParsedExpr) -> ParsedSubExpr { - SubExpr::new(x, span) -} - #[derive(Debug)] enum Either { Left(A), diff --git a/dhall_syntax/src/printer.rs b/dhall_syntax/src/printer.rs index 70b224e..95eafe5 100644 --- a/dhall_syntax/src/printer.rs +++ b/dhall_syntax/src/printer.rs @@ -111,21 +111,21 @@ enum PrintPhase { // Wraps an Expr with a phase, so that phase selsction can be done // separate from the actual printing #[derive(Clone)] -struct PhasedExpr<'a, S, A>(&'a SubExpr, PrintPhase); +struct PhasedExpr<'a, A>(&'a SubExpr, PrintPhase); -impl<'a, S: Clone, A: Display + Clone> Display for PhasedExpr<'a, S, A> { +impl<'a, A: Display + Clone> Display for PhasedExpr<'a, A> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { self.0.as_ref().fmt_phase(f, self.1) } } -impl<'a, S: Clone, A: Display + Clone> PhasedExpr<'a, S, A> { - fn phase(self, phase: PrintPhase) -> PhasedExpr<'a, S, A> { +impl<'a, A: Display + Clone> PhasedExpr<'a, A> { + fn phase(self, phase: PrintPhase) -> PhasedExpr<'a, A> { PhasedExpr(self.0, phase) } } -impl Expr { +impl Expr { fn fmt_phase( &self, f: &mut fmt::Formatter, @@ -196,7 +196,7 @@ impl Expr { } } -impl Display for SubExpr { +impl Display for SubExpr { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { self.as_ref().fmt_phase(f, PrintPhase::Base) } -- cgit v1.2.3 From 77af0bbc171618f48531cc6b1d77e18089928885 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2019 20:54:15 +0200 Subject: Stop tracking the absence of Embed values at the type level --- dhall_syntax/src/core/context.rs | 2 +- dhall_syntax/src/core/expr.rs | 16 ++-------------- dhall_syntax/src/core/visitor.rs | 15 +-------------- dhall_syntax/src/printer.rs | 6 ------ 4 files changed, 4 insertions(+), 35 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/src/core/context.rs b/dhall_syntax/src/core/context.rs index eeec121..6844baa 100644 --- a/dhall_syntax/src/core/context.rs +++ b/dhall_syntax/src/core/context.rs @@ -4,7 +4,7 @@ use std::hash::Hash; /// A `(Context a)` associates `Text` labels with values of type `a` /// -/// The `Context` is used for type-checking when `(a = Expr X)` +/// The `Context` is used for type-checking when `(a = Expr)` /// /// * You create a `Context` using `empty` and `insert` /// * You transform a `Context` using `fmap` diff --git a/dhall_syntax/src/core/expr.rs b/dhall_syntax/src/core/expr.rs index 0cbece3..e08d816 100644 --- a/dhall_syntax/src/core/expr.rs +++ b/dhall_syntax/src/core/expr.rs @@ -10,9 +10,9 @@ pub type Double = NaiveDouble; /// An empty type #[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub enum X {} +pub enum Void {} -pub fn trivial_result(x: Result) -> T { +pub fn trivial_result(x: Result) -> T { match x { Ok(x) => x, Err(e) => match e {}, @@ -351,12 +351,6 @@ impl Expr { } } -impl Expr { - pub fn absurd(&self) -> Expr { - self.visit(&mut visitor::AbsurdVisitor) - } -} - impl SubExpr { pub fn as_ref(&self) -> &Expr { &self.0.as_ref().0 @@ -416,12 +410,6 @@ impl SubExpr { } } -impl SubExpr { - pub fn absurd(&self) -> SubExpr { - SubExpr::from_expr_no_span(self.as_ref().absurd()) - } -} - impl Clone for SubExpr { fn clone(&self) -> Self { SubExpr(Rc::clone(&self.0)) diff --git a/dhall_syntax/src/core/visitor.rs b/dhall_syntax/src/core/visitor.rs index 50ec68a..e39e95b 100644 --- a/dhall_syntax/src/core/visitor.rs +++ b/dhall_syntax/src/core/visitor.rs @@ -243,7 +243,7 @@ impl<'a, T, SE1, SE2, E1, E2> ExprFFallibleVisitor<'a, SE1, SE2, E1, E2> where T: ExprFInFallibleVisitor<'a, SE1, SE2, E1, E2>, { - type Error = X; + type Error = Void; fn visit_subexpr(&mut self, subexpr: &'a SE1) -> Result { Ok(self.0.visit_subexpr(subexpr)) @@ -379,16 +379,3 @@ where (self.0)(embed) } } - -pub struct AbsurdVisitor; - -impl<'a, 'b, E> ExprFInFallibleVisitor<'a, SubExpr, SubExpr, X, E> - for &'b mut AbsurdVisitor -{ - fn visit_subexpr(&mut self, subexpr: &'a SubExpr) -> SubExpr { - SubExpr::from_expr_no_span(subexpr.as_ref().visit(&mut **self)) - } - fn visit_embed(self, embed: &'a X) -> E { - match *embed {} - } -} diff --git a/dhall_syntax/src/printer.rs b/dhall_syntax/src/printer.rs index 95eafe5..25d4ca8 100644 --- a/dhall_syntax/src/printer.rs +++ b/dhall_syntax/src/printer.rs @@ -491,9 +491,3 @@ impl Display for V { }; // Annotate subexpressions with the appropriate phase, defaulting to Base - let phased_self = match self.map_ref_simple(|e| PhasedExpr(e, Base)) { + let phased_self = match self.map_ref(|e| PhasedExpr(e, Base)) { Pi(a, b, c) => { if &String::from(&a) == "_" { Pi(a, b.phase(Operator), c) -- cgit v1.2.3 From 07956ccb1daf4a6819f64776f70b6f5f26869184 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2019 21:38:17 +0200 Subject: Cleanup visitor code --- dhall_syntax/src/core/visitor.rs | 193 +++++++++++---------------------------- 1 file changed, 54 insertions(+), 139 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/src/core/visitor.rs b/dhall_syntax/src/core/visitor.rs index 647f76f..f2f3b32 100644 --- a/dhall_syntax/src/core/visitor.rs +++ b/dhall_syntax/src/core/visitor.rs @@ -2,52 +2,56 @@ use crate::*; use std::iter::FromIterator; /// A way too generic Visitor trait. -pub trait GenericVisitor: Sized { - fn visit(self, input: Input) -> Return; +pub trait GenericVisitor: Sized { + fn visit(self, input: Input) -> Output; } -/// A visitor trait that can be used to traverse `ExprF`s. We need this pattern -/// so that Rust lets us have as much mutability as we can. -/// For example, `traverse_embed` cannot be made using only `traverse_ref`, because -/// `traverse_ref` takes a `FnMut` so we would need to pass multiple mutable -/// reverences to this argument to `traverse_ref`. But Rust's ownership system -/// is all about preventing exactly this ! So we have to be more clever. -/// The visitor pattern allows us to have only one mutable thing the whole -/// time: the visitor itself. The visitor can then carry around multiple closures -/// or just one, and Rust is ok with either. See for example TraverseRefVisitor -/// and TraverseEmbedVisitor. -/// This is very generic. For a more legible trait, see ExprFInFallibleVisitor -pub trait ExprFVeryGenericVisitor<'a, Ret, SE1, E1>: Sized { +/// A visitor trait that can be used to traverse `ExprF`s. We need this pattern so that Rust lets +/// us have as much mutability as we can. +/// For example, `traverse_ref_with_special_handling_of_binders` cannot be made using only +/// `traverse_ref`, because `traverse_ref` takes a `FnMut` so we would need to pass multiple +/// mutable reverences to this argument to `traverse_ref`. But Rust's ownership system is all about +/// preventing exactly this ! So we have to be more clever. The visitor pattern allows us to have +/// only one mutable thing the whole time: the visitor itself. The visitor can then carry around +/// multiple closures or just one, and Rust is ok with either. See for example TraverseRefVisitor. +pub trait ExprFFallibleVisitor<'a, SE1, SE2, E1, E2>: Sized { type Error; - type SE2; - type E2; - fn visit_subexpr( - &mut self, - subexpr: &'a SE1, - ) -> Result; + fn visit_subexpr(&mut self, subexpr: &'a SE1) -> Result; + fn visit_embed(self, embed: &'a E1) -> Result; fn visit_subexpr_under_binder( - self, - label: &'a Label, + mut self, + _label: &'a Label, subexpr: &'a SE1, - ) -> Result; + ) -> Result { + self.visit_subexpr(subexpr) + } +} - fn visit_embed_squash(self, embed: &'a E1) -> Result; +/// Like ExprFFallibleVisitor, but without the error handling. +pub trait ExprFInFallibleVisitor<'a, SE1, SE2, E1, E2>: Sized { + fn visit_subexpr(&mut self, subexpr: &'a SE1) -> SE2; + fn visit_embed(self, embed: &'a E1) -> E2; - // Called with the result of the map, in the non-embed case. - // Useful to change the result type, and/or avoid some loss of info - fn visit_resulting_exprf( - result: ExprF, - ) -> Result; + fn visit_subexpr_under_binder( + mut self, + _label: &'a Label, + subexpr: &'a SE1, + ) -> SE2 { + self.visit_subexpr(subexpr) + } } -impl<'a, T, Ret, SE1, E1> - GenericVisitor<&'a ExprF, Result> for T +impl<'a, T, SE1, SE2, E1, E2> + GenericVisitor<&'a ExprF, Result, T::Error>> for T where - T: ExprFVeryGenericVisitor<'a, Ret, SE1, E1>, + T: ExprFFallibleVisitor<'a, SE1, SE2, E1, E2>, { - fn visit(self, input: &'a ExprF) -> Result { + fn visit( + self, + input: &'a ExprF, + ) -> Result, T::Error> { fn vec<'a, T, U, Err, F: FnMut(&'a T) -> Result>( x: &'a [T], f: F, @@ -63,27 +67,27 @@ where None => None, }) } - fn dupmap<'a, V, Ret, SE, E, T>( - x: impl IntoIterator, + fn dupmap<'a, V, SE1, SE2, E1, E2, T>( + x: impl IntoIterator, mut v: V, ) -> Result where - SE: 'a, - T: FromIterator<(Label, V::SE2)>, - V: ExprFVeryGenericVisitor<'a, Ret, SE, E>, + SE1: 'a, + T: FromIterator<(Label, SE2)>, + V: ExprFFallibleVisitor<'a, SE1, SE2, E1, E2>, { x.into_iter() .map(|(k, x)| Ok((k.clone(), v.visit_subexpr(x)?))) .collect() } - fn optdupmap<'a, V, Ret, SE, E, T>( - x: impl IntoIterator)>, + fn optdupmap<'a, V, SE1, SE2, E1, E2, T>( + x: impl IntoIterator)>, mut v: V, ) -> Result where - SE: 'a, - T: FromIterator<(Label, Option)>, - V: ExprFVeryGenericVisitor<'a, Ret, SE, E>, + SE1: 'a, + T: FromIterator<(Label, Option)>, + V: ExprFFallibleVisitor<'a, SE1, SE2, E1, E2>, { x.into_iter() .map(|(k, x)| { @@ -100,7 +104,7 @@ where let mut v = self; use crate::ExprF::*; - T::visit_resulting_exprf(match input { + Ok(match input { Var(v) => Var(v.clone()), Lam(l, t, e) => { let t = v.visit_subexpr(t)?; @@ -149,90 +153,18 @@ where Field(e, l) => Field(v.visit_subexpr(e)?, l.clone()), Projection(e, ls) => Projection(v.visit_subexpr(e)?, ls.clone()), Assert(e) => Assert(v.visit_subexpr(e)?), - Embed(a) => return v.visit_embed_squash(a), + Embed(a) => Embed(v.visit_embed(a)?), }) } } -/// Like ExprFVeryGenericVisitor, but sets the return -/// type to ExprF<_> -pub trait ExprFFallibleVisitor<'a, SE1, SE2, E1, E2>: Sized { - type Error; - - fn visit_subexpr(&mut self, subexpr: &'a SE1) -> Result; - fn visit_embed(self, embed: &'a E1) -> Result; - - fn visit_subexpr_under_binder( - mut self, - _label: &'a Label, - subexpr: &'a SE1, - ) -> Result { - self.visit_subexpr(subexpr) - } - - fn visit_embed_squash( - self, - embed: &'a E1, - ) -> Result, Self::Error> { - Ok(ExprF::Embed(self.visit_embed(embed)?)) - } -} - -impl<'a, T, SE1, SE2, E1, E2> - ExprFVeryGenericVisitor<'a, ExprF, SE1, E1> for T +impl<'a, T, SE1, SE2, E1, E2> GenericVisitor<&'a ExprF, ExprF> + for T where - T: ExprFFallibleVisitor<'a, SE1, SE2, E1, E2>, + T: ExprFInFallibleVisitor<'a, SE1, SE2, E1, E2>, { - type Error = T::Error; - type SE2 = SE2; - type E2 = E2; - - fn visit_subexpr( - &mut self, - subexpr: &'a SE1, - ) -> Result { - self.visit_subexpr(subexpr) - } - - fn visit_subexpr_under_binder( - self, - label: &'a Label, - subexpr: &'a SE1, - ) -> Result { - self.visit_subexpr_under_binder(label, subexpr) - } - - fn visit_embed_squash( - self, - embed: &'a E1, - ) -> Result, Self::Error> { - self.visit_embed_squash(embed) - } - - // Called with the result of the map, in the non-embed case. - // Useful to change the result type, and/or avoid some loss of info - fn visit_resulting_exprf( - result: ExprF, - ) -> Result, Self::Error> { - Ok(result) - } -} - -/// Like ExprFFallibleVisitor, but without the error handling. -pub trait ExprFInFallibleVisitor<'a, SE1, SE2, E1, E2>: Sized { - fn visit_subexpr(&mut self, subexpr: &'a SE1) -> SE2; - fn visit_embed(self, embed: &'a E1) -> E2; - - fn visit_subexpr_under_binder( - mut self, - _label: &'a Label, - subexpr: &'a SE1, - ) -> SE2 { - self.visit_subexpr(subexpr) - } - - fn visit_embed_squash(self, embed: &'a E1) -> ExprF { - ExprF::Embed(self.visit_embed(embed)) + fn visit(self, input: &'a ExprF) -> ExprF { + trivial_result(InfallibleWrapper(self).visit(input)) } } @@ -259,23 +191,6 @@ where ) -> Result { Ok(self.0.visit_subexpr_under_binder(label, subexpr)) } - - fn visit_embed_squash( - self, - embed: &'a E1, - ) -> Result, Self::Error> { - Ok(self.0.visit_embed_squash(embed)) - } -} - -impl<'a, T, SE1, SE2, E1, E2> GenericVisitor<&'a ExprF, ExprF> - for T -where - T: ExprFInFallibleVisitor<'a, SE1, SE2, E1, E2>, -{ - fn visit(self, input: &'a ExprF) -> ExprF { - trivial_result(InfallibleWrapper(self).visit(input)) - } } pub struct TraverseRefWithBindersVisitor { -- cgit v1.2.3 From 8d45d633dfa60e8d64c9e6e742de4e33496bf0fa Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2019 22:11:45 +0200 Subject: Store Imports in their own node instead of in Embed --- dhall_syntax/src/core/expr.rs | 15 ++++++++++++--- dhall_syntax/src/core/visitor.rs | 11 ++++++----- dhall_syntax/src/parser.rs | 14 +++++++------- dhall_syntax/src/printer.rs | 16 +++++++++++----- 4 files changed, 36 insertions(+), 20 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/src/core/expr.rs b/dhall_syntax/src/core/expr.rs index 74eb993..4c809f8 100644 --- a/dhall_syntax/src/core/expr.rs +++ b/dhall_syntax/src/core/expr.rs @@ -12,6 +12,12 @@ pub type Double = NaiveDouble; #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum Void {} +impl std::fmt::Display for Void { + fn fmt(&self, _f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + match *self {} + } +} + pub fn trivial_result(x: Result) -> T { match x { Ok(x) => x, @@ -233,7 +239,9 @@ pub enum ExprF { Field(SubExpr, Label), /// `e.{ x, y, z }` Projection(SubExpr, DupTreeSet { // Precedence is magically handled by the ordering of BinOps. ExprF::BinOp(op, _, _) if phase > PrintPhase::BinOp(*op) => true, ExprF::App(_, _) if phase > PrintPhase::App => true, - Field(_, _) | Projection(_, _) if phase > Import => true, + Field(_, _) | Projection(_, _) if phase > PrintPhase::Import => { + true + } _ => false, }; @@ -165,8 +168,8 @@ impl Expr { } } Merge(a, b, c) => Merge( - a.phase(Import), - b.phase(Import), + a.phase(PrintPhase::Import), + b.phase(PrintPhase::Import), c.map(|x| x.phase(PrintPhase::App)), ), Annot(a, b) => Annot(a.phase(Operator), b), @@ -175,8 +178,11 @@ impl Expr { a.phase(PrintPhase::BinOp(op)), b.phase(PrintPhase::BinOp(op)), ), - SomeLit(e) => SomeLit(e.phase(Import)), - ExprF::App(f, a) => ExprF::App(f.phase(Import), a.phase(Import)), + SomeLit(e) => SomeLit(e.phase(PrintPhase::Import)), + ExprF::App(f, a) => ExprF::App( + f.phase(PrintPhase::Import), + a.phase(PrintPhase::Import), + ), Field(a, b) => Field(a.phase(Primitive), b), Projection(e, ls) => Projection(e.phase(Primitive), ls), e => e, -- cgit v1.2.3 From 43094c8c52319aa82b03e73700ca1b5f65da13c6 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2019 22:24:04 +0200 Subject: The syntax has very little sharing; no need for Rc anymore --- dhall_syntax/src/core/expr.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/src/core/expr.rs b/dhall_syntax/src/core/expr.rs index 4c809f8..e3f8cec 100644 --- a/dhall_syntax/src/core/expr.rs +++ b/dhall_syntax/src/core/expr.rs @@ -168,8 +168,8 @@ pub enum Builtin { } // Each node carries an annotation. In practice it's either X (no annotation) or a Span. -#[derive(Debug)] -pub struct SubExpr(Rc<(Expr, Option)>); +#[derive(Debug, Clone)] +pub struct SubExpr(Box<(Expr, Option)>); impl std::cmp::PartialEq for SubExpr { fn eq(&self, other: &Self) -> bool { @@ -336,11 +336,11 @@ impl SubExpr { } pub fn new(x: Expr, n: Span) -> Self { - SubExpr(Rc::new((x, Some(n)))) + SubExpr(Box::new((x, Some(n)))) } pub fn from_expr_no_span(x: Expr) -> Self { - SubExpr(Rc::new((x, None))) + SubExpr(Box::new((x, None))) } pub fn from_builtin(b: Builtin) -> Self { @@ -348,7 +348,7 @@ impl SubExpr { } pub fn rewrap(&self, x: Expr) -> SubExpr { - SubExpr(Rc::new((x, (self.0).1.clone()))) + SubExpr(Box::new((x, (self.0).1.clone()))) } } @@ -361,12 +361,6 @@ impl SubExpr { } } -impl Clone for SubExpr { - fn clone(&self) -> Self { - SubExpr(Rc::clone(&self.0)) - } -} - // Should probably rename this pub fn rc(x: Expr) -> SubExpr { SubExpr::from_expr_no_span(x) -- cgit v1.2.3 From c600bae72198350b78fe19cf993b7f4c6f35225a Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 13 Aug 2019 23:08:48 +0200 Subject: Implement Hash for ParsedSubExpr --- dhall_syntax/src/core/expr.rs | 28 +++++++++++++++++++++++----- dhall_syntax/src/core/text.rs | 2 +- 2 files changed, 24 insertions(+), 6 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/src/core/expr.rs b/dhall_syntax/src/core/expr.rs index e3f8cec..9729efb 100644 --- a/dhall_syntax/src/core/expr.rs +++ b/dhall_syntax/src/core/expr.rs @@ -61,6 +61,15 @@ impl PartialEq for NaiveDouble { impl Eq for NaiveDouble {} +impl std::hash::Hash for NaiveDouble { + fn hash(&self, state: &mut H) + where + H: std::hash::Hasher, + { + self.0.to_bits().hash(state) + } +} + impl From for NaiveDouble { fn from(x: f64) -> Self { NaiveDouble(x) @@ -74,7 +83,7 @@ impl From for f64 { } /// Constants for a pure type system -#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Const { Type, Kind, @@ -86,7 +95,7 @@ pub enum Const { /// The `Label` field is the variable's name (i.e. \"`x`\"). /// The `Int` field is a DeBruijn index. /// See dhall-lang/standard/semantics.md for details -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct V, PrintPhase); +struct PhasedExpr<'a, A>(&'a Expr, PrintPhase); impl<'a, A: Display + Clone> Display for PhasedExpr<'a, A> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { @@ -126,7 +126,7 @@ impl<'a, A: Display + Clone> PhasedExpr<'a, A> { } } -impl Expr { +impl RawExpr { fn fmt_phase( &self, f: &mut fmt::Formatter, @@ -202,7 +202,7 @@ impl Expr { } } -impl Display for SubExpr { +impl Display for Expr { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { self.as_ref().fmt_phase(f, PrintPhase::Base) } -- cgit v1.2.3 From aba7e62e49ac9dead0a2868f739091d2d15ff0d1 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 31 Aug 2019 21:59:39 +0200 Subject: Implement parsing of `toMap` keyword --- dhall_syntax/src/core/expr.rs | 2 ++ dhall_syntax/src/core/visitor.rs | 3 +++ dhall_syntax/src/parser.rs | 8 +++++++- dhall_syntax/src/printer.rs | 11 +++++++++++ 4 files changed, 23 insertions(+), 1 deletion(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/src/core/expr.rs b/dhall_syntax/src/core/expr.rs index 2d73a64..51b6c47 100644 --- a/dhall_syntax/src/core/expr.rs +++ b/dhall_syntax/src/core/expr.rs @@ -243,6 +243,8 @@ pub enum ExprF { UnionType(DupTreeMap>), /// `merge x y : t` Merge(SubExpr, SubExpr, Option), + /// `toMap x : t` + ToMap(SubExpr, Option), /// `e.x` Field(SubExpr, Label), /// `e.{ x, y, z }` diff --git a/dhall_syntax/src/core/visitor.rs b/dhall_syntax/src/core/visitor.rs index 49fff60..435771e 100644 --- a/dhall_syntax/src/core/visitor.rs +++ b/dhall_syntax/src/core/visitor.rs @@ -150,6 +150,9 @@ where v.visit_subexpr(y)?, opt(t, |e| v.visit_subexpr(e))?, ), + ToMap(x, t) => { + ToMap(v.visit_subexpr(x)?, opt(t, |e| v.visit_subexpr(e))?) + } Field(e, l) => Field(v.visit_subexpr(e)?, l.clone()), Projection(e, ls) => Projection(v.visit_subexpr(e)?, ls.clone()), Assert(e) => Assert(v.visit_subexpr(e)?), diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index defa79b..24ecc1e 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -827,6 +827,7 @@ make_parser! { rule!(assert<()>); rule!(if_<()>); rule!(in_<()>); + rule!(toMap<()>); rule!(empty_list_literal; span; children!( [application_expression(e)] => { @@ -863,6 +864,9 @@ make_parser! { [assert(()), expression(x)] => { spanned(span, Assert(x)) }, + [toMap(()), import_expression(x), application_expression(y)] => { + spanned(span, ToMap(x, Some(y))) + }, [operator_expression(e)] => e, [operator_expression(e), expression(annot)] => { spanned(span, Annot(e, annot)) @@ -934,7 +938,6 @@ make_parser! { )); rule!(Some_<()>); - rule!(toMap<()>); rule!(application_expression; children!( [first_application_expression(e)] => e, @@ -951,6 +954,9 @@ make_parser! { [merge(()), import_expression(x), import_expression(y)] => { spanned(span, Merge(x, y, None)) }, + [toMap(()), import_expression(x)] => { + spanned(span, ToMap(x, None)) + }, [import_expression(e)] => e, )); diff --git a/dhall_syntax/src/printer.rs b/dhall_syntax/src/printer.rs index 276590e..8571d11 100644 --- a/dhall_syntax/src/printer.rs +++ b/dhall_syntax/src/printer.rs @@ -41,6 +41,12 @@ impl Display for ExprF { write!(f, " : {}", c)?; } } + ToMap(a, b) => { + write!(f, "toMap {}", a)?; + if let Some(b) = b { + write!(f, " : {}", b)?; + } + } Annot(a, b) => { write!(f, "{} : {}", a, b)?; } @@ -144,6 +150,7 @@ impl RawExpr { | NEListLit(_) | SomeLit(_) | Merge(_, _, _) + | ToMap(_, _) | Annot(_, _) if phase > Base => { @@ -172,6 +179,10 @@ impl RawExpr { b.phase(PrintPhase::Import), c.map(|x| x.phase(PrintPhase::App)), ), + ToMap(a, b) => ToMap( + a.phase(PrintPhase::Import), + b.map(|x| x.phase(PrintPhase::App)), + ), Annot(a, b) => Annot(a.phase(Operator), b), ExprF::BinOp(op, a, b) => ExprF::BinOp( op, -- cgit v1.2.3 From befc4cda44a4f1e26aa0a301dfc92b455cbcbf18 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 31 Aug 2019 22:37:48 +0200 Subject: Don't URL-decode path segments --- dhall_syntax/Cargo.toml | 2 +- dhall_syntax/src/parser.rs | 27 ++++++++++++++++++++++----- dhall_syntax/src/printer.rs | 22 +++++----------------- 3 files changed, 28 insertions(+), 23 deletions(-) (limited to 'dhall_syntax') diff --git a/dhall_syntax/Cargo.toml b/dhall_syntax/Cargo.toml index 08d0195..1da10c7 100644 --- a/dhall_syntax/Cargo.toml +++ b/dhall_syntax/Cargo.toml @@ -10,7 +10,7 @@ doctest = false [dependencies] itertools = "0.8.0" -percent-encoding = "1.0.1" +percent-encoding = "2.1.0" pest = "2.1" either = "1.5.2" take_mut = "0.2.2" diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index 24ecc1e..53fd68a 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -669,12 +669,29 @@ make_parser! { rule!(unquoted_path_component<&'a str>; captured_str!(s) => s); rule!(quoted_path_component<&'a str>; captured_str!(s) => s); rule!(path_component; children!( - [unquoted_path_component(s)] => { - percent_encoding::percent_decode(s.as_bytes()) - .decode_utf8_lossy() - .into_owned() + [unquoted_path_component(s)] => s.to_string(), + [quoted_path_component(s)] => { + const RESERVED: &percent_encoding::AsciiSet = + &percent_encoding::CONTROLS + .add(b'=').add(b':').add(b'/').add(b'?') + .add(b'#').add(b'[').add(b']').add(b'@') + .add(b'!').add(b'$').add(b'&').add(b'\'') + .add(b'(').add(b')').add(b'*').add(b'+') + .add(b',').add(b';'); + s.chars() + .map(|c| { + // Percent-encode ascii chars + if c.is_ascii() { + percent_encoding::utf8_percent_encode( + &c.to_string(), + RESERVED, + ).to_string() + } else { + c.to_string() + } + }) + .collect() }, - [quoted_path_component(s)] => s.to_string(), )); rule!(path>; children!( [path_component(components)..] => { diff --git a/dhall_syntax/src/printer.rs b/dhall_syntax/src/printer.rs index 8571d11..f3dc648 100644 --- a/dhall_syntax/src/printer.rs +++ b/dhall_syntax/src/printer.rs @@ -360,15 +360,9 @@ impl Display for Import { use FilePrefix::*; use ImportLocation::*; use ImportMode::*; - let fmt_remote_path_component = |s: &str| -> String { - use percent_encoding::{ - utf8_percent_encode, PATH_SEGMENT_ENCODE_SET, - }; - utf8_percent_encode(s, PATH_SEGMENT_ENCODE_SET).to_string() - }; - let fmt_local_path_component = |s: &str| -> String { + let quote_if_needed = |s: &str| -> String { if s.chars().all(|c| c.is_ascii_alphanumeric()) { - s.to_owned() + s.to_string() } else { format!("\"{}\"", s) } @@ -383,19 +377,13 @@ impl Display for Import { Absolute => "", }; write!(f, "{}/", prefix)?; - let path: String = path - .iter() - .map(|c| fmt_local_path_component(c.as_ref())) - .join("/"); + let path: String = + path.iter().map(|c| quote_if_needed(&*c)).join("/"); f.write_str(&path)?; } Remote(url) => { write!(f, "{}://{}/", url.scheme, url.authority,)?; - let path: String = url - .path - .iter() - .map(|c| fmt_remote_path_component(c.as_ref())) - .join("/"); + let path: String = url.path.iter().join("/"); f.write_str(&path)?; if let Some(q) = &url.query { write!(f, "?{}", q)? -- cgit v1.2.3