diff options
author | Nadrieril Feneanar | 2020-02-19 17:25:57 +0000 |
---|---|---|
committer | GitHub | 2020-02-19 17:25:57 +0000 |
commit | ffbde252a850c7d96e1000e1be52792c41733a28 (patch) | |
tree | e668b7f764fb4981a802bc619e0b2ff62fa9ce16 /dhall/src/syntax/text | |
parent | e4b3a879907b6dcc75d25847ae21a23d0201aae1 (diff) | |
parent | 7cbfc1a0d32766a383d1f48902502adaa2234d2f (diff) |
Merge pull request #131 from Nadrieril/hir
Decouple main expression types
Diffstat (limited to 'dhall/src/syntax/text')
-rw-r--r-- | dhall/src/syntax/text/parser.rs | 21 | ||||
-rw-r--r-- | dhall/src/syntax/text/printer.rs | 47 |
2 files changed, 37 insertions, 31 deletions
diff --git a/dhall/src/syntax/text/parser.rs b/dhall/src/syntax/text/parser.rs index 8d571c0..f3ebd2b 100644 --- a/dhall/src/syntax/text/parser.rs +++ b/dhall/src/syntax/text/parser.rs @@ -5,23 +5,20 @@ use std::rc::Rc; use pest_consume::{match_nodes, Parser}; -use crate::syntax; use crate::syntax::map::{DupTreeMap, DupTreeSet}; use crate::syntax::ExprKind::*; +use crate::syntax::LitKind::*; use crate::syntax::{ - Double, FilePath, FilePrefix, Hash, ImportLocation, ImportMode, Integer, - InterpolatedText, InterpolatedTextContents, Label, NaiveDouble, Natural, - Scheme, Span, URL, V, + Double, Expr, FilePath, FilePrefix, Hash, ImportLocation, ImportMode, + Integer, InterpolatedText, InterpolatedTextContents, Label, NaiveDouble, + Natural, Scheme, Span, UnspannedExpr, URL, V, }; -use crate::Normalized; // 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 Expr = syntax::Expr<Normalized>; -type UnspannedExpr = syntax::UnspannedExpr<Normalized>; type ParsedText = InterpolatedText<Expr>; type ParsedTextContents = InterpolatedTextContents<Expr>; type ParseInput<'input> = pest_consume::Node<'input, Rule, Rc<str>>; @@ -348,8 +345,8 @@ impl DhallParser { let e = match crate::syntax::Builtin::parse(s) { Some(b) => Builtin(b), None => match s { - "True" => BoolLit(true), - "False" => BoolLit(false), + "True" => Lit(Bool(true)), + "False" => Lit(Bool(false)), "Type" => Const(crate::syntax::Const::Type), "Kind" => Const(crate::syntax::Const::Kind), "Sort" => Const(crate::syntax::Const::Sort), @@ -837,9 +834,9 @@ impl DhallParser { #[alias(expression, shortcut = true)] 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)), - [integer_literal(n)] => spanned(input, IntegerLit(n)), + [double_literal(n)] => spanned(input, Lit(Double(n))), + [natural_literal(n)] => spanned(input, Lit(Natural(n))), + [integer_literal(n)] => spanned(input, Lit(Integer(n))), [double_quote_literal(s)] => spanned(input, TextLit(s)), [single_quote_literal(s)] => spanned(input, TextLit(s)), [record_type_or_literal(e)] => spanned(input, e), diff --git a/dhall/src/syntax/text/printer.rs b/dhall/src/syntax/text/printer.rs index 06dd70f..8891d41 100644 --- a/dhall/src/syntax/text/printer.rs +++ b/dhall/src/syntax/text/printer.rs @@ -19,17 +19,17 @@ enum PrintPhase { // Wraps an Expr with a phase, so that phase selection can be done separate from the actual // printing. #[derive(Clone)] -struct PhasedExpr<'a, E>(&'a Expr<E>, PrintPhase); +struct PhasedExpr<'a>(&'a Expr, PrintPhase); -impl<'a, E: Display + Clone> PhasedExpr<'a, E> { - fn phase(self, phase: PrintPhase) -> PhasedExpr<'a, E> { +impl<'a> PhasedExpr<'a> { + fn phase(self, phase: PrintPhase) -> PhasedExpr<'a> { PhasedExpr(self.0, phase) } } -impl<E: Display + Clone> UnspannedExpr<E> { +impl UnspannedExpr { // Annotate subexpressions with the appropriate phase, defaulting to Base - fn annotate_with_phases<'a>(&'a self) -> ExprKind<PhasedExpr<'a, E>, E> { + fn annotate_with_phases<'a>(&'a self) -> ExprKind<PhasedExpr<'a>> { use crate::syntax::ExprKind::*; use PrintPhase::*; let with_base = self.map_ref(|e| PhasedExpr(e, Base)); @@ -134,7 +134,7 @@ where } /// Generic instance that delegates to subexpressions -impl<SE: Display + Clone, E: Display> Display for ExprKind<SE, E> { +impl<SE: Display + Clone> Display for ExprKind<SE> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { use crate::syntax::ExprKind::*; match self { @@ -196,15 +196,7 @@ impl<SE: Display + Clone, E: Display> Display for ExprKind<SE, E> { Var(a) => a.fmt(f)?, Const(k) => k.fmt(f)?, Builtin(v) => v.fmt(f)?, - BoolLit(true) => f.write_str("True")?, - BoolLit(false) => f.write_str("False")?, - NaturalLit(a) => a.fmt(f)?, - IntegerLit(a) if *a >= 0 => { - f.write_str("+")?; - a.fmt(f)?; - } - IntegerLit(a) => a.fmt(f)?, - DoubleLit(a) => a.fmt(f)?, + Lit(a) => a.fmt(f)?, TextLit(a) => a.fmt(f)?, RecordType(a) if a.is_empty() => f.write_str("{}")?, RecordType(a) => fmt_list("{ ", ", ", " }", a, f, |(k, t), f| { @@ -232,19 +224,36 @@ impl<SE: Display + Clone, E: Display> Display for ExprKind<SE, E> { write!(f, "{}::{}", a, b)?; } Import(a) => a.fmt(f)?, - Embed(a) => a.fmt(f)?, } Ok(()) } } -impl<E: Display + Clone> Display for Expr<E> { +impl Display for Expr { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + self.kind().fmt_phase(f, PrintPhase::Base) + } +} + +impl Display for LitKind { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - self.as_ref().fmt_phase(f, PrintPhase::Base) + use LitKind::*; + match self { + Bool(true) => f.write_str("True")?, + Bool(false) => f.write_str("False")?, + Natural(a) => a.fmt(f)?, + Integer(a) if *a >= 0 => { + f.write_str("+")?; + a.fmt(f)?; + } + Integer(a) => a.fmt(f)?, + Double(a) => a.fmt(f)?, + } + Ok(()) } } -impl<'a, E: Display + Clone> Display for PhasedExpr<'a, E> { +impl<'a> Display for PhasedExpr<'a> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { self.0.as_ref().fmt_phase(f, self.1) } |