From 29208b0bf3dd2667f92774dacb3a7f058c4cd895 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 5 Mar 2020 15:50:45 +0000 Subject: Implement some normalization simplifications --- dhall/src/semantics/nze/normalize.rs | 156 ++++++++++++++++++----------------- 1 file changed, 80 insertions(+), 76 deletions(-) (limited to 'dhall/src/semantics') diff --git a/dhall/src/semantics/nze/normalize.rs b/dhall/src/semantics/nze/normalize.rs index dedd659..27862ee 100644 --- a/dhall/src/semantics/nze/normalize.rs +++ b/dhall/src/semantics/nze/normalize.rs @@ -233,7 +233,7 @@ pub(crate) fn normalize_one_layer(expr: ExprKind, env: &NzEnv) -> NirKind { ExprKind::Builtin(b) => Ret::Nir(Nir::from_builtin_env(b, env)), ExprKind::Assert(_) => Ret::Expr(expr), ExprKind::App(v, a) => Ret::Nir(v.app(a)), - ExprKind::Lit(l) => Ret::NirKind(Lit(l.clone())), + ExprKind::Lit(l) => Ret::NirKind(Lit(l)), ExprKind::SomeLit(e) => Ret::NirKind(NEOptionalLit(e)), ExprKind::EmptyListLit(t) => { let arg = match t.kind() { @@ -286,23 +286,6 @@ pub(crate) fn normalize_one_layer(expr: ExprKind, env: &NzEnv) -> NirKind { None => Ret::Expr(expr), }, - ExprKind::Projection(_, ref ls) if ls.is_empty() => { - Ret::NirKind(RecordLit(HashMap::new())) - } - ExprKind::Projection(ref v, ref ls) => match v.kind() { - RecordLit(kvs) => Ret::NirKind(RecordLit( - ls.iter() - .filter_map(|l| kvs.get(l).map(|x| (l.clone(), x.clone()))) - .collect(), - )), - PartialExpr(ExprKind::Projection(v2, _)) => { - return normalize_one_layer( - ExprKind::Projection(v2.clone(), ls.clone()), - env, - ) - } - _ => Ret::Expr(expr), - }, ExprKind::Field(ref v, ref l) => match v.kind() { RecordLit(kvs) => match kvs.get(l) { Some(r) => Ret::Nir(r.clone()), @@ -311,6 +294,12 @@ pub(crate) fn normalize_one_layer(expr: ExprKind, env: &NzEnv) -> NirKind { UnionType(kts) => { Ret::NirKind(UnionConstructor(l.clone(), kts.clone())) } + PartialExpr(ExprKind::Projection(x, _)) => { + return normalize_one_layer( + ExprKind::Field(x.clone(), l.clone()), + env, + ) + } PartialExpr(ExprKind::BinOp( BinOp::RightBiasedRecordMerge, x, @@ -329,11 +318,11 @@ pub(crate) fn normalize_one_layer(expr: ExprKind, env: &NzEnv) -> NirKind { Some(r) => Ret::Expr(ExprKind::Field( Nir::from_kind(PartialExpr(ExprKind::BinOp( BinOp::RightBiasedRecordMerge, - Nir::from_kind(RecordLit({ - let mut kvs = HashMap::new(); - kvs.insert(l.clone(), r.clone()); - kvs - })), + Nir::from_kind(RecordLit( + Some((l.clone(), r.clone())) + .into_iter() + .collect(), + )), y.clone(), ))), l.clone(), @@ -347,66 +336,81 @@ pub(crate) fn normalize_one_layer(expr: ExprKind, env: &NzEnv) -> NirKind { }, _ => Ret::Expr(expr), }, - PartialExpr(ExprKind::BinOp( - BinOp::RecursiveRecordTypeMerge, - x, - y, - )) => match (x.kind(), y.kind()) { - (RecordLit(kvs), _) => match kvs.get(l) { - Some(_) => Ret::Expr(expr), - None => { - return normalize_one_layer( - ExprKind::Field(y.clone(), l.clone()), - env, - ) - } - }, - (_, RecordLit(kvs)) => match kvs.get(l) { - Some(_) => Ret::Expr(expr), - None => { - return normalize_one_layer( - ExprKind::Field(x.clone(), l.clone()), - env, - ) - } - }, - _ => Ret::Expr(expr), - }, + PartialExpr(ExprKind::BinOp(BinOp::RecursiveRecordMerge, x, y)) => { + match (x.kind(), y.kind()) { + (RecordLit(kvs), _) => match kvs.get(l) { + Some(r) => Ret::Expr(ExprKind::Field( + Nir::from_kind(PartialExpr(ExprKind::BinOp( + BinOp::RecursiveRecordMerge, + Nir::from_kind(RecordLit( + Some((l.clone(), r.clone())) + .into_iter() + .collect(), + )), + y.clone(), + ))), + l.clone(), + )), + None => { + return normalize_one_layer( + ExprKind::Field(y.clone(), l.clone()), + env, + ) + } + }, + (_, RecordLit(kvs)) => match kvs.get(l) { + Some(r) => Ret::Expr(ExprKind::Field( + Nir::from_kind(PartialExpr(ExprKind::BinOp( + BinOp::RecursiveRecordMerge, + x.clone(), + Nir::from_kind(RecordLit( + Some((l.clone(), r.clone())) + .into_iter() + .collect(), + )), + ))), + l.clone(), + )), + None => { + return normalize_one_layer( + ExprKind::Field(x.clone(), l.clone()), + env, + ) + } + }, + _ => Ret::Expr(expr), + } + } + _ => Ret::Expr(expr), + }, + ExprKind::Projection(_, ref ls) if ls.is_empty() => { + Ret::NirKind(RecordLit(HashMap::new())) + } + ExprKind::Projection(ref v, ref ls) => match v.kind() { + RecordLit(kvs) => Ret::NirKind(RecordLit( + ls.iter() + .filter_map(|l| kvs.get(l).map(|x| (l.clone(), x.clone()))) + .collect(), + )), PartialExpr(ExprKind::Projection(v2, _)) => { return normalize_one_layer( - ExprKind::Field(v2.clone(), l.clone()), + ExprKind::Projection(v2.clone(), ls.clone()), env, ) } _ => Ret::Expr(expr), }, - - ExprKind::ProjectionByExpr(ref v, ref t) => match dbg!(v).kind() { - RecordLit(kvs) => match dbg!(t).kind() { - RecordType(kts) => Ret::NirKind(RecordLit( - kts.iter() - .filter_map(|(l, _)| { - kvs.get(l).map(|x| (l.clone(), x.clone())) - }) - .collect(), - )), - _ => Ret::Expr(expr), - }, - _ => match dbg!(t).kind() { - RecordType(kts) => { - use crate::syntax::map::DupTreeSet; - use std::iter::FromIterator; - - let ts = DupTreeSet::from_iter( - kts.iter().map(|(l, _)| l.clone()), - ); - return normalize_one_layer( - ExprKind::Projection(v.clone(), ts), - env, - ); - } - _ => Ret::Expr(expr), - }, + ExprKind::ProjectionByExpr(ref v, ref t) => match t.kind() { + RecordType(kts) => { + return normalize_one_layer( + ExprKind::Projection( + v.clone(), + kts.keys().cloned().collect(), + ), + env, + ) + } + _ => Ret::Expr(expr), }, ExprKind::Merge(ref handlers, ref variant, _) => { -- cgit v1.2.3 From 81ce30dde067ca0067fda32b1e0ade1dbdfbdf58 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 29 Feb 2020 23:21:18 +0000 Subject: Implement `as Location` imports --- dhall/src/semantics/resolve/hir.rs | 3 + dhall/src/semantics/resolve/resolve.rs | 129 +++++++++++++++++++++++++-------- 2 files changed, 102 insertions(+), 30 deletions(-) (limited to 'dhall/src/semantics') diff --git a/dhall/src/semantics/resolve/hir.rs b/dhall/src/semantics/resolve/hir.rs index 2f3464a..317708a 100644 --- a/dhall/src/semantics/resolve/hir.rs +++ b/dhall/src/semantics/resolve/hir.rs @@ -72,6 +72,9 @@ impl Hir { ) -> Result, TypeError> { type_with(env, self, None) } + pub fn typecheck_noenv<'hir>(&'hir self) -> Result, TypeError> { + self.typecheck(&TyEnv::new()) + } /// Eval the Hir. It will actually get evaluated only as needed on demand. pub fn eval(&self, env: impl Into) -> Nir { diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs index 82800ec..b27dd2c 100644 --- a/dhall/src/semantics/resolve/resolve.rs +++ b/dhall/src/semantics/resolve/resolve.rs @@ -1,3 +1,4 @@ +use itertools::Itertools; use std::borrow::Cow; use std::path::{Path, PathBuf}; @@ -5,7 +6,11 @@ use crate::error::ErrorBuilder; use crate::error::{Error, ImportError}; use crate::semantics::{mkerr, Hir, HirKind, ImportEnv, NameEnv, Type}; use crate::syntax; -use crate::syntax::{BinOp, Expr, ExprKind, FilePath, ImportLocation, URL}; +use crate::syntax::map::DupTreeMap; +use crate::syntax::{ + BinOp, Builtin, Expr, ExprKind, FilePath, FilePrefix, ImportLocation, + ImportMode, Span, URL, +}; use crate::{Parsed, ParsedExpr, Resolved}; // TODO: evaluate import headers @@ -25,24 +30,95 @@ fn resolve_one_import( import: &Import, root: &ImportRoot, ) -> Result { - use self::ImportRoot::*; - use syntax::FilePrefix::*; - use syntax::ImportLocation::*; let cwd = match root { - LocalDir(cwd) => cwd, + ImportRoot::LocalDir(cwd) => cwd, }; - match &import.location { - Local(prefix, path) => { - let path_buf: PathBuf = path.file_path.iter().collect(); - let path_buf = match prefix { - // TODO: fail gracefully - Parent => cwd.parent().unwrap().join(path_buf), - Here => cwd.join(path_buf), + + match import.mode { + ImportMode::Code => { + match &import.location { + ImportLocation::Local(prefix, path) => { + let path_buf: PathBuf = path.file_path.iter().collect(); + let path_buf = match prefix { + // TODO: fail gracefully + FilePrefix::Parent => { + cwd.parent().unwrap().join(path_buf) + } + FilePrefix::Here => cwd.join(path_buf), + _ => unimplemented!("{:?}", import), + }; + Ok(load_import(env, &path_buf)?) + } + _ => unimplemented!("{:?}", import), + } + } + ImportMode::RawText => unimplemented!("{:?}", import), + ImportMode::Location => { + let mkexpr = |kind| Expr::new(kind, Span::Artificial); + let text_type = mkexpr(ExprKind::Builtin(Builtin::Text)); + let mut location_union = DupTreeMap::default(); + location_union.insert("Local".into(), Some(text_type.clone())); + location_union.insert("Remote".into(), Some(text_type.clone())); + location_union + .insert("Environment".into(), Some(text_type.clone())); + location_union.insert("Missing".into(), None); + let location_union = mkexpr(ExprKind::UnionType(location_union)); + + let expr = match &import.location { + ImportLocation::Local(prefix, path) => { + let mut cwd: Vec = cwd + .components() + .map(|component| { + component.as_os_str().to_string_lossy().into_owned() + }) + .collect(); + let root = match prefix { + FilePrefix::Here => cwd, + FilePrefix::Parent => { + cwd.push("..".to_string()); + cwd + } + FilePrefix::Absolute => vec![], + FilePrefix::Home => vec![], + }; + let path: Vec<_> = root + .into_iter() + .chain(path.file_path.iter().cloned()) + .collect(); + let path = + (FilePath { file_path: path }).canonicalize().file_path; + let prefix = match prefix { + FilePrefix::Here | FilePrefix::Parent => ".", + FilePrefix::Absolute => "", + FilePrefix::Home => "~", + }; + let path = Some(prefix.to_string()) + .into_iter() + .chain(path) + .join("/"); + + mkexpr(ExprKind::App( + mkexpr(ExprKind::Field(location_union, "Local".into())), + mkexpr(ExprKind::TextLit(path.into())), + )) + } + ImportLocation::Env(name) => mkexpr(ExprKind::App( + mkexpr(ExprKind::Field( + location_union, + "Environment".into(), + )), + mkexpr(ExprKind::TextLit(name.clone().into())), + )), + ImportLocation::Missing => { + mkexpr(ExprKind::Field(location_union, "Missing".into())) + } _ => unimplemented!("{:?}", import), }; - Ok(load_import(env, &path_buf)?) + + let hir = skip_resolve(&expr)?; + let ty = hir.typecheck_noenv()?.ty().clone(); + Ok((hir, ty)) } - _ => unimplemented!("{:?}", import), } } @@ -166,21 +242,15 @@ pub trait Canonicalize { impl Canonicalize for FilePath { fn canonicalize(&self) -> FilePath { let mut file_path = Vec::new(); - let mut file_path_components = self.file_path.clone().into_iter(); - - loop { - let component = file_path_components.next(); - match component.as_ref() { - // ─────────────────── - // canonicalize(ε) = ε - None => break, + for c in &self.file_path { + match c.as_ref() { // canonicalize(directory₀) = directory₁ // ─────────────────────────────────────── // canonicalize(directory₀/.) = directory₁ - Some(c) if c == "." => continue, + "." => continue, - Some(c) if c == ".." => match file_path_components.next() { + ".." => match file_path.last() { // canonicalize(directory₀) = ε // ──────────────────────────── // canonicalize(directory₀/..) = /.. @@ -189,21 +259,20 @@ impl Canonicalize for FilePath { // canonicalize(directory₀) = directory₁/.. // ────────────────────────────────────────────── // canonicalize(directory₀/..) = directory₁/../.. - Some(ref c) if c == ".." => { - file_path.push("..".to_string()); - file_path.push("..".to_string()); - } + Some(c) if c == ".." => file_path.push("..".to_string()), // canonicalize(directory₀) = directory₁/component // ─────────────────────────────────────────────── ; If "component" is not // canonicalize(directory₀/..) = directory₁ ; ".." - Some(_) => continue, + Some(_) => { + file_path.pop(); + } }, // canonicalize(directory₀) = directory₁ // ───────────────────────────────────────────────────────── ; If no other // canonicalize(directory₀/component) = directory₁/component ; rule matches - Some(c) => file_path.push(c.clone()), + _ => file_path.push(c.clone()), } } -- cgit v1.2.3 From 386f34af802a812c2af8ece2cc427cfb5a7c1fe8 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 1 Mar 2020 17:26:59 +0000 Subject: Implement `missing` and `env:VAR` imports --- dhall/src/semantics/resolve/resolve.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'dhall/src/semantics') diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs index b27dd2c..bf7aabb 100644 --- a/dhall/src/semantics/resolve/resolve.rs +++ b/dhall/src/semantics/resolve/resolve.rs @@ -1,6 +1,7 @@ use itertools::Itertools; use std::borrow::Cow; -use std::path::{Path, PathBuf}; +use std::env; +use std::path::PathBuf; use crate::error::ErrorBuilder; use crate::error::{Error, ImportError}; @@ -47,8 +48,21 @@ fn resolve_one_import( FilePrefix::Here => cwd.join(path_buf), _ => unimplemented!("{:?}", import), }; - Ok(load_import(env, &path_buf)?) + + let parsed = Parsed::parse_file(&path_buf)?; + let typed = resolve_with_env(env, parsed)?.typecheck()?; + Ok((typed.normalize().to_hir(), typed.ty().clone())) + } + ImportLocation::Env(var_name) => { + let val = match env::var(var_name) { + Ok(val) => val, + Err(_) => Err(ImportError::MissingEnvVar)?, + }; + let parsed = Parsed::parse_str(&val)?; + let typed = resolve_with_env(env, parsed)?.typecheck()?; + Ok((typed.normalize().to_hir(), typed.ty().clone())) } + ImportLocation::Missing => Err(ImportError::Missing.into()), _ => unimplemented!("{:?}", import), } } @@ -122,12 +136,6 @@ fn resolve_one_import( } } -fn load_import(env: &mut ImportEnv, f: &Path) -> Result { - let parsed = Parsed::parse_file(f)?; - let typed = resolve_with_env(env, parsed)?.typecheck()?; - Ok((typed.normalize().to_hir(), typed.ty().clone())) -} - /// Desugar the first level of the expression. fn desugar(expr: &Expr) -> Cow<'_, Expr> { match expr.kind() { -- cgit v1.2.3 From cbc1825313389746f08df5502568b2e13e04790d Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 1 Mar 2020 18:06:46 +0000 Subject: Refactor resolve a bit --- dhall/src/semantics/resolve/resolve.rs | 156 ++++++++++++++++----------------- 1 file changed, 76 insertions(+), 80 deletions(-) (limited to 'dhall/src/semantics') diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs index bf7aabb..ad2cd75 100644 --- a/dhall/src/semantics/resolve/resolve.rs +++ b/dhall/src/semantics/resolve/resolve.rs @@ -1,4 +1,3 @@ -use itertools::Itertools; use std::borrow::Cow; use std::env; use std::path::PathBuf; @@ -10,7 +9,7 @@ use crate::syntax; use crate::syntax::map::DupTreeMap; use crate::syntax::{ BinOp, Builtin, Expr, ExprKind, FilePath, FilePrefix, ImportLocation, - ImportMode, Span, URL, + ImportMode, Span, UnspannedExpr, URL, }; use crate::{Parsed, ParsedExpr, Resolved}; @@ -26,109 +25,106 @@ pub(crate) enum ImportRoot { LocalDir(PathBuf), } -fn resolve_one_import( - env: &mut ImportEnv, - import: &Import, +fn mkexpr(kind: UnspannedExpr) -> Expr { + Expr::new(kind, Span::Artificial) +} + +fn make_aslocation_uniontype() -> Expr { + let text_type = mkexpr(ExprKind::Builtin(Builtin::Text)); + let mut union = DupTreeMap::default(); + union.insert("Local".into(), Some(text_type.clone())); + union.insert("Remote".into(), Some(text_type.clone())); + union.insert("Environment".into(), Some(text_type.clone())); + union.insert("Missing".into(), None); + mkexpr(ExprKind::UnionType(union)) +} + +fn compute_relative_path( root: &ImportRoot, -) -> Result { + prefix: &FilePrefix, + path: &FilePath, +) -> PathBuf { let cwd = match root { ImportRoot::LocalDir(cwd) => cwd, }; + let mut cwd: Vec = cwd + .components() + .map(|component| component.as_os_str().to_string_lossy().into_owned()) + .collect(); + let root = match prefix { + FilePrefix::Here => cwd, + FilePrefix::Parent => { + cwd.push("..".to_string()); + cwd + } + FilePrefix::Absolute => vec![], + FilePrefix::Home => vec![], + }; + let path: Vec<_> = root + .into_iter() + .chain(path.file_path.iter().cloned()) + .collect(); + let path = (FilePath { file_path: path }).canonicalize().file_path; + let prefix = match prefix { + FilePrefix::Here | FilePrefix::Parent => ".", + FilePrefix::Absolute => "/", + FilePrefix::Home => "~", + }; + Some(prefix.to_string()).into_iter().chain(path).collect() +} +fn resolve_one_import( + env: &mut ImportEnv, + import: &Import, + root: &ImportRoot, +) -> Result { match import.mode { ImportMode::Code => { - match &import.location { + let parsed = match &import.location { ImportLocation::Local(prefix, path) => { - let path_buf: PathBuf = path.file_path.iter().collect(); - let path_buf = match prefix { - // TODO: fail gracefully - FilePrefix::Parent => { - cwd.parent().unwrap().join(path_buf) - } - FilePrefix::Here => cwd.join(path_buf), - _ => unimplemented!("{:?}", import), - }; - - let parsed = Parsed::parse_file(&path_buf)?; - let typed = resolve_with_env(env, parsed)?.typecheck()?; - Ok((typed.normalize().to_hir(), typed.ty().clone())) + let path = compute_relative_path(root, prefix, path); + Parsed::parse_file(&path)? } ImportLocation::Env(var_name) => { let val = match env::var(var_name) { Ok(val) => val, Err(_) => Err(ImportError::MissingEnvVar)?, }; - let parsed = Parsed::parse_str(&val)?; - let typed = resolve_with_env(env, parsed)?.typecheck()?; - Ok((typed.normalize().to_hir(), typed.ty().clone())) + Parsed::parse_str(&val)? } - ImportLocation::Missing => Err(ImportError::Missing.into()), + ImportLocation::Missing => Err(ImportError::Missing)?, _ => unimplemented!("{:?}", import), - } + }; + + let typed = resolve_with_env(env, parsed)?.typecheck()?; + Ok((typed.normalize().to_hir(), typed.ty().clone())) } ImportMode::RawText => unimplemented!("{:?}", import), ImportMode::Location => { - let mkexpr = |kind| Expr::new(kind, Span::Artificial); - let text_type = mkexpr(ExprKind::Builtin(Builtin::Text)); - let mut location_union = DupTreeMap::default(); - location_union.insert("Local".into(), Some(text_type.clone())); - location_union.insert("Remote".into(), Some(text_type.clone())); - location_union - .insert("Environment".into(), Some(text_type.clone())); - location_union.insert("Missing".into(), None); - let location_union = mkexpr(ExprKind::UnionType(location_union)); - - let expr = match &import.location { + let (field_name, arg) = match &import.location { ImportLocation::Local(prefix, path) => { - let mut cwd: Vec = cwd - .components() - .map(|component| { - component.as_os_str().to_string_lossy().into_owned() - }) - .collect(); - let root = match prefix { - FilePrefix::Here => cwd, - FilePrefix::Parent => { - cwd.push("..".to_string()); - cwd - } - FilePrefix::Absolute => vec![], - FilePrefix::Home => vec![], - }; - let path: Vec<_> = root - .into_iter() - .chain(path.file_path.iter().cloned()) - .collect(); - let path = - (FilePath { file_path: path }).canonicalize().file_path; - let prefix = match prefix { - FilePrefix::Here | FilePrefix::Parent => ".", - FilePrefix::Absolute => "", - FilePrefix::Home => "~", - }; - let path = Some(prefix.to_string()) - .into_iter() - .chain(path) - .join("/"); - - mkexpr(ExprKind::App( - mkexpr(ExprKind::Field(location_union, "Local".into())), - mkexpr(ExprKind::TextLit(path.into())), - )) + let path = compute_relative_path(root, prefix, path) + .to_string_lossy() + .into_owned(); + ("Local", Some(path)) } - ImportLocation::Env(name) => mkexpr(ExprKind::App( - mkexpr(ExprKind::Field( - location_union, - "Environment".into(), - )), - mkexpr(ExprKind::TextLit(name.clone().into())), - )), - ImportLocation::Missing => { - mkexpr(ExprKind::Field(location_union, "Missing".into())) + ImportLocation::Env(name) => { + ("Environment", Some(name.clone())) } + ImportLocation::Missing => ("Missing", None), _ => unimplemented!("{:?}", import), }; + let asloc_ty = make_aslocation_uniontype(); + let expr = mkexpr(ExprKind::Field(asloc_ty, field_name.into())); + let expr = match arg { + Some(arg) => mkexpr(ExprKind::App( + expr, + mkexpr(ExprKind::TextLit(arg.into())), + )), + None => expr, + }; + let hir = skip_resolve(&expr)?; let ty = hir.typecheck_noenv()?.ty().clone(); Ok((hir, ty)) -- cgit v1.2.3 From df4495f30708180591b630bb720cfe81ff4118ce Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 1 Mar 2020 18:18:01 +0000 Subject: Implement `as Text` imports --- dhall/src/semantics/parse.rs | 5 ++--- dhall/src/semantics/resolve/resolve.rs | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 4 deletions(-) (limited to 'dhall/src/semantics') diff --git a/dhall/src/semantics/parse.rs b/dhall/src/semantics/parse.rs index ee35536..ffd5eca 100644 --- a/dhall/src/semantics/parse.rs +++ b/dhall/src/semantics/parse.rs @@ -9,9 +9,8 @@ use crate::syntax::parse_expr; use crate::Parsed; pub(crate) fn parse_file(f: &Path) -> Result { - let mut buffer = String::new(); - File::open(f)?.read_to_string(&mut buffer)?; - let expr = parse_expr(&*buffer)?; + let text = std::fs::read_to_string(f)?; + let expr = parse_expr(&text)?; let root = ImportRoot::LocalDir(f.parent().unwrap().to_owned()); Ok(Parsed(expr, root)) } diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs index ad2cd75..f419858 100644 --- a/dhall/src/semantics/resolve/resolve.rs +++ b/dhall/src/semantics/resolve/resolve.rs @@ -99,7 +99,26 @@ fn resolve_one_import( let typed = resolve_with_env(env, parsed)?.typecheck()?; Ok((typed.normalize().to_hir(), typed.ty().clone())) } - ImportMode::RawText => unimplemented!("{:?}", import), + ImportMode::RawText => { + let text = match &import.location { + ImportLocation::Local(prefix, path) => { + let path = compute_relative_path(root, prefix, path); + std::fs::read_to_string(path)? + } + ImportLocation::Env(var_name) => match env::var(var_name) { + Ok(val) => val, + Err(_) => Err(ImportError::MissingEnvVar)?, + }, + ImportLocation::Missing => Err(ImportError::Missing)?, + _ => unimplemented!("{:?}", import), + }; + + let hir = Hir::new( + HirKind::Expr(ExprKind::TextLit(text.into())), + Span::Artificial, + ); + Ok((hir, Type::from_builtin(Builtin::Text))) + } ImportMode::Location => { let (field_name, arg) = match &import.location { ImportLocation::Local(prefix, path) => { -- cgit v1.2.3 From 5a9a5859eec0cf7deebf7fa07fe99f8dc8722ec8 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 1 Mar 2020 19:37:24 +0000 Subject: Implement remote `as Location` resolution --- dhall/src/semantics/resolve/resolve.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'dhall/src/semantics') diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs index f419858..8a8c9b6 100644 --- a/dhall/src/semantics/resolve/resolve.rs +++ b/dhall/src/semantics/resolve/resolve.rs @@ -1,3 +1,4 @@ +use itertools::Itertools; use std::borrow::Cow; use std::env; use std::path::PathBuf; @@ -127,11 +128,21 @@ fn resolve_one_import( .into_owned(); ("Local", Some(path)) } + ImportLocation::Remote(url) => { + let path = + url.path.canonicalize().file_path.iter().join("/"); + let mut url_str = + format!("{}://{}/{}", url.scheme, url.authority, path); + if let Some(q) = &url.query { + url_str.push('?'); + url_str.push_str(q.as_ref()); + } + ("Remote", Some(url_str)) + } ImportLocation::Env(name) => { ("Environment", Some(name.clone())) } ImportLocation::Missing => ("Missing", None), - _ => unimplemented!("{:?}", import), }; let asloc_ty = make_aslocation_uniontype(); -- cgit v1.2.3 From 903d6c0bba36a6696eb337ae84b962f4cc48b5b5 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 4 Mar 2020 21:26:01 +0000 Subject: Implement remote imports and cleanup import chaining --- dhall/src/semantics/parse.rs | 18 ++- dhall/src/semantics/resolve/resolve.rs | 202 ++++++++++++++++++++------------- 2 files changed, 139 insertions(+), 81 deletions(-) (limited to 'dhall/src/semantics') diff --git a/dhall/src/semantics/parse.rs b/dhall/src/semantics/parse.rs index ffd5eca..45860d0 100644 --- a/dhall/src/semantics/parse.rs +++ b/dhall/src/semantics/parse.rs @@ -1,9 +1,10 @@ use std::fs::File; use std::io::Read; use std::path::Path; +use url::Url; use crate::error::Error; -use crate::semantics::resolve::ImportRoot; +use crate::semantics::resolve::ImportLocation; use crate::syntax::binary; use crate::syntax::parse_expr; use crate::Parsed; @@ -11,19 +12,26 @@ use crate::Parsed; pub(crate) fn parse_file(f: &Path) -> Result { let text = std::fs::read_to_string(f)?; let expr = parse_expr(&text)?; - let root = ImportRoot::LocalDir(f.parent().unwrap().to_owned()); + let root = ImportLocation::Local(f.to_owned()); + Ok(Parsed(expr, root)) +} + +pub(crate) fn parse_remote(url: Url) -> Result { + let body = reqwest::blocking::get(url.clone()).unwrap().text().unwrap(); + let expr = parse_expr(&body)?; + let root = ImportLocation::Remote(url); Ok(Parsed(expr, root)) } pub(crate) fn parse_str(s: &str) -> Result { let expr = parse_expr(s)?; - let root = ImportRoot::LocalDir(std::env::current_dir()?); + let root = ImportLocation::Missing; Ok(Parsed(expr, root)) } pub(crate) fn parse_binary(data: &[u8]) -> Result { let expr = binary::decode(data)?; - let root = ImportRoot::LocalDir(std::env::current_dir()?); + let root = ImportLocation::Missing; Ok(Parsed(expr, root)) } @@ -31,6 +39,6 @@ pub(crate) fn parse_binary_file(f: &Path) -> Result { let mut buffer = Vec::new(); File::open(f)?.read_to_end(&mut buffer)?; let expr = binary::decode(&buffer)?; - let root = ImportRoot::LocalDir(f.parent().unwrap().to_owned()); + let root = ImportLocation::Local(f.to_owned()); Ok(Parsed(expr, root)) } diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs index 8a8c9b6..782f5f7 100644 --- a/dhall/src/semantics/resolve/resolve.rs +++ b/dhall/src/semantics/resolve/resolve.rs @@ -2,6 +2,7 @@ use itertools::Itertools; use std::borrow::Cow; use std::env; use std::path::PathBuf; +use url::Url; use crate::error::ErrorBuilder; use crate::error::{Error, ImportError}; @@ -9,8 +10,8 @@ use crate::semantics::{mkerr, Hir, HirKind, ImportEnv, NameEnv, Type}; use crate::syntax; use crate::syntax::map::DupTreeMap; use crate::syntax::{ - BinOp, Builtin, Expr, ExprKind, FilePath, FilePrefix, ImportLocation, - ImportMode, Span, UnspannedExpr, URL, + BinOp, Builtin, Expr, ExprKind, FilePath, FilePrefix, ImportMode, + ImportTarget, Span, UnspannedExpr, URL, }; use crate::{Parsed, ParsedExpr, Resolved}; @@ -20,10 +21,109 @@ pub(crate) type Import = syntax::Import<()>; /// Owned Hir with a type. Different from Tir because the Hir is owned. pub(crate) type TypedHir = (Hir, Type); -/// A root from which to resolve relative imports. +/// The location of some data, usually some dhall code. #[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) enum ImportRoot { - LocalDir(PathBuf), +pub(crate) enum ImportLocation { + /// Local file + Local(PathBuf), + /// Remote file + Remote(Url), + /// Environment variable + Env(String), + /// Data without a location + Missing, +} + +impl ImportLocation { + /// Given an import pointing to `target` found in the current location, compute the next + /// location, or error if not allowed. + fn chain( + &self, + target: &ImportTarget<()>, + ) -> Result { + Ok(match target { + ImportTarget::Local(prefix, path) => { + self.chain_local(prefix, path)? + } + ImportTarget::Remote(remote) => { + let mut url = Url::parse(&format!( + "{}://{}", + remote.scheme, remote.authority + ))?; + url.set_path(&remote.path.file_path.iter().join("/")); + url.set_query(remote.query.as_ref().map(String::as_ref)); + ImportLocation::Remote(url) + } + ImportTarget::Env(var_name) => { + ImportLocation::Env(var_name.clone()) + } + ImportTarget::Missing => ImportLocation::Missing, + }) + } + + fn chain_local( + &self, + prefix: &FilePrefix, + path: &FilePath, + ) -> Result { + Ok(match self { + ImportLocation::Local(..) + | ImportLocation::Env(..) + | ImportLocation::Missing => { + let dir = match self { + ImportLocation::Local(path) => { + path.parent().unwrap().to_owned() + } + ImportLocation::Env(..) | ImportLocation::Missing => { + std::env::current_dir()? + } + _ => unreachable!(), + }; + let mut dir: Vec = dir + .components() + .map(|component| { + component.as_os_str().to_string_lossy().into_owned() + }) + .collect(); + let root = match prefix { + FilePrefix::Here => dir, + FilePrefix::Parent => { + dir.push("..".to_string()); + dir + } + FilePrefix::Absolute => vec![], + FilePrefix::Home => vec![], + }; + let path: Vec<_> = root + .into_iter() + .chain(path.file_path.iter().cloned()) + .collect(); + let path = + (FilePath { file_path: path }).canonicalize().file_path; + let prefix = match prefix { + FilePrefix::Here | FilePrefix::Parent => ".", + FilePrefix::Absolute => "/", + FilePrefix::Home => "~", + }; + let path = + Some(prefix.to_string()).into_iter().chain(path).collect(); + ImportLocation::Local(path) + } + ImportLocation::Remote(url) => { + let mut url = url.clone(); + match prefix { + FilePrefix::Here => {} + FilePrefix::Parent => { + url = url.join("..")?; + } + FilePrefix::Absolute => panic!("error"), + FilePrefix::Home => panic!("error"), + } + url = url.join(&path.file_path.join("/"))?; + ImportLocation::Remote(url) + } + }) + } } fn mkexpr(kind: UnspannedExpr) -> Expr { @@ -40,52 +140,17 @@ fn make_aslocation_uniontype() -> Expr { mkexpr(ExprKind::UnionType(union)) } -fn compute_relative_path( - root: &ImportRoot, - prefix: &FilePrefix, - path: &FilePath, -) -> PathBuf { - let cwd = match root { - ImportRoot::LocalDir(cwd) => cwd, - }; - let mut cwd: Vec = cwd - .components() - .map(|component| component.as_os_str().to_string_lossy().into_owned()) - .collect(); - let root = match prefix { - FilePrefix::Here => cwd, - FilePrefix::Parent => { - cwd.push("..".to_string()); - cwd - } - FilePrefix::Absolute => vec![], - FilePrefix::Home => vec![], - }; - let path: Vec<_> = root - .into_iter() - .chain(path.file_path.iter().cloned()) - .collect(); - let path = (FilePath { file_path: path }).canonicalize().file_path; - let prefix = match prefix { - FilePrefix::Here | FilePrefix::Parent => ".", - FilePrefix::Absolute => "/", - FilePrefix::Home => "~", - }; - Some(prefix.to_string()).into_iter().chain(path).collect() -} - fn resolve_one_import( env: &mut ImportEnv, import: &Import, - root: &ImportRoot, + location: &ImportLocation, ) -> Result { + let location = location.chain(&import.location)?; match import.mode { ImportMode::Code => { - let parsed = match &import.location { - ImportLocation::Local(prefix, path) => { - let path = compute_relative_path(root, prefix, path); - Parsed::parse_file(&path)? - } + let parsed = match location { + ImportLocation::Local(path) => Parsed::parse_file(&path)?, + ImportLocation::Remote(url) => Parsed::parse_remote(url)?, ImportLocation::Env(var_name) => { let val = match env::var(var_name) { Ok(val) => val, @@ -94,24 +159,22 @@ fn resolve_one_import( Parsed::parse_str(&val)? } ImportLocation::Missing => Err(ImportError::Missing)?, - _ => unimplemented!("{:?}", import), }; let typed = resolve_with_env(env, parsed)?.typecheck()?; Ok((typed.normalize().to_hir(), typed.ty().clone())) } ImportMode::RawText => { - let text = match &import.location { - ImportLocation::Local(prefix, path) => { - let path = compute_relative_path(root, prefix, path); - std::fs::read_to_string(path)? + let text = match location { + ImportLocation::Local(path) => std::fs::read_to_string(&path)?, + ImportLocation::Remote(url) => { + reqwest::blocking::get(url).unwrap().text().unwrap() } ImportLocation::Env(var_name) => match env::var(var_name) { Ok(val) => val, Err(_) => Err(ImportError::MissingEnvVar)?, }, ImportLocation::Missing => Err(ImportError::Missing)?, - _ => unimplemented!("{:?}", import), }; let hir = Hir::new( @@ -121,27 +184,14 @@ fn resolve_one_import( Ok((hir, Type::from_builtin(Builtin::Text))) } ImportMode::Location => { - let (field_name, arg) = match &import.location { - ImportLocation::Local(prefix, path) => { - let path = compute_relative_path(root, prefix, path) - .to_string_lossy() - .into_owned(); - ("Local", Some(path)) + let (field_name, arg) = match location { + ImportLocation::Local(path) => { + ("Local", Some(path.to_string_lossy().into_owned())) } ImportLocation::Remote(url) => { - let path = - url.path.canonicalize().file_path.iter().join("/"); - let mut url_str = - format!("{}://{}/{}", url.scheme, url.authority, path); - if let Some(q) = &url.query { - url_str.push('?'); - url_str.push_str(q.as_ref()); - } - ("Remote", Some(url_str)) - } - ImportLocation::Env(name) => { - ("Environment", Some(name.clone())) + ("Remote", Some(url.to_string())) } + ImportLocation::Env(name) => ("Environment", Some(name)), ImportLocation::Missing => ("Missing", None), }; @@ -314,21 +364,21 @@ impl Canonicalize for FilePath { } } -impl Canonicalize for ImportLocation { - fn canonicalize(&self) -> ImportLocation { +impl Canonicalize for ImportTarget { + fn canonicalize(&self) -> ImportTarget { match self { - ImportLocation::Local(prefix, file) => { - ImportLocation::Local(*prefix, file.canonicalize()) + ImportTarget::Local(prefix, file) => { + ImportTarget::Local(*prefix, file.canonicalize()) } - ImportLocation::Remote(url) => ImportLocation::Remote(URL { + ImportTarget::Remote(url) => ImportTarget::Remote(URL { scheme: url.scheme, authority: url.authority.clone(), path: url.path.canonicalize(), query: url.query.clone(), headers: url.headers.clone(), }), - ImportLocation::Env(name) => ImportLocation::Env(name.to_string()), - ImportLocation::Missing => ImportLocation::Missing, + ImportTarget::Env(name) => ImportTarget::Env(name.to_string()), + ImportTarget::Missing => ImportTarget::Missing, } } } -- cgit v1.2.3 From 31cefbdf0364a3d224420365049885051734669b Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 4 Mar 2020 21:36:41 +0000 Subject: Cache imports correctly --- dhall/src/semantics/resolve/env.rs | 24 ++++++++++++------------ dhall/src/semantics/resolve/resolve.rs | 14 +++++++------- 2 files changed, 19 insertions(+), 19 deletions(-) (limited to 'dhall/src/semantics') diff --git a/dhall/src/semantics/resolve/env.rs b/dhall/src/semantics/resolve/env.rs index 43676cc..2342dcc 100644 --- a/dhall/src/semantics/resolve/env.rs +++ b/dhall/src/semantics/resolve/env.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use crate::error::{Error, ImportError}; -use crate::semantics::{AlphaVar, Import, TypedHir, VarEnv}; +use crate::semantics::{AlphaVar, ImportLocation, TypedHir, VarEnv}; use crate::syntax::{Label, V}; /// Environment for resolving names. @@ -10,8 +10,8 @@ pub(crate) struct NameEnv { names: Vec