summaryrefslogtreecommitdiff
path: root/dhall/src/semantics/phase/parse.rs
blob: 00db422a47cc007b4bb41d89431a876e729fb219 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use std::fs::File;
use std::io::Read;
use std::path::Path;

use crate::error::Error;
use crate::semantics::phase::resolve::ImportRoot;
use crate::semantics::phase::Parsed;
use crate::syntax::binary;
use crate::syntax::parse_expr;

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(data: &[u8]) -> Result<Parsed, Error> {
    let expr = binary::decode(data)?;
    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 = binary::decode(&buffer)?;
    let root = ImportRoot::LocalDir(f.parent().unwrap().to_owned());
    Ok(Parsed(expr, root))
}