summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNadrieril2019-03-22 18:12:13 +0100
committerNadrieril2019-03-22 18:12:13 +0100
commit8110651ccf498bcf3f0cd55f3b1730d9972cf254 (patch)
tree2e0b08153582b23bce178f4ca3b945b01ddd6306
parent9a8a14ba3863b3bc5e79cd8070d6779451ff8466 (diff)
Handle quoted labels
-rw-r--r--dhall/tests/parser.rs2
-rw-r--r--dhall_core/src/label.rs7
-rw-r--r--dhall_core/src/parser.rs7
-rw-r--r--dhall_core/src/printer.rs15
-rw-r--r--dhall_parser/build.rs2
5 files changed, 23 insertions, 10 deletions
diff --git a/dhall/tests/parser.rs b/dhall/tests/parser.rs
index 637341d..e73e78c 100644
--- a/dhall/tests/parser.rs
+++ b/dhall/tests/parser.rs
@@ -45,7 +45,7 @@ parser_success!(spec_parser_success_operators, "operators");
// parser_success!(spec_parser_success_parenthesizeUsing, "parenthesizeUsing");
parser_success!(spec_parser_success_pathTermination, "pathTermination");
parser_success!(spec_parser_success_paths, "paths");
-// parser_success!(spec_parser_success_quotedLabel, "quotedLabel");
+parser_success!(spec_parser_success_quotedLabel, "quotedLabel");
parser_success!(spec_parser_success_quotedPaths, "quotedPaths");
parser_success!(spec_parser_success_record, "record");
parser_success!(spec_parser_success_reservedPrefix, "reservedPrefix");
diff --git a/dhall_core/src/label.rs b/dhall_core/src/label.rs
index 3633b93..8b371af 100644
--- a/dhall_core/src/label.rs
+++ b/dhall_core/src/label.rs
@@ -1,4 +1,3 @@
-use std::fmt::{self, Display};
use std::rc::Rc;
// The type for labels throughout the AST
@@ -25,12 +24,6 @@ impl From<Label> for String {
}
}
-impl Display for Label {
- fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
- self.0.as_ref().fmt(f)
- }
-}
-
impl Label {
pub fn from_str(s: &str) -> Label {
Label(s.into())
diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs
index 165b393..8908c78 100644
--- a/dhall_core/src/parser.rs
+++ b/dhall_core/src/parser.rs
@@ -257,7 +257,12 @@ fn can_be_shortcutted(rule: Rule) -> bool {
make_parser! {
rule!(EOI<()>; raw_pair!(_) => ());
- rule!(label_raw<Label>; captured_str!(s) => Label::from(s.trim().to_owned()));
+ rule!(simple_label<Label>; captured_str!(s) => Label::from(s.trim().to_owned()));
+ rule!(quoted_label<Label>; captured_str!(s) => Label::from(s.trim().to_owned()));
+ rule!(label_raw<Label>; children!(
+ [simple_label(l)] => l,
+ [quoted_label(l)] => l,
+ ));
rule!(double_quote_literal<ParsedText>; children!(
[double_quote_chunk(chunks..)] => {
diff --git a/dhall_core/src/printer.rs b/dhall_core/src/printer.rs
index 7b108d5..508c1c8 100644
--- a/dhall_core/src/printer.rs
+++ b/dhall_core/src/printer.rs
@@ -285,6 +285,21 @@ impl Display for Const {
}
}
+impl Display for Label {
+ fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+ let s = String::from(self.clone());
+ let is_keyword = |s| match s {
+ "let" | "in" | "if" | "then" | "else" => true,
+ _ => false,
+ };
+ if s.chars().all(|c| c.is_ascii_alphanumeric()) && !is_keyword(&s) {
+ write!(f, "{}", s)
+ } else {
+ write!(f, "`{}`", s)
+ }
+ }
+}
+
impl Display for Import {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
use std::path::PathBuf;
diff --git a/dhall_parser/build.rs b/dhall_parser/build.rs
index a07c7e0..fc398ff 100644
--- a/dhall_parser/build.rs
+++ b/dhall_parser/build.rs
@@ -35,7 +35,7 @@ fn main() -> std::io::Result<()> {
writeln!(&mut file)?;
writeln!(
&mut file,
- "simple_label = _{{
+ "simple_label = {{
keyword_raw ~ simple_label_next_char+
| !keyword_raw ~ simple_label_first_char ~ simple_label_next_char*
}}"