summaryrefslogtreecommitdiff
path: root/dhall_syntax/src
diff options
context:
space:
mode:
authorFintan Halpenny2019-09-06 12:43:52 +0200
committerFintan Halpenny2019-09-06 12:43:52 +0200
commit4424d3ece5db10527ae446a685a579ae768ef943 (patch)
tree688d29c25f12c2c5347e3d24db8b9b7fb83c7b04 /dhall_syntax/src
parent52c91e08db68e05f760ebfd465b84fe4107731df (diff)
parentbcaeab3b7b114d8782be9589ad673ab9ab8c59fd (diff)
Merge remote-tracking branch 'origin/master' into fintan/canonicalize
Diffstat (limited to 'dhall_syntax/src')
-rw-r--r--dhall_syntax/src/core/expr.rs101
-rw-r--r--dhall_syntax/src/core/import.rs34
-rw-r--r--dhall_syntax/src/core/map.rs65
-rw-r--r--dhall_syntax/src/core/text.rs56
-rw-r--r--dhall_syntax/src/core/visitor.rs412
-rw-r--r--dhall_syntax/src/parser.rs446
6 files changed, 646 insertions, 468 deletions
diff --git a/dhall_syntax/src/core/expr.rs b/dhall_syntax/src/core/expr.rs
index eeee4d8..2cb23c9 100644
--- a/dhall_syntax/src/core/expr.rs
+++ b/dhall_syntax/src/core/expr.rs
@@ -1,7 +1,7 @@
use std::rc::Rc;
use crate::map::{DupTreeMap, DupTreeSet};
-use crate::visitor;
+use crate::visitor::{self, ExprFMutVisitor, ExprFVisitor};
use crate::*;
pub type Integer = isize;
@@ -256,13 +256,6 @@ pub enum ExprF<SubExpr, Embed> {
}
impl<SE, E> ExprF<SE, E> {
- pub(crate) fn visit<'a, V, Return>(&'a self, v: V) -> Return
- where
- V: visitor::GenericVisitor<&'a ExprF<SE, E>, Return>,
- {
- v.visit(self)
- }
-
pub fn traverse_ref_with_special_handling_of_binders<'a, SE2, Err>(
&'a self,
visit_subexpr: impl FnMut(&'a SE) -> Result<SE2, Err>,
@@ -271,10 +264,11 @@ impl<SE, E> ExprF<SE, E> {
where
E: Clone,
{
- self.visit(visitor::TraverseRefWithBindersVisitor {
+ visitor::TraverseRefWithBindersVisitor {
visit_subexpr,
visit_under_binder,
- })
+ }
+ .visit(self)
}
fn traverse_ref<'a, SE2, Err>(
@@ -284,7 +278,14 @@ impl<SE, E> ExprF<SE, E> {
where
E: Clone,
{
- self.visit(visitor::TraverseRefVisitor { visit_subexpr })
+ visitor::TraverseRefVisitor { visit_subexpr }.visit(self)
+ }
+
+ fn traverse_mut<'a, Err>(
+ &'a mut self,
+ visit_subexpr: impl FnMut(&'a mut SE) -> Result<(), Err>,
+ ) -> Result<(), Err> {
+ visitor::TraverseMutVisitor { visit_subexpr }.visit(self)
}
pub fn map_ref_with_special_handling_of_binders<'a, SE2>(
@@ -310,38 +311,9 @@ impl<SE, E> ExprF<SE, E> {
{
trivial_result(self.traverse_ref(|x| Ok(map_subexpr(x))))
}
-}
-impl<E> RawExpr<E> {
- pub fn traverse_resolve<E2, Err>(
- &self,
- visit_import: impl FnMut(&Import<Expr<E2>>) -> Result<E2, Err>,
- ) -> Result<RawExpr<E2>, Err> {
- self.traverse_resolve_with_visitor(&mut visitor::ResolveVisitor(
- visit_import,
- ))
- }
-
- pub(crate) fn traverse_resolve_with_visitor<E2, Err, F1>(
- &self,
- visitor: &mut visitor::ResolveVisitor<F1>,
- ) -> Result<RawExpr<E2>, Err>
- where
- F1: FnMut(&Import<Expr<E2>>) -> Result<E2, Err>,
- {
- match self {
- ExprF::BinOp(BinOp::ImportAlt, l, r) => l
- .as_ref()
- .traverse_resolve_with_visitor(visitor)
- .or_else(|_| r.as_ref().traverse_resolve_with_visitor(visitor)),
- _ => {
- let e = self.visit(&mut *visitor)?;
- Ok(match &e {
- ExprF::Import(import) => ExprF::Embed((visitor.0)(import)?),
- _ => e,
- })
- }
- }
+ pub fn map_mut<'a>(&'a mut self, mut map_subexpr: impl FnMut(&'a mut SE)) {
+ trivial_result(self.traverse_mut(|x| Ok(map_subexpr(x))))
}
}
@@ -349,6 +321,9 @@ impl<E> Expr<E> {
pub fn as_ref(&self) -> &RawExpr<E> {
&self.0.as_ref().0
}
+ pub fn as_mut(&mut self) -> &mut RawExpr<E> {
+ &mut self.0.as_mut().0
+ }
pub fn new(x: RawExpr<E>, n: Span) -> Self {
Expr(Box::new((x, Some(n))))
@@ -365,14 +340,42 @@ impl<E> Expr<E> {
pub fn rewrap<E2>(&self, x: RawExpr<E2>) -> Expr<E2> {
Expr(Box::new((x, (self.0).1.clone())))
}
-}
-impl<E> Expr<E> {
- pub fn traverse_resolve<E2, Err>(
- &self,
- visit_import: impl FnMut(&Import<Expr<E2>>) -> Result<E2, Err>,
- ) -> Result<Expr<E2>, Err> {
- Ok(self.rewrap(self.as_ref().traverse_resolve(visit_import)?))
+ pub fn traverse_resolve_mut<Err, F1>(
+ &mut self,
+ f: &mut F1,
+ ) -> Result<(), Err>
+ where
+ E: Clone,
+ F1: FnMut(Import<Expr<E>>) -> Result<E, Err>,
+ {
+ match self.as_mut() {
+ ExprF::BinOp(BinOp::ImportAlt, l, r) => {
+ let garbage_expr = ExprF::BoolLit(false);
+ let new_self = if l.traverse_resolve_mut(f).is_ok() {
+ l
+ } else {
+ r.traverse_resolve_mut(f)?;
+ r
+ };
+ *self.as_mut() =
+ std::mem::replace(new_self.as_mut(), garbage_expr);
+ }
+ _ => {
+ self.as_mut().traverse_mut(|e| e.traverse_resolve_mut(f))?;
+ if let ExprF::Import(import) = self.as_mut() {
+ let garbage_import = Import {
+ mode: ImportMode::Code,
+ location: ImportLocation::Missing,
+ hash: None,
+ };
+ // Move out of &mut import
+ let import = std::mem::replace(import, garbage_import);
+ *self.as_mut() = ExprF::Embed(f(import)?);
+ }
+ }
+ }
+ Ok(())
}
}
diff --git a/dhall_syntax/src/core/import.rs b/dhall_syntax/src/core/import.rs
index cc38bb0..da3e99b 100644
--- a/dhall_syntax/src/core/import.rs
+++ b/dhall_syntax/src/core/import.rs
@@ -62,7 +62,7 @@ pub struct Import<SubExpr> {
}
impl<SE> URL<SE> {
- pub fn visit_subexpr<'a, Err, SE2>(
+ pub fn traverse_ref<'a, Err, SE2>(
&'a self,
f: impl FnOnce(&'a SE) -> Result<SE2, Err>,
) -> Result<URL<SE2>, Err> {
@@ -75,32 +75,56 @@ impl<SE> URL<SE> {
headers,
})
}
+ pub fn traverse_mut<'a, Err>(
+ &'a mut self,
+ f: impl FnOnce(&'a mut SE) -> Result<(), Err>,
+ ) -> Result<(), Err> {
+ if let Some(header) = &mut self.headers {
+ f(header)?;
+ }
+ Ok(())
+ }
}
impl<SE> ImportLocation<SE> {
- pub fn visit_subexpr<'a, Err, SE2>(
+ pub fn traverse_ref<'a, Err, SE2>(
&'a self,
f: impl FnOnce(&'a SE) -> Result<SE2, Err>,
) -> Result<ImportLocation<SE2>, Err> {
use ImportLocation::*;
Ok(match self {
Local(prefix, path) => Local(*prefix, path.clone()),
- Remote(url) => Remote(url.visit_subexpr(f)?),
+ Remote(url) => Remote(url.traverse_ref(f)?),
Env(env) => Env(env.clone()),
Missing => Missing,
})
}
+ pub fn traverse_mut<'a, Err>(
+ &'a mut self,
+ f: impl FnOnce(&'a mut SE) -> Result<(), Err>,
+ ) -> Result<(), Err> {
+ if let ImportLocation::Remote(url) = self {
+ url.traverse_mut(f)?;
+ }
+ Ok(())
+ }
}
impl<SE> Import<SE> {
- pub fn visit_subexpr<'a, Err, SE2>(
+ pub fn traverse_ref<'a, Err, SE2>(
&'a self,
f: impl FnOnce(&'a SE) -> Result<SE2, Err>,
) -> Result<Import<SE2>, Err> {
Ok(Import {
mode: self.mode,
- location: self.location.visit_subexpr(f)?,
+ location: self.location.traverse_ref(f)?,
hash: self.hash.clone(),
})
}
+ pub fn traverse_mut<'a, Err>(
+ &'a mut self,
+ f: impl FnOnce(&'a mut SE) -> Result<(), Err>,
+ ) -> Result<(), Err> {
+ self.location.traverse_mut(f)
+ }
}
diff --git a/dhall_syntax/src/core/map.rs b/dhall_syntax/src/core/map.rs
index 6a0ebda..c4c6126 100644
--- a/dhall_syntax/src/core/map.rs
+++ b/dhall_syntax/src/core/map.rs
@@ -13,6 +13,8 @@ mod one_or_more {
}
pub type Iter<'a, T> = Either<slice::Iter<'a, T>, iter::Once<&'a T>>;
+ pub type IterMut<'a, T> =
+ Either<slice::IterMut<'a, T>, iter::Once<&'a mut T>>;
pub type IntoIter<T> = Either<vec::IntoIter<T>, iter::Once<T>>;
impl<T> OneOrMore<T> {
@@ -36,6 +38,13 @@ mod one_or_more {
OneOrMore::One(x) => Either::Right(iter::once(x)),
}
}
+
+ pub fn iter_mut(&mut self) -> IterMut<'_, T> {
+ match self {
+ OneOrMore::More(vec) => Either::Left(vec.iter_mut()),
+ OneOrMore::One(x) => Either::Right(iter::once(x)),
+ }
+ }
}
impl<T> IntoIterator for OneOrMore<T> {
@@ -76,6 +85,19 @@ mod dup_tree_map {
iter: IterInternal<'a, K, V>,
size: usize,
}
+ pub type IterMutInternalIntermediate<'a, K, V> =
+ iter::Zip<iter::Repeat<&'a K>, one_or_more::IterMut<'a, V>>;
+ pub type IterMutInternal<'a, K, V> = iter::FlatMap<
+ btree_map::IterMut<'a, K, OneOrMore<V>>,
+ IterMutInternalIntermediate<'a, K, V>,
+ for<'b> fn(
+ (&'b K, &'b mut OneOrMore<V>),
+ ) -> IterMutInternalIntermediate<'b, K, V>,
+ >;
+ pub struct IterMut<'a, K, V> {
+ iter: IterMutInternal<'a, K, V>,
+ size: usize,
+ }
pub type IntoIterInternalIntermediate<K, V> =
iter::Zip<iter::Repeat<K>, one_or_more::IntoIter<V>>;
pub type IntoIterInternal<K, V> = iter::FlatMap<
@@ -134,6 +156,21 @@ mod dup_tree_map {
size: self.size,
}
}
+
+ pub fn iter_mut(&mut self) -> IterMut<'_, K, V>
+ where
+ K: Ord,
+ {
+ fn foo<'a, K, V>(
+ (k, oom): (&'a K, &'a mut OneOrMore<V>),
+ ) -> IterMutInternalIntermediate<'a, K, V> {
+ iter::repeat(k).zip(oom.iter_mut())
+ }
+ IterMut {
+ iter: self.map.iter_mut().flat_map(foo),
+ size: self.size,
+ }
+ }
}
impl<K, V> Default for DupTreeMap<K, V>
@@ -180,6 +217,18 @@ mod dup_tree_map {
}
}
+ impl<'a, K, V> IntoIterator for &'a mut DupTreeMap<K, V>
+ where
+ K: Ord,
+ {
+ type Item = (&'a K, &'a mut V);
+ type IntoIter = IterMut<'a, K, V>;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.iter_mut()
+ }
+ }
+
impl<K, V> iter::FromIterator<(K, V)> for DupTreeMap<K, V>
where
K: Ord,
@@ -212,6 +261,22 @@ mod dup_tree_map {
}
}
+ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
+ type Item = (&'a K, &'a mut V);
+
+ fn next(&mut self) -> Option<Self::Item> {
+ let next = self.iter.next();
+ if next.is_some() {
+ self.size -= 1;
+ }
+ next
+ }
+
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ (self.size, Some(self.size))
+ }
+ }
+
impl<K, V> Iterator for IntoIter<K, V>
where
K: Clone,
diff --git a/dhall_syntax/src/core/text.rs b/dhall_syntax/src/core/text.rs
index 10fd68a..fb390ee 100644
--- a/dhall_syntax/src/core/text.rs
+++ b/dhall_syntax/src/core/text.rs
@@ -40,6 +40,52 @@ impl<SubExpr> InterpolatedTextContents<SubExpr> {
Text(s) => s.is_empty(),
}
}
+
+ pub fn traverse_ref<'a, SubExpr2, E, F>(
+ &'a self,
+ mut f: F,
+ ) -> Result<InterpolatedTextContents<SubExpr2>, E>
+ where
+ F: FnMut(&'a SubExpr) -> Result<SubExpr2, E>,
+ {
+ use InterpolatedTextContents::{Expr, Text};
+ Ok(match self {
+ Expr(e) => Expr(f(e)?),
+ Text(s) => Text(s.clone()),
+ })
+ }
+ pub fn traverse_mut<'a, E, F>(&'a mut self, mut f: F) -> Result<(), E>
+ where
+ F: FnMut(&'a mut SubExpr) -> Result<(), E>,
+ {
+ use InterpolatedTextContents::Expr;
+ if let Expr(e) = self {
+ f(e)?;
+ }
+ Ok(())
+ }
+ pub fn map_ref<'a, SubExpr2, F>(
+ &'a self,
+ mut f: F,
+ ) -> InterpolatedTextContents<SubExpr2>
+ where
+ F: FnMut(&'a SubExpr) -> SubExpr2,
+ {
+ use InterpolatedTextContents::{Expr, Text};
+ match self {
+ Expr(e) => Expr(f(e)),
+ Text(s) => Text(s.clone()),
+ }
+ }
+ pub fn map_mut<'a, F>(&'a mut self, mut f: F)
+ where
+ F: FnMut(&'a mut SubExpr),
+ {
+ use InterpolatedTextContents::Expr;
+ if let Expr(e) = self {
+ f(e);
+ }
+ }
}
impl<SubExpr> InterpolatedText<SubExpr> {
@@ -76,6 +122,16 @@ impl<SubExpr> InterpolatedText<SubExpr> {
})
}
+ pub fn traverse_mut<'a, E, F>(&'a mut self, mut f: F) -> Result<(), E>
+ where
+ F: FnMut(&'a mut SubExpr) -> Result<(), E>,
+ {
+ for (e, _) in &mut self.tail {
+ f(e)?
+ }
+ Ok(())
+ }
+
pub fn iter<'a>(
&'a self,
) -> impl Iterator<Item = InterpolatedTextContents<&'a SubExpr>> + 'a {
diff --git a/dhall_syntax/src/core/visitor.rs b/dhall_syntax/src/core/visitor.rs
index 435771e..39a027f 100644
--- a/dhall_syntax/src/core/visitor.rs
+++ b/dhall_syntax/src/core/visitor.rs
@@ -1,11 +1,6 @@
use crate::*;
use std::iter::FromIterator;
-/// A way too generic Visitor trait.
-pub trait GenericVisitor<Input, Output>: 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_ref_with_special_handling_of_binders` cannot be made using only
@@ -14,7 +9,7 @@ pub trait GenericVisitor<Input, Output>: Sized {
/// 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 {
+pub trait ExprFVisitor<'a, SE1, SE2, E1, E2>: Sized {
type Error;
fn visit_subexpr(&mut self, subexpr: &'a SE1) -> Result<SE2, Self::Error>;
@@ -27,174 +22,263 @@ pub trait ExprFFallibleVisitor<'a, SE1, SE2, E1, E2>: Sized {
) -> Result<SE2, Self::Error> {
self.visit_subexpr(subexpr)
}
+
+ fn visit(
+ self,
+ input: &'a ExprF<SE1, E1>,
+ ) -> Result<ExprF<SE2, E2>, Self::Error> {
+ visit_ref(self, input)
+ }
}
-/// 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;
+/// Like `ExprFVisitor`, but by mutable reference
+pub trait ExprFMutVisitor<'a, SE, E>: Sized {
+ type Error;
+
+ fn visit_subexpr(&mut self, subexpr: &'a mut SE)
+ -> Result<(), Self::Error>;
+ fn visit_embed(self, _embed: &'a mut E) -> Result<(), Self::Error> {
+ Ok(())
+ }
fn visit_subexpr_under_binder(
mut self,
- _label: &'a Label,
- subexpr: &'a SE1,
- ) -> SE2 {
+ _label: &'a mut Label,
+ subexpr: &'a mut SE,
+ ) -> Result<(), Self::Error> {
self.visit_subexpr(subexpr)
}
+
+ fn visit(self, input: &'a mut ExprF<SE, E>) -> Result<(), Self::Error> {
+ visit_mut(self, input)
+ }
}
-impl<'a, T, SE1, SE2, E1, E2>
- GenericVisitor<&'a ExprF<SE1, E1>, Result<ExprF<SE2, E2>, T::Error>> for T
+fn visit_ref<'a, V, SE1, SE2, E1, E2>(
+ mut v: V,
+ input: &'a ExprF<SE1, E1>,
+) -> Result<ExprF<SE2, E2>, V::Error>
where
- T: ExprFFallibleVisitor<'a, SE1, SE2, E1, E2>,
+ V: ExprFVisitor<'a, SE1, SE2, E1, E2>,
{
- fn visit(
- self,
- input: &'a ExprF<SE1, E1>,
- ) -> Result<ExprF<SE2, E2>, T::Error> {
- fn vec<'a, T, U, Err, F: FnMut(&'a T) -> Result<U, Err>>(
- x: &'a [T],
- f: F,
- ) -> Result<Vec<U>, Err> {
- x.iter().map(f).collect()
- }
- fn opt<'a, T, U, Err, F: FnOnce(&'a T) -> Result<U, Err>>(
- x: &'a Option<T>,
- f: F,
- ) -> Result<Option<U>, Err> {
- Ok(match x {
- Some(x) => Some(f(x)?),
- None => None,
+ fn vec<'a, T, U, Err, F: FnMut(&'a T) -> Result<U, Err>>(
+ x: &'a [T],
+ f: F,
+ ) -> Result<Vec<U>, Err> {
+ x.iter().map(f).collect()
+ }
+ fn opt<'a, T, U, Err, F: FnOnce(&'a T) -> Result<U, Err>>(
+ x: &'a Option<T>,
+ f: F,
+ ) -> Result<Option<U>, Err> {
+ Ok(match x {
+ Some(x) => Some(f(x)?),
+ None => None,
+ })
+ }
+ fn dupmap<'a, V, SE1, SE2, E1, E2, T>(
+ x: impl IntoIterator<Item = (&'a Label, &'a SE1)>,
+ mut v: V,
+ ) -> Result<T, V::Error>
+ where
+ SE1: 'a,
+ T: FromIterator<(Label, SE2)>,
+ V: ExprFVisitor<'a, SE1, SE2, E1, E2>,
+ {
+ x.into_iter()
+ .map(|(k, x)| Ok((k.clone(), v.visit_subexpr(x)?)))
+ .collect()
+ }
+ fn optdupmap<'a, V, SE1, SE2, E1, E2, T>(
+ x: impl IntoIterator<Item = (&'a Label, &'a Option<SE1>)>,
+ mut v: V,
+ ) -> Result<T, V::Error>
+ where
+ SE1: 'a,
+ T: FromIterator<(Label, Option<SE2>)>,
+ V: ExprFVisitor<'a, SE1, SE2, E1, E2>,
+ {
+ x.into_iter()
+ .map(|(k, x)| {
+ Ok((
+ k.clone(),
+ match x {
+ Some(x) => Some(v.visit_subexpr(x)?),
+ None => None,
+ },
+ ))
})
+ .collect()
+ }
+
+ use crate::ExprF::*;
+ Ok(match input {
+ Var(v) => Var(v.clone()),
+ Lam(l, t, e) => {
+ let t = v.visit_subexpr(t)?;
+ let e = v.visit_subexpr_under_binder(l, e)?;
+ Lam(l.clone(), t, e)
}
- fn dupmap<'a, V, SE1, SE2, E1, E2, T>(
- x: impl IntoIterator<Item = (&'a Label, &'a SE1)>,
- mut v: V,
- ) -> Result<T, V::Error>
- where
- 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()
+ Pi(l, t, e) => {
+ let t = v.visit_subexpr(t)?;
+ let e = v.visit_subexpr_under_binder(l, e)?;
+ Pi(l.clone(), t, e)
}
- fn optdupmap<'a, V, SE1, SE2, E1, E2, T>(
- x: impl IntoIterator<Item = (&'a Label, &'a Option<SE1>)>,
- mut v: V,
- ) -> Result<T, V::Error>
- where
- SE1: 'a,
- T: FromIterator<(Label, Option<SE2>)>,
- V: ExprFFallibleVisitor<'a, SE1, SE2, E1, E2>,
- {
- x.into_iter()
- .map(|(k, x)| {
- Ok((
- k.clone(),
- match x {
- Some(x) => Some(v.visit_subexpr(x)?),
- None => None,
- },
- ))
- })
- .collect()
+ Let(l, t, a, e) => {
+ let t = opt(t, &mut |e| v.visit_subexpr(e))?;
+ let a = v.visit_subexpr(a)?;
+ let e = v.visit_subexpr_under_binder(l, e)?;
+ Let(l.clone(), t, a, e)
}
-
- let mut v = self;
- use crate::ExprF::*;
- Ok(match input {
- Var(v) => Var(v.clone()),
- Lam(l, t, e) => {
- let t = v.visit_subexpr(t)?;
- let e = v.visit_subexpr_under_binder(l, e)?;
- Lam(l.clone(), t, e)
- }
- Pi(l, t, e) => {
- let t = v.visit_subexpr(t)?;
- let e = v.visit_subexpr_under_binder(l, e)?;
- Pi(l.clone(), t, e)
- }
- Let(l, t, a, e) => {
- let t = opt(t, &mut |e| v.visit_subexpr(e))?;
- let a = v.visit_subexpr(a)?;
- let e = v.visit_subexpr_under_binder(l, e)?;
- Let(l.clone(), t, a, e)
- }
- App(f, a) => App(v.visit_subexpr(f)?, v.visit_subexpr(a)?),
- Annot(x, t) => Annot(v.visit_subexpr(x)?, v.visit_subexpr(t)?),
- Const(k) => Const(*k),
- Builtin(v) => Builtin(*v),
- BoolLit(b) => BoolLit(*b),
- NaturalLit(n) => NaturalLit(*n),
- IntegerLit(n) => IntegerLit(*n),
- DoubleLit(n) => DoubleLit(*n),
- TextLit(t) => TextLit(t.traverse_ref(|e| v.visit_subexpr(e))?),
- BinOp(o, x, y) => {
- BinOp(*o, v.visit_subexpr(x)?, v.visit_subexpr(y)?)
- }
- BoolIf(b, t, f) => BoolIf(
- v.visit_subexpr(b)?,
- v.visit_subexpr(t)?,
- v.visit_subexpr(f)?,
- ),
- EmptyListLit(t) => EmptyListLit(v.visit_subexpr(t)?),
- NEListLit(es) => NEListLit(vec(es, |e| v.visit_subexpr(e))?),
- SomeLit(e) => SomeLit(v.visit_subexpr(e)?),
- RecordType(kts) => RecordType(dupmap(kts, v)?),
- RecordLit(kvs) => RecordLit(dupmap(kvs, v)?),
- UnionType(kts) => UnionType(optdupmap(kts, v)?),
- Merge(x, y, t) => Merge(
- v.visit_subexpr(x)?,
- 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)?),
- Import(i) => Import(i.visit_subexpr(|e| v.visit_subexpr(e))?),
- Embed(a) => Embed(v.visit_embed(a)?),
- })
- }
+ App(f, a) => App(v.visit_subexpr(f)?, v.visit_subexpr(a)?),
+ Annot(x, t) => Annot(v.visit_subexpr(x)?, v.visit_subexpr(t)?),
+ Const(k) => Const(*k),
+ Builtin(v) => Builtin(*v),
+ BoolLit(b) => BoolLit(*b),
+ NaturalLit(n) => NaturalLit(*n),
+ IntegerLit(n) => IntegerLit(*n),
+ DoubleLit(n) => DoubleLit(*n),
+ TextLit(t) => TextLit(t.traverse_ref(|e| v.visit_subexpr(e))?),
+ BinOp(o, x, y) => BinOp(*o, v.visit_subexpr(x)?, v.visit_subexpr(y)?),
+ BoolIf(b, t, f) => BoolIf(
+ v.visit_subexpr(b)?,
+ v.visit_subexpr(t)?,
+ v.visit_subexpr(f)?,
+ ),
+ EmptyListLit(t) => EmptyListLit(v.visit_subexpr(t)?),
+ NEListLit(es) => NEListLit(vec(es, |e| v.visit_subexpr(e))?),
+ SomeLit(e) => SomeLit(v.visit_subexpr(e)?),
+ RecordType(kts) => RecordType(dupmap(kts, v)?),
+ RecordLit(kvs) => RecordLit(dupmap(kvs, v)?),
+ UnionType(kts) => UnionType(optdupmap(kts, v)?),
+ Merge(x, y, t) => Merge(
+ v.visit_subexpr(x)?,
+ 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)?),
+ Import(i) => Import(i.traverse_ref(|e| v.visit_subexpr(e))?),
+ Embed(a) => Embed(v.visit_embed(a)?),
+ })
}
-impl<'a, T, SE1, SE2, E1, E2> GenericVisitor<&'a ExprF<SE1, E1>, ExprF<SE2, E2>>
- for T
+fn visit_mut<'a, V, SE, E>(
+ mut v: V,
+ input: &'a mut ExprF<SE, E>,
+) -> Result<(), V::Error>
where
- T: ExprFInFallibleVisitor<'a, SE1, SE2, E1, E2>,
+ V: ExprFMutVisitor<'a, SE, E>,
{
- fn visit(self, input: &'a ExprF<SE1, E1>) -> ExprF<SE2, E2> {
- trivial_result(InfallibleWrapper(self).visit(input))
+ fn vec<'a, V, SE, E>(v: &mut V, x: &'a mut Vec<SE>) -> Result<(), V::Error>
+ where
+ V: ExprFMutVisitor<'a, SE, E>,
+ {
+ for x in x {
+ v.visit_subexpr(x)?;
+ }
+ Ok(())
}
-}
-
-struct InfallibleWrapper<T>(T);
-
-impl<'a, T, SE1, SE2, E1, E2> ExprFFallibleVisitor<'a, SE1, SE2, E1, E2>
- for InfallibleWrapper<T>
-where
- T: ExprFInFallibleVisitor<'a, SE1, SE2, E1, E2>,
-{
- type Error = !;
-
- fn visit_subexpr(&mut self, subexpr: &'a SE1) -> Result<SE2, Self::Error> {
- Ok(self.0.visit_subexpr(subexpr))
+ fn opt<'a, V, SE, E>(
+ v: &mut V,
+ x: &'a mut Option<SE>,
+ ) -> Result<(), V::Error>
+ where
+ V: ExprFMutVisitor<'a, SE, E>,
+ {
+ if let Some(x) = x {
+ v.visit_subexpr(x)?;
+ }
+ Ok(())
}
- fn visit_embed(self, embed: &'a E1) -> Result<E2, Self::Error> {
- Ok(self.0.visit_embed(embed))
+ fn dupmap<'a, V, SE, E>(
+ mut v: V,
+ x: impl IntoIterator<Item = (&'a Label, &'a mut SE)>,
+ ) -> Result<(), V::Error>
+ where
+ SE: 'a,
+ V: ExprFMutVisitor<'a, SE, E>,
+ {
+ for (_, x) in x {
+ v.visit_subexpr(x)?;
+ }
+ Ok(())
+ }
+ fn optdupmap<'a, V, SE, E>(
+ mut v: V,
+ x: impl IntoIterator<Item = (&'a Label, &'a mut Option<SE>)>,
+ ) -> Result<(), V::Error>
+ where
+ SE: 'a,
+ V: ExprFMutVisitor<'a, SE, E>,
+ {
+ for (_, x) in x {
+ opt(&mut v, x)?;
+ }
+ Ok(())
}
- fn visit_subexpr_under_binder(
- self,
- label: &'a Label,
- subexpr: &'a SE1,
- ) -> Result<SE2, Self::Error> {
- Ok(self.0.visit_subexpr_under_binder(label, subexpr))
+ use crate::ExprF::*;
+ match input {
+ Var(_) | Const(_) | Builtin(_) | BoolLit(_) | NaturalLit(_)
+ | IntegerLit(_) | DoubleLit(_) => {}
+ Lam(l, t, e) => {
+ v.visit_subexpr(t)?;
+ v.visit_subexpr_under_binder(l, e)?;
+ }
+ Pi(l, t, e) => {
+ v.visit_subexpr(t)?;
+ v.visit_subexpr_under_binder(l, e)?;
+ }
+ Let(l, t, a, e) => {
+ opt(&mut v, t)?;
+ v.visit_subexpr(a)?;
+ v.visit_subexpr_under_binder(l, e)?;
+ }
+ App(f, a) => {
+ v.visit_subexpr(f)?;
+ v.visit_subexpr(a)?;
+ }
+ Annot(x, t) => {
+ v.visit_subexpr(x)?;
+ v.visit_subexpr(t)?;
+ }
+ TextLit(t) => t.traverse_mut(|e| v.visit_subexpr(e))?,
+ BinOp(_, x, y) => {
+ v.visit_subexpr(x)?;
+ v.visit_subexpr(y)?;
+ }
+ BoolIf(b, t, f) => {
+ v.visit_subexpr(b)?;
+ v.visit_subexpr(t)?;
+ v.visit_subexpr(f)?;
+ }
+ EmptyListLit(t) => v.visit_subexpr(t)?,
+ NEListLit(es) => vec(&mut v, es)?,
+ SomeLit(e) => v.visit_subexpr(e)?,
+ RecordType(kts) => dupmap(v, kts)?,
+ RecordLit(kvs) => dupmap(v, kvs)?,
+ UnionType(kts) => optdupmap(v, kts)?,
+ Merge(x, y, t) => {
+ v.visit_subexpr(x)?;
+ v.visit_subexpr(y)?;
+ opt(&mut v, t)?;
+ }
+ ToMap(x, t) => {
+ v.visit_subexpr(x)?;
+ opt(&mut v, t)?;
+ }
+ Field(e, _) => v.visit_subexpr(e)?,
+ Projection(e, _) => v.visit_subexpr(e)?,
+ Assert(e) => v.visit_subexpr(e)?,
+ Import(i) => i.traverse_mut(|e| v.visit_subexpr(e))?,
+ Embed(a) => v.visit_embed(a)?,
}
+ Ok(())
}
pub struct TraverseRefWithBindersVisitor<F1, F2> {
@@ -202,7 +286,7 @@ pub struct TraverseRefWithBindersVisitor<F1, F2> {
pub visit_under_binder: F2,
}
-impl<'a, SE, E, SE2, Err, F1, F2> ExprFFallibleVisitor<'a, SE, SE2, E, E>
+impl<'a, SE, E, SE2, Err, F1, F2> ExprFVisitor<'a, SE, SE2, E, E>
for TraverseRefWithBindersVisitor<F1, F2>
where
SE: 'a,
@@ -231,7 +315,7 @@ pub struct TraverseRefVisitor<F1> {
pub visit_subexpr: F1,
}
-impl<'a, SE, E, SE2, Err, F1> ExprFFallibleVisitor<'a, SE, SE2, E, E>
+impl<'a, SE, E, SE2, Err, F1> ExprFVisitor<'a, SE, SE2, E, E>
for TraverseRefVisitor<F1>
where
SE: 'a,
@@ -248,26 +332,22 @@ where
}
}
-pub struct ResolveVisitor<F1>(pub F1);
+pub struct TraverseMutVisitor<F1> {
+ pub visit_subexpr: F1,
+}
-impl<'a, 'b, E, E2, Err, F1> ExprFFallibleVisitor<'a, Expr<E>, Expr<E2>, E, E2>
- for &'b mut ResolveVisitor<F1>
+impl<'a, SE, E, Err, F1> ExprFMutVisitor<'a, SE, E> for TraverseMutVisitor<F1>
where
- F1: FnMut(&Import<Expr<E2>>) -> Result<E2, Err>,
+ SE: 'a,
+ E: 'a,
+ F1: FnMut(&'a mut SE) -> Result<(), Err>,
{
type Error = Err;
fn visit_subexpr(
&mut self,
- subexpr: &'a Expr<E>,
- ) -> Result<Expr<E2>, Self::Error> {
- Ok(subexpr.rewrap(
- subexpr
- .as_ref()
- .traverse_resolve_with_visitor(&mut **self)?,
- ))
- }
- fn visit_embed(self, _embed: &'a E) -> Result<E2, Self::Error> {
- unimplemented!()
+ subexpr: &'a mut SE,
+ ) -> Result<(), Self::Error> {
+ (self.visit_subexpr)(subexpr)
}
}
diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs
index 1262774..41d6d04 100644
--- a/dhall_syntax/src/parser.rs
+++ b/dhall_syntax/src/parser.rs
@@ -60,17 +60,32 @@ impl<'input> ParseInput<'input, Rule> {
original_input_str: self.original_input_str.clone(),
}
}
+ /// If the contained pair has exactly one child, return a new Self containing it.
+ fn single_child(&self) -> Option<Self> {
+ let mut children = self.pair.clone().into_inner();
+ if let Some(child) = children.next() {
+ if children.next().is_none() {
+ return Some(self.with_pair(child));
+ }
+ }
+ None
+ }
fn as_span(&self) -> Span {
Span::make(self.original_input_str.clone(), self.pair.as_span())
}
fn as_str(&self) -> &'input str {
self.pair.as_str()
}
+ fn as_rule(&self) -> Rule {
+ self.pair.as_rule()
+ }
}
-// Used to retrieve the `Rule` enum associated with the `Self` type in `parse_children`.
+// Used by the macros.
trait PestConsumer {
- type RuleEnum: pest::RuleType;
+ type Rule: pest::RuleType;
+ fn rule_alias(rule: Self::Rule) -> String;
+ fn allows_shortcut(rule: Self::Rule) -> bool;
}
fn debug_pair(pair: Pair<Rule>) -> String {
@@ -231,21 +246,17 @@ struct Parsers;
#[make_parser(Rule)]
impl Parsers {
- fn EOI(_: ParseInput<Rule>) -> ParseResult<()> {
+ fn EOI(_input: ParseInput<Rule>) -> ParseResult<()> {
Ok(())
}
+ #[alias(label)]
fn simple_label(input: ParseInput<Rule>) -> ParseResult<Label> {
- Ok(Label::from(input.as_str().trim().to_owned()))
+ Ok(Label::from(input.as_str()))
}
+ #[alias(label)]
fn quoted_label(input: ParseInput<Rule>) -> ParseResult<Label> {
- Ok(Label::from(input.as_str().trim().to_owned()))
- }
- fn label(input: ParseInput<Rule>) -> ParseResult<Label> {
- Ok(parse_children!(input;
- [simple_label(l)] => l,
- [quoted_label(l)] => l,
- ))
+ Ok(Label::from(input.as_str()))
}
fn double_quote_literal<E: Clone>(
@@ -262,17 +273,15 @@ impl Parsers {
input: ParseInput<Rule>,
) -> ParseResult<ParsedTextContents<E>> {
Ok(parse_children!(input;
- [interpolation(e)] => {
+ [expression(e)] => {
InterpolatedTextContents::Expr(e)
},
- [double_quote_escaped(s)] => {
- InterpolatedTextContents::Text(s)
- },
[double_quote_char(s)] => {
- InterpolatedTextContents::Text(s.to_owned())
+ InterpolatedTextContents::Text(s)
},
))
}
+ #[alias(double_quote_char)]
fn double_quote_escaped(input: ParseInput<Rule>) -> ParseResult<String> {
Ok(match input.as_str() {
"\"" => "\"".to_owned(),
@@ -345,8 +354,8 @@ impl Parsers {
}
fn double_quote_char<'a>(
input: ParseInput<'a, Rule>,
- ) -> ParseResult<&'a str> {
- Ok(input.as_str())
+ ) -> ParseResult<String> {
+ Ok(input.as_str().to_owned())
}
fn single_quote_literal<E: Clone>(
@@ -356,6 +365,7 @@ impl Parsers {
[single_quote_continue(lines)] => {
let newline: ParsedText<E> = "\n".to_string().into();
+ // Reverse lines and chars in each line
let mut lines: Vec<ParsedText<E>> = lines
.into_iter()
.rev()
@@ -377,44 +387,27 @@ impl Parsers {
) -> ParseResult<&'a str> {
Ok(input.as_str())
}
- fn escaped_quote_pair<'a>(_: ParseInput<'a, Rule>) -> ParseResult<&'a str> {
+ #[alias(single_quote_char)]
+ fn escaped_quote_pair<'a>(
+ _input: ParseInput<'a, Rule>,
+ ) -> ParseResult<&'a str> {
Ok("''")
}
+ #[alias(single_quote_char)]
fn escaped_interpolation<'a>(
- _: ParseInput<'a, Rule>,
+ _input: ParseInput<'a, Rule>,
) -> ParseResult<&'a str> {
Ok("${")
}
- fn interpolation<E: Clone>(
- input: ParseInput<Rule>,
- ) -> ParseResult<Expr<E>> {
- Ok(parse_children!(input;
- [expression(e)] => e
- ))
- }
// Returns a vec of lines in reversed order, where each line is also in reversed order.
fn single_quote_continue<E: Clone>(
input: ParseInput<Rule>,
) -> ParseResult<Vec<Vec<ParsedTextContents<E>>>> {
Ok(parse_children!(input;
- [interpolation(c), single_quote_continue(lines)] => {
- let c = InterpolatedTextContents::Expr(c);
- let mut lines = lines;
- lines.last_mut().unwrap().push(c);
- lines
- },
- [escaped_quote_pair(c), single_quote_continue(lines)] => {
+ [expression(e), single_quote_continue(lines)] => {
+ let c = InterpolatedTextContents::Expr(e);
let mut lines = lines;
- // TODO: don't allocate for every char
- let c = InterpolatedTextContents::Text(c.to_owned());
- lines.last_mut().unwrap().push(c);
- lines
- },
- [escaped_interpolation(c), single_quote_continue(lines)] => {
- let mut lines = lines;
- // TODO: don't allocate for every char
- let c = InterpolatedTextContents::Text(c.to_owned());
lines.last_mut().unwrap().push(c);
lines
},
@@ -435,38 +428,41 @@ impl Parsers {
))
}
+ #[alias(expression)]
fn builtin<E: Clone>(input: ParseInput<Rule>) -> ParseResult<Expr<E>> {
let s = input.as_str();
- let span = input.as_span();
- Ok(spanned(
- span,
- match crate::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),
- _ => Err(input
- .error(format!("Unrecognized builtin: '{}'", s)))?,
- }
- }
+ let e = match crate::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),
+ _ => Err(input.error(format!("Unrecognized builtin: '{}'", s)))?,
},
- ))
+ };
+ Ok(spanned(input.as_span(), e))
}
- fn NaN(_: ParseInput<Rule>) -> ParseResult<()> {
- Ok(())
+ #[alias(double_literal)]
+ fn NaN(_input: ParseInput<Rule>) -> ParseResult<core::Double> {
+ Ok(std::f64::NAN.into())
}
- fn minus_infinity_literal(_: ParseInput<Rule>) -> ParseResult<()> {
- Ok(())
+ #[alias(double_literal)]
+ fn minus_infinity_literal(
+ _input: ParseInput<Rule>,
+ ) -> ParseResult<core::Double> {
+ Ok(std::f64::NEG_INFINITY.into())
}
- fn plus_infinity_literal(_: ParseInput<Rule>) -> ParseResult<()> {
- Ok(())
+ #[alias(double_literal)]
+ fn plus_infinity_literal(
+ _input: ParseInput<Rule>,
+ ) -> ParseResult<core::Double> {
+ Ok(std::f64::INFINITY.into())
}
+ #[alias(double_literal)]
fn numeric_double_literal(
input: ParseInput<Rule>,
) -> ParseResult<core::Double> {
@@ -481,15 +477,6 @@ impl Parsers {
}
}
- fn double_literal(input: ParseInput<Rule>) -> ParseResult<core::Double> {
- Ok(parse_children!(input;
- [numeric_double_literal(n)] => n,
- [minus_infinity_literal(_)] => std::f64::NEG_INFINITY.into(),
- [plus_infinity_literal(_)] => std::f64::INFINITY.into(),
- [NaN(_)] => std::f64::NAN.into(),
- ))
- }
-
fn natural_literal(input: ParseInput<Rule>) -> ParseResult<core::Natural> {
input
.as_str()
@@ -506,13 +493,13 @@ impl Parsers {
.map_err(|e| input.error(format!("{}", e)))
}
+ #[alias(expression, shortcut = true)]
fn identifier<E: Clone>(input: ParseInput<Rule>) -> ParseResult<Expr<E>> {
- let span = input.as_span();
Ok(parse_children!(input;
[variable(v)] => {
- spanned(span, Var(v))
+ spanned(input.as_span(), Var(v))
},
- [builtin(e)] => e,
+ [expression(e)] => e,
))
}
@@ -527,42 +514,40 @@ impl Parsers {
))
}
+ #[alias(path_component)]
fn unquoted_path_component<'a>(
input: ParseInput<'a, Rule>,
- ) -> ParseResult<&'a str> {
- Ok(input.as_str())
+ ) -> ParseResult<String> {
+ Ok(input.as_str().to_string())
}
+ #[alias(path_component)]
fn quoted_path_component<'a>(
input: ParseInput<'a, Rule>,
- ) -> ParseResult<&'a str> {
- Ok(input.as_str())
- }
- fn path_component(input: ParseInput<Rule>) -> ParseResult<String> {
- Ok(parse_children!(input;
- [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()
- },
- ))
+ ) -> ParseResult<String> {
+ #[rustfmt::skip]
+ 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';');
+ Ok(input
+ .as_str()
+ .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())
}
fn path(input: ParseInput<Rule>) -> ParseResult<FilePath> {
Ok(parse_children!(input;
@@ -572,30 +557,40 @@ impl Parsers {
))
}
- fn local(input: ParseInput<Rule>) -> ParseResult<(FilePrefix, FilePath)> {
+ #[alias(import_type)]
+ fn local<E: Clone>(
+ input: ParseInput<Rule>,
+ ) -> ParseResult<ImportLocation<Expr<E>>> {
Ok(parse_children!(input;
- [parent_path(l)] => l,
- [here_path(l)] => l,
- [home_path(l)] => l,
- [absolute_path(l)] => l,
+ [local_path((prefix, p))] => ImportLocation::Local(prefix, p),
))
}
- fn parent_path(input: ParseInput<Rule>) -> ParseResult<(FilePrefix, FilePath)> {
+ #[alias(local_path)]
+ fn parent_path(
+ input: ParseInput<Rule>,
+ ) -> ParseResult<(FilePrefix, FilePath)> {
Ok(parse_children!(input;
[path(p)] => (FilePrefix::Parent, p)
))
}
- fn here_path(input: ParseInput<Rule>) -> ParseResult<(FilePrefix, FilePath)> {
+ #[alias(local_path)]
+ fn here_path(
+ input: ParseInput<Rule>,
+ ) -> ParseResult<(FilePrefix, FilePath)> {
Ok(parse_children!(input;
[path(p)] => (FilePrefix::Here, p)
))
}
- fn home_path(input: ParseInput<Rule>) -> ParseResult<(FilePrefix, FilePath)> {
+ #[alias(local_path)]
+ fn home_path(
+ input: ParseInput<Rule>,
+ ) -> ParseResult<(FilePrefix, FilePath)> {
Ok(parse_children!(input;
[path(p)] => (FilePrefix::Home, p)
))
}
+ #[alias(local_path)]
fn absolute_path(
input: ParseInput<Rule>,
) -> ParseResult<(FilePrefix, FilePath)> {
@@ -641,25 +636,31 @@ impl Parsers {
Ok(input.as_str().to_owned())
}
- fn http<E: Clone>(input: ParseInput<Rule>) -> ParseResult<URL<Expr<E>>> {
- Ok(parse_children!(input;
- [http_raw(url)] => url,
- [http_raw(url), import_expression(e)] =>
- URL { headers: Some(e), ..url },
- ))
+ #[alias(import_type)]
+ fn http<E: Clone>(
+ input: ParseInput<Rule>,
+ ) -> ParseResult<ImportLocation<Expr<E>>> {
+ Ok(ImportLocation::Remote(parse_children!(input;
+ [http_raw(url)] => url,
+ [http_raw(url), expression(e)] => URL { headers: Some(e), ..url },
+ )))
}
- fn env(input: ParseInput<Rule>) -> ParseResult<String> {
+ #[alias(import_type)]
+ fn env<E: Clone>(
+ input: ParseInput<Rule>,
+ ) -> ParseResult<ImportLocation<Expr<E>>> {
Ok(parse_children!(input;
- [bash_environment_variable(s)] => s,
- [posix_environment_variable(s)] => s,
+ [environment_variable(v)] => ImportLocation::Env(v),
))
}
+ #[alias(environment_variable)]
fn bash_environment_variable(
input: ParseInput<Rule>,
) -> ParseResult<String> {
Ok(input.as_str().to_owned())
}
+ #[alias(environment_variable)]
fn posix_environment_variable(
input: ParseInput<Rule>,
) -> ParseResult<String> {
@@ -686,27 +687,11 @@ impl Parsers {
})
}
- fn missing(_: ParseInput<Rule>) -> ParseResult<()> {
- Ok(())
- }
-
- fn import_type<E: Clone>(
- input: ParseInput<Rule>,
+ #[alias(import_type)]
+ fn missing<E: Clone>(
+ _input: ParseInput<Rule>,
) -> ParseResult<ImportLocation<Expr<E>>> {
- Ok(parse_children!(input;
- [missing(_)] => {
- ImportLocation::Missing
- },
- [env(e)] => {
- ImportLocation::Env(e)
- },
- [http(url)] => {
- ImportLocation::Remote(url)
- },
- [local((prefix, p))] => {
- ImportLocation::Local(prefix, p)
- },
- ))
+ Ok(ImportLocation::Missing)
}
fn hash(input: ParseInput<Rule>) -> ParseResult<Hash> {
@@ -722,78 +707,65 @@ impl Parsers {
fn import_hashed<E: Clone>(
input: ParseInput<Rule>,
) -> ParseResult<crate::Import<Expr<E>>> {
+ use crate::Import;
+ let mode = ImportMode::Code;
Ok(parse_children!(input;
- [import_type(location)] =>
- crate::Import {mode: ImportMode::Code, location, hash: None },
- [import_type(location), hash(h)] =>
- crate::Import {mode: ImportMode::Code, location, hash: Some(h) },
+ [import_type(location)] => Import { mode, location, hash: None },
+ [import_type(location), hash(h)] => Import { mode, location, hash: Some(h) },
))
}
- fn Text(_: ParseInput<Rule>) -> ParseResult<()> {
- Ok(())
+ #[alias(import_mode)]
+ fn Text(_input: ParseInput<Rule>) -> ParseResult<ImportMode> {
+ Ok(ImportMode::RawText)
}
- fn Location(_: ParseInput<Rule>) -> ParseResult<()> {
- Ok(())
+ #[alias(import_mode)]
+ fn Location(_input: ParseInput<Rule>) -> ParseResult<ImportMode> {
+ Ok(ImportMode::Location)
}
+ #[alias(expression)]
fn import<E: Clone>(input: ParseInput<Rule>) -> ParseResult<Expr<E>> {
- let span = input.as_span();
- Ok(parse_children!(input;
+ use crate::Import;
+ let import = parse_children!(input;
[import_hashed(imp)] => {
- spanned(span, Import(crate::Import {
- mode: ImportMode::Code,
- ..imp
- }))
- },
- [import_hashed(imp), Text(_)] => {
- spanned(span, Import(crate::Import {
- mode: ImportMode::RawText,
- ..imp
- }))
+ Import { mode: ImportMode::Code, ..imp }
},
- [import_hashed(imp), Location(_)] => {
- spanned(span, Import(crate::Import {
- mode: ImportMode::Location,
- ..imp
- }))
+ [import_hashed(imp), import_mode(mode)] => {
+ Import { mode, ..imp }
},
- ))
+ );
+ Ok(spanned(input.as_span(), Import(import)))
}
- fn lambda(_: ParseInput<Rule>) -> ParseResult<()> {
- Ok(())
- }
- fn forall(_: ParseInput<Rule>) -> ParseResult<()> {
+ fn lambda(_input: ParseInput<Rule>) -> ParseResult<()> {
Ok(())
}
- fn arrow(_: ParseInput<Rule>) -> ParseResult<()> {
+ fn forall(_input: ParseInput<Rule>) -> ParseResult<()> {
Ok(())
}
- fn merge(_: ParseInput<Rule>) -> ParseResult<()> {
+ fn arrow(_input: ParseInput<Rule>) -> ParseResult<()> {
Ok(())
}
- fn assert(_: ParseInput<Rule>) -> ParseResult<()> {
+ fn merge(_input: ParseInput<Rule>) -> ParseResult<()> {
Ok(())
}
- fn if_(_: ParseInput<Rule>) -> ParseResult<()> {
+ fn assert(_input: ParseInput<Rule>) -> ParseResult<()> {
Ok(())
}
- fn in_(_: ParseInput<Rule>) -> ParseResult<()> {
+ fn if_(_input: ParseInput<Rule>) -> ParseResult<()> {
Ok(())
}
- fn toMap(_: ParseInput<Rule>) -> ParseResult<()> {
+ fn toMap(_input: ParseInput<Rule>) -> ParseResult<()> {
Ok(())
}
+ #[alias(expression)]
fn empty_list_literal<E: Clone>(
input: ParseInput<Rule>,
) -> ParseResult<Expr<E>> {
- let span = input.as_span();
Ok(parse_children!(input;
- [application_expression(e)] => {
- spanned(span, EmptyListLit(e))
- },
+ [expression(e)] => spanned(input.as_span(), EmptyListLit(e)),
))
}
@@ -808,7 +780,7 @@ impl Parsers {
expression(right)] => {
spanned(span, BoolIf(cond, left, right))
},
- [let_binding(bindings).., in_(()), expression(final_expr)] => {
+ [let_binding(bindings).., expression(final_expr)] => {
bindings.rev().fold(
final_expr,
|acc, x| unspanned(Let(x.0, x.1, x.2, acc))
@@ -818,24 +790,22 @@ impl Parsers {
arrow(()), expression(body)] => {
spanned(span, Pi(l, typ, body))
},
- [operator_expression(typ), arrow(()), expression(body)] => {
+ [expression(typ), arrow(()), expression(body)] => {
spanned(span, Pi("_".into(), typ, body))
},
- [merge(()), import_expression(x), import_expression(y),
- application_expression(z)] => {
+ [merge(()), expression(x), expression(y), expression(z)] => {
spanned(span, Merge(x, y, Some(z)))
},
- [empty_list_literal(e)] => e,
[assert(()), expression(x)] => {
spanned(span, Assert(x))
},
- [toMap(()), import_expression(x), application_expression(y)] => {
+ [toMap(()), expression(x), expression(y)] => {
spanned(span, ToMap(x, Some(y)))
},
- [operator_expression(e)] => e,
- [operator_expression(e), expression(annot)] => {
+ [expression(e), expression(annot)] => {
spanned(span, Annot(e, annot))
},
+ [expression(e)] => e,
))
}
@@ -850,14 +820,8 @@ impl Parsers {
))
}
- fn List(_: ParseInput<Rule>) -> ParseResult<()> {
- Ok(())
- }
- fn Optional(_: ParseInput<Rule>) -> ParseResult<()> {
- Ok(())
- }
-
- #[prec_climb(application_expression, PRECCLIMBER)]
+ #[alias(expression, shortcut = true)]
+ #[prec_climb(expression, PRECCLIMBER)]
fn operator_expression<E: Clone>(
input: ParseInput<Rule>,
l: Expr<E>,
@@ -886,55 +850,48 @@ impl Parsers {
Ok(unspanned(BinOp(op, l, r)))
}
- fn Some_(_: ParseInput<Rule>) -> ParseResult<()> {
+ fn Some_(_input: ParseInput<Rule>) -> ParseResult<()> {
Ok(())
}
+ #[alias(expression, shortcut = true)]
fn application_expression<E: Clone>(
input: ParseInput<Rule>,
) -> ParseResult<Expr<E>> {
Ok(parse_children!(input;
- [first_application_expression(e)] => e,
- [first_application_expression(first),
- import_expression(rest)..] => {
+ [expression(e)] => e,
+ [expression(first), expression(rest)..] => {
rest.fold(first, |acc, e| unspanned(App(acc, e)))
},
))
}
+ #[alias(expression, shortcut = true)]
fn first_application_expression<E: Clone>(
input: ParseInput<Rule>,
) -> ParseResult<Expr<E>> {
let span = input.as_span();
Ok(parse_children!(input;
- [Some_(()), import_expression(e)] => {
+ [Some_(()), expression(e)] => {
spanned(span, SomeLit(e))
},
- [merge(()), import_expression(x), import_expression(y)] => {
+ [merge(()), expression(x), expression(y)] => {
spanned(span, Merge(x, y, None))
},
- [toMap(()), import_expression(x)] => {
+ [toMap(()), expression(x)] => {
spanned(span, ToMap(x, None))
},
- [import_expression(e)] => e,
- ))
- }
-
- fn import_expression<E: Clone>(
- input: ParseInput<Rule>,
- ) -> ParseResult<Expr<E>> {
- Ok(parse_children!(input;
- [selector_expression(e)] => e,
- [import(e)] => e,
+ [expression(e)] => e,
))
}
+ #[alias(expression, shortcut = true)]
fn selector_expression<E: Clone>(
input: ParseInput<Rule>,
) -> ParseResult<Expr<E>> {
Ok(parse_children!(input;
- [primitive_expression(e)] => e,
- [primitive_expression(first), selector(rest)..] => {
+ [expression(e)] => e,
+ [expression(first), selector(rest)..] => {
rest.fold(first, |acc, e| unspanned(match e {
Either::Left(l) => Field(acc, l),
Either::Right(ls) => Projection(acc, ls),
@@ -959,6 +916,7 @@ impl Parsers {
))
}
+ #[alias(expression, shortcut = true)]
fn primitive_expression<E: Clone>(
input: ParseInput<Rule>,
) -> ParseResult<Expr<E>> {
@@ -969,46 +927,41 @@ impl Parsers {
[integer_literal(n)] => spanned(span, IntegerLit(n)),
[double_quote_literal(s)] => spanned(span, TextLit(s)),
[single_quote_literal(s)] => spanned(span, TextLit(s)),
- [empty_record_type(e)] => e,
- [empty_record_literal(e)] => e,
- [non_empty_record_type_or_literal(e)] => e,
- [union_type(e)] => e,
- [non_empty_list_literal(e)] => e,
- [identifier(e)] => e,
[expression(e)] => e,
))
}
+ #[alias(expression)]
fn empty_record_literal<E: Clone>(
input: ParseInput<Rule>,
) -> ParseResult<Expr<E>> {
- let span = input.as_span();
- Ok(spanned(span, RecordLit(Default::default())))
+ Ok(spanned(input.as_span(), RecordLit(Default::default())))
}
+ #[alias(expression)]
fn empty_record_type<E: Clone>(
input: ParseInput<Rule>,
) -> ParseResult<Expr<E>> {
- let span = input.as_span();
- Ok(spanned(span, RecordType(Default::default())))
+ Ok(spanned(input.as_span(), RecordType(Default::default())))
}
+ #[alias(expression)]
fn non_empty_record_type_or_literal<E: Clone>(
input: ParseInput<Rule>,
) -> ParseResult<Expr<E>> {
- let span = input.as_span();
- Ok(parse_children!(input;
+ let e = parse_children!(input;
[label(first_label), non_empty_record_type(rest)] => {
let (first_expr, mut map) = rest;
map.insert(first_label, first_expr);
- spanned(span, RecordType(map))
+ RecordType(map)
},
[label(first_label), non_empty_record_literal(rest)] => {
let (first_expr, mut map) = rest;
map.insert(first_label, first_expr);
- spanned(span, RecordLit(map))
+ RecordLit(map)
},
- ))
+ );
+ Ok(spanned(input.as_span(), e))
}
fn non_empty_record_type<E: Clone>(
@@ -1047,19 +1000,16 @@ impl Parsers {
))
}
+ #[alias(expression)]
fn union_type<E: Clone>(input: ParseInput<Rule>) -> ParseResult<Expr<E>> {
- let span = input.as_span();
- Ok(parse_children!(input;
- [empty_union_type(_)] => {
- spanned(span, UnionType(Default::default()))
- },
- [union_type_entry(entries)..] => {
- spanned(span, UnionType(entries.collect()))
- },
- ))
+ let map = parse_children!(input;
+ [empty_union_type(_)] => Default::default(),
+ [union_type_entry(entries)..] => entries.collect(),
+ );
+ Ok(spanned(input.as_span(), UnionType(map)))
}
- fn empty_union_type(_: ParseInput<Rule>) -> ParseResult<()> {
+ fn empty_union_type(_input: ParseInput<Rule>) -> ParseResult<()> {
Ok(())
}
@@ -1072,13 +1022,13 @@ impl Parsers {
))
}
+ #[alias(expression)]
fn non_empty_list_literal<E: Clone>(
input: ParseInput<Rule>,
) -> ParseResult<Expr<E>> {
- let span = input.as_span();
Ok(parse_children!(input;
[expression(items)..] => spanned(
- span,
+ input.as_span(),
NEListLit(items.collect())
)
))