summaryrefslogtreecommitdiff
path: root/dhall_core
diff options
context:
space:
mode:
authorNadrieril2019-03-21 22:08:23 +0100
committerNadrieril2019-03-21 22:08:23 +0100
commit4b99a3fb46191a83fa8551f21b98cff689bbb338 (patch)
tree0d07b39dbcf794c9fe1174aeb6fb4923df2fd9b7 /dhall_core
parentea08d2528d2ee46446ac8981e72af5c610e43ce1 (diff)
Improve import handling in parser
Diffstat (limited to 'dhall_core')
-rw-r--r--dhall_core/src/core.rs6
-rw-r--r--dhall_core/src/parser.rs21
-rw-r--r--dhall_core/src/printer.rs14
3 files changed, 32 insertions, 9 deletions
diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs
index 9f26b65..bc5a666 100644
--- a/dhall_core/src/core.rs
+++ b/dhall_core/src/core.rs
@@ -164,9 +164,9 @@ pub enum Builtin {
TextShow,
}
-pub type ParsedExpr<S> = SubExpr<S, Import>;
-pub type ResolvedExpr<S> = SubExpr<S, X>;
-pub type DhallExpr = ResolvedExpr<X>;
+pub type ParsedExpr = SubExpr<X, Import>;
+pub type ResolvedExpr = SubExpr<X, X>;
+pub type DhallExpr = ResolvedExpr;
pub type SubExpr<Note, Embed> = Rc<Expr<Note, Embed>>;
diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs
index 7de75d2..865e791 100644
--- a/dhall_core/src/parser.rs
+++ b/dhall_core/src/parser.rs
@@ -12,7 +12,6 @@ use crate::*;
// their own crate because they are quite general and useful. For now they
// are here and hopefully you can figure out how they work.
-type ParsedExpr = crate::ParsedExpr<X>;
type ParsedText = InterpolatedText<X, Import>;
type ParsedTextContents<'a> = InterpolatedTextContents<'a, X, Import>;
@@ -370,9 +369,21 @@ make_parser! {
}
);
- rule!(path<PathBuf>;
- captured_str!(s) => (".".to_owned() + s).into()
- );
+ rule!(unquoted_path_component<&'a str>; captured_str!(s) => s);
+ rule!(quoted_path_component<&'a str>; captured_str!(s) => s);
+ rule!(path_component<&'a str>; children!(
+ [unquoted_path_component(s)] => s,
+ [quoted_path_component(s)] => s,
+ ));
+ rule!(path<PathBuf>; children!(
+ [path_component(components..)] => {
+ let mut path = PathBuf::new();
+ for s in components {
+ path.push(s);
+ }
+ path
+ }
+ ));
rule_group!(local_raw<(FilePrefix, PathBuf)>);
@@ -728,7 +739,7 @@ make_parser! {
));
}
-pub fn parse_expr(s: &str) -> ParseResult<crate::ParsedExpr<X>> {
+pub fn parse_expr(s: &str) -> ParseResult<ParsedExpr> {
let mut pairs = DhallParser::parse(Rule::final_expression, s)?;
let expr = parse_any(pairs.next().unwrap())?;
assert_eq!(pairs.next(), None);
diff --git a/dhall_core/src/printer.rs b/dhall_core/src/printer.rs
index e43f1be..bd38863 100644
--- a/dhall_core/src/printer.rs
+++ b/dhall_core/src/printer.rs
@@ -286,7 +286,19 @@ impl Display for Const {
impl Display for Import {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
- <Self as fmt::Debug>::fmt(self, f)
+ use FilePrefix::*;
+ use ImportLocation::*;
+ match &self.location {
+ Local(prefix, path) => {
+ let prefix = match prefix {
+ Here => ".",
+ Parent => "..",
+ Home => "~",
+ Absolute => "",
+ };
+ write!(f, "{}/{}", prefix, path.to_str().unwrap())
+ }
+ }
}
}