summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--dhall/src/core/valuef.rs3
-rw-r--r--dhall/src/phase/binary.rs10
-rw-r--r--dhall/src/phase/typecheck.rs6
-rw-r--r--dhall_syntax/src/core/expr.rs18
-rw-r--r--dhall_syntax/src/core/span.rs13
-rw-r--r--dhall_syntax/src/parser.rs14
6 files changed, 35 insertions, 29 deletions
diff --git a/dhall/src/core/valuef.rs b/dhall/src/core/valuef.rs
index 4e457e6..e5d0807 100644
--- a/dhall/src/core/valuef.rs
+++ b/dhall/src/core/valuef.rs
@@ -1,12 +1,13 @@
use std::collections::HashMap;
use dhall_syntax::{
- rc, Builtin, Const, ExprF, Integer, InterpolatedTextContents, Label,
+ Builtin, Const, ExprF, Integer, InterpolatedTextContents, Label,
NaiveDouble, Natural,
};
use crate::core::value::{ToExprOptions, Value};
use crate::core::var::{AlphaLabel, AlphaVar, Shift, Subst};
+use crate::phase::typecheck::rc;
use crate::phase::{Normalized, NormalizedExpr};
/// A semantic value. Subexpressions are Values, which are partially evaluated expressions that are
diff --git a/dhall/src/phase/binary.rs b/dhall/src/phase/binary.rs
index 8c98f77..b4f18da 100644
--- a/dhall/src/phase/binary.rs
+++ b/dhall/src/phase/binary.rs
@@ -5,8 +5,9 @@ use std::vec;
use dhall_syntax::map::DupTreeMap;
use dhall_syntax::{
- rc, Expr, ExprF, FilePath, FilePrefix, Hash, Import, ImportLocation,
- ImportMode, Integer, InterpolatedText, Label, Natural, Scheme, URL, V,
+ Expr, ExprF, FilePath, FilePrefix, Hash, Import, ImportLocation,
+ ImportMode, Integer, InterpolatedText, Label, Natural, RawExpr, Scheme,
+ Span, URL, V,
};
use crate::error::{DecodeError, EncodeError};
@@ -24,6 +25,11 @@ pub(crate) fn encode<E>(expr: &Expr<E>) -> Result<Vec<u8>, EncodeError> {
.map_err(|e| EncodeError::CBORError(e))
}
+// Should probably rename this
+pub fn rc<E>(x: RawExpr<E>) -> Expr<E> {
+ Expr::new(x, Span::Decoded)
+}
+
fn cbor_value_to_dhall(data: &cbor::Value) -> Result<DecodedExpr, DecodeError> {
use cbor::Value::*;
use dhall_syntax::{BinOp, Builtin, Const};
diff --git a/dhall/src/phase/typecheck.rs b/dhall/src/phase/typecheck.rs
index 9013c1f..4d8c7d3 100644
--- a/dhall/src/phase/typecheck.rs
+++ b/dhall/src/phase/typecheck.rs
@@ -2,7 +2,7 @@ use std::cmp::max;
use std::collections::HashMap;
use dhall_syntax::{
- rc, Builtin, Const, Expr, ExprF, InterpolatedTextContents, Label,
+ Builtin, Const, Expr, ExprF, InterpolatedTextContents, Label, RawExpr, Span,
};
use crate::core::context::TypecheckContext;
@@ -142,6 +142,10 @@ pub(crate) fn const_to_value(c: Const) -> Value {
}
}
+pub fn rc<E>(x: RawExpr<E>) -> Expr<E> {
+ Expr::new(x, Span::Artificial)
+}
+
// Ad-hoc macro to help construct the types of builtins
macro_rules! make_type {
(Type) => { ExprF::Const(Const::Type) };
diff --git a/dhall_syntax/src/core/expr.rs b/dhall_syntax/src/core/expr.rs
index 70ce1dc..750b58b 100644
--- a/dhall_syntax/src/core/expr.rs
+++ b/dhall_syntax/src/core/expr.rs
@@ -1,4 +1,3 @@
-
use crate::map::{DupTreeMap, DupTreeSet};
use crate::visitor::{self, ExprFMutVisitor, ExprFVisitor};
use crate::*;
@@ -143,7 +142,7 @@ pub enum Builtin {
// Each node carries an annotation.
#[derive(Debug, Clone)]
-pub struct Expr<Embed>(Box<(RawExpr<Embed>, Option<Span>)>);
+pub struct Expr<Embed>(Box<(RawExpr<Embed>, Span)>);
pub type RawExpr<Embed> = ExprF<Expr<Embed>, Embed>;
@@ -299,16 +298,12 @@ impl<E> Expr<E> {
pub fn as_mut(&mut self) -> &mut RawExpr<E> {
&mut self.0.as_mut().0
}
- pub fn span(&self) -> Option<Span> {
+ pub fn span(&self) -> Span {
self.0.as_ref().1.clone()
}
- pub(crate) fn new(x: RawExpr<E>, n: Span) -> Self {
- Expr(Box::new((x, Some(n))))
- }
-
- pub fn from_expr_no_span(x: RawExpr<E>) -> Self {
- Expr(Box::new((x, None)))
+ pub fn new(x: RawExpr<E>, n: Span) -> Self {
+ Expr(Box::new((x, n)))
}
pub fn rewrap<E2>(&self, x: RawExpr<E2>) -> Expr<E2> {
@@ -353,11 +348,6 @@ impl<E> Expr<E> {
}
}
-// Should probably rename this
-pub fn rc<E>(x: RawExpr<E>) -> Expr<E> {
- Expr::from_expr_no_span(x)
-}
-
/// Add an isize to an usize
/// Panics on over/underflow
fn add_ui(u: usize, i: isize) -> Option<usize> {
diff --git a/dhall_syntax/src/core/span.rs b/dhall_syntax/src/core/span.rs
index f0eb89a..fc8de6e 100644
--- a/dhall_syntax/src/core/span.rs
+++ b/dhall_syntax/src/core/span.rs
@@ -14,10 +14,14 @@ pub struct ParsedSpan {
end: usize,
}
-/// A location in the source text
#[derive(Debug, Clone)]
pub enum Span {
+ /// A location in the source text
Parsed(ParsedSpan),
+ /// For expressions obtained from decoding binary
+ Decoded,
+ /// For expressions constructed during normalization/typecheck
+ Artificial,
}
impl Span {
@@ -28,17 +32,22 @@ impl Span {
end: sp.end(),
})
}
+
/// Takes the union of the two spans. Assumes that the spans come from the same input.
/// This will also capture any input between the spans.
pub fn union(&self, other: &Span) -> Self {
use std::cmp::{max, min};
use Span::*;
match (self, other) {
- (Parsed(x), Parsed(y)) => Span::Parsed(ParsedSpan {
+ (Parsed(x), Parsed(y)) => Parsed(ParsedSpan {
input: x.input.clone(),
start: min(x.start, y.start),
end: max(x.end, y.end),
}),
+ _ => panic!(
+ "Tried to union incompatible spans: {:?} and {:?}",
+ self, other
+ ),
}
}
}
diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs
index 83f62ee..eaded50 100644
--- a/dhall_syntax/src/parser.rs
+++ b/dhall_syntax/src/parser.rs
@@ -650,7 +650,7 @@ impl DhallParser {
final_expr,
|acc, x| {
spanned_union(
- acc.span().unwrap(),
+ acc.span(),
x.3,
Let(x.0, x.1, x.2, acc)
)
@@ -717,11 +717,7 @@ impl DhallParser {
r => Err(op.error(format!("Rule {:?} isn't an operator", r)))?,
};
- Ok(spanned_union(
- l.span().unwrap(),
- r.span().unwrap(),
- BinOp(op, l, r),
- ))
+ Ok(spanned_union(l.span(), r.span(), BinOp(op, l, r)))
}
fn Some_(_input: ParseInput) -> ParseResult<()> {
@@ -739,8 +735,8 @@ impl DhallParser {
first,
|acc, e| {
spanned_union(
- acc.span().unwrap(),
- e.span().unwrap(),
+ acc.span(),
+ e.span(),
App(acc, e)
)
}
@@ -778,7 +774,7 @@ impl DhallParser {
first,
|acc, e| {
spanned_union(
- acc.span().unwrap(),
+ acc.span(),
e.1,
match e.0 {
Either::Left(l) => Field(acc, l),