From a7363042a16364a6dafdd545f4069dcf04a4197e Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 27 Aug 2019 23:18:13 +0200 Subject: Rename SubExpr to Expr, and Expr to RawExpr For clarity, and consistency with Value --- dhall_syntax/src/core/expr.rs | 64 ++++++++++++++++++++-------------------- dhall_syntax/src/core/visitor.rs | 9 +++--- dhall_syntax/src/parser.rs | 64 ++++++++++++++++++++-------------------- dhall_syntax/src/printer.rs | 6 ++-- 4 files changed, 71 insertions(+), 72 deletions(-) (limited to 'dhall_syntax/src') diff --git a/dhall_syntax/src/core/expr.rs b/dhall_syntax/src/core/expr.rs index 32166a6..2d73a64 100644 --- a/dhall_syntax/src/core/expr.rs +++ b/dhall_syntax/src/core/expr.rs @@ -166,19 +166,21 @@ pub enum Builtin { TextShow, } -// Each node carries an annotation. In practice it's either X (no annotation) or a Span. +// Each node carries an annotation. #[derive(Debug, Clone)] -pub struct SubExpr(Box<(Expr, Option)>); +pub struct Expr(Box<(RawExpr, Option)>); -impl std::cmp::PartialEq for SubExpr { +pub type RawExpr = ExprF, Embed>; + +impl std::cmp::PartialEq for Expr { fn eq(&self, other: &Self) -> bool { self.0.as_ref().0 == other.0.as_ref().0 } } -impl std::cmp::Eq for SubExpr {} +impl std::cmp::Eq for Expr {} -impl std::hash::Hash for SubExpr { +impl std::hash::Hash for Expr { fn hash(&self, state: &mut H) where H: std::hash::Hasher, @@ -187,8 +189,6 @@ impl std::hash::Hash for SubExpr { } } -pub type Expr = ExprF, Embed>; - /// Syntax tree for expressions // Having the recursion out of the enum definition enables writing // much more generic code and improves pattern-matching behind @@ -310,11 +310,11 @@ impl ExprF { } } -impl Expr { +impl RawExpr { pub fn traverse_resolve( &self, - visit_import: impl FnMut(&Import>) -> Result, - ) -> Result, Err> { + visit_import: impl FnMut(&Import>) -> Result, + ) -> Result, Err> { self.traverse_resolve_with_visitor(&mut visitor::ResolveVisitor( visit_import, )) @@ -323,9 +323,9 @@ impl Expr { pub(crate) fn traverse_resolve_with_visitor( &self, visitor: &mut visitor::ResolveVisitor, - ) -> Result, Err> + ) -> Result, Err> where - F1: FnMut(&Import>) -> Result, + F1: FnMut(&Import>) -> Result, { match self { ExprF::BinOp(BinOp::ImportAlt, l, r) => l @@ -343,52 +343,52 @@ impl Expr { } } -impl SubExpr { - pub fn as_ref(&self) -> &Expr { +impl Expr { + pub fn as_ref(&self) -> &RawExpr { &self.0.as_ref().0 } - pub fn new(x: Expr, n: Span) -> Self { - SubExpr(Box::new((x, Some(n)))) + pub fn new(x: RawExpr, n: Span) -> Self { + Expr(Box::new((x, Some(n)))) } - pub fn from_expr_no_span(x: Expr) -> Self { - SubExpr(Box::new((x, None))) + pub fn from_expr_no_span(x: RawExpr) -> Self { + Expr(Box::new((x, None))) } pub fn from_builtin(b: Builtin) -> Self { - SubExpr::from_expr_no_span(ExprF::Builtin(b)) + Expr::from_expr_no_span(ExprF::Builtin(b)) } - pub fn rewrap(&self, x: Expr) -> SubExpr { - SubExpr(Box::new((x, (self.0).1.clone()))) + pub fn rewrap(&self, x: RawExpr) -> Expr { + Expr(Box::new((x, (self.0).1.clone()))) } } -impl SubExpr { +impl Expr { pub fn traverse_resolve( &self, - visit_import: impl FnMut(&Import>) -> Result, - ) -> Result, Err> { + visit_import: impl FnMut(&Import>) -> Result, + ) -> Result, Err> { Ok(self.rewrap(self.as_ref().traverse_resolve(visit_import)?)) } } // Should probably rename this -pub fn rc(x: Expr) -> SubExpr { - SubExpr::from_expr_no_span(x) +pub fn rc(x: RawExpr) -> Expr { + Expr::from_expr_no_span(x) } pub(crate) fn spanned( span: Span, - x: crate::parser::ParsedExpr, -) -> crate::parser::ParsedSubExpr { - SubExpr::new(x, span) + x: crate::parser::ParsedRawExpr, +) -> crate::parser::ParsedExpr { + Expr::new(x, span) } pub(crate) fn unspanned( - x: crate::parser::ParsedExpr, -) -> crate::parser::ParsedSubExpr { - SubExpr::from_expr_no_span(x) + x: crate::parser::ParsedRawExpr, +) -> crate::parser::ParsedExpr { + Expr::from_expr_no_span(x) } /// Add an isize to an usize diff --git a/dhall_syntax/src/core/visitor.rs b/dhall_syntax/src/core/visitor.rs index 5a5fcc9..49fff60 100644 --- a/dhall_syntax/src/core/visitor.rs +++ b/dhall_syntax/src/core/visitor.rs @@ -247,18 +247,17 @@ where pub struct ResolveVisitor(pub F1); -impl<'a, 'b, E, E2, Err, F1> - ExprFFallibleVisitor<'a, SubExpr, SubExpr, E, E2> +impl<'a, 'b, E, E2, Err, F1> ExprFFallibleVisitor<'a, Expr, Expr, E, E2> for &'b mut ResolveVisitor where - F1: FnMut(&Import>) -> Result, + F1: FnMut(&Import>) -> Result, { type Error = Err; fn visit_subexpr( &mut self, - subexpr: &'a SubExpr, - ) -> Result, Self::Error> { + subexpr: &'a Expr, + ) -> Result, Self::Error> { Ok(subexpr.rewrap( subexpr .as_ref() diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index d631a19..defa79b 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -18,10 +18,10 @@ 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. +pub(crate) type ParsedRawExpr = RawExpr; pub(crate) type ParsedExpr = Expr; -pub(crate) type ParsedSubExpr = SubExpr; -type ParsedText = InterpolatedText; -type ParsedTextContents = InterpolatedTextContents; +type ParsedText = InterpolatedText; +type ParsedTextContents = InterpolatedTextContents; pub type ParseError = pest::error::Error; @@ -551,7 +551,7 @@ make_parser! { rule!(escaped_interpolation<&'a str>; captured_str!(_) => "${" ); - rule!(interpolation; children!( + rule!(interpolation; children!( [expression(e)] => e )); @@ -593,7 +593,7 @@ make_parser! { }, )); - rule!(builtin; span; + rule!(builtin; span; captured_str!(s) => { spanned(span, match crate::Builtin::parse(s) { Some(b) => Builtin(b), @@ -650,7 +650,7 @@ make_parser! { } ); - rule!(identifier; span; children!( + rule!(identifier; span; children!( [variable(v)] => { spanned(span, Var(v)) }, @@ -708,7 +708,7 @@ make_parser! { _ => unreachable!(), }); - rule!(http_raw>; children!( + rule!(http_raw>; children!( [scheme(sch), authority(auth), path(p)] => URL { scheme: sch, authority: auth, @@ -729,7 +729,7 @@ make_parser! { rule!(query; captured_str!(s) => s.to_owned()); - rule!(http>; children!( + rule!(http>; children!( [http_raw(url)] => url, [http_raw(url), import_expression(e)] => URL { headers: Some(e), ..url }, @@ -764,7 +764,7 @@ make_parser! { rule!(missing<()>); - rule!(import_type>; children!( + rule!(import_type>; children!( [missing(_)] => { ImportLocation::Missing }, @@ -789,7 +789,7 @@ make_parser! { Hash::SHA256(hex::decode(hash).unwrap()) }); - rule!(import_hashed>; children!( + rule!(import_hashed>; children!( [import_type(location)] => crate::Import {mode: ImportMode::Code, location, hash: None }, [import_type(location), hash(h)] => @@ -799,7 +799,7 @@ make_parser! { rule!(Text<()>); rule!(Location<()>); - rule!(import; span; children!( + rule!(import; span; children!( [import_hashed(imp)] => { spanned(span, Import(crate::Import { mode: ImportMode::Code, @@ -828,13 +828,13 @@ make_parser! { rule!(if_<()>); rule!(in_<()>); - rule!(empty_list_literal; span; children!( + rule!(empty_list_literal; span; children!( [application_expression(e)] => { spanned(span, EmptyListLit(e)) }, )); - rule!(expression; span; children!( + rule!(expression; span; children!( [lambda(()), label(l), expression(typ), arrow(()), expression(body)] => { spanned(span, Lam(l, typ, body)) @@ -869,7 +869,7 @@ make_parser! { }, )); - rule!(let_binding<(Label, Option, ParsedSubExpr)>; + rule!(let_binding<(Label, Option, ParsedExpr)>; children!( [label(name), expression(annot), expression(expr)] => (name, Some(annot), expr), @@ -880,7 +880,7 @@ make_parser! { rule!(List<()>); rule!(Optional<()>); - rule!(operator_expression; prec_climb!( + rule!(operator_expression; prec_climb!( application_expression, { use Rule::*; @@ -936,14 +936,14 @@ make_parser! { rule!(Some_<()>); rule!(toMap<()>); - rule!(application_expression; children!( + rule!(application_expression; children!( [first_application_expression(e)] => e, [first_application_expression(first), import_expression(rest)..] => { rest.fold(first, |acc, e| unspanned(App(acc, e))) }, )); - rule!(first_application_expression; span; + rule!(first_application_expression; span; children!( [Some_(()), import_expression(e)] => { spanned(span, SomeLit(e)) @@ -954,13 +954,13 @@ make_parser! { [import_expression(e)] => e, )); - rule!(import_expression; span; + rule!(import_expression; span; children!( [selector_expression(e)] => e, [import(e)] => e, )); - rule!(selector_expression; children!( + rule!(selector_expression; children!( [primitive_expression(e)] => e, [primitive_expression(first), selector(rest)..] => { rest.fold(first, |acc, e| unspanned(match e { @@ -980,7 +980,7 @@ make_parser! { [label(ls)..] => ls.collect(), )); - rule!(primitive_expression; span; children!( + rule!(primitive_expression; span; children!( [double_literal(n)] => spanned(span, DoubleLit(n)), [natural_literal(n)] => spanned(span, NaturalLit(n)), [integer_literal(n)] => spanned(span, IntegerLit(n)), @@ -995,15 +995,15 @@ make_parser! { [expression(e)] => e, )); - rule!(empty_record_literal; span; + rule!(empty_record_literal; span; captured_str!(_) => spanned(span, RecordLit(Default::default())) ); - rule!(empty_record_type; span; + rule!(empty_record_type; span; captured_str!(_) => spanned(span, RecordType(Default::default())) ); - rule!(non_empty_record_type_or_literal; span; + rule!(non_empty_record_type_or_literal; span; children!( [label(first_label), non_empty_record_type(rest)] => { let (first_expr, mut map) = rest; @@ -1018,28 +1018,28 @@ make_parser! { )); rule!(non_empty_record_type - <(ParsedSubExpr, DupTreeMap)>; children!( + <(ParsedExpr, DupTreeMap)>; children!( [expression(expr), record_type_entry(entries)..] => { (expr, entries.collect()) } )); - rule!(record_type_entry<(Label, ParsedSubExpr)>; children!( + rule!(record_type_entry<(Label, ParsedExpr)>; children!( [label(name), expression(expr)] => (name, expr) )); rule!(non_empty_record_literal - <(ParsedSubExpr, DupTreeMap)>; children!( + <(ParsedExpr, DupTreeMap)>; children!( [expression(expr), record_literal_entry(entries)..] => { (expr, entries.collect()) } )); - rule!(record_literal_entry<(Label, ParsedSubExpr)>; children!( + rule!(record_literal_entry<(Label, ParsedExpr)>; children!( [label(name), expression(expr)] => (name, expr) )); - rule!(union_type; span; children!( + rule!(union_type; span; children!( [empty_union_type(_)] => { spanned(span, UnionType(Default::default())) }, @@ -1050,12 +1050,12 @@ make_parser! { rule!(empty_union_type<()>); - rule!(union_type_entry<(Label, Option)>; children!( + rule!(union_type_entry<(Label, Option)>; children!( [label(name), expression(expr)] => (name, Some(expr)), [label(name)] => (name, None), )); - rule!(non_empty_list_literal; span; + rule!(non_empty_list_literal; span; children!( [expression(items)..] => spanned( span, @@ -1063,12 +1063,12 @@ make_parser! { ) )); - rule!(final_expression; children!( + rule!(final_expression; children!( [expression(e), EOI(_)] => e )); } -pub fn parse_expr(s: &str) -> ParseResult { +pub fn parse_expr(s: &str) -> ParseResult { let mut pairs = DhallParser::parse(Rule::final_expression, s)?; let rc_input = s.to_string().into(); let expr = EntryPoint::final_expression(rc_input, pairs.next().unwrap())?; diff --git a/dhall_syntax/src/printer.rs b/dhall_syntax/src/printer.rs index 256ea65..276590e 100644 --- a/dhall_syntax/src/printer.rs +++ b/dhall_syntax/src/printer.rs @@ -112,7 +112,7 @@ enum PrintPhase { // Wraps an Expr with a phase, so that phase selsction can be done // separate from the actual printing #[derive(Clone)] -struct PhasedExpr<'a, A>(&'a SubExpr, PrintPhase); +struct PhasedExpr<'a, A>(&'a Expr, PrintPhase); impl<'a, A: Display + Clone> Display for PhasedExpr<'a, A> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { @@ -126,7 +126,7 @@ impl<'a, A: Display + Clone> PhasedExpr<'a, A> { } } -impl Expr { +impl RawExpr { fn fmt_phase( &self, f: &mut fmt::Formatter, @@ -202,7 +202,7 @@ impl Expr { } } -impl Display for SubExpr { +impl Display for Expr { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { self.as_ref().fmt_phase(f, PrintPhase::Base) } -- cgit v1.2.3