summaryrefslogtreecommitdiff
path: root/dhall_syntax
diff options
context:
space:
mode:
Diffstat (limited to 'dhall_syntax')
-rw-r--r--dhall_syntax/src/core/expr.rs15
-rw-r--r--dhall_syntax/src/core/visitor.rs11
-rw-r--r--dhall_syntax/src/parser.rs14
-rw-r--r--dhall_syntax/src/printer.rs16
4 files changed, 36 insertions, 20 deletions
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<T>(x: Result<T, Void>) -> T {
match x {
Ok(x) => x,
@@ -233,7 +239,9 @@ pub enum ExprF<SubExpr, Embed> {
Field(SubExpr, Label),
/// `e.{ x, y, z }`
Projection(SubExpr, DupTreeSet<Label>),
- /// Embeds an import or the result of resolving the import
+ /// `./some/path`
+ Import(Import),
+ /// Embeds the result of resolving an import
Embed(Embed),
}
@@ -294,7 +302,7 @@ impl<SE, E> ExprF<SE, E> {
}
}
-impl Expr<Import> {
+impl<E> Expr<E> {
pub fn traverse_resolve<E2, Err>(
&self,
visit_import: impl FnMut(&Import) -> Result<E2, Err>,
@@ -316,6 +324,7 @@ impl Expr<Import> {
.as_ref()
.traverse_resolve_with_visitor(visitor)
.or(r.as_ref().traverse_resolve_with_visitor(visitor)),
+ ExprF::Import(import) => Ok(ExprF::Embed((visitor.0)(import)?)),
_ => self.visit(visitor),
}
}
@@ -343,7 +352,7 @@ impl<E> SubExpr<E> {
}
}
-impl SubExpr<Import> {
+impl<E> SubExpr<E> {
pub fn traverse_resolve<E2, Err>(
&self,
visit_import: impl FnMut(&Import) -> Result<E2, Err>,
diff --git a/dhall_syntax/src/core/visitor.rs b/dhall_syntax/src/core/visitor.rs
index f2f3b32..f3d9194 100644
--- a/dhall_syntax/src/core/visitor.rs
+++ b/dhall_syntax/src/core/visitor.rs
@@ -153,6 +153,7 @@ 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)?),
+ Import(a) => Import(a.clone()),
Embed(a) => Embed(v.visit_embed(a)?),
})
}
@@ -246,8 +247,8 @@ where
pub struct ResolveVisitor<F1>(pub F1);
-impl<'a, 'b, E2, Err, F1>
- ExprFFallibleVisitor<'a, SubExpr<Import>, SubExpr<E2>, Import, E2>
+impl<'a, 'b, E, E2, Err, F1>
+ ExprFFallibleVisitor<'a, SubExpr<E>, SubExpr<E2>, E, E2>
for &'b mut ResolveVisitor<F1>
where
F1: FnMut(&Import) -> Result<E2, Err>,
@@ -256,7 +257,7 @@ where
fn visit_subexpr(
&mut self,
- subexpr: &'a SubExpr<Import>,
+ subexpr: &'a SubExpr<E>,
) -> Result<SubExpr<E2>, Self::Error> {
Ok(subexpr.rewrap(
subexpr
@@ -264,7 +265,7 @@ where
.traverse_resolve_with_visitor(&mut **self)?,
))
}
- fn visit_embed(self, embed: &'a Import) -> Result<E2, Self::Error> {
- (self.0)(embed)
+ fn visit_embed(self, _embed: &'a E) -> Result<E2, Self::Error> {
+ unimplemented!()
}
}
diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs
index 1ab6e6d..a8dd359 100644
--- a/dhall_syntax/src/parser.rs
+++ b/dhall_syntax/src/parser.rs
@@ -15,10 +15,10 @@ 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.
-pub(crate) type ParsedExpr = Expr<Import>;
-pub(crate) type ParsedSubExpr = SubExpr<Import>;
-type ParsedText = InterpolatedText<SubExpr<Import>>;
-type ParsedTextContents = InterpolatedTextContents<SubExpr<Import>>;
+pub(crate) type ParsedExpr = Expr<Void>;
+pub(crate) type ParsedSubExpr = SubExpr<Void>;
+type ParsedText = InterpolatedText<ParsedSubExpr>;
+type ParsedTextContents = InterpolatedTextContents<ParsedSubExpr>;
pub type ParseError = pest::error::Error<Rule>;
@@ -691,19 +691,19 @@ make_parser! {
rule!(import<ParsedSubExpr> as expression; span; children!(
[import_hashed(location_hashed)] => {
- spanned(span, Embed(Import {
+ spanned(span, Import(crate::Import {
mode: ImportMode::Code,
location_hashed
}))
},
[import_hashed(location_hashed), Text(_)] => {
- spanned(span, Embed(Import {
+ spanned(span, Import(crate::Import {
mode: ImportMode::RawText,
location_hashed
}))
},
[import_hashed(location_hashed), Location(_)] => {
- spanned(span, Embed(Import {
+ spanned(span, Import(crate::Import {
mode: ImportMode::Location,
location_hashed
}))
diff --git a/dhall_syntax/src/printer.rs b/dhall_syntax/src/printer.rs
index 48e56be..5f5f99b 100644
--- a/dhall_syntax/src/printer.rs
+++ b/dhall_syntax/src/printer.rs
@@ -88,6 +88,7 @@ impl<SE: Display + Clone, E: Display> Display for ExprF<SE, E> {
}
Ok(())
})?,
+ Import(a) => a.fmt(f)?,
Embed(a) => a.fmt(f)?,
}
Ok(())
@@ -151,7 +152,9 @@ impl<A: Display + Clone> Expr<A> {
// 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<A: Display + Clone> Expr<A> {
}
}
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<A: Display + Clone> Expr<A> {
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,