diff options
Diffstat (limited to '')
-rw-r--r-- | dhall/build.rs | 185 |
1 files changed, 94 insertions, 91 deletions
diff --git a/dhall/build.rs b/dhall/build.rs index 3021f03..9b00684 100644 --- a/dhall/build.rs +++ b/dhall/build.rs @@ -1,7 +1,7 @@ use std::env; use std::ffi::OsString; -use std::fs::File; -use std::io::{BufRead, BufReader, Read, Write}; +use std::fs::{read_to_string, File}; +use std::io::{BufRead, BufReader, Write}; use std::path::{Path, PathBuf}; use walkdir::WalkDir; @@ -51,8 +51,7 @@ fn dhall_files_in_dir<'a>( }) } -#[derive(Debug, Clone)] -struct TestFeature<F> { +struct TestFeature { /// Name of the module, used in the output of `cargo test` module_name: &'static str, /// Directory containing the tests files @@ -60,7 +59,7 @@ struct TestFeature<F> { /// Relevant variant of `dhall::tests::Test` variant: &'static str, /// Given a file name, whether to exclude it - path_filter: F, + path_filter: Box<dyn FnMut(&str) -> bool>, /// Type of the input file input_type: FileType, /// Type of the output file, if any @@ -69,7 +68,7 @@ struct TestFeature<F> { fn make_test_module( w: &mut impl Write, // Where to output the generated code - mut feature: TestFeature<impl FnMut(&str) -> bool>, + mut feature: TestFeature, ) -> std::io::Result<()> { let tests_dir = feature.directory; writeln!(w, "mod {} {{", feature.module_name)?; @@ -113,15 +112,13 @@ fn generate_tests() -> std::io::Result<()> { let parser_tests_path = Path::new(&out_dir).join("spec_tests.rs"); let spec_tests_dir = Path::new("../dhall-lang/tests/"); - let mut file = File::create(parser_tests_path)?; - make_test_module( - &mut file, + let tests = vec![ TestFeature { module_name: "parser_success", directory: spec_tests_dir.join("parser/success/"), variant: "ParserSuccess", - path_filter: |path: &str| { + path_filter: Box::new(|path: &str| { false // Too slow in debug mode || path == "largeExpression" @@ -131,31 +128,25 @@ fn generate_tests() -> std::io::Result<()> { || path == "unit/import/urls/emptyPath0" || path == "unit/import/urls/emptyPath1" || path == "unit/import/urls/emptyPathSegment" - }, + // TODO: waiting for https://github.com/dhall-lang/dhall-lang/pull/871 + || path == "preferMissingNoSpaces" + }), input_type: FileType::Text, output_type: Some(FileType::Binary), }, - )?; - - make_test_module( - &mut file, TestFeature { module_name: "parser_failure", directory: spec_tests_dir.join("parser/failure/"), variant: "ParserFailure", - path_filter: |_path: &str| false, + path_filter: Box::new(|_path: &str| false), input_type: FileType::Text, output_type: None, }, - )?; - - make_test_module( - &mut file, TestFeature { module_name: "printer", directory: spec_tests_dir.join("parser/success/"), variant: "Printer", - path_filter: |path: &str| { + path_filter: Box::new(|path: &str| { false // Too slow in debug mode || path == "largeExpression" @@ -163,19 +154,17 @@ fn generate_tests() -> std::io::Result<()> { || path == "unit/import/urls/emptyPath0" || path == "unit/import/urls/emptyPath1" || path == "unit/import/urls/emptyPathSegment" - }, + // TODO: waiting for https://github.com/dhall-lang/dhall-lang/pull/871 + || path == "preferMissingNoSpaces" + }), input_type: FileType::Text, output_type: Some(FileType::Binary), }, - )?; - - make_test_module( - &mut file, TestFeature { module_name: "binary_encoding", directory: spec_tests_dir.join("parser/success/"), variant: "BinaryEncoding", - path_filter: |path: &str| { + path_filter: Box::new(|path: &str| { false // Too slow in debug mode || path == "largeExpression" @@ -189,43 +178,39 @@ fn generate_tests() -> std::io::Result<()> { || path == "unit/import/urls/emptyPath0" || path == "unit/import/urls/emptyPath1" || path == "unit/import/urls/emptyPathSegment" - }, + // TODO: waiting for https://github.com/dhall-lang/dhall-lang/pull/871 + || path == "preferMissingNoSpaces" + }), input_type: FileType::Text, output_type: Some(FileType::Binary), }, - )?; - - make_test_module( - &mut file, TestFeature { module_name: "binary_decoding_success", directory: spec_tests_dir.join("binary-decode/success/"), variant: "BinaryDecodingSuccess", - path_filter: |_path: &str| false, + path_filter: Box::new(|path: &str| { + false + // We don't support bignums + || path == "unit/IntegerBigNegative" + || path == "unit/IntegerBigPositive" + || path == "unit/NaturalBig" + }), input_type: FileType::Binary, output_type: Some(FileType::Text), }, - )?; - - make_test_module( - &mut file, TestFeature { module_name: "binary_decoding_failure", directory: spec_tests_dir.join("binary-decode/failure/"), variant: "BinaryDecodingFailure", - path_filter: |_path: &str| false, + path_filter: Box::new(|_path: &str| false), input_type: FileType::Binary, output_type: None, }, - )?; - - make_test_module( - &mut file, TestFeature { module_name: "import_success", directory: spec_tests_dir.join("import/success/"), variant: "ImportSuccess", - path_filter: |path: &str| { + path_filter: Box::new(|path: &str| { false || path == "alternativeEnvNatural" || path == "alternativeEnvSimple" @@ -236,38 +221,31 @@ fn generate_tests() -> std::io::Result<()> { || path == "hashFromCache" || path == "headerForwarding" || path == "noHeaderForwarding" - }, + }), input_type: FileType::Text, output_type: Some(FileType::Text), }, - )?; - - make_test_module( - &mut file, TestFeature { module_name: "import_failure", directory: spec_tests_dir.join("import/failure/"), variant: "ImportFailure", - path_filter: |path: &str| { + path_filter: Box::new(|path: &str| { false || path == "alternativeEnv" || path == "alternativeEnvMissing" || path == "hashMismatch" || path == "missing" || path == "referentiallyInsane" - }, + || path == "customHeadersUsingBoundVariable" + }), input_type: FileType::Text, output_type: None, }, - )?; - - make_test_module( - &mut file, TestFeature { module_name: "beta_normalize", directory: spec_tests_dir.join("normalization/success/"), variant: "Normalization", - path_filter: |path: &str| { + path_filter: Box::new(|path: &str| { // We don't support bignums path == "simple/integerToDouble" // Too slow @@ -301,34 +279,32 @@ fn generate_tests() -> std::io::Result<()> { || path == "unit/RightBiasedMergeEquivalentArguments" || path == "unit/NestedRecordProjection" || path == "unit/NestedRecordProjectionByType" - }, + // TODO: record completion + || path == "simple/completion" + || path == "unit/Completion" + // TODO: waiting for https://github.com/dhall-lang/dhall-lang/pull/871 + || path == "unit/MergeNone" + || path == "unit/MergeSome" + }), input_type: FileType::Text, output_type: Some(FileType::Text), }, - )?; - - make_test_module( - &mut file, TestFeature { module_name: "alpha_normalize", directory: spec_tests_dir.join("alpha-normalization/success/"), variant: "AlphaNormalization", - path_filter: |path: &str| { + path_filter: Box::new(|path: &str| { // This test doesn't typecheck path == "unit/FunctionNestedBindingXXFree" - }, + }), input_type: FileType::Text, output_type: Some(FileType::Text), }, - )?; - - make_test_module( - &mut file, TestFeature { module_name: "type_inference_success", directory: spec_tests_dir.join("type-inference/success/"), variant: "TypeInferenceSuccess", - path_filter: |path: &str| { + path_filter: Box::new(|path: &str| { false // Too slow || path == "prelude" @@ -339,24 +315,24 @@ fn generate_tests() -> std::io::Result<()> { // TODO: toMap || path == "unit/ToMap" || path == "unit/ToMapAnnotated" + || path == "unit/ToMapInferTypeFromRecord" || path == "simple/toMapEmptyNormalizeAnnotation" - }, + // TODO: record completion + || path == "simple/completion" + || path == "unit/Completion" + // TODO: waiting for https://github.com/dhall-lang/dhall-lang/pull/871 + || path == "unit/MergeNone" + || path == "unit/MergeSome" + }), input_type: FileType::Text, output_type: Some(FileType::Text), }, - )?; - - make_test_module( - &mut file, TestFeature { module_name: "type_inference_failure", directory: spec_tests_dir.join("type-inference/failure/"), variant: "TypeInferenceFailure", - path_filter: |path: &str| { + path_filter: Box::new(|path: &str| { false - // TODO: Enable imports in typecheck tests - || path == "importBoundary" - || path == "customHeadersUsingBoundVariable" // TODO: projection by expression || path == "unit/RecordProjectionByTypeFieldTypeMismatch" || path == "unit/RecordProjectionByTypeNotPresent" @@ -368,24 +344,23 @@ fn generate_tests() -> std::io::Result<()> { || path == "unit/MistypedToMap3" || path == "unit/MistypedToMap4" || path == "unit/NonRecordToMap" + || path == "unit/ToMapEmptyInvalidAnnotation" || path == "unit/ToMapWrongKind" - }, + // TODO: record completion + || path == "unit/CompletionMissingRequiredField" + || path == "unit/CompletionWithWrongDefaultType" + || path == "unit/CompletionWithWrongFieldName" + || path == "unit/CompletionWithWrongOverridenType" + }), input_type: FileType::Text, output_type: None, }, - )?; - - make_test_module( - &mut file, TestFeature { module_name: "type_error", directory: spec_tests_dir.join("type-inference/failure/"), variant: "TypeError", - path_filter: |path: &str| { + path_filter: Box::new(|path: &str| { false - // TODO: Enable imports in typecheck tests - || path == "importBoundary" - || path == "customHeadersUsingBoundVariable" // TODO: projection by expression || path == "unit/RecordProjectionByTypeFieldTypeMismatch" || path == "unit/RecordProjectionByTypeNotPresent" @@ -397,12 +372,23 @@ fn generate_tests() -> std::io::Result<()> { || path == "unit/MistypedToMap3" || path == "unit/MistypedToMap4" || path == "unit/NonRecordToMap" + || path == "unit/ToMapEmptyInvalidAnnotation" || path == "unit/ToMapWrongKind" - }, + // TODO: record completion + || path == "unit/CompletionMissingRequiredField" + || path == "unit/CompletionWithWrongDefaultType" + || path == "unit/CompletionWithWrongFieldName" + || path == "unit/CompletionWithWrongOverridenType" + }), input_type: FileType::Text, output_type: None, }, - )?; + ]; + + let mut file = File::create(parser_tests_path)?; + for test in tests { + make_test_module(&mut file, test)?; + } Ok(()) } @@ -415,10 +401,9 @@ fn convert_abnf_to_pest() -> std::io::Result<()> { 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 data = read_to_string(abnf_path)?; + data.push('\n'); + let data = data.replace('∀', ""); // TODO: waiting for abnf 0.6.1 let mut rules = abnf_to_pest::parse_abnf(&data)?; for line in BufReader::new(File::open(visibility_path)?).lines() { @@ -435,6 +420,24 @@ fn convert_abnf_to_pest() -> std::io::Result<()> { rules.remove("url_path"); writeln!(&mut file, "url_path = _{{ path }}")?; + // Work around some greediness issue in the grammar. + rules.remove("missing"); + writeln!( + &mut file, + r#"missing = {{ "missing" ~ !simple_label_next_char }}"# + )?; + + // Prefer my nice error message to illegible parse errors. + rules.remove("unicode_escape"); + rules.remove("unbraced_escape"); + rules.remove("braced_escape"); + rules.remove("braced_codepoint"); + rules.remove("unicode_suffix"); + writeln!( + &mut file, + r#"unicode_escape = _{{ HEXDIG{{4}} | "{{" ~ HEXDIG+ ~ "}}" }}"# + )?; + rules.remove("simple_label"); writeln!( &mut file, |