summaryrefslogtreecommitdiff
path: root/dhall/src/syntax/text
diff options
context:
space:
mode:
authorNadrieril Feneanar2019-12-19 21:33:26 +0000
committerGitHub2019-12-19 21:33:26 +0000
commit91ef0cf697d56c91a8d15937aa4669dc221cd6c1 (patch)
treed3f00cf31d4386b82c6fb09eda3f690415dd8902 /dhall/src/syntax/text
parent3f00e4ca3fe22f88a1d0633e254df0bff781c6d3 (diff)
parent1e4f15d1891b497ecf6632432bc9252dc6a4507d (diff)
Merge pull request #117 from Nadrieril/merge-crates
Merge a bunch of sub-crates
Diffstat (limited to '')
-rw-r--r--dhall/src/syntax/text/mod.rs2
-rw-r--r--dhall/src/syntax/text/parser.rs (renamed from dhall_syntax/src/parser.rs)180
-rw-r--r--dhall/src/syntax/text/printer.rs (renamed from dhall_syntax/src/printer.rs)14
3 files changed, 88 insertions, 108 deletions
diff --git a/dhall/src/syntax/text/mod.rs b/dhall/src/syntax/text/mod.rs
new file mode 100644
index 0000000..c868288
--- /dev/null
+++ b/dhall/src/syntax/text/mod.rs
@@ -0,0 +1,2 @@
+pub mod parser;
+pub mod printer;
diff --git a/dhall_syntax/src/parser.rs b/dhall/src/syntax/text/parser.rs
index f5d161f..f6b6577 100644
--- a/dhall_syntax/src/parser.rs
+++ b/dhall/src/syntax/text/parser.rs
@@ -3,36 +3,42 @@ use pest::prec_climber as pcl;
use pest::prec_climber::PrecClimber;
use std::rc::Rc;
-use dgp::Rule;
-use dhall_generated_parser as dgp;
use pest_consume::{match_nodes, Parser};
-use crate::map::{DupTreeMap, DupTreeSet};
-use crate::ExprF::*;
-use crate::*;
+use crate::semantics::phase::Normalized;
+use crate::syntax;
+use crate::syntax::core;
+use crate::syntax::map::{DupTreeMap, DupTreeSet};
+use crate::syntax::ExprF::*;
+use crate::syntax::{
+ FilePath, FilePrefix, Hash, ImportLocation, ImportMode, InterpolatedText,
+ InterpolatedTextContents, Label, NaiveDouble, RawExpr, Scheme, Span, URL,
+ V,
+};
// This file consumes the parse tree generated by pest and turns it into
// our own AST. All those custom macros should eventually moved into
// 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 ParsedText<E> = InterpolatedText<Expr<E>>;
-type ParsedTextContents<E> = InterpolatedTextContents<Expr<E>>;
+type Expr = syntax::Expr<Normalized>;
+type ParsedText = InterpolatedText<Expr>;
+type ParsedTextContents = InterpolatedTextContents<Expr>;
type ParseInput<'input> = pest_consume::Node<'input, Rule, Rc<str>>;
pub type ParseError = pest::error::Error<Rule>;
pub type ParseResult<T> = Result<T, ParseError>;
#[derive(Debug)]
-enum Selector<E> {
+enum Selector {
Field(Label),
Projection(DupTreeSet<Label>),
- ProjectionByExpr(Expr<E>),
+ ProjectionByExpr(Expr),
}
-impl crate::Builtin {
+impl crate::syntax::Builtin {
pub fn parse(s: &str) -> Option<Self> {
- use crate::Builtin::*;
+ use crate::syntax::Builtin::*;
match s {
"Bool" => Some(Bool),
"Natural" => Some(Natural),
@@ -71,16 +77,16 @@ impl crate::Builtin {
fn input_to_span(input: ParseInput) -> Span {
Span::make(input.user_data().clone(), input.as_pair().as_span())
}
-fn spanned<E>(input: ParseInput, x: RawExpr<E>) -> Expr<E> {
+fn spanned(input: ParseInput, x: RawExpr<Normalized>) -> Expr {
Expr::new(x, input_to_span(input))
}
-fn spanned_union<E>(span1: Span, span2: Span, x: RawExpr<E>) -> Expr<E> {
+fn spanned_union(span1: Span, span2: Span, x: RawExpr<Normalized>) -> Expr {
Expr::new(x, span1.union(&span2))
}
// Trim the shared indent off of a vec of lines, as defined by the Dhall semantics of multiline
// literals.
-fn trim_indent<E: Clone>(lines: &mut Vec<ParsedText<E>>) {
+fn trim_indent(lines: &mut Vec<ParsedText>) {
let is_indent = |c: char| c == ' ' || c == '\t';
// There is at least one line so this is safe
@@ -147,9 +153,11 @@ lazy_static::lazy_static! {
};
}
+#[derive(Parser)]
+#[grammar = "dhall.pest"]
struct DhallParser;
-#[pest_consume::parser(parser = dgp::DhallParser, rule = dgp::Rule)]
+#[pest_consume::parser(parser = DhallParser, rule = Rule)]
impl DhallParser {
fn EOI(_input: ParseInput) -> ParseResult<()> {
Ok(())
@@ -164,9 +172,7 @@ impl DhallParser {
Ok(Label::from(input.as_str()))
}
- fn double_quote_literal<E: Clone>(
- input: ParseInput,
- ) -> ParseResult<ParsedText<E>> {
+ fn double_quote_literal(input: ParseInput) -> ParseResult<ParsedText> {
Ok(match_nodes!(input.into_children();
[double_quote_chunk(chunks)..] => {
chunks.collect()
@@ -174,9 +180,9 @@ impl DhallParser {
))
}
- fn double_quote_chunk<E: Clone>(
+ fn double_quote_chunk(
input: ParseInput,
- ) -> ParseResult<ParsedTextContents<E>> {
+ ) -> ParseResult<ParsedTextContents> {
Ok(match_nodes!(input.into_children();
[expression(e)] => {
InterpolatedTextContents::Expr(e)
@@ -261,18 +267,16 @@ impl DhallParser {
Ok(input.as_str().to_owned())
}
- fn single_quote_literal<E: Clone>(
- input: ParseInput,
- ) -> ParseResult<ParsedText<E>> {
+ fn single_quote_literal(input: ParseInput) -> ParseResult<ParsedText> {
Ok(match_nodes!(input.into_children();
[single_quote_continue(lines)] => {
- let newline: ParsedText<E> = "\n".to_string().into();
+ let newline: ParsedText = "\n".to_string().into();
// Reverse lines and chars in each line
- let mut lines: Vec<ParsedText<E>> = lines
+ let mut lines: Vec<ParsedText> = lines
.into_iter()
.rev()
- .map(|l| l.into_iter().rev().collect::<ParsedText<E>>())
+ .map(|l| l.into_iter().rev().collect::<ParsedText>())
.collect();
trim_indent(&mut lines);
@@ -281,7 +285,7 @@ impl DhallParser {
.into_iter()
.intersperse(newline)
.flat_map(InterpolatedText::into_iter)
- .collect::<ParsedText<E>>()
+ .collect::<ParsedText>()
}
))
}
@@ -298,9 +302,9 @@ impl DhallParser {
}
// Returns a vec of lines in reversed order, where each line is also in reversed order.
- fn single_quote_continue<E: Clone>(
+ fn single_quote_continue(
input: ParseInput,
- ) -> ParseResult<Vec<Vec<ParsedTextContents<E>>>> {
+ ) -> ParseResult<Vec<Vec<ParsedTextContents>>> {
Ok(match_nodes!(input.into_children();
[expression(e), single_quote_continue(lines)] => {
let c = InterpolatedTextContents::Expr(e);
@@ -326,16 +330,16 @@ impl DhallParser {
}
#[alias(expression)]
- fn builtin<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> {
+ fn builtin(input: ParseInput) -> ParseResult<Expr> {
let s = input.as_str();
- let e = match crate::Builtin::parse(s) {
+ let e = match crate::syntax::Builtin::parse(s) {
Some(b) => Builtin(b),
None => match s {
"True" => BoolLit(true),
"False" => BoolLit(false),
- "Type" => Const(crate::Const::Type),
- "Kind" => Const(crate::Const::Kind),
- "Sort" => Const(crate::Const::Sort),
+ "Type" => Const(crate::syntax::Const::Type),
+ "Kind" => Const(crate::syntax::Const::Kind),
+ "Sort" => Const(crate::syntax::Const::Sort),
_ => {
Err(input.error(format!("Unrecognized builtin: '{}'", s)))?
}
@@ -387,7 +391,7 @@ impl DhallParser {
}
#[alias(expression, shortcut = true)]
- fn identifier<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> {
+ fn identifier(input: ParseInput) -> ParseResult<Expr> {
Ok(match_nodes!(input.children();
[variable(v)] => spanned(input, Var(v)),
[expression(e)] => e,
@@ -441,9 +445,7 @@ impl DhallParser {
}
#[alias(import_type)]
- fn local<E: Clone>(
- input: ParseInput,
- ) -> ParseResult<ImportLocation<Expr<E>>> {
+ fn local(input: ParseInput) -> ParseResult<ImportLocation<Expr>> {
Ok(match_nodes!(input.into_children();
[local_path((prefix, p))] => ImportLocation::Local(prefix, p),
))
@@ -482,7 +484,7 @@ impl DhallParser {
})
}
- fn http_raw<E: Clone>(input: ParseInput) -> ParseResult<URL<Expr<E>>> {
+ fn http_raw(input: ParseInput) -> ParseResult<URL<Expr>> {
Ok(match_nodes!(input.into_children();
[scheme(sch), authority(auth), path(p)] => URL {
scheme: sch,
@@ -510,9 +512,7 @@ impl DhallParser {
}
#[alias(import_type)]
- fn http<E: Clone>(
- input: ParseInput,
- ) -> ParseResult<ImportLocation<Expr<E>>> {
+ fn http(input: ParseInput) -> ParseResult<ImportLocation<Expr>> {
Ok(ImportLocation::Remote(match_nodes!(input.into_children();
[http_raw(url)] => url,
[http_raw(url), expression(e)] => URL { headers: Some(e), ..url },
@@ -520,9 +520,7 @@ impl DhallParser {
}
#[alias(import_type)]
- fn env<E: Clone>(
- input: ParseInput,
- ) -> ParseResult<ImportLocation<Expr<E>>> {
+ fn env(input: ParseInput) -> ParseResult<ImportLocation<Expr>> {
Ok(match_nodes!(input.into_children();
[environment_variable(v)] => ImportLocation::Env(v),
))
@@ -557,9 +555,7 @@ impl DhallParser {
}
#[alias(import_type)]
- fn missing<E: Clone>(
- _input: ParseInput,
- ) -> ParseResult<ImportLocation<Expr<E>>> {
+ fn missing(_input: ParseInput) -> ParseResult<ImportLocation<Expr>> {
Ok(ImportLocation::Missing)
}
@@ -573,10 +569,10 @@ impl DhallParser {
Ok(Hash::SHA256(hex::decode(hash).unwrap()))
}
- fn import_hashed<E: Clone>(
+ fn import_hashed(
input: ParseInput,
- ) -> ParseResult<crate::Import<Expr<E>>> {
- use crate::Import;
+ ) -> ParseResult<crate::syntax::Import<Expr>> {
+ use crate::syntax::Import;
let mode = ImportMode::Code;
Ok(match_nodes!(input.into_children();
[import_type(location)] => Import { mode, location, hash: None },
@@ -594,8 +590,8 @@ impl DhallParser {
}
#[alias(expression)]
- fn import<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> {
- use crate::Import;
+ fn import(input: ParseInput) -> ParseResult<Expr> {
+ use crate::syntax::Import;
let import = match_nodes!(input.children();
[import_hashed(imp)] => {
Import { mode: ImportMode::Code, ..imp }
@@ -630,13 +626,13 @@ impl DhallParser {
}
#[alias(expression)]
- fn empty_list_literal<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> {
+ fn empty_list_literal(input: ParseInput) -> ParseResult<Expr> {
Ok(match_nodes!(input.children();
[expression(e)] => spanned(input, EmptyListLit(e)),
))
}
- fn expression<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> {
+ fn expression(input: ParseInput) -> ParseResult<Expr> {
Ok(match_nodes!(input.children();
[lambda(()), label(l), expression(typ),
arrow(()), expression(body)] => {
@@ -681,9 +677,9 @@ impl DhallParser {
))
}
- fn let_binding<E: Clone>(
+ fn let_binding(
input: ParseInput,
- ) -> ParseResult<(Label, Option<Expr<E>>, Expr<E>, Span)> {
+ ) -> ParseResult<(Label, Option<Expr>, Expr, Span)> {
Ok(match_nodes!(input.children();
[label(name), expression(annot), expression(expr)] =>
(name, Some(annot), expr, input_to_span(input)),
@@ -694,12 +690,12 @@ impl DhallParser {
#[alias(expression, shortcut = true)]
#[prec_climb(expression, PRECCLIMBER)]
- fn operator_expression<E: Clone>(
- l: Expr<E>,
+ fn operator_expression(
+ l: Expr,
op: ParseInput,
- r: Expr<E>,
- ) -> ParseResult<Expr<E>> {
- use crate::BinOp::*;
+ r: Expr,
+ ) -> ParseResult<Expr> {
+ use crate::syntax::BinOp::*;
use Rule::*;
let op = match op.as_rule() {
import_alt => ImportAlt,
@@ -726,9 +722,7 @@ impl DhallParser {
}
#[alias(expression, shortcut = true)]
- fn application_expression<E: Clone>(
- input: ParseInput,
- ) -> ParseResult<Expr<E>> {
+ fn application_expression(input: ParseInput) -> ParseResult<Expr> {
Ok(match_nodes!(input.children();
[expression(e)] => e,
[expression(first), expression(rest)..] => {
@@ -747,9 +741,7 @@ impl DhallParser {
}
#[alias(expression, shortcut = true)]
- fn first_application_expression<E: Clone>(
- input: ParseInput,
- ) -> ParseResult<Expr<E>> {
+ fn first_application_expression(input: ParseInput) -> ParseResult<Expr> {
Ok(match_nodes!(input.children();
[Some_(()), expression(e)] => {
spanned(input, SomeLit(e))
@@ -765,9 +757,7 @@ impl DhallParser {
}
#[alias(expression, shortcut = true)]
- fn selector_expression<E: Clone>(
- input: ParseInput,
- ) -> ParseResult<Expr<E>> {
+ fn selector_expression(input: ParseInput) -> ParseResult<Expr> {
Ok(match_nodes!(input.children();
[expression(e)] => e,
[expression(first), selector(rest)..] => {
@@ -789,9 +779,7 @@ impl DhallParser {
))
}
- fn selector<E: Clone>(
- input: ParseInput,
- ) -> ParseResult<(Selector<E>, Span)> {
+ fn selector(input: ParseInput) -> ParseResult<(Selector, Span)> {
let stor = match_nodes!(input.children();
[label(l)] => Selector::Field(l),
[labels(ls)] => Selector::Projection(ls),
@@ -807,9 +795,7 @@ impl DhallParser {
}
#[alias(expression, shortcut = true)]
- fn primitive_expression<E: Clone>(
- input: ParseInput,
- ) -> ParseResult<Expr<E>> {
+ fn primitive_expression(input: ParseInput) -> ParseResult<Expr> {
Ok(match_nodes!(input.children();
[double_literal(n)] => spanned(input, DoubleLit(n)),
[natural_literal(n)] => spanned(input, NaturalLit(n)),
@@ -821,21 +807,19 @@ impl DhallParser {
}
#[alias(expression)]
- fn empty_record_literal<E: Clone>(
- input: ParseInput,
- ) -> ParseResult<Expr<E>> {
+ fn empty_record_literal(input: ParseInput) -> ParseResult<Expr> {
Ok(spanned(input, RecordLit(Default::default())))
}
#[alias(expression)]
- fn empty_record_type<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> {
+ fn empty_record_type(input: ParseInput) -> ParseResult<Expr> {
Ok(spanned(input, RecordType(Default::default())))
}
#[alias(expression)]
- fn non_empty_record_type_or_literal<E: Clone>(
+ fn non_empty_record_type_or_literal(
input: ParseInput,
- ) -> ParseResult<Expr<E>> {
+ ) -> ParseResult<Expr> {
let e = match_nodes!(input.children();
[label(first_label), non_empty_record_type(rest)] => {
let (first_expr, mut map) = rest;
@@ -851,9 +835,9 @@ impl DhallParser {
Ok(spanned(input, e))
}
- fn non_empty_record_type<E: Clone>(
+ fn non_empty_record_type(
input: ParseInput,
- ) -> ParseResult<(Expr<E>, DupTreeMap<Label, Expr<E>>)> {
+ ) -> ParseResult<(Expr, DupTreeMap<Label, Expr>)> {
Ok(match_nodes!(input.into_children();
[expression(expr), record_type_entry(entries)..] => {
(expr, entries.collect())
@@ -861,17 +845,15 @@ impl DhallParser {
))
}
- fn record_type_entry<E: Clone>(
- input: ParseInput,
- ) -> ParseResult<(Label, Expr<E>)> {
+ fn record_type_entry(input: ParseInput) -> ParseResult<(Label, Expr)> {
Ok(match_nodes!(input.into_children();
[label(name), expression(expr)] => (name, expr)
))
}
- fn non_empty_record_literal<E: Clone>(
+ fn non_empty_record_literal(
input: ParseInput,
- ) -> ParseResult<(Expr<E>, DupTreeMap<Label, Expr<E>>)> {
+ ) -> ParseResult<(Expr, DupTreeMap<Label, Expr>)> {
Ok(match_nodes!(input.into_children();
[expression(expr), record_literal_entry(entries)..] => {
(expr, entries.collect())
@@ -879,16 +861,14 @@ impl DhallParser {
))
}
- fn record_literal_entry<E: Clone>(
- input: ParseInput,
- ) -> ParseResult<(Label, Expr<E>)> {
+ fn record_literal_entry(input: ParseInput) -> ParseResult<(Label, Expr)> {
Ok(match_nodes!(input.into_children();
[label(name), expression(expr)] => (name, expr)
))
}
#[alias(expression)]
- fn union_type<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> {
+ fn union_type(input: ParseInput) -> ParseResult<Expr> {
let map = match_nodes!(input.children();
[empty_union_type(_)] => Default::default(),
[union_type_entry(entries)..] => entries.collect(),
@@ -900,9 +880,9 @@ impl DhallParser {
Ok(())
}
- fn union_type_entry<E: Clone>(
+ fn union_type_entry(
input: ParseInput,
- ) -> ParseResult<(Label, Option<Expr<E>>)> {
+ ) -> ParseResult<(Label, Option<Expr>)> {
Ok(match_nodes!(input.children();
[label(name), expression(expr)] => (name, Some(expr)),
[label(name)] => (name, None),
@@ -910,9 +890,7 @@ impl DhallParser {
}
#[alias(expression)]
- fn non_empty_list_literal<E: Clone>(
- input: ParseInput,
- ) -> ParseResult<Expr<E>> {
+ fn non_empty_list_literal(input: ParseInput) -> ParseResult<Expr> {
Ok(match_nodes!(input.children();
[expression(items)..] => spanned(
input,
@@ -922,14 +900,14 @@ impl DhallParser {
}
#[alias(expression)]
- fn final_expression<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> {
+ fn final_expression(input: ParseInput) -> ParseResult<Expr> {
Ok(match_nodes!(input.into_children();
[expression(e), EOI(_)] => e
))
}
}
-pub fn parse_expr<E: Clone>(input_str: &str) -> ParseResult<Expr<E>> {
+pub fn parse_expr(input_str: &str) -> ParseResult<Expr> {
let rc_input_str = input_str.to_string().into();
let inputs = DhallParser::parse_with_userdata(
Rule::final_expression,
diff --git a/dhall_syntax/src/printer.rs b/dhall/src/syntax/text/printer.rs
index ce6ff97..8df456b 100644
--- a/dhall_syntax/src/printer.rs
+++ b/dhall/src/syntax/text/printer.rs
@@ -1,11 +1,11 @@
-use crate::*;
+use crate::syntax::*;
use itertools::Itertools;
use std::fmt::{self, Display};
/// Generic instance that delegates to subexpressions
impl<SE: Display + Clone, E: Display> Display for ExprF<SE, E> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
- use crate::ExprF::*;
+ use crate::syntax::ExprF::*;
match self {
Lam(a, b, c) => {
write!(f, "λ({} : {}) → {}", a, b, c)?;
@@ -141,7 +141,7 @@ impl<A: Display + Clone> RawExpr<A> {
f: &mut fmt::Formatter,
phase: PrintPhase,
) -> Result<(), fmt::Error> {
- use crate::ExprF::*;
+ use crate::syntax::ExprF::*;
use PrintPhase::*;
let needs_paren = match self {
@@ -298,7 +298,7 @@ impl Display for Const {
impl Display for BinOp {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
- use crate::BinOp::*;
+ use crate::syntax::BinOp::*;
f.write_str(match self {
BoolOr => "||",
TextAppend => "++",
@@ -344,7 +344,7 @@ impl Display for Label {
let is_reserved = match s.as_str() {
"let" | "in" | "if" | "then" | "else" | "Type" | "Kind"
| "Sort" | "True" | "False" => true,
- _ => crate::Builtin::parse(&s).is_some(),
+ _ => crate::syntax::Builtin::parse(&s).is_some(),
};
if !is_reserved && s.chars().all(|c| c.is_ascii_alphanumeric()) {
write!(f, "{}", s)
@@ -443,7 +443,7 @@ impl<SubExpr: Display> Display for Import<SubExpr> {
impl Display for Builtin {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
- use crate::Builtin::*;
+ use crate::syntax::Builtin::*;
f.write_str(match *self {
Bool => "Bool",
Natural => "Natural",
@@ -480,7 +480,7 @@ impl Display for Builtin {
impl Display for Scheme {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
- use crate::Scheme::*;
+ use crate::syntax::Scheme::*;
f.write_str(match *self {
HTTP => "http",
HTTPS => "https",