From e27adcdce55dc15c97bb0ac6d5bc0b082d2232c2 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 9 Mar 2019 14:32:07 +0100 Subject: Use new Label type instead of &str in parser --- dhall/src/imports.rs | 2 +- dhall/src/main.rs | 4 +- dhall_core/src/core.rs | 57 +++++++++++++++++++--- dhall_core/src/grammar_util.rs | 4 -- dhall_core/src/lib.rs | 1 - dhall_core/src/parser.rs | 104 +++++++++++++++++++++-------------------- dhall_generator/src/lib.rs | 4 +- 7 files changed, 110 insertions(+), 66 deletions(-) delete mode 100644 dhall_core/src/grammar_util.rs diff --git a/dhall/src/imports.rs b/dhall/src/imports.rs index 9ae66a7..d9f0b33 100644 --- a/dhall/src/imports.rs +++ b/dhall/src/imports.rs @@ -75,7 +75,7 @@ pub fn load_dhall_file( let mut buffer = String::new(); File::open(f)?.read_to_string(&mut buffer)?; let expr = parser::parse_expr(&*buffer)?; - let expr = expr.take_ownership_of_labels(); + let expr = expr.map_label(&|l| String::from(l.clone())); let expr = if resolve_imports { let root = ImportRoot::LocalDir(f.parent().unwrap().to_owned()); let resolve = |import: &Import| -> Expr { diff --git a/dhall/src/main.rs b/dhall/src/main.rs index d072fe7..db69a46 100644 --- a/dhall/src/main.rs +++ b/dhall/src/main.rs @@ -65,8 +65,8 @@ fn main() { } }; - let expr: Expr = - imports::panic_imports(&expr.take_ownership_of_labels()); + let expr: Expr = + imports::panic_imports(&expr); let type_expr = match typecheck::type_of(&expr) { Err(e) => { diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index eed9e0c..7bd1318 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -68,6 +68,41 @@ pub struct Import { pub hash: Option<()>, } +// The type for labels throughout the AST +// It owns the data because otherwise lifetimes would make recursive imports impossible +#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct Label(String); + +impl From for Label { + fn from(s: String) -> Self { + Label(s) + } +} + +impl From<&'static str> for Label { + fn from(s: &'static str) -> Self { + Label(s.to_owned()) + } +} + +impl From