From 617e8c2200546ddd3a4480d8c83cb8703f6595b3 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Dec 2019 12:47:31 +0000 Subject: Derive Parser in dhall_syntax directly --- Cargo.lock | 1 - dhall_syntax/Cargo.toml | 1 - dhall_syntax/src/parser.rs | 6 +++--- 3 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aa4b536..da99b8e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -108,7 +108,6 @@ dependencies = [ name = "dhall_syntax" version = "0.1.0" dependencies = [ - "dhall_generated_parser 0.1.0", "either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/dhall_syntax/Cargo.toml b/dhall_syntax/Cargo.toml index 2724fa5..d732bff 100644 --- a/dhall_syntax/Cargo.toml +++ b/dhall_syntax/Cargo.toml @@ -16,6 +16,5 @@ either = "1.5.2" take_mut = "0.2.2" hex = "0.3.2" lazy_static = "1.4.0" -dhall_generated_parser = { path = "../dhall_generated_parser" } # pest_consume = { path = "../../pest_consume/pest_consume" } pest_consume = "1.0" diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index f5d161f..3ea766f 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -3,8 +3,6 @@ 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}; @@ -147,9 +145,11 @@ lazy_static::lazy_static! { }; } +#[derive(Parser)] +#[grammar = "../../dhall_generated_parser/src/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(()) -- cgit v1.2.3 From 4c3552e23f788f971dc5879c99e1e659d8ddae8f Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 13 Dec 2019 12:53:02 +0000 Subject: Merge dhall_generated_parser into dhall_syntax --- Cargo.lock | 10 +- Cargo.toml | 1 - dhall_generated_parser/.gitignore | 1 - dhall_generated_parser/Cargo.toml | 18 --- dhall_generated_parser/build.rs | 90 ----------- dhall_generated_parser/src/dhall.abnf | 1 - dhall_generated_parser/src/dhall.pest.visibility | 182 ----------------------- dhall_generated_parser/src/lib.rs | 22 --- dhall_syntax/.gitignore | 1 + dhall_syntax/Cargo.toml | 4 + dhall_syntax/build.rs | 90 +++++++++++ dhall_syntax/src/dhall.abnf | 1 + dhall_syntax/src/dhall.pest.visibility | 182 +++++++++++++++++++++++ dhall_syntax/src/parser.rs | 2 +- 14 files changed, 280 insertions(+), 325 deletions(-) delete mode 100644 dhall_generated_parser/.gitignore delete mode 100644 dhall_generated_parser/Cargo.toml delete mode 100644 dhall_generated_parser/build.rs delete mode 120000 dhall_generated_parser/src/dhall.abnf delete mode 100644 dhall_generated_parser/src/dhall.pest.visibility delete mode 100644 dhall_generated_parser/src/lib.rs create mode 100644 dhall_syntax/.gitignore create mode 100644 dhall_syntax/build.rs create mode 120000 dhall_syntax/src/dhall.abnf create mode 100644 dhall_syntax/src/dhall.pest.visibility diff --git a/Cargo.lock b/Cargo.lock index da99b8e..257fade 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,15 +85,6 @@ dependencies = [ "walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "dhall_generated_parser" -version = "0.1.0" -dependencies = [ - "abnf_to_pest 0.1.1", - "pest 2.1.2 (registry+https://github.com/rust-lang/crates.io-index)", - "pest_derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "dhall_proc_macros" version = "0.1.0" @@ -108,6 +99,7 @@ dependencies = [ name = "dhall_syntax" version = "0.1.0" dependencies = [ + "abnf_to_pest 0.1.1", "either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 1e40748..b603e21 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,6 @@ cargo-features = ["profile-overrides"] members = [ "abnf_to_pest", "dhall", - "dhall_generated_parser", "dhall_syntax", "dhall_proc_macros", "improved_slice_patterns", diff --git a/dhall_generated_parser/.gitignore b/dhall_generated_parser/.gitignore deleted file mode 100644 index 8a0bac6..0000000 --- a/dhall_generated_parser/.gitignore +++ /dev/null @@ -1 +0,0 @@ -src/dhall.pest diff --git a/dhall_generated_parser/Cargo.toml b/dhall_generated_parser/Cargo.toml deleted file mode 100644 index b1f0d02..0000000 --- a/dhall_generated_parser/Cargo.toml +++ /dev/null @@ -1,18 +0,0 @@ -[package] -name = "dhall_generated_parser" -version = "0.1.0" -authors = ["Nadrieril "] -license = "BSD-2-Clause" -edition = "2018" -build = "build.rs" - -[lib] -test = false -doctest = false - -[build-dependencies] -abnf_to_pest = { version = "0.1.1", path = "../abnf_to_pest" } - -[dependencies] -pest = "2.1" -pest_derive = "2.1" diff --git a/dhall_generated_parser/build.rs b/dhall_generated_parser/build.rs deleted file mode 100644 index d846f92..0000000 --- a/dhall_generated_parser/build.rs +++ /dev/null @@ -1,90 +0,0 @@ -use std::fs::File; -use std::io::{BufRead, BufReader, Read, Write}; - -use abnf_to_pest::render_rules_to_pest; - -fn main() -> std::io::Result<()> { - let abnf_path = "src/dhall.abnf"; - let visibility_path = "src/dhall.pest.visibility"; - let pest_path = "src/dhall.pest"; - println!("cargo:rerun-if-changed={}", abnf_path); - println!("cargo:rerun-if-changed={}", visibility_path); - - let mut file = File::open(abnf_path)?; - let mut data = Vec::new(); - file.read_to_end(&mut data)?; - data.push('\n' as u8); - - let mut rules = abnf_to_pest::parse_abnf(&data)?; - for line in BufReader::new(File::open(visibility_path)?).lines() { - let line = line?; - if line.len() >= 2 && &line[0..2] == "# " { - rules.get_mut(&line[2..]).map(|x| x.silent = true); - } - } - - let mut file = File::create(pest_path)?; - writeln!(&mut file, "// AUTO-GENERATED FILE. See build.rs.")?; - - // TODO: this is a cheat; properly support RFC3986 URLs instead - rules.remove("url_path"); - writeln!(&mut file, "url_path = _{{ path }}")?; - - rules.remove("simple_label"); - writeln!( - &mut file, - "simple_label = {{ - keyword ~ simple_label_next_char+ - | !keyword ~ simple_label_first_char ~ simple_label_next_char* - }}" - )?; - - rules.remove("nonreserved_label"); - writeln!( - &mut file, - "nonreserved_label = _{{ - !(builtin ~ !simple_label_next_char) ~ label - }}" - )?; - - // Setup grammar for precedence climbing - rules.remove("operator_expression"); - writeln!(&mut file, r##" - import_alt = {{ "?" ~ whsp1 }} - bool_or = {{ "||" }} - natural_plus = {{ "+" ~ whsp1 }} - text_append = {{ "++" }} - list_append = {{ "#" }} - bool_and = {{ "&&" }} - natural_times = {{ "*" }} - bool_eq = {{ "==" }} - bool_ne = {{ "!=" }} - - operator = _{{ - equivalent | - bool_ne | - bool_eq | - natural_times | - combine_types | - prefer | - combine | - bool_and | - list_append | - text_append | - natural_plus | - bool_or | - import_alt - }} - operator_expression = {{ application_expression ~ (whsp ~ operator ~ whsp ~ application_expression)* }} - "##)?; - - writeln!( - &mut file, - "final_expression = ${{ SOI ~ complete_expression ~ EOI }}" - )?; - - writeln!(&mut file)?; - writeln!(&mut file, "{}", render_rules_to_pest(rules).pretty(80))?; - - Ok(()) -} diff --git a/dhall_generated_parser/src/dhall.abnf b/dhall_generated_parser/src/dhall.abnf deleted file mode 120000 index ce13b8e..0000000 --- a/dhall_generated_parser/src/dhall.abnf +++ /dev/null @@ -1 +0,0 @@ -../../dhall-lang/standard/dhall.abnf \ No newline at end of file diff --git a/dhall_generated_parser/src/dhall.pest.visibility b/dhall_generated_parser/src/dhall.pest.visibility deleted file mode 100644 index 17c1edc..0000000 --- a/dhall_generated_parser/src/dhall.pest.visibility +++ /dev/null @@ -1,182 +0,0 @@ -# end_of_line -# valid_non_ascii -# tab -# block_comment -# block_comment_char -# block_comment_continue -# not_end_of_line -# line_comment -# whitespace_chunk -# whsp -# whsp1 -# ALPHA -# DIGIT -# ALPHANUM -# HEXDIG -# simple_label_first_char -# simple_label_next_char -simple_label -# quoted_label_char -quoted_label -# label -# nonreserved_label -# any_label -double_quote_chunk -double_quote_escaped -# unicode_escape -double_quote_char -double_quote_literal -single_quote_continue -escaped_quote_pair -escaped_interpolation -single_quote_char -single_quote_literal -# interpolation -# text_literal -if_ -# then -# else_ -# let_ -# in_ -# as_ -# using -merge -missing -# Infinity -NaN -Some_ -toMap -assert -# keyword -builtin -# Optional -Text -# List -Location -# Bool -# True -# False -# None_ -# Natural -# Integer -# Double -# Type -# Kind -# Sort -# Natural_fold -# Natural_build -# Natural_isZero -# Natural_even -# Natural_odd -# Natural_toInteger -# Natural_show -# Integer_toDouble -# Integer_show -# Natural_subtract -# Double_show -# List_build -# List_fold -# List_length -# List_head -# List_last -# List_indexed -# List_reverse -# Optional_fold -# Optional_build -# Text_show -combine -combine_types -equivalent -prefer -lambda -forall -arrow -# exponent -numeric_double_literal -minus_infinity_literal -plus_infinity_literal -# double_literal -natural_literal -integer_literal -identifier -variable -# path_character -# quoted_path_character -unquoted_path_component -quoted_path_component -# path_component -path -local -parent_path -here_path -home_path -absolute_path -scheme -http_raw -authority -# userinfo -# host -# port -# IP_literal -# IPvFuture -# IPv6address -# h16 -# ls32 -# IPv4address -# dec_octet -# domain -# domainlabel -# pchar -query -# pct_encoded -# unreserved -# sub_delims -http -env -bash_environment_variable -posix_environment_variable -posix_environment_variable_character -# import_type -hash -import_hashed -import -expression -# annotated_expression -let_binding -empty_list_literal -operator_expression -import_alt_expression -or_expression -plus_expression -text_append_expression -list_append_expression -and_expression -combine_expression -prefer_expression -combine_types_expression -times_expression -equal_expression -not_equal_expression -equivalent_expression -application_expression -first_application_expression -# import_expression -selector_expression -selector -labels -# type_selector -primitive_expression -# record_type_or_literal -empty_record_literal -empty_record_type -non_empty_record_type_or_literal -non_empty_record_type -record_type_entry -non_empty_record_literal -record_literal_entry -union_type -empty_union_type -# non_empty_union_type -union_type_entry -non_empty_list_literal -# complete_expression diff --git a/dhall_generated_parser/src/lib.rs b/dhall_generated_parser/src/lib.rs deleted file mode 100644 index fbb9ccd..0000000 --- a/dhall_generated_parser/src/lib.rs +++ /dev/null @@ -1,22 +0,0 @@ -//! This crate only contains a [pest][pest]-generated parser for the [dhall][dhall] language. -//! It is part of the [dhall-rust][dhall-rust] crate. -//! -//! [pest]: https://pest.rs -//! [dhall]: https://dhall-lang.org/ -//! [dhall-rust]: https://github.com/Nadrieril/dhall-rust - -// This crate only contains the grammar-generated parser. The rest of the -// parser is in dhall_syntax. This separation is because compiling the -// grammar-generated parser is extremely slow. -// See the https://pest.rs documentation for details on what this crate contains. -// The pest file is auto-generated and is located at ./dhall.pest. -// It is generated from grammar.abnf in a rather straightforward manner. Some -// additional overrides are done in ../build.rs. -// The lines that are commented out in ./dhall.pest.visibility are marked as -// silent (see pest docs for what that means) in the generated pest file. - -use pest_derive::Parser; - -#[derive(Parser)] -#[grammar = "dhall.pest"] -pub struct DhallParser; diff --git a/dhall_syntax/.gitignore b/dhall_syntax/.gitignore new file mode 100644 index 0000000..8a0bac6 --- /dev/null +++ b/dhall_syntax/.gitignore @@ -0,0 +1 @@ +src/dhall.pest diff --git a/dhall_syntax/Cargo.toml b/dhall_syntax/Cargo.toml index d732bff..7708954 100644 --- a/dhall_syntax/Cargo.toml +++ b/dhall_syntax/Cargo.toml @@ -4,10 +4,14 @@ version = "0.1.0" authors = ["NanoTech ", "Nadrieril "] license = "BSD-2-Clause" edition = "2018" +build = "build.rs" [lib] doctest = false +[build-dependencies] +abnf_to_pest = { version = "0.1.1", path = "../abnf_to_pest" } + [dependencies] itertools = "0.8.0" percent-encoding = "2.1.0" diff --git a/dhall_syntax/build.rs b/dhall_syntax/build.rs new file mode 100644 index 0000000..d846f92 --- /dev/null +++ b/dhall_syntax/build.rs @@ -0,0 +1,90 @@ +use std::fs::File; +use std::io::{BufRead, BufReader, Read, Write}; + +use abnf_to_pest::render_rules_to_pest; + +fn main() -> std::io::Result<()> { + let abnf_path = "src/dhall.abnf"; + let visibility_path = "src/dhall.pest.visibility"; + let pest_path = "src/dhall.pest"; + println!("cargo:rerun-if-changed={}", abnf_path); + println!("cargo:rerun-if-changed={}", visibility_path); + + let mut file = File::open(abnf_path)?; + let mut data = Vec::new(); + file.read_to_end(&mut data)?; + data.push('\n' as u8); + + let mut rules = abnf_to_pest::parse_abnf(&data)?; + for line in BufReader::new(File::open(visibility_path)?).lines() { + let line = line?; + if line.len() >= 2 && &line[0..2] == "# " { + rules.get_mut(&line[2..]).map(|x| x.silent = true); + } + } + + let mut file = File::create(pest_path)?; + writeln!(&mut file, "// AUTO-GENERATED FILE. See build.rs.")?; + + // TODO: this is a cheat; properly support RFC3986 URLs instead + rules.remove("url_path"); + writeln!(&mut file, "url_path = _{{ path }}")?; + + rules.remove("simple_label"); + writeln!( + &mut file, + "simple_label = {{ + keyword ~ simple_label_next_char+ + | !keyword ~ simple_label_first_char ~ simple_label_next_char* + }}" + )?; + + rules.remove("nonreserved_label"); + writeln!( + &mut file, + "nonreserved_label = _{{ + !(builtin ~ !simple_label_next_char) ~ label + }}" + )?; + + // Setup grammar for precedence climbing + rules.remove("operator_expression"); + writeln!(&mut file, r##" + import_alt = {{ "?" ~ whsp1 }} + bool_or = {{ "||" }} + natural_plus = {{ "+" ~ whsp1 }} + text_append = {{ "++" }} + list_append = {{ "#" }} + bool_and = {{ "&&" }} + natural_times = {{ "*" }} + bool_eq = {{ "==" }} + bool_ne = {{ "!=" }} + + operator = _{{ + equivalent | + bool_ne | + bool_eq | + natural_times | + combine_types | + prefer | + combine | + bool_and | + list_append | + text_append | + natural_plus | + bool_or | + import_alt + }} + operator_expression = {{ application_expression ~ (whsp ~ operator ~ whsp ~ application_expression)* }} + "##)?; + + writeln!( + &mut file, + "final_expression = ${{ SOI ~ complete_expression ~ EOI }}" + )?; + + writeln!(&mut file)?; + writeln!(&mut file, "{}", render_rules_to_pest(rules).pretty(80))?; + + Ok(()) +} diff --git a/dhall_syntax/src/dhall.abnf b/dhall_syntax/src/dhall.abnf new file mode 120000 index 0000000..ce13b8e --- /dev/null +++ b/dhall_syntax/src/dhall.abnf @@ -0,0 +1 @@ +../../dhall-lang/standard/dhall.abnf \ No newline at end of file diff --git a/dhall_syntax/src/dhall.pest.visibility b/dhall_syntax/src/dhall.pest.visibility new file mode 100644 index 0000000..17c1edc --- /dev/null +++ b/dhall_syntax/src/dhall.pest.visibility @@ -0,0 +1,182 @@ +# end_of_line +# valid_non_ascii +# tab +# block_comment +# block_comment_char +# block_comment_continue +# not_end_of_line +# line_comment +# whitespace_chunk +# whsp +# whsp1 +# ALPHA +# DIGIT +# ALPHANUM +# HEXDIG +# simple_label_first_char +# simple_label_next_char +simple_label +# quoted_label_char +quoted_label +# label +# nonreserved_label +# any_label +double_quote_chunk +double_quote_escaped +# unicode_escape +double_quote_char +double_quote_literal +single_quote_continue +escaped_quote_pair +escaped_interpolation +single_quote_char +single_quote_literal +# interpolation +# text_literal +if_ +# then +# else_ +# let_ +# in_ +# as_ +# using +merge +missing +# Infinity +NaN +Some_ +toMap +assert +# keyword +builtin +# Optional +Text +# List +Location +# Bool +# True +# False +# None_ +# Natural +# Integer +# Double +# Type +# Kind +# Sort +# Natural_fold +# Natural_build +# Natural_isZero +# Natural_even +# Natural_odd +# Natural_toInteger +# Natural_show +# Integer_toDouble +# Integer_show +# Natural_subtract +# Double_show +# List_build +# List_fold +# List_length +# List_head +# List_last +# List_indexed +# List_reverse +# Optional_fold +# Optional_build +# Text_show +combine +combine_types +equivalent +prefer +lambda +forall +arrow +# exponent +numeric_double_literal +minus_infinity_literal +plus_infinity_literal +# double_literal +natural_literal +integer_literal +identifier +variable +# path_character +# quoted_path_character +unquoted_path_component +quoted_path_component +# path_component +path +local +parent_path +here_path +home_path +absolute_path +scheme +http_raw +authority +# userinfo +# host +# port +# IP_literal +# IPvFuture +# IPv6address +# h16 +# ls32 +# IPv4address +# dec_octet +# domain +# domainlabel +# pchar +query +# pct_encoded +# unreserved +# sub_delims +http +env +bash_environment_variable +posix_environment_variable +posix_environment_variable_character +# import_type +hash +import_hashed +import +expression +# annotated_expression +let_binding +empty_list_literal +operator_expression +import_alt_expression +or_expression +plus_expression +text_append_expression +list_append_expression +and_expression +combine_expression +prefer_expression +combine_types_expression +times_expression +equal_expression +not_equal_expression +equivalent_expression +application_expression +first_application_expression +# import_expression +selector_expression +selector +labels +# type_selector +primitive_expression +# record_type_or_literal +empty_record_literal +empty_record_type +non_empty_record_type_or_literal +non_empty_record_type +record_type_entry +non_empty_record_literal +record_literal_entry +union_type +empty_union_type +# non_empty_union_type +union_type_entry +non_empty_list_literal +# complete_expression diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index 3ea766f..044d3f1 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -146,7 +146,7 @@ lazy_static::lazy_static! { } #[derive(Parser)] -#[grammar = "../../dhall_generated_parser/src/dhall.pest"] +#[grammar = "dhall.pest"] struct DhallParser; #[pest_consume::parser(parser = DhallParser, rule = Rule)] -- cgit v1.2.3 From 929a34c177f8b18ae791f77bbd4630a862fb1070 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 15 Dec 2019 19:42:57 +0000 Subject: Reexport dhall_syntax as a module in dhall --- dhall/src/core/context.rs | 2 +- dhall/src/core/value.rs | 2 +- dhall/src/core/valuef.rs | 4 ++-- dhall/src/core/var.rs | 10 +++++----- dhall/src/error/mod.rs | 2 +- dhall/src/lib.rs | 4 ++++ dhall/src/phase/binary.rs | 16 ++++++++-------- dhall/src/phase/mod.rs | 2 +- dhall/src/phase/normalize.rs | 10 +++++----- dhall/src/phase/parse.rs | 2 +- dhall/src/phase/resolve.rs | 8 ++++---- dhall/src/phase/typecheck.rs | 24 ++++++++++++------------ serde_dhall/src/lib.rs | 2 +- serde_dhall/src/serde.rs | 2 +- serde_dhall/src/static_type.rs | 2 +- 15 files changed, 48 insertions(+), 44 deletions(-) diff --git a/dhall/src/core/context.rs b/dhall/src/core/context.rs index 2bf39c5..00e1493 100644 --- a/dhall/src/core/context.rs +++ b/dhall/src/core/context.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::rc::Rc; -use dhall_syntax::{Label, V}; +use crate::syntax::{Label, V}; use crate::core::value::Value; use crate::core::valuef::ValueF; diff --git a/dhall/src/core/value.rs b/dhall/src/core/value.rs index d4f5131..bd7a9b9 100644 --- a/dhall/src/core/value.rs +++ b/dhall/src/core/value.rs @@ -1,7 +1,7 @@ use std::cell::{Ref, RefCell, RefMut}; use std::rc::Rc; -use dhall_syntax::{Builtin, Const, Span}; +use crate::syntax::{Builtin, Const, Span}; use crate::core::context::TypecheckContext; use crate::core::valuef::ValueF; diff --git a/dhall/src/core/valuef.rs b/dhall/src/core/valuef.rs index e5d0807..e9ac391 100644 --- a/dhall/src/core/valuef.rs +++ b/dhall/src/core/valuef.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use dhall_syntax::{ +use crate::syntax::{ Builtin, Const, ExprF, Integer, InterpolatedTextContents, Label, NaiveDouble, Natural, }; @@ -117,7 +117,7 @@ impl ValueF { .collect(), )), ValueF::Equivalence(x, y) => rc(ExprF::BinOp( - dhall_syntax::BinOp::Equivalence, + crate::syntax::BinOp::Equivalence, x.to_expr(opts), y.to_expr(opts), )), diff --git a/dhall/src/core/var.rs b/dhall/src/core/var.rs index 3795f10..f9d3478 100644 --- a/dhall/src/core/var.rs +++ b/dhall/src/core/var.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use dhall_syntax::{Label, V}; +use crate::syntax::{Label, V}; /// Stores a pair of variables: a normal one and one /// that corresponds to the alpha-normalized version of the first one. @@ -190,7 +190,7 @@ impl Shift for std::cell::RefCell { } } -impl Shift for dhall_syntax::ExprF { +impl Shift for crate::syntax::ExprF { fn shift(&self, delta: isize, var: &AlphaVar) -> Option { Some(self.traverse_ref_with_special_handling_of_binders( |v| Ok(v.shift(delta, var)?), @@ -222,7 +222,7 @@ where } } -impl Shift for dhall_syntax::InterpolatedTextContents { +impl Shift for crate::syntax::InterpolatedTextContents { fn shift(&self, delta: isize, var: &AlphaVar) -> Option { Some(self.traverse_ref(|x| Ok(x.shift(delta, var)?))?) } @@ -262,7 +262,7 @@ impl> Subst for std::cell::RefCell { } } -impl, E: Clone> Subst for dhall_syntax::ExprF { +impl, E: Clone> Subst for crate::syntax::ExprF { fn subst_shift(&self, var: &AlphaVar, val: &S) -> Self { self.map_ref_with_special_handling_of_binders( |v| v.subst_shift(var, val), @@ -277,7 +277,7 @@ impl> Subst for Vec { } } -impl> Subst for dhall_syntax::InterpolatedTextContents { +impl> Subst for crate::syntax::InterpolatedTextContents { fn subst_shift(&self, var: &AlphaVar, val: &S) -> Self { self.map_ref(|x| x.subst_shift(var, val)) } diff --git a/dhall/src/error/mod.rs b/dhall/src/error/mod.rs index e4baea0..1d58e6f 100644 --- a/dhall/src/error/mod.rs +++ b/dhall/src/error/mod.rs @@ -1,6 +1,6 @@ use std::io::Error as IOError; -use dhall_syntax::{BinOp, Import, Label, ParseError, Span}; +use crate::syntax::{BinOp, Import, Label, ParseError, Span}; use crate::core::context::TypecheckContext; use crate::core::value::Value; diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index ed4435a..66c6e5b 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -15,3 +15,7 @@ mod tests; pub mod core; pub mod error; pub mod phase; + +pub mod syntax { + pub use dhall_syntax::*; +} diff --git a/dhall/src/phase/binary.rs b/dhall/src/phase/binary.rs index 0639120..b1e7638 100644 --- a/dhall/src/phase/binary.rs +++ b/dhall/src/phase/binary.rs @@ -3,8 +3,8 @@ use serde_cbor::value::value as cbor; use std::iter::FromIterator; use std::vec; -use dhall_syntax::map::DupTreeMap; -use dhall_syntax::{ +use crate::syntax::map::DupTreeMap; +use crate::syntax::{ Expr, ExprF, FilePath, FilePrefix, Hash, Import, ImportLocation, ImportMode, Integer, InterpolatedText, Label, Natural, RawExpr, Scheme, Span, URL, V, @@ -32,7 +32,7 @@ pub fn rc(x: RawExpr) -> Expr { fn cbor_value_to_dhall(data: &cbor::Value) -> Result { use cbor::Value::*; - use dhall_syntax::{BinOp, Builtin, Const}; + use crate::syntax::{BinOp, Builtin, Const}; use ExprF::*; Ok(rc(match data { String(s) => match Builtin::parse(s) { @@ -350,7 +350,7 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { "import/type".to_owned(), ))?, }; - Import(dhall_syntax::Import { + Import(crate::syntax::Import { mode, hash, location, @@ -473,8 +473,8 @@ where S: serde::ser::Serializer, { use cbor::Value::{String, I64, U64}; - use dhall_syntax::Builtin; - use dhall_syntax::ExprF::*; + use crate::syntax::Builtin; + use crate::syntax::ExprF::*; use std::iter::once; use self::Serialize::{RecordMap, UnionMap}; @@ -548,7 +548,7 @@ where once(tag(4)).chain(once(null())).chain(xs.iter().map(expr)), ), TextLit(xs) => { - use dhall_syntax::InterpolatedTextContents::{Expr, Text}; + use crate::syntax::InterpolatedTextContents::{Expr, Text}; ser.collect_seq(once(tag(18)).chain(xs.iter().map(|x| match x { Expr(x) => expr(x), Text(x) => cbor(String(x.clone())), @@ -559,7 +559,7 @@ where UnionType(map) => ser_seq!(ser; tag(11), UnionMap(map)), Field(x, l) => ser_seq!(ser; tag(9), expr(x), label(l)), BinOp(op, x, y) => { - use dhall_syntax::BinOp::*; + use crate::syntax::BinOp::*; let op = match op { BoolOr => 0, BoolAnd => 1, diff --git a/dhall/src/phase/mod.rs b/dhall/src/phase/mod.rs index 337ce3d..918c4d0 100644 --- a/dhall/src/phase/mod.rs +++ b/dhall/src/phase/mod.rs @@ -1,7 +1,7 @@ use std::fmt::Display; use std::path::Path; -use dhall_syntax::{Builtin, Const, Expr}; +use crate::syntax::{Builtin, Const, Expr}; use crate::core::value::{ToExprOptions, Value}; use crate::core::valuef::ValueF; diff --git a/dhall/src/phase/normalize.rs b/dhall/src/phase/normalize.rs index b712027..3ed53f4 100644 --- a/dhall/src/phase/normalize.rs +++ b/dhall/src/phase/normalize.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; -use dhall_syntax::Const::Type; -use dhall_syntax::{ +use crate::syntax::Const::Type; +use crate::syntax::{ BinOp, Builtin, ExprF, InterpolatedText, InterpolatedTextContents, Label, NaiveDouble, }; @@ -47,7 +47,7 @@ macro_rules! make_closure { }}; (1 + $($rest:tt)*) => { ValueF::PartialExpr(ExprF::BinOp( - dhall_syntax::BinOp::NaturalPlus, + crate::syntax::BinOp::NaturalPlus, make_closure!($($rest)*), Value::from_valuef_and_type( ValueF::NaturalLit(1), @@ -62,7 +62,7 @@ macro_rules! make_closure { let tail = make_closure!($($tail)*); let list_type = tail.get_type_not_sort(); ValueF::PartialExpr(ExprF::BinOp( - dhall_syntax::BinOp::ListAppend, + crate::syntax::BinOp::ListAppend, ValueF::NEListLit(vec![head]) .into_value_with_type(list_type.clone()), tail, @@ -76,7 +76,7 @@ pub(crate) fn apply_builtin( args: Vec, ty: &Value, ) -> ValueF { - use dhall_syntax::Builtin::*; + use crate::syntax::Builtin::*; use ValueF::*; // Small helper enum diff --git a/dhall/src/phase/parse.rs b/dhall/src/phase/parse.rs index 540ceea..e277e54 100644 --- a/dhall/src/phase/parse.rs +++ b/dhall/src/phase/parse.rs @@ -2,7 +2,7 @@ use std::fs::File; use std::io::Read; use std::path::Path; -use dhall_syntax::parse_expr; +use crate::syntax::parse_expr; use crate::error::Error; use crate::phase::resolve::ImportRoot; diff --git a/dhall/src/phase/resolve.rs b/dhall/src/phase/resolve.rs index c302bfa..5920f82 100644 --- a/dhall/src/phase/resolve.rs +++ b/dhall/src/phase/resolve.rs @@ -3,9 +3,9 @@ use std::path::{Path, PathBuf}; use crate::error::{Error, ImportError}; use crate::phase::{Normalized, NormalizedExpr, Parsed, Resolved}; -use dhall_syntax::{FilePath, ImportLocation, URL}; +use crate::syntax::{FilePath, ImportLocation, URL}; -type Import = dhall_syntax::Import; +type Import = crate::syntax::Import; /// A root from which to resolve relative imports. #[derive(Debug, Clone, PartialEq, Eq)] @@ -24,8 +24,8 @@ fn resolve_import( import_stack: &ImportStack, ) -> Result { use self::ImportRoot::*; - use dhall_syntax::FilePrefix::*; - use dhall_syntax::ImportLocation::*; + use crate::syntax::FilePrefix::*; + use crate::syntax::ImportLocation::*; let cwd = match root { LocalDir(cwd) => cwd, }; diff --git a/dhall/src/phase/typecheck.rs b/dhall/src/phase/typecheck.rs index ef8340d..9a41be9 100644 --- a/dhall/src/phase/typecheck.rs +++ b/dhall/src/phase/typecheck.rs @@ -1,7 +1,7 @@ use std::cmp::max; use std::collections::HashMap; -use dhall_syntax::{ +use crate::syntax::{ Builtin, Const, Expr, ExprF, InterpolatedTextContents, Label, RawExpr, Span, }; @@ -155,7 +155,7 @@ macro_rules! make_type { (Double) => { ExprF::Builtin(Builtin::Double) }; (Text) => { ExprF::Builtin(Builtin::Text) }; ($var:ident) => { - ExprF::Var(dhall_syntax::V(stringify!($var).into(), 0)) + ExprF::Var(crate::syntax::V(stringify!($var).into(), 0)) }; (Optional $ty:ident) => { ExprF::App( @@ -170,7 +170,7 @@ macro_rules! make_type { ) }; ({ $($label:ident : $ty:ident),* }) => {{ - let mut kts = dhall_syntax::map::DupTreeMap::new(); + let mut kts = crate::syntax::map::DupTreeMap::new(); $( kts.insert( Label::from(stringify!($label)), @@ -203,7 +203,7 @@ macro_rules! make_type { } fn type_of_builtin(b: Builtin) -> Expr { - use dhall_syntax::Builtin::*; + use crate::syntax::Builtin::*; rc(match b { Bool | Natural | Integer | Double | Text => make_type!(Type), List | Optional => make_type!( @@ -303,7 +303,7 @@ fn type_with( ctx: &TypecheckContext, e: Expr, ) -> Result { - use dhall_syntax::ExprF::{Annot, Embed, Lam, Let, Pi, Var}; + use crate::syntax::ExprF::{Annot, Embed, Lam, Let, Pi, Var}; let span = e.span(); Ok(match e.as_ref() { @@ -362,10 +362,10 @@ fn type_last_layer( span: Span, ) -> Result { use crate::error::TypeMessage::*; - use dhall_syntax::BinOp::*; - use dhall_syntax::Builtin::*; - use dhall_syntax::Const::Type; - use dhall_syntax::ExprF::*; + use crate::syntax::BinOp::*; + use crate::syntax::Builtin::*; + use crate::syntax::Const::Type; + use crate::syntax::ExprF::*; let mkerr = |msg: TypeMessage| Err(TypeError::new(ctx, msg)); /// Intermediary return type @@ -434,7 +434,7 @@ fn type_last_layer( } EmptyListLit(t) => { match &*t.as_whnf() { - ValueF::AppliedBuiltin(dhall_syntax::Builtin::List, args) + ValueF::AppliedBuiltin(crate::syntax::Builtin::List, args) if args.len() == 1 => {} _ => return mkerr(InvalidListType(t.clone())), } @@ -457,7 +457,7 @@ fn type_last_layer( return mkerr(InvalidListType(t)); } - RetTypeOnly(Value::from_builtin(dhall_syntax::Builtin::List).app(t)) + RetTypeOnly(Value::from_builtin(crate::syntax::Builtin::List).app(t)) } SomeLit(x) => { let t = x.get_type()?; @@ -466,7 +466,7 @@ fn type_last_layer( } RetTypeOnly( - Value::from_builtin(dhall_syntax::Builtin::Optional).app(t), + Value::from_builtin(crate::syntax::Builtin::Optional).app(t), ) } RecordType(kts) => RetWhole(tck_record_type( diff --git a/serde_dhall/src/lib.rs b/serde_dhall/src/lib.rs index d371d6d..91b47e5 100644 --- a/serde_dhall/src/lib.rs +++ b/serde_dhall/src/lib.rs @@ -123,7 +123,7 @@ pub use value::Value; // A Dhall value. pub mod value { use dhall::phase::{NormalizedExpr, Parsed, Typed}; - use dhall_syntax::Builtin; + use dhall::syntax::Builtin; use super::de::{Error, Result}; diff --git a/serde_dhall/src/serde.rs b/serde_dhall/src/serde.rs index 26708c1..2972ea2 100644 --- a/serde_dhall/src/serde.rs +++ b/serde_dhall/src/serde.rs @@ -1,7 +1,7 @@ use std::borrow::Cow; use dhall::phase::NormalizedExpr; -use dhall_syntax::ExprF; +use dhall::syntax::ExprF; use crate::de::{Deserialize, Error, Result}; use crate::Value; diff --git a/serde_dhall/src/static_type.rs b/serde_dhall/src/static_type.rs index 67a7bc4..1323aa3 100644 --- a/serde_dhall/src/static_type.rs +++ b/serde_dhall/src/static_type.rs @@ -1,4 +1,4 @@ -use dhall_syntax::{Builtin, Integer, Natural}; +use dhall::syntax::{Builtin, Integer, Natural}; use crate::Value; -- cgit v1.2.3 From 7abd49772643ee4e131ef47de66db22ba7c1e037 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 15 Dec 2019 19:47:20 +0000 Subject: Move contents of dhall under a semantics submodule --- dhall/src/core/context.rs | 145 ------ dhall/src/core/mod.rs | 4 - dhall/src/core/value.rs | 338 -------------- dhall/src/core/valuef.rs | 323 ------------- dhall/src/core/var.rs | 295 ------------ dhall/src/error/mod.rs | 179 -------- dhall/src/lib.rs | 5 +- dhall/src/phase/binary.rs | 748 ------------------------------ dhall/src/phase/mod.rs | 254 ---------- dhall/src/phase/normalize.rs | 794 -------------------------------- dhall/src/phase/parse.rs | 37 -- dhall/src/phase/resolve.rs | 180 -------- dhall/src/phase/typecheck.rs | 815 --------------------------------- dhall/src/semantics/core/context.rs | 145 ++++++ dhall/src/semantics/core/mod.rs | 4 + dhall/src/semantics/core/value.rs | 338 ++++++++++++++ dhall/src/semantics/core/valuef.rs | 323 +++++++++++++ dhall/src/semantics/core/var.rs | 295 ++++++++++++ dhall/src/semantics/error/mod.rs | 179 ++++++++ dhall/src/semantics/mod.rs | 3 + dhall/src/semantics/phase/binary.rs | 748 ++++++++++++++++++++++++++++++ dhall/src/semantics/phase/mod.rs | 254 ++++++++++ dhall/src/semantics/phase/normalize.rs | 794 ++++++++++++++++++++++++++++++++ dhall/src/semantics/phase/parse.rs | 37 ++ dhall/src/semantics/phase/resolve.rs | 180 ++++++++ dhall/src/semantics/phase/typecheck.rs | 815 +++++++++++++++++++++++++++++++++ 26 files changed, 4117 insertions(+), 4115 deletions(-) delete mode 100644 dhall/src/core/context.rs delete mode 100644 dhall/src/core/mod.rs delete mode 100644 dhall/src/core/value.rs delete mode 100644 dhall/src/core/valuef.rs delete mode 100644 dhall/src/core/var.rs delete mode 100644 dhall/src/error/mod.rs delete mode 100644 dhall/src/phase/binary.rs delete mode 100644 dhall/src/phase/mod.rs delete mode 100644 dhall/src/phase/normalize.rs delete mode 100644 dhall/src/phase/parse.rs delete mode 100644 dhall/src/phase/resolve.rs delete mode 100644 dhall/src/phase/typecheck.rs create mode 100644 dhall/src/semantics/core/context.rs create mode 100644 dhall/src/semantics/core/mod.rs create mode 100644 dhall/src/semantics/core/value.rs create mode 100644 dhall/src/semantics/core/valuef.rs create mode 100644 dhall/src/semantics/core/var.rs create mode 100644 dhall/src/semantics/error/mod.rs create mode 100644 dhall/src/semantics/mod.rs create mode 100644 dhall/src/semantics/phase/binary.rs create mode 100644 dhall/src/semantics/phase/mod.rs create mode 100644 dhall/src/semantics/phase/normalize.rs create mode 100644 dhall/src/semantics/phase/parse.rs create mode 100644 dhall/src/semantics/phase/resolve.rs create mode 100644 dhall/src/semantics/phase/typecheck.rs diff --git a/dhall/src/core/context.rs b/dhall/src/core/context.rs deleted file mode 100644 index 00e1493..0000000 --- a/dhall/src/core/context.rs +++ /dev/null @@ -1,145 +0,0 @@ -use std::collections::HashMap; -use std::rc::Rc; - -use crate::syntax::{Label, V}; - -use crate::core::value::Value; -use crate::core::valuef::ValueF; -use crate::core::var::{AlphaVar, Shift, Subst}; -use crate::error::TypeError; - -#[derive(Debug, Clone)] -enum CtxItem { - Kept(AlphaVar, Value), - Replaced(Value), -} - -#[derive(Debug, Clone)] -pub(crate) struct TypecheckContext(Rc>); - -impl TypecheckContext { - pub fn new() -> Self { - TypecheckContext(Rc::new(Vec::new())) - } - pub fn insert_type(&self, x: &Label, t: Value) -> Self { - let mut vec = self.0.as_ref().clone(); - vec.push((x.clone(), CtxItem::Kept(x.into(), t.under_binder(x)))); - TypecheckContext(Rc::new(vec)) - } - pub fn insert_value(&self, x: &Label, e: Value) -> Result { - let mut vec = self.0.as_ref().clone(); - vec.push((x.clone(), CtxItem::Replaced(e))); - Ok(TypecheckContext(Rc::new(vec))) - } - pub fn lookup(&self, var: &V