From 7bbf42dc5d3727dffcb036ffe30dd433faff1950 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 21 Mar 2019 15:11:55 +0100 Subject: Reorganize dhall_core a bit --- dhall/src/imports.rs | 8 +- dhall/src/main.rs | 2 +- dhall/src/normalize.rs | 2 +- dhall/tests/common/mod.rs | 2 +- dhall_core/src/core.rs | 682 +++------------------------------ dhall_core/src/lib.rs | 13 +- dhall_core/src/parser.rs | 923 +++++++++++++++++++++++---------------------- dhall_generator/src/lib.rs | 24 +- 8 files changed, 558 insertions(+), 1098 deletions(-) diff --git a/dhall/src/imports.rs b/dhall/src/imports.rs index 22f7863..2435663 100644 --- a/dhall/src/imports.rs +++ b/dhall/src/imports.rs @@ -43,11 +43,11 @@ fn resolve_import( #[derive(Debug)] pub enum DhallError { - ParseError(parser::ParseError), + ParseError(ParseError), IOError(std::io::Error), } -impl From for DhallError { - fn from(e: parser::ParseError) -> Self { +impl From for DhallError { + fn from(e: ParseError) -> Self { DhallError::ParseError(e) } } @@ -72,7 +72,7 @@ pub fn load_dhall_file( ) -> Result, DhallError> { let mut buffer = String::new(); File::open(f)?.read_to_string(&mut buffer)?; - let expr = parser::parse_expr(&*buffer)?; + let expr = parse_expr(&*buffer)?; 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 810d789..1a2e309 100644 --- a/dhall/src/main.rs +++ b/dhall/src/main.rs @@ -58,7 +58,7 @@ fn print_error(message: &str, source: &str, start: usize, end: usize) { fn main() { let mut buffer = String::new(); io::stdin().read_to_string(&mut buffer).unwrap(); - let expr = match parser::parse_expr(&buffer) { + let expr = match parse_expr(&buffer) { Ok(e) => e, Err(e) => { print_error(&format!("Parse error {}", e), &buffer, 0, 0); diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index 6257faf..7eba0cc 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -1,5 +1,5 @@ #![allow(non_snake_case)] -use dhall_core::core::*; +use dhall_core::*; use dhall_generator::dhall_expr; use std::fmt; use std::rc::Rc; diff --git a/dhall/tests/common/mod.rs b/dhall/tests/common/mod.rs index 75aee38..ffc6d06 100644 --- a/dhall/tests/common/mod.rs +++ b/dhall/tests/common/mod.rs @@ -76,7 +76,7 @@ pub fn run_test(base_path: &str, feature: Feature) { assert_eq_pretty!(expr, expected); // Round-trip pretty-printer - let expr = parser::parse_expr(&expr.to_string()).unwrap(); + let expr = parse_expr(&expr.to_string()).unwrap(); let expr = dhall::imports::panic_imports(&expr); assert_eq!(expr, expected); } diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index e7c9f2a..68e781d 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -1,11 +1,17 @@ #![allow(non_snake_case)] +use crate::*; use std::collections::BTreeMap; -use std::fmt::{self, Display}; -use std::iter::FromIterator; -use std::ops::Add; -use std::path::PathBuf; use std::rc::Rc; +pub type Double = f64; +pub type Int = isize; +pub type Integer = isize; +pub type Natural = usize; + +/// An empty type +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +pub enum X {} + /// Constants for a pure type system /// /// The only axiom is: @@ -33,82 +39,9 @@ pub enum Const { Kind, } -/// The beginning of a file path which anchors subsequent path components -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum FilePrefix { - /// Absolute path - Absolute, - /// Path relative to . - Here, - /// Path relative to .. - Parent, - /// Path relative to ~ - Home, -} - -/// The location of import (i.e. local vs. remote vs. environment) -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum ImportLocation { - Local(FilePrefix, PathBuf), - // TODO: other import types -} - -/// How to interpret the import's contents (i.e. as Dhall code or raw text) -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum ImportMode { - Code, - // TODO - // RawText, -} - -/// Reference to an external resource -#[derive(Debug, Clone, PartialEq, Eq)] -pub struct Import { - pub mode: ImportMode, - pub location: ImportLocation, - // TODO - 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(Rc); - -impl From for Label { - fn from(s: String) -> Self { - let s: &str = &s; - Label(s.into()) - } -} - -impl<'a> From<&'a str> for Label { - fn from(s: &'a str) -> Self { - Label(Rc::from(s)) - } -} - -impl From