diff options
author | Nadrieril Feneanar | 2019-09-20 21:12:57 +0200 |
---|---|---|
committer | GitHub | 2019-09-20 21:12:57 +0200 |
commit | e5bba4a0d148c647ff9c1a372a6470b3b3bfdc6e (patch) | |
tree | 45e5d0c715b9db8c1b30a019a6c971a7340c5117 /dhall_syntax | |
parent | 4424d3ece5db10527ae446a685a579ae768ef943 (diff) | |
parent | 4daead27eb65e3a38869924f0f3ed1f425de1b33 (diff) |
Merge branch 'master' into fintan/canonicalize
Diffstat (limited to '')
-rw-r--r-- | dhall_syntax/Cargo.toml | 1 | ||||
-rw-r--r-- | dhall_syntax/src/core/expr.rs | 22 | ||||
-rw-r--r-- | dhall_syntax/src/lib.rs | 2 | ||||
-rw-r--r-- | dhall_syntax/src/parser.rs | 542 |
4 files changed, 234 insertions, 333 deletions
diff --git a/dhall_syntax/Cargo.toml b/dhall_syntax/Cargo.toml index eb492d0..b98c4a4 100644 --- a/dhall_syntax/Cargo.toml +++ b/dhall_syntax/Cargo.toml @@ -18,3 +18,4 @@ hex = "0.3.2" lazy_static = "1.4.0" dhall_generated_parser = { path = "../dhall_generated_parser" } dhall_proc_macros = { path = "../dhall_proc_macros" } +pest_consume = "1.0" diff --git a/dhall_syntax/src/core/expr.rs b/dhall_syntax/src/core/expr.rs index 2cb23c9..74b481f 100644 --- a/dhall_syntax/src/core/expr.rs +++ b/dhall_syntax/src/core/expr.rs @@ -37,6 +37,16 @@ impl Span { end: sp.end(), } } + /// Takes the union of the two spans. Assumes that the spans come from the same input. + /// This will also capture any input between the spans. + pub fn union(&self, other: &Span) -> Self { + use std::cmp::{max, min}; + Span { + input: self.input.clone(), + start: min(self.start, other.start), + end: max(self.start, other.start), + } + } } /// Double with bitwise equality @@ -324,8 +334,11 @@ impl<E> Expr<E> { pub fn as_mut(&mut self) -> &mut RawExpr<E> { &mut self.0.as_mut().0 } + pub fn span(&self) -> Option<Span> { + self.0.as_ref().1.clone() + } - pub fn new(x: RawExpr<E>, n: Span) -> Self { + pub(crate) fn new(x: RawExpr<E>, n: Span) -> Self { Expr(Box::new((x, Some(n)))) } @@ -384,13 +397,6 @@ pub fn rc<E>(x: RawExpr<E>) -> Expr<E> { Expr::from_expr_no_span(x) } -pub(crate) fn spanned<E>(span: Span, x: RawExpr<E>) -> Expr<E> { - Expr::new(x, span) -} -pub(crate) fn unspanned<E>(x: RawExpr<E>) -> Expr<E> { - Expr::from_expr_no_span(x) -} - /// Add an isize to an usize /// Panics on over/underflow fn add_ui(u: usize, i: isize) -> Option<usize> { diff --git a/dhall_syntax/src/lib.rs b/dhall_syntax/src/lib.rs index 95f40c2..b8fa19f 100644 --- a/dhall_syntax/src/lib.rs +++ b/dhall_syntax/src/lib.rs @@ -1,7 +1,5 @@ #![feature(trace_macros)] -#![feature(slice_patterns)] #![feature(never_type)] -#![feature(proc_macro_hygiene)] #![allow( clippy::many_single_char_names, clippy::should_implement_trait, diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index 41d6d04..f2dea53 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -1,13 +1,11 @@ use itertools::Itertools; -use pest::iterators::Pair; use pest::prec_climber as pcl; use pest::prec_climber::PrecClimber; -use pest::Parser; -use std::borrow::Cow; use std::rc::Rc; -use dhall_generated_parser::{DhallParser, Rule}; -use dhall_proc_macros::{make_parser, parse_children}; +use dgp::Rule; +use dhall_generated_parser as dgp; +use pest_consume::{match_nodes, Parser}; use crate::map::{DupTreeMap, DupTreeSet}; use crate::ExprF::*; @@ -20,115 +18,11 @@ use crate::*; type ParsedText<E> = InterpolatedText<Expr<E>>; type ParsedTextContents<E> = InterpolatedTextContents<Expr<E>>; +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, Clone)] -struct ParseInput<'input, Rule> -where - Rule: pest::RuleType, -{ - pair: Pair<'input, Rule>, - original_input_str: Rc<str>, -} - -impl<'input> ParseInput<'input, Rule> { - fn error(&self, message: String) -> ParseError { - let message = format!( - "{} while matching on:\n{}", - message, - debug_pair(self.pair.clone()) - ); - let e = pest::error::ErrorVariant::CustomError { message }; - pest::error::Error::new_from_span(e, self.pair.as_span()) - } - fn parse(input_str: &'input str, rule: Rule) -> ParseResult<Self> { - let mut pairs = DhallParser::parse(rule, input_str)?; - // TODO: proper errors - let pair = pairs.next().unwrap(); - assert_eq!(pairs.next(), None); - Ok(ParseInput { - original_input_str: input_str.to_string().into(), - pair, - }) - } - fn with_pair(&self, new_pair: Pair<'input, Rule>) -> Self { - ParseInput { - pair: new_pair, - original_input_str: self.original_input_str.clone(), - } - } - /// If the contained pair has exactly one child, return a new Self containing it. - fn single_child(&self) -> Option<Self> { - let mut children = self.pair.clone().into_inner(); - if let Some(child) = children.next() { - if children.next().is_none() { - return Some(self.with_pair(child)); - } - } - None - } - fn as_span(&self) -> Span { - Span::make(self.original_input_str.clone(), self.pair.as_span()) - } - fn as_str(&self) -> &'input str { - self.pair.as_str() - } - fn as_rule(&self) -> Rule { - self.pair.as_rule() - } -} - -// Used by the macros. -trait PestConsumer { - type Rule: pest::RuleType; - fn rule_alias(rule: Self::Rule) -> String; - fn allows_shortcut(rule: Self::Rule) -> bool; -} - -fn debug_pair(pair: Pair<Rule>) -> String { - use std::fmt::Write; - let mut s = String::new(); - fn aux(s: &mut String, indent: usize, prefix: String, pair: Pair<Rule>) { - let indent_str = "| ".repeat(indent); - let rule = pair.as_rule(); - let contents = pair.as_str(); - let mut inner = pair.into_inner(); - let mut first = true; - while let Some(p) = inner.next() { - if first { - first = false; - let last = inner.peek().is_none(); - if last && p.as_str() == contents { - let prefix = format!("{}{:?} > ", prefix, rule); - aux(s, indent, prefix, p); - continue; - } else { - writeln!( - s, - r#"{}{}{:?}: "{}""#, - indent_str, prefix, rule, contents - ) - .unwrap(); - } - } - aux(s, indent + 1, "".into(), p); - } - if first { - writeln!( - s, - r#"{}{}{:?}: "{}""#, - indent_str, prefix, rule, contents - ) - .unwrap(); - } - } - aux(&mut s, 0, "".into(), pair); - s -} - #[derive(Debug)] enum Either<A, B> { Left(A), @@ -173,6 +67,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> { + Expr::new(x, input_to_span(input)) +} +fn spanned_union<E>(span1: Span, span2: Span, x: RawExpr<E>) -> Expr<E> { + 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>>) { @@ -242,27 +146,27 @@ lazy_static::lazy_static! { }; } -struct Parsers; +struct DhallParser; -#[make_parser(Rule)] -impl Parsers { - fn EOI(_input: ParseInput<Rule>) -> ParseResult<()> { +#[pest_consume::parser(parser = dgp::DhallParser, rule = dgp::Rule)] +impl DhallParser { + fn EOI(_input: ParseInput) -> ParseResult<()> { Ok(()) } #[alias(label)] - fn simple_label(input: ParseInput<Rule>) -> ParseResult<Label> { + fn simple_label(input: ParseInput) -> ParseResult<Label> { Ok(Label::from(input.as_str())) } #[alias(label)] - fn quoted_label(input: ParseInput<Rule>) -> ParseResult<Label> { + fn quoted_label(input: ParseInput) -> ParseResult<Label> { Ok(Label::from(input.as_str())) } fn double_quote_literal<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<ParsedText<E>> { - Ok(parse_children!(input; + Ok(match_nodes!(input.into_children(); [double_quote_chunk(chunks)..] => { chunks.collect() } @@ -270,9 +174,9 @@ impl Parsers { } fn double_quote_chunk<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<ParsedTextContents<E>> { - Ok(parse_children!(input; + Ok(match_nodes!(input.into_children(); [expression(e)] => { InterpolatedTextContents::Expr(e) }, @@ -282,7 +186,7 @@ impl Parsers { )) } #[alias(double_quote_char)] - fn double_quote_escaped(input: ParseInput<Rule>) -> ParseResult<String> { + fn double_quote_escaped(input: ParseInput) -> ParseResult<String> { Ok(match input.as_str() { "\"" => "\"".to_owned(), "$" => "$".to_owned(), @@ -352,16 +256,14 @@ impl Parsers { } }) } - fn double_quote_char<'a>( - input: ParseInput<'a, Rule>, - ) -> ParseResult<String> { + fn double_quote_char(input: ParseInput) -> ParseResult<String> { Ok(input.as_str().to_owned()) } fn single_quote_literal<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<ParsedText<E>> { - Ok(parse_children!(input; + Ok(match_nodes!(input.into_children(); [single_quote_continue(lines)] => { let newline: ParsedText<E> = "\n".to_string().into(); @@ -382,29 +284,23 @@ impl Parsers { } )) } - fn single_quote_char<'a>( - input: ParseInput<'a, Rule>, - ) -> ParseResult<&'a str> { + fn single_quote_char(input: ParseInput) -> ParseResult<&str> { Ok(input.as_str()) } #[alias(single_quote_char)] - fn escaped_quote_pair<'a>( - _input: ParseInput<'a, Rule>, - ) -> ParseResult<&'a str> { + fn escaped_quote_pair(_input: ParseInput) -> ParseResult<&str> { Ok("''") } #[alias(single_quote_char)] - fn escaped_interpolation<'a>( - _input: ParseInput<'a, Rule>, - ) -> ParseResult<&'a str> { + fn escaped_interpolation(_input: ParseInput) -> ParseResult<&str> { Ok("${") } // Returns a vec of lines in reversed order, where each line is also in reversed order. fn single_quote_continue<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<Vec<Vec<ParsedTextContents<E>>>> { - Ok(parse_children!(input; + Ok(match_nodes!(input.into_children(); [expression(e), single_quote_continue(lines)] => { let c = InterpolatedTextContents::Expr(e); let mut lines = lines; @@ -429,7 +325,7 @@ impl Parsers { } #[alias(expression)] - fn builtin<E: Clone>(input: ParseInput<Rule>) -> ParseResult<Expr<E>> { + fn builtin<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { let s = input.as_str(); let e = match crate::Builtin::parse(s) { Some(b) => Builtin(b), @@ -442,30 +338,24 @@ impl Parsers { _ => Err(input.error(format!("Unrecognized builtin: '{}'", s)))?, }, }; - Ok(spanned(input.as_span(), e)) + Ok(spanned(input, e)) } #[alias(double_literal)] - fn NaN(_input: ParseInput<Rule>) -> ParseResult<core::Double> { + fn NaN(_input: ParseInput) -> ParseResult<core::Double> { Ok(std::f64::NAN.into()) } #[alias(double_literal)] - fn minus_infinity_literal( - _input: ParseInput<Rule>, - ) -> ParseResult<core::Double> { + fn minus_infinity_literal(_input: ParseInput) -> ParseResult<core::Double> { Ok(std::f64::NEG_INFINITY.into()) } #[alias(double_literal)] - fn plus_infinity_literal( - _input: ParseInput<Rule>, - ) -> ParseResult<core::Double> { + fn plus_infinity_literal(_input: ParseInput) -> ParseResult<core::Double> { Ok(std::f64::INFINITY.into()) } #[alias(double_literal)] - fn numeric_double_literal( - input: ParseInput<Rule>, - ) -> ParseResult<core::Double> { + fn numeric_double_literal(input: ParseInput) -> ParseResult<core::Double> { let s = input.as_str().trim(); match s.parse::<f64>() { Ok(x) if x.is_infinite() => Err(input.error(format!( @@ -477,7 +367,7 @@ impl Parsers { } } - fn natural_literal(input: ParseInput<Rule>) -> ParseResult<core::Natural> { + fn natural_literal(input: ParseInput) -> ParseResult<core::Natural> { input .as_str() .trim() @@ -485,7 +375,7 @@ impl Parsers { .map_err(|e| input.error(format!("{}", e))) } - fn integer_literal(input: ParseInput<Rule>) -> ParseResult<core::Integer> { + fn integer_literal(input: ParseInput) -> ParseResult<core::Integer> { input .as_str() .trim() @@ -494,36 +384,26 @@ impl Parsers { } #[alias(expression, shortcut = true)] - fn identifier<E: Clone>(input: ParseInput<Rule>) -> ParseResult<Expr<E>> { - Ok(parse_children!(input; - [variable(v)] => { - spanned(input.as_span(), Var(v)) - }, + fn identifier<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { + Ok(match_nodes!(input.children(); + [variable(v)] => spanned(input, Var(v)), [expression(e)] => e, )) } - fn variable(input: ParseInput<Rule>) -> ParseResult<V<Label>> { - Ok(parse_children!(input; - [label(l), natural_literal(idx)] => { - V(l, idx) - }, - [label(l)] => { - V(l, 0) - }, + fn variable(input: ParseInput) -> ParseResult<V<Label>> { + Ok(match_nodes!(input.into_children(); + [label(l), natural_literal(idx)] => V(l, idx), + [label(l)] => V(l, 0), )) } #[alias(path_component)] - fn unquoted_path_component<'a>( - input: ParseInput<'a, Rule>, - ) -> ParseResult<String> { + fn unquoted_path_component(input: ParseInput) -> ParseResult<String> { Ok(input.as_str().to_string()) } #[alias(path_component)] - fn quoted_path_component<'a>( - input: ParseInput<'a, Rule>, - ) -> ParseResult<String> { + fn quoted_path_component(input: ParseInput) -> ParseResult<String> { #[rustfmt::skip] const RESERVED: &percent_encoding::AsciiSet = &percent_encoding::CONTROLS @@ -549,8 +429,8 @@ impl Parsers { }) .collect()) } - fn path(input: ParseInput<Rule>) -> ParseResult<FilePath> { - Ok(parse_children!(input; + fn path(input: ParseInput) -> ParseResult<FilePath> { + Ok(match_nodes!(input.into_children(); [path_component(components)..] => { FilePath { file_path: components.collect() } } @@ -559,47 +439,43 @@ impl Parsers { #[alias(import_type)] fn local<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<ImportLocation<Expr<E>>> { - Ok(parse_children!(input; + Ok(match_nodes!(input.into_children(); [local_path((prefix, p))] => ImportLocation::Local(prefix, p), )) } #[alias(local_path)] fn parent_path( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<(FilePrefix, FilePath)> { - Ok(parse_children!(input; + Ok(match_nodes!(input.into_children(); [path(p)] => (FilePrefix::Parent, p) )) } #[alias(local_path)] - fn here_path( - input: ParseInput<Rule>, - ) -> ParseResult<(FilePrefix, FilePath)> { - Ok(parse_children!(input; + fn here_path(input: ParseInput) -> ParseResult<(FilePrefix, FilePath)> { + Ok(match_nodes!(input.into_children(); [path(p)] => (FilePrefix::Here, p) )) } #[alias(local_path)] - fn home_path( - input: ParseInput<Rule>, - ) -> ParseResult<(FilePrefix, FilePath)> { - Ok(parse_children!(input; + fn home_path(input: ParseInput) -> ParseResult<(FilePrefix, FilePath)> { + Ok(match_nodes!(input.into_children(); [path(p)] => (FilePrefix::Home, p) )) } #[alias(local_path)] fn absolute_path( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<(FilePrefix, FilePath)> { - Ok(parse_children!(input; + Ok(match_nodes!(input.into_children(); [path(p)] => (FilePrefix::Absolute, p) )) } - fn scheme(input: ParseInput<Rule>) -> ParseResult<Scheme> { + fn scheme(input: ParseInput) -> ParseResult<Scheme> { Ok(match input.as_str() { "http" => Scheme::HTTP, "https" => Scheme::HTTPS, @@ -607,10 +483,8 @@ impl Parsers { }) } - fn http_raw<E: Clone>( - input: ParseInput<Rule>, - ) -> ParseResult<URL<Expr<E>>> { - Ok(parse_children!(input; + fn http_raw<E: Clone>(input: ParseInput) -> ParseResult<URL<Expr<E>>> { + Ok(match_nodes!(input.into_children(); [scheme(sch), authority(auth), path(p)] => URL { scheme: sch, authority: auth, @@ -628,19 +502,19 @@ impl Parsers { )) } - fn authority(input: ParseInput<Rule>) -> ParseResult<String> { + fn authority(input: ParseInput) -> ParseResult<String> { Ok(input.as_str().to_owned()) } - fn query(input: ParseInput<Rule>) -> ParseResult<String> { + fn query(input: ParseInput) -> ParseResult<String> { Ok(input.as_str().to_owned()) } #[alias(import_type)] fn http<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<ImportLocation<Expr<E>>> { - Ok(ImportLocation::Remote(parse_children!(input; + Ok(ImportLocation::Remote(match_nodes!(input.into_children(); [http_raw(url)] => url, [http_raw(url), expression(e)] => URL { headers: Some(e), ..url }, ))) @@ -648,53 +522,49 @@ impl Parsers { #[alias(import_type)] fn env<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<ImportLocation<Expr<E>>> { - Ok(parse_children!(input; + Ok(match_nodes!(input.into_children(); [environment_variable(v)] => ImportLocation::Env(v), )) } #[alias(environment_variable)] - fn bash_environment_variable( - input: ParseInput<Rule>, - ) -> ParseResult<String> { + fn bash_environment_variable(input: ParseInput) -> ParseResult<String> { Ok(input.as_str().to_owned()) } #[alias(environment_variable)] - fn posix_environment_variable( - input: ParseInput<Rule>, - ) -> ParseResult<String> { - Ok(parse_children!(input; + fn posix_environment_variable(input: ParseInput) -> ParseResult<String> { + Ok(match_nodes!(input.into_children(); [posix_environment_variable_character(chars)..] => { chars.collect() }, )) } - fn posix_environment_variable_character<'a>( - input: ParseInput<'a, Rule>, - ) -> ParseResult<Cow<'a, str>> { + fn posix_environment_variable_character( + input: ParseInput, + ) -> ParseResult<&str> { Ok(match input.as_str() { - "\\\"" => Cow::Owned("\"".to_owned()), - "\\\\" => Cow::Owned("\\".to_owned()), - "\\a" => Cow::Owned("\u{0007}".to_owned()), - "\\b" => Cow::Owned("\u{0008}".to_owned()), - "\\f" => Cow::Owned("\u{000C}".to_owned()), - "\\n" => Cow::Owned("\n".to_owned()), - "\\r" => Cow::Owned("\r".to_owned()), - "\\t" => Cow::Owned("\t".to_owned()), - "\\v" => Cow::Owned("\u{000B}".to_owned()), - s => Cow::Borrowed(s), + "\\\"" => "\"", + "\\\\" => "\\", + "\\a" => "\u{0007}", + "\\b" => "\u{0008}", + "\\f" => "\u{000C}", + "\\n" => "\n", + "\\r" => "\r", + "\\t" => "\t", + "\\v" => "\u{000B}", + s => s, }) } #[alias(import_type)] fn missing<E: Clone>( - _input: ParseInput<Rule>, + _input: ParseInput, ) -> ParseResult<ImportLocation<Expr<E>>> { Ok(ImportLocation::Missing) } - fn hash(input: ParseInput<Rule>) -> ParseResult<Hash> { + fn hash(input: ParseInput) -> ParseResult<Hash> { let s = input.as_str().trim(); let protocol = &s[..6]; let hash = &s[7..]; @@ -705,29 +575,29 @@ impl Parsers { } fn import_hashed<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<crate::Import<Expr<E>>> { use crate::Import; let mode = ImportMode::Code; - Ok(parse_children!(input; + Ok(match_nodes!(input.into_children(); [import_type(location)] => Import { mode, location, hash: None }, [import_type(location), hash(h)] => Import { mode, location, hash: Some(h) }, )) } #[alias(import_mode)] - fn Text(_input: ParseInput<Rule>) -> ParseResult<ImportMode> { + fn Text(_input: ParseInput) -> ParseResult<ImportMode> { Ok(ImportMode::RawText) } #[alias(import_mode)] - fn Location(_input: ParseInput<Rule>) -> ParseResult<ImportMode> { + fn Location(_input: ParseInput) -> ParseResult<ImportMode> { Ok(ImportMode::Location) } #[alias(expression)] - fn import<E: Clone>(input: ParseInput<Rule>) -> ParseResult<Expr<E>> { + fn import<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { use crate::Import; - let import = parse_children!(input; + let import = match_nodes!(input.children(); [import_hashed(imp)] => { Import { mode: ImportMode::Code, ..imp } }, @@ -735,97 +605,99 @@ impl Parsers { Import { mode, ..imp } }, ); - Ok(spanned(input.as_span(), Import(import))) + Ok(spanned(input, Import(import))) } - fn lambda(_input: ParseInput<Rule>) -> ParseResult<()> { + fn lambda(_input: ParseInput) -> ParseResult<()> { Ok(()) } - fn forall(_input: ParseInput<Rule>) -> ParseResult<()> { + fn forall(_input: ParseInput) -> ParseResult<()> { Ok(()) } - fn arrow(_input: ParseInput<Rule>) -> ParseResult<()> { + fn arrow(_input: ParseInput) -> ParseResult<()> { Ok(()) } - fn merge(_input: ParseInput<Rule>) -> ParseResult<()> { + fn merge(_input: ParseInput) -> ParseResult<()> { Ok(()) } - fn assert(_input: ParseInput<Rule>) -> ParseResult<()> { + fn assert(_input: ParseInput) -> ParseResult<()> { Ok(()) } - fn if_(_input: ParseInput<Rule>) -> ParseResult<()> { + fn if_(_input: ParseInput) -> ParseResult<()> { Ok(()) } - fn toMap(_input: ParseInput<Rule>) -> ParseResult<()> { + fn toMap(_input: ParseInput) -> ParseResult<()> { Ok(()) } #[alias(expression)] - fn empty_list_literal<E: Clone>( - input: ParseInput<Rule>, - ) -> ParseResult<Expr<E>> { - Ok(parse_children!(input; - [expression(e)] => spanned(input.as_span(), EmptyListLit(e)), + fn empty_list_literal<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { + Ok(match_nodes!(input.children(); + [expression(e)] => spanned(input, EmptyListLit(e)), )) } - fn expression<E: Clone>(input: ParseInput<Rule>) -> ParseResult<Expr<E>> { - let span = input.as_span(); - Ok(parse_children!(input; + fn expression<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { + Ok(match_nodes!(input.children(); [lambda(()), label(l), expression(typ), arrow(()), expression(body)] => { - spanned(span, Lam(l, typ, body)) + spanned(input, Lam(l, typ, body)) }, [if_(()), expression(cond), expression(left), expression(right)] => { - spanned(span, BoolIf(cond, left, right)) + spanned(input, BoolIf(cond, left, right)) }, [let_binding(bindings).., expression(final_expr)] => { bindings.rev().fold( final_expr, - |acc, x| unspanned(Let(x.0, x.1, x.2, acc)) + |acc, x| { + spanned_union( + acc.span().unwrap(), + x.3, + Let(x.0, x.1, x.2, acc) + ) + } ) }, [forall(()), label(l), expression(typ), arrow(()), expression(body)] => { - spanned(span, Pi(l, typ, body)) + spanned(input, Pi(l, typ, body)) }, [expression(typ), arrow(()), expression(body)] => { - spanned(span, Pi("_".into(), typ, body)) + spanned(input, Pi("_".into(), typ, body)) }, [merge(()), expression(x), expression(y), expression(z)] => { - spanned(span, Merge(x, y, Some(z))) + spanned(input, Merge(x, y, Some(z))) }, [assert(()), expression(x)] => { - spanned(span, Assert(x)) + spanned(input, Assert(x)) }, [toMap(()), expression(x), expression(y)] => { - spanned(span, ToMap(x, Some(y))) + spanned(input, ToMap(x, Some(y))) }, [expression(e), expression(annot)] => { - spanned(span, Annot(e, annot)) + spanned(input, Annot(e, annot)) }, [expression(e)] => e, )) } fn let_binding<E: Clone>( - input: ParseInput<Rule>, - ) -> ParseResult<(Label, Option<Expr<E>>, Expr<E>)> { - Ok(parse_children!(input; + input: ParseInput, + ) -> ParseResult<(Label, Option<Expr<E>>, Expr<E>, Span)> { + Ok(match_nodes!(input.children(); [label(name), expression(annot), expression(expr)] => - (name, Some(annot), expr), + (name, Some(annot), expr, input_to_span(input)), [label(name), expression(expr)] => - (name, None, expr), + (name, None, expr, input_to_span(input)), )) } #[alias(expression, shortcut = true)] #[prec_climb(expression, PRECCLIMBER)] fn operator_expression<E: Clone>( - input: ParseInput<Rule>, l: Expr<E>, - op: Pair<Rule>, + op: ParseInput, r: Expr<E>, ) -> ParseResult<Expr<E>> { use crate::BinOp::*; @@ -844,42 +716,54 @@ impl Parsers { bool_eq => BoolEQ, bool_ne => BoolNE, equivalent => Equivalence, - r => Err(input.error(format!("Rule {:?} isn't an operator", r)))?, + r => Err(op.error(format!("Rule {:?} isn't an operator", r)))?, }; - Ok(unspanned(BinOp(op, l, r))) + Ok(spanned_union( + l.span().unwrap(), + r.span().unwrap(), + BinOp(op, l, r), + )) } - fn Some_(_input: ParseInput<Rule>) -> ParseResult<()> { + fn Some_(_input: ParseInput) -> ParseResult<()> { Ok(()) } #[alias(expression, shortcut = true)] fn application_expression<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<Expr<E>> { - Ok(parse_children!(input; + Ok(match_nodes!(input.children(); [expression(e)] => e, [expression(first), expression(rest)..] => { - rest.fold(first, |acc, e| unspanned(App(acc, e))) + rest.fold( + first, + |acc, e| { + spanned_union( + acc.span().unwrap(), + e.span().unwrap(), + App(acc, e) + ) + } + ) }, )) } #[alias(expression, shortcut = true)] fn first_application_expression<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<Expr<E>> { - let span = input.as_span(); - Ok(parse_children!(input; + Ok(match_nodes!(input.children(); [Some_(()), expression(e)] => { - spanned(span, SomeLit(e)) + spanned(input, SomeLit(e)) }, [merge(()), expression(x), expression(y)] => { - spanned(span, Merge(x, y, None)) + spanned(input, Merge(x, y, None)) }, [toMap(()), expression(x)] => { - spanned(span, ToMap(x, None)) + spanned(input, ToMap(x, None)) }, [expression(e)] => e, )) @@ -887,69 +771,75 @@ impl Parsers { #[alias(expression, shortcut = true)] fn selector_expression<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<Expr<E>> { - Ok(parse_children!(input; + Ok(match_nodes!(input.children(); [expression(e)] => e, [expression(first), selector(rest)..] => { - rest.fold(first, |acc, e| unspanned(match e { - Either::Left(l) => Field(acc, l), - Either::Right(ls) => Projection(acc, ls), - })) + rest.fold( + first, + |acc, e| { + spanned_union( + acc.span().unwrap(), + e.1, + match e.0 { + Either::Left(l) => Field(acc, l), + Either::Right(ls) => Projection(acc, ls), + } + ) + } + ) }, )) } fn selector( - input: ParseInput<Rule>, - ) -> ParseResult<Either<Label, DupTreeSet<Label>>> { - Ok(parse_children!(input; - [label(l)] => Either::Left(l), - [labels(ls)] => Either::Right(ls), + input: ParseInput, + ) -> ParseResult<(Either<Label, DupTreeSet<Label>>, Span)> { + Ok(match_nodes!(input.children(); + [label(l)] => (Either::Left(l), input_to_span(input)), + [labels(ls)] => (Either::Right(ls), input_to_span(input)), // [expression(_e)] => unimplemented!("selection by expression"), // TODO )) } - fn labels(input: ParseInput<Rule>) -> ParseResult<DupTreeSet<Label>> { - Ok(parse_children!(input; + fn labels(input: ParseInput) -> ParseResult<DupTreeSet<Label>> { + Ok(match_nodes!(input.into_children(); [label(ls)..] => ls.collect(), )) } #[alias(expression, shortcut = true)] fn primitive_expression<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<Expr<E>> { - let span = input.as_span(); - Ok(parse_children!(input; - [double_literal(n)] => spanned(span, DoubleLit(n)), - [natural_literal(n)] => spanned(span, NaturalLit(n)), - [integer_literal(n)] => spanned(span, IntegerLit(n)), - [double_quote_literal(s)] => spanned(span, TextLit(s)), - [single_quote_literal(s)] => spanned(span, TextLit(s)), + Ok(match_nodes!(input.children(); + [double_literal(n)] => spanned(input, DoubleLit(n)), + [natural_literal(n)] => spanned(input, NaturalLit(n)), + [integer_literal(n)] => spanned(input, IntegerLit(n)), + [double_quote_literal(s)] => spanned(input, TextLit(s)), + [single_quote_literal(s)] => spanned(input, TextLit(s)), [expression(e)] => e, )) } #[alias(expression)] fn empty_record_literal<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<Expr<E>> { - Ok(spanned(input.as_span(), RecordLit(Default::default()))) + Ok(spanned(input, RecordLit(Default::default()))) } #[alias(expression)] - fn empty_record_type<E: Clone>( - input: ParseInput<Rule>, - ) -> ParseResult<Expr<E>> { - Ok(spanned(input.as_span(), RecordType(Default::default()))) + fn empty_record_type<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { + Ok(spanned(input, RecordType(Default::default()))) } #[alias(expression)] fn non_empty_record_type_or_literal<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<Expr<E>> { - let e = parse_children!(input; + let e = match_nodes!(input.children(); [label(first_label), non_empty_record_type(rest)] => { let (first_expr, mut map) = rest; map.insert(first_label, first_expr); @@ -961,13 +851,13 @@ impl Parsers { RecordLit(map) }, ); - Ok(spanned(input.as_span(), e)) + Ok(spanned(input, e)) } fn non_empty_record_type<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<(Expr<E>, DupTreeMap<Label, Expr<E>>)> { - Ok(parse_children!(input; + Ok(match_nodes!(input.into_children(); [expression(expr), record_type_entry(entries)..] => { (expr, entries.collect()) } @@ -975,17 +865,17 @@ impl Parsers { } fn record_type_entry<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<(Label, Expr<E>)> { - Ok(parse_children!(input; + Ok(match_nodes!(input.into_children(); [label(name), expression(expr)] => (name, expr) )) } fn non_empty_record_literal<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<(Expr<E>, DupTreeMap<Label, Expr<E>>)> { - Ok(parse_children!(input; + Ok(match_nodes!(input.into_children(); [expression(expr), record_literal_entry(entries)..] => { (expr, entries.collect()) } @@ -993,30 +883,30 @@ impl Parsers { } fn record_literal_entry<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<(Label, Expr<E>)> { - Ok(parse_children!(input; + Ok(match_nodes!(input.into_children(); [label(name), expression(expr)] => (name, expr) )) } #[alias(expression)] - fn union_type<E: Clone>(input: ParseInput<Rule>) -> ParseResult<Expr<E>> { - let map = parse_children!(input; + fn union_type<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { + let map = match_nodes!(input.children(); [empty_union_type(_)] => Default::default(), [union_type_entry(entries)..] => entries.collect(), ); - Ok(spanned(input.as_span(), UnionType(map))) + Ok(spanned(input, UnionType(map))) } - fn empty_union_type(_input: ParseInput<Rule>) -> ParseResult<()> { + fn empty_union_type(_input: ParseInput) -> ParseResult<()> { Ok(()) } fn union_type_entry<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<(Label, Option<Expr<E>>)> { - Ok(parse_children!(input; + Ok(match_nodes!(input.children(); [label(name), expression(expr)] => (name, Some(expr)), [label(name)] => (name, None), )) @@ -1024,26 +914,32 @@ impl Parsers { #[alias(expression)] fn non_empty_list_literal<E: Clone>( - input: ParseInput<Rule>, + input: ParseInput, ) -> ParseResult<Expr<E>> { - Ok(parse_children!(input; + Ok(match_nodes!(input.children(); [expression(items)..] => spanned( - input.as_span(), + input, NEListLit(items.collect()) ) )) } - fn final_expression<E: Clone>( - input: ParseInput<Rule>, - ) -> ParseResult<Expr<E>> { - Ok(parse_children!(input; + #[alias(expression)] + fn final_expression<E: Clone>(input: ParseInput) -> ParseResult<Expr<E>> { + Ok(match_nodes!(input.into_children(); [expression(e), EOI(_)] => e )) } } -pub fn parse_expr<E: Clone>(s: &str) -> ParseResult<Expr<E>> { - let input = ParseInput::parse(s, Rule::final_expression)?; - Parsers::final_expression(input) +pub fn parse_expr<E: Clone>(input_str: &str) -> ParseResult<Expr<E>> { + let rc_input_str = input_str.to_string().into(); + let inputs = DhallParser::parse_with_userdata( + Rule::final_expression, + input_str, + rc_input_str, + )?; + Ok(match_nodes!(<DhallParser>; inputs; + [expression(e)] => e, + )) } |