summaryrefslogtreecommitdiff
path: root/dhall/src/phase/parse.rs
diff options
context:
space:
mode:
authorNadrieril2019-05-07 18:12:04 +0200
committerNadrieril2019-05-07 18:12:04 +0200
commit3da450aa3fae23214aa982643b9bc4dd0ea4eaa6 (patch)
tree02e00cd008d2e7dc899b9211379596fe792f41c8 /dhall/src/phase/parse.rs
parentd8a3e831fb67f86269c4baa99f9f0798a73a7247 (diff)
parent14dfeb8e7d2aa87a361a711a485243449426b144 (diff)
Merge branch 'reorganize'
Diffstat (limited to '')
-rw-r--r--dhall/src/phase/parse.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/dhall/src/phase/parse.rs b/dhall/src/phase/parse.rs
new file mode 100644
index 0000000..6b426af
--- /dev/null
+++ b/dhall/src/phase/parse.rs
@@ -0,0 +1,38 @@
+use std::fs::File;
+use std::io::Read;
+use std::path::Path;
+
+use dhall_syntax::parse_expr;
+
+use crate::error::Error;
+use crate::phase::resolve::ImportRoot;
+use crate::phase::Parsed;
+
+pub(crate) fn parse_file(f: &Path) -> Result<Parsed, Error> {
+ let mut buffer = String::new();
+ File::open(f)?.read_to_string(&mut buffer)?;
+ let expr = parse_expr(&*buffer)?;
+ let root = ImportRoot::LocalDir(f.parent().unwrap().to_owned());
+ Ok(Parsed(expr, root))
+}
+
+pub(crate) fn parse_str(s: &str) -> Result<Parsed, Error> {
+ let expr = parse_expr(s)?;
+ let root = ImportRoot::LocalDir(std::env::current_dir()?);
+ Ok(Parsed(expr, root))
+}
+
+pub(crate) fn parse_binary_file(f: &Path) -> Result<Parsed, Error> {
+ let mut buffer = Vec::new();
+ File::open(f)?.read_to_end(&mut buffer)?;
+ let expr = crate::phase::binary::decode(&buffer)?;
+ let root = ImportRoot::LocalDir(f.parent().unwrap().to_owned());
+ Ok(Parsed(expr.note_absurd(), root))
+}
+
+#[cfg(test)]
+mod spec_tests {
+ #![rustfmt::skip]
+ // See build.rs
+ include!(concat!(env!("OUT_DIR"), "/parser_tests.rs"));
+}