From 7bbf42dc5d3727dffcb036ffe30dd433faff1950 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 21 Mar 2019 15:11:55 +0100 Subject: Reorganize dhall_core a bit --- dhall_core/src/parser.rs | 923 ++++++++++++++++++++++++----------------------- 1 file changed, 479 insertions(+), 444 deletions(-) (limited to 'dhall_core/src/parser.rs') diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs index 66581a3..896a93f 100644 --- a/dhall_core/src/parser.rs +++ b/dhall_core/src/parser.rs @@ -2,27 +2,62 @@ use pest::iterators::Pair; use pest::Parser; use std::collections::BTreeMap; use std::path::PathBuf; -use std::rc::Rc; use dhall_parser::{DhallParser, Rule}; -use crate::core; -use crate::core::*; +use crate::*; // 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. -pub type ParsedExpr = Expr; -pub type ParsedText = InterpolatedText; -pub type ParsedTextContents<'a> = InterpolatedTextContents<'a, X, Import>; -pub type RcExpr = Rc; +type ParsedExpr = crate::ParsedExpr; +type ParsedText = InterpolatedText; +type ParsedTextContents<'a> = InterpolatedTextContents<'a, X, Import>; pub type ParseError = pest::error::Error; pub type ParseResult = Result; +impl Builtin { + pub fn parse(s: &str) -> Option { + use self::Builtin::*; + match s { + "Bool" => Some(Bool), + "Natural" => Some(Natural), + "Integer" => Some(Integer), + "Double" => Some(Double), + "Text" => Some(Text), + "List" => Some(List), + "Optional" => Some(Optional), + "Some" => Some(OptionalSome), + "None" => Some(OptionalNone), + "Natural/build" => Some(NaturalBuild), + "Natural/fold" => Some(NaturalFold), + "Natural/isZero" => Some(NaturalIsZero), + "Natural/even" => Some(NaturalEven), + "Natural/odd" => Some(NaturalOdd), + "Natural/toInteger" => Some(NaturalToInteger), + "Natural/show" => Some(NaturalShow), + "Integer/toDouble" => Some(IntegerToDouble), + "Integer/show" => Some(IntegerShow), + "Double/show" => Some(DoubleShow), + "List/build" => Some(ListBuild), + "List/fold" => Some(ListFold), + "List/length" => Some(ListLength), + "List/head" => Some(ListHead), + "List/last" => Some(ListLast), + "List/indexed" => Some(ListIndexed), + "List/reverse" => Some(ListReverse), + "Optional/fold" => Some(OptionalFold), + "Optional/build" => Some(OptionalBuild), + "Text/show" => Some(TextShow), + _ => None, + } + } +} + pub fn custom_parse_error(pair: &Pair, msg: String) -> ParseError { let msg = format!("{} while matching on:\n{}", msg, debug_pair(pair.clone())); @@ -221,466 +256,466 @@ fn can_be_shortcutted(rule: Rule) -> bool { } make_parser! { -rule!(EOI<()>; raw_pair!(_) => ()); + rule!(EOI<()>; raw_pair!(_) => ()); -rule!(label_raw