From 22a5eac0bfb22bfe27973c78ef0e8a9b418ee844 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 28 Feb 2019 19:18:04 +0100 Subject: Start rewriting parser in pest --- Cargo.lock | 126 ++++++++- Cargo.toml | 2 + src/dhall.pest | 836 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/parser.rs | 60 ++++ tests/macros.rs | 14 +- tests/tests.rs | 67 ++++- 6 files changed, 1088 insertions(+), 17 deletions(-) create mode 100644 src/dhall.pest diff --git a/Cargo.lock b/Cargo.lock index 9ff727a..f50b56d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,6 +6,11 @@ dependencies = [ "memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "arrayref" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "ascii-canvas" version = "1.0.0" @@ -47,6 +52,15 @@ name = "bitflags" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "block-buffer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "block-buffer" version = "0.7.0" @@ -66,6 +80,11 @@ dependencies = [ "byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "byte-tools" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "byte-tools" version = "0.3.1" @@ -102,6 +121,8 @@ dependencies = [ "lalrpop 0.16.3 (registry+https://github.com/rust-lang/crates.io-index)", "lalrpop-util 0.16.3 (registry+https://github.com/rust-lang/crates.io-index)", "nom 3.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "pest 2.1.0 (git+https://github.com/pest-parser/pest)", + "pest_derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "term-painter 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -110,6 +131,14 @@ name = "diff" version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "digest" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "digest" version = "0.8.0" @@ -124,7 +153,7 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)", "strsim 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -158,6 +187,14 @@ name = "fuchsia-cprng" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "generic-array" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "generic-array" version = "0.12.0" @@ -197,7 +234,7 @@ dependencies = [ "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "lalrpop-util 0.16.3 (registry+https://github.com/rust-lang/crates.io-index)", "petgraph 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex-syntax 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)", @@ -230,6 +267,11 @@ dependencies = [ "cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "maplit" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "memchr" version = "1.0.2" @@ -266,6 +308,53 @@ name = "ordermap" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "pest" +version = "2.1.0" +source = "git+https://github.com/pest-parser/pest#70b5ae08eb71c73077bdc29827f2939ea8d7a4a5" +dependencies = [ + "ucd-trie 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pest" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ucd-trie 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pest_derive" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "pest 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pest_generator 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pest_generator" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "pest 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "pest_meta 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "pest_meta" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "maplit 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "pest 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sha-1 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "petgraph" version = "0.4.13" @@ -432,7 +521,7 @@ dependencies = [ [[package]] name = "regex" -version = "1.1.0" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "aho-corasick 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", @@ -465,6 +554,17 @@ dependencies = [ "syn 0.15.26 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "sha-1" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)", + "fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "sha2" version = "0.8.0" @@ -567,6 +667,11 @@ name = "typenum" version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "ucd-trie" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "ucd-util" version = "0.1.3" @@ -613,20 +718,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] "checksum aho-corasick 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "81ce3d38065e618af2d7b77e10c5ad9a069859b4be3c2250f674af3840d9c8a5" +"checksum arrayref 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "0d382e583f07208808f6b1249e60848879ba3543f57c32277bf52d69c2f0f0ee" "checksum ascii-canvas 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b385d69402821a1c254533a011a312531cbcc0e3e24f19bbb4747a5a2daf37e2" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" "checksum autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6d640bee2da49f60a4068a7fae53acde8982514ab7bae8b8cea9e88cbcfd799" "checksum bit-set 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6f1efcc46c18245a69c38fcc5cc650f16d3a59d034f3106e9ed63748f695730a" "checksum bit-vec 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4440d5cb623bb7390ae27fec0bb6c61111969860f8e3ae198bfa0663645e67cf" "checksum bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "228047a76f468627ca71776ecdebd732a3423081fcf5125585bcd7c49886ce12" +"checksum block-buffer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a076c298b9ecdb530ed9d967e74a6027d6a7478924520acddcddc24c1c8ab3ab" "checksum block-buffer 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "49665c62e0e700857531fa5d3763e91b539ff1abeebd56808d378b495870d60d" "checksum block-padding 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d75255892aeb580d3c566f213a2b6fdc1c66667839f45719ee1d30ebf2aea591" +"checksum byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40" "checksum byte-tools 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" "checksum bytecount 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "be0fdd54b507df8f22012890aadd099979befdba27713c767993f8380112ca7c" "checksum byteorder 1.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a019b10a2a7cdeb292db131fc8113e57ea2a908f6e7894b0c3c671893b65dbeb" "checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum diff 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "3c2b69f912779fbb121ceb775d74d51e915af17aaebc38d28a592843a2dd0a3a" +"checksum digest 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)" = "03b072242a8cbaf9c145665af9d250c59af3b958f83ed6824e13533cf76d5b90" "checksum digest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05f47366984d3ad862010e22c7ce81a7dbcaebbdfb37241a620f8b6596ee135c" "checksum docopt 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "db2906c2579b5b7207fc1e328796a9a8835dc44e22dbe8e460b1d636f9a7b225" "checksum either 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c67353c641dc847124ea1902d69bd753dee9bb3beff9aa3662ecf86c971d1fac" @@ -635,6 +744,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum fixedbitset 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "86d4de0081402f5e88cdac65c8dcdcc73118c1a7a465e2a05f0da05843a8ea33" "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" "checksum generic-array 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0f28c2f5bfb5960175af447a2da7c18900693738343dc896ffbcabd9839592" +"checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d" "checksum itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b8467d9c1cebe26feb08c640139247fac215782d35371ade9a2136ed6085358" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum lalrpop 0.16.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4e2e80bee40b22bca46665b4ef1f3cd88ed0fb043c971407eac17a0712c02572" @@ -642,12 +752,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum lazy_static 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bc5729f27f159ddd61f4df6228e827e86643d4d3e7c32183cb30a1c08f604a14" "checksum libc 0.2.49 (registry+https://github.com/rust-lang/crates.io-index)" = "413f3dfc802c5dc91dc570b05125b6cda9855edfaa9825c9849807876376e70e" "checksum log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c84ec4b527950aa83a329754b01dbe3f58361d1c5efacd1f6d68c494d08a17c6" +"checksum maplit 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "08cbb6b4fef96b6d77bfc40ec491b1690c779e77b05cd9f07f787ed376fd4c43" "checksum memchr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "148fab2e51b4f1cfc66da2a7c32981d1d3c083a803978268bb11fe4b86925e7a" "checksum memchr 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2efc7bc57c883d4a4d6e3246905283d8dae951bb3bd32f49d6ef297f546e1c39" "checksum new_debug_unreachable 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "f40f005c60db6e03bae699e414c58bf9aa7ea02a2d0b9bfbcf19286cc4c82b30" "checksum nom 3.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05aec50c70fd288702bcd93284a8444607f3292dbdf2a30de5ea5dcdbe72287b" "checksum opaque-debug 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "93f5bb2e8e8dec81642920ccff6b61f1eb94fa3020c5a325c9851ff604152409" "checksum ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a86ed3f5f244b372d6b1a00b72ef7f8876d0bc6a78a4c9985c53614041512063" +"checksum pest 2.1.0 (git+https://github.com/pest-parser/pest)" = "" +"checksum pest 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "54f0c72a98d8ab3c99560bfd16df8059cc10e1f9a8e83e6e3b97718dd766e9c3" +"checksum pest_derive 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "833d1ae558dc601e9a60366421196a8d94bc0ac980476d0b67e1d0988d72b2d0" +"checksum pest_generator 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "63120576c4efd69615b5537d3d052257328a4ca82876771d6944424ccfd9f646" +"checksum pest_meta 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f5a3492a4ed208ffc247adcdcc7ba2a95be3104f58877d0d02f0df39bf3efb5e" "checksum petgraph 0.4.13 (registry+https://github.com/rust-lang/crates.io-index)" = "9c3659d1ee90221741f65dd128d9998311b0e40c5d3c23a62445938214abce4f" "checksum phf_generator 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)" = "09364cc93c159b8b06b1f4dd8a4398984503483891b0c26b867cf431fb132662" "checksum phf_shared 0.7.24 (registry+https://github.com/rust-lang/crates.io-index)" = "234f71a15de2288bcb7e3b6515828d22af7ec8598ee6d24c3b526fa0a80b67a0" @@ -667,10 +783,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" "checksum redox_syscall 0.1.51 (registry+https://github.com/rust-lang/crates.io-index)" = "423e376fffca3dfa06c9e9790a9ccd282fafb3cc6e6397d01dbf64f9bacc6b85" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" -"checksum regex 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "37e7cbbd370869ce2e8dff25c7018702d10b21a20ef7135316f8daecd6c25b7f" +"checksum regex 1.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "53ee8cfdddb2e0291adfb9f13d31d3bbe0a03c9a402c01b1e24188d86c35b24f" "checksum regex-syntax 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)" = "8c2f35eedad5295fdf00a63d7d4b238135723f92b434ec06774dad15c7ab0861" "checksum serde 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)" = "9f301d728f2b94c9a7691c90f07b0b4e8a4517181d9461be94c04bddeb4bd850" "checksum serde_derive 1.0.88 (registry+https://github.com/rust-lang/crates.io-index)" = "beed18e6f5175aef3ba670e57c60ef3b1b74d250d962a26604bff4c80e970dd4" +"checksum sha-1 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "51b9d1f3b5de8a167ab06834a7c883bd197f2191e1dda1a22d9ccfeedbf9aded" "checksum sha2 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7b4d8bfd0e469f417657573d8451fb33d16cfe0989359b93baf3a1ffc639543d" "checksum siphasher 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "0b8de496cf83d4ed58b6be86c3a275b8602f6ffe98d3024a869e124147a9a3ac" "checksum string_cache 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "25d70109977172b127fe834e5449e5ab1740b9ba49fa18a2020f509174f25423" @@ -683,6 +800,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169" +"checksum ucd-trie 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "71a9c5b1fe77426cf144cc30e49e955270f5086e31a6441dfa8b32efc09b9d77" "checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737" diff --git a/Cargo.toml b/Cargo.toml index 06d978a..f55d24f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,5 @@ bytecount = "0.5.1" lalrpop-util = "0.16.3" nom = "3.0.0" term-painter = "0.2.3" +pest = { git = "https://github.com/pest-parser/pest" } +pest_derive = "2.0" diff --git a/src/dhall.pest b/src/dhall.pest new file mode 100644 index 0000000..873428c --- /dev/null +++ b/src/dhall.pest @@ -0,0 +1,836 @@ +/// ; ABNF syntax based on RFC 5234 +/// ; +/// ; The character encoding for Dhall is UTF-8 +/// ; +/// ; Some notes on implementing this grammar: +/// ; +/// ; First, do not use a lexer to tokenize the file before parsing. Instead, treat +/// ; the individual characters of the file as the tokens to feed into the parser. +/// ; You should not use a lexer because Dhall's grammar supports two features which +/// ; cannot be correctly supported by a lexer: +/// ; +/// ; * String interpolation (i.e. "foo ${Natural/toInteger bar} baz") +/// ; * Nested block comments (i.e. "{- foo {- bar -} baz -}") +/// ; +/// ; Second, this grammar assumes that your parser can backtrack and/or try +/// ; multiple parses simultaneously. For example, consider this expression: +/// ; +/// ; List ./MyType +/// ; +/// ; A parser might first try to parse the period as the beginning of a field +/// ; selector, only to realize immediately afterwards that `/MyType` is not a valid +/// ; name for a field. A conforming parser must backtrack so that the expression +/// ; `./MyType` can instead be correctly interpreted as a relative path +/// ; +/// ; Third, if there are multiple valid parses then prefer the first parse +/// ; according to the ordering of alternatives. That is, the order of evaluation +/// ; of the alternatives is left-to-right. +/// ; +/// ; For example, the grammar for single quoted string literals is: +/// ; +/// ; single-quote-continue = +/// ; "'''" single-quote-continue +/// ; / "${" complete-expression "}" single-quote-continue +/// ; / "''${" single-quote-continue +/// ; / "''" +/// ; / %x20-10FFFF single-quote-continue +/// ; / tab single-quote-continue +/// ; / end-of-line single-quote-continue +/// ; +/// ; single-quote-literal = "''" single-quote-continue +/// ; +/// ; ... which permits valid parses for the following code: +/// ; +/// ; "''''''''''''''''" +/// ; +/// ; If you tried to parse all alternatives then there are at least two valid +/// ; interpretations for the above code: +/// ; +/// ; * A single quoted literal with four escape sequences of the form "'''" +/// ; * i.e. "''" followed by "'''" four times in a row followed by "''" +/// ; * Four empty single quoted literals +/// ; * i.e. "''''" four times in a row +/// ; +/// ; The correct interpretation is the first one because parsing the escape +/// ; sequence "'''" takes precedence over parsing the termination sequence "''", +/// ; according to the order of the alternatives in the `single-quote-continue` +/// ; rule. +/// ; +/// ; Some parsing libraries do not backtrack by default but allow the user to +/// ; selectively backtrack in certain parts of the grammar. Usually parsing +/// ; libraries do this to improve efficiency and error messages. Dhall's grammar +/// ; takes that into account by minimizing the number of rules that require the +/// ; parser to backtrack and comments below will highlight where you need to +/// ; explicitly backtrack +/// ; +/// ; Specifically, if you see an uninterrupted literal in a grammar rule such as: +/// ; +/// ; "->" +/// ; +/// ; ... or: +/// ; +/// ; %x66.6f.72.61.6c.6c +/// ; +/// ; ... then that string literal is parsed as a single unit, meaning that you +/// ; should backtrack if you parse only part of the literal +/// ; +/// ; In all other cases you can assume that you do not need to backtrack unless +/// ; there is a comment explicitly asking you to backtrack +/// ; +/// ; When parsing a repeated construct, prefer alternatives that parse as many +/// ; repetitions as possible. On in other words: +/// ; +/// ; [a] = a / "" +/// ; +/// ; a* = a* a / "" +/// ; +/// ; Note that the latter rule also specifies that repetition produces +/// ; left-associated expressions. For example, function application is +/// ; left-associative and all operators are left-associative when they are not +/// ; parenthesized. +/// ; +/// ; Additionally, try alternatives in an order that minimizes backtracking +/// ; according to the following rule: +/// ; +/// ; (a / b) (c / d) = a c / a d / b c / b d +/// +/// ; NOTE: There are many line endings in the wild +/// ; +/// ; See: https://en.wikipedia.org/wiki/Newline +/// ; +/// ; For simplicity this supports Unix and Windows line-endings, which are the most +/// ; common +/// end-of-line = +/// %x0A ; "\n" +/// / %x0D.0A ; "\r\n" +/// +/// tab = %x09 ; "\t" +/// +/// block-comment = "{-" block-comment-continue +/// +/// block-comment-chunk = +/// block-comment +/// / %x20-10FFFF +/// / tab +/// / end-of-line +/// +/// block-comment-continue = "-}" / block-comment-chunk block-comment-continue +/// +/// not-end-of-line = %x20-10FFFF / tab +/// +/// ; NOTE: Slightly different from Haskell-style single-line comments because this +/// ; does not require a space after the dashes +/// line-comment = "--" *not-end-of-line end-of-line +/// +/// whitespace-chunk = +/// " " +/// / tab +/// / end-of-line +/// / line-comment +/// / block-comment +whitespace_chunk = _{ + " " + // | tab + // | end_of_line + // | line_comment + // | block_comment +} +/// +/// whitespace = *whitespace-chunk +whitespace = _{ whitespace_chunk* } +/// +/// nonempty-whitespace = 1*whitespace-chunk +nonempty_whitespace = _{ whitespace_chunk+ } +/// +/// ; Uppercase or lowercase ASCII letter +/// ALPHA = %x41-5A / %x61-7A +/// +/// ; ASCII digit +/// DIGIT = %x30-39 ; 0-9 +/// +/// HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F" +/// +/// ; A simple label cannot be one of the following reserved names: +/// ; +/// ; * Bool +/// ; * Optional +/// ; * None +/// ; * Natural +/// ; * Integer +/// ; * Double +/// ; * Text +/// ; * List +/// ; * True +/// ; * False +/// ; * NaN +/// ; * Infinity +/// ; * Type +/// ; * Kind +/// ; * Sort +/// ; * Natural/fold +/// ; * Natural/build +/// ; * Natural/isZero +/// ; * Natural/even +/// ; * Natural/odd +/// ; * Natural/toInteger +/// ; * Natural/show +/// ; * Integer/toDouble +/// ; * Integer/show +/// ; * Double/show +/// ; * List/build +/// ; * List/fold +/// ; * List/length +/// ; * List/head +/// ; * List/last +/// ; * List/indexed +/// ; * List/reverse +/// ; * Optional/fold +/// ; * Optional/build +/// ; * Text/show +/// ; * if +/// ; * then +/// ; * else +/// ; * let +/// ; * in +/// ; * as +/// ; * using +/// ; * merge +/// ; * constructors +/// ; * Some +/// simple-label = (ALPHA / "_") *(ALPHA / DIGIT / "-" / "/" / "_") +/// +/// quoted-label = 1*(ALPHA / DIGIT / "-" / "/" / "_" / ":" / "." / "$") +/// +/// ; NOTE: Dhall does not support Unicode labels, mainly to minimize the potential +/// ; for code obfuscation +/// label = ("`" quoted-label "`" / simple-label) whitespace +/// +/// ; Dhall's double-quoted strings are equivalent to JSON strings except with +/// ; support for string interpolation (and escaping string interpolation) +/// ; +/// ; Dhall uses almost the same escaping rules as JSON (RFC7159) with one +/// ; exception: Dhall adds a new `\$` escape sequence for dollar signs. This +/// ; additional escape sequences lets you escape string interpolation by writing +/// ; `\${` +/// ; +/// ; > The representation of strings is similar to conventions used in the C +/// ; > family of programming languages. A string begins and ends with +/// ; > quotation marks. All Unicode characters may be placed within the +/// ; > quotation marks, except for the characters that must be escaped: +/// ; > quotation mark, reverse solidus, and the control characters (U+0000 +/// ; > through U+001F). +/// ; > +/// ; > Any character may be escaped. If the character is in the Basic +/// ; > Multilingual Plane (U+0000 through U+FFFF), then it may be +/// ; > represented as a six-character sequence: a reverse solidus, followed +/// ; > by the lowercase letter u, followed by four hexadecimal digits that +/// ; > encode the character's code point. The hexadecimal letters A though +/// ; > F can be upper or lower case. So, for example, a string containing +/// ; > only a single reverse solidus character may be represented as +/// ; > "\u005C". +/// ; > +/// ; > Alternatively, there are two-character sequence escape +/// ; > representations of some popular characters. So, for example, a +/// ; > string containing only a single reverse solidus character may be +/// ; > represented more compactly as "\\". +/// ; > +/// ; > To escape an extended character that is not in the Basic Multilingual +/// ; > Plane, the character is represented as a 12-character sequence, +/// ; > encoding the UTF-16 surrogate pair. So, for example, a string +/// ; > containing only the G clef character (U+1D11E) may be represented as +/// ; > "\uD834\uDD1E". +/// double-quote-chunk = +/// "${" complete-expression "}" ; Interpolation +/// / %x5C ; '\' Beginning of escape sequence +/// ( %x22 ; '"' quotation mark U+0022 +/// / %x24 ; '$' dollar sign U+0024 +/// / %x5C ; '\' reverse solidus U+005C +/// / %x2F ; '/' solidus U+002F +/// / %x62 ; 'b' backspace U+0008 +/// / %x66 ; 'f' form feed U+000C +/// / %x6E ; 'n' line feed U+000A +/// / %x72 ; 'r' carriage return U+000D +/// / %x74 ; 't' tab U+0009 +/// / %x75 4HEXDIG ; 'uXXXX' U+XXXX +/// ) +/// ; Printable characters except double quote and backslash +/// / %x20-21 +/// ; %x22 = '"' +/// / %x23-5B +/// ; %x5C = "\" +/// / %x5D-10FFFF +/// +/// double-quote-literal = %x22 *double-quote-chunk %x22 +/// +/// ; NOTE: The only way to end a single-quote string literal with a single quote is +/// ; to either interpolate the single quote, like this: +/// ; +/// ; ''ABC${"'"}'' +/// ; +/// ; ... or concatenate another string, like this: +/// ; +/// ; ''ABC'' ++ "'" +/// ; +/// ; If you try to end the string literal with a single quote then you get "'''", +/// ; which is interpreted as an escaped pair of single quotes +/// single-quote-continue = +/// ; Escape two single quotes (i.e. replace this sequence with "''") +/// "'''" single-quote-continue +/// ; Interpolation +/// / "${" complete-expression "}" single-quote-continue +/// ; Escape interpolation (i.e. replace this sequence with "${") +/// / "''${" single-quote-continue +/// / "''" ; End of text literal +/// / %x20-10FFFF single-quote-continue +/// / tab single-quote-continue +/// / end-of-line single-quote-continue +/// +/// single-quote-literal = "''" end-of-line single-quote-continue +/// +/// text-literal = (double-quote-literal / single-quote-literal) whitespace +/// +/// ; RFC 5234 interprets string literals as case-insensitive and recommends using +/// ; hex instead for case-sensitive strings +/// ; +/// ; If you don't feel like reading hex, these are all the same as the rule name, +/// ; except without the '-raw' ending, and converting dashes in the rule name +/// ; to forward slashes +/// if-raw = %x69.66 +/// then-raw = %x74.68.65.6e +/// else-raw = %x65.6c.73.65 +/// let-raw = %x6c.65.74 +/// in-raw = %x69.6e +/// as-raw = %x61.73 +/// using-raw = %x75.73.69.6e.67 +/// merge-raw = %x6d.65.72.67.65 +/// missing-raw = %x6d.69.73.73.69.6e.67 +/// Some-raw = %x53.6f.6d.65 +/// constructors-raw = %x63.6f.6e.73.74.72.75.63.74.6f.72.73 +/// Natural-fold-raw = %x4e.61.74.75.72.61.6c.2f.66.6f.6c.64 +/// Natural-build-raw = %x4e.61.74.75.72.61.6c.2f.62.75.69.6c.64 +/// Natural-isZero-raw = %x4e.61.74.75.72.61.6c.2f.69.73.5a.65.72.6f +/// Natural-even-raw = %x4e.61.74.75.72.61.6c.2f.65.76.65.6e +/// Natural-odd-raw = %x4e.61.74.75.72.61.6c.2f.6f.64.64 +/// Natural-toInteger-raw = %x4e.61.74.75.72.61.6c.2f.74.6f.49.6e.74.65.67.65.72 +/// Natural-show-raw = %x4e.61.74.75.72.61.6c.2f.73.68.6f.77 +/// Integer-toDouble-raw = %x49.6e.74.65.67.65.72.2f.74.6f.44.6f.75.62.6c.65 +/// Integer-show-raw = %x49.6e.74.65.67.65.72.2f.73.68.6f.77 +/// Double-show-raw = %x44.6f.75.62.6c.65.2f.73.68.6f.77 +/// List-build-raw = %x4c.69.73.74.2f.62.75.69.6c.64 +/// List-fold-raw = %x4c.69.73.74.2f.66.6f.6c.64 +/// List-length-raw = %x4c.69.73.74.2f.6c.65.6e.67.74.68 +/// List-head-raw = %x4c.69.73.74.2f.68.65.61.64 +/// List-last-raw = %x4c.69.73.74.2f.6c.61.73.74 +/// List-indexed-raw = %x4c.69.73.74.2f.69.6e.64.65.78.65.64 +/// List-reverse-raw = %x4c.69.73.74.2f.72.65.76.65.72.73.65 +/// Optional-fold-raw = %x4f.70.74.69.6f.6e.61.6c.2f.66.6f.6c.64 +/// Optional-build-raw = %x4f.70.74.69.6f.6e.61.6c.2f.62.75.69.6c.64 +/// Text-show-raw = %x54.65.78.74.2f.73.68.6f.77 +/// Bool-raw = %x42.6f.6f.6c +/// Optional-raw = %x4f.70.74.69.6f.6e.61.6c +/// None-raw = %x4e.6f.6e.65 +/// Natural-raw = %x4e.61.74.75.72.61.6c +/// Integer-raw = %x49.6e.74.65.67.65.72 +/// Double-raw = %x44.6f.75.62.6c.65 +/// Text-raw = %x54.65.78.74 +/// List-raw = %x4c.69.73.74 +/// True-raw = %x54.72.75.65 +/// False-raw = %x46.61.6c.73.65 +/// NaN-raw = %x4e.61.4e +/// Infinity-raw = %x49.6e.66.69.6e.69.74.79 +/// Type-raw = %x54.79.70.65 +/// Kind-raw = %x4b.69.6e.64 +/// Sort-raw = %x53.6f.72.74 +/// +/// reserved-raw = +/// Bool-raw +/// / Optional-raw +/// / None-raw +/// / Natural-raw +/// / Integer-raw +/// / Double-raw +/// / Text-raw +/// / List-raw +/// / True-raw +/// / False-raw +/// / NaN-raw +/// / Infinity-raw +/// / Type-raw +/// / Kind-raw +/// / Sort-raw +/// +/// reserved-namespaced-raw = +/// Natural-fold-raw +/// / Natural-build-raw +/// / Natural-isZero-raw +/// / Natural-even-raw +/// / Natural-odd-raw +/// / Natural-toInteger-raw +/// / Natural-show-raw +/// / Integer-toDouble-raw +/// / Integer-show-raw +/// / Double-show-raw +/// / List-build-raw +/// / List-fold-raw +/// / List-length-raw +/// / List-head-raw +/// / List-last-raw +/// / List-indexed-raw +/// / List-reverse-raw +/// / Optional-fold-raw +/// / Optional-build-raw +/// / Text-show-raw +/// +/// reserved = reserved-raw whitespace +/// reserved-namespaced = reserved-namespaced-raw whitespace +/// +/// ; Whitespaced rules for reserved words, to be used when matching expressions +/// if = if-raw nonempty-whitespace +/// then = then-raw nonempty-whitespace +/// else = else-raw nonempty-whitespace +/// let = let-raw nonempty-whitespace +/// in = in-raw nonempty-whitespace +/// as = as-raw nonempty-whitespace +/// using = using-raw nonempty-whitespace +/// merge = merge-raw nonempty-whitespace +/// constructors = constructors-raw nonempty-whitespace +/// Some = Some-raw nonempty-whitespace +/// +/// Optional = Optional-raw whitespace +/// Text = Text-raw whitespace +/// List = List-raw whitespace +/// +/// equal = "=" whitespace +/// or = "||" whitespace +/// plus = "+" nonempty-whitespace ; To disambiguate `f +2` +plus = _{ "+" ~ nonempty_whitespace } +/// text-append = "++" whitespace +/// list-append = "#" nonempty-whitespace ; To disambiguate `http://a/a#a` +/// and = "&&" whitespace +/// times = "*" whitespace +times = _{ "*" ~ nonempty_whitespace } +/// double-equal = "==" whitespace +/// not-equal = "!=" whitespace +/// dot = "." whitespace +/// open-brace = "{" whitespace +/// close-brace = "}" whitespace +/// open-bracket = "[" whitespace +/// close-bracket = "]" whitespace +/// open-angle = "<" whitespace +/// close-angle = ">" whitespace +/// bar = "|" whitespace +/// comma = "," whitespace +/// open-parens = "(" whitespace +open_parens = _{ "(" ~ whitespace } +/// close-parens = ")" whitespace +close_parens = _{ ")" ~ whitespace } +/// at = "@" whitespace +/// colon = ":" nonempty-whitespace ; To disambiguate `env:VARIABLE` from type annotations +colon = _{ ":" ~ nonempty_whitespace } +/// import-alt = "?" nonempty-whitespace ; To disambiguate `http://a/a?a` +/// +/// combine = ( %x2227 / "/\" ) whitespace +/// combine-types = ( %x2A53 / "//\\" ) whitespace +/// prefer = ( %x2AFD / "//" ) whitespace +/// lambda = ( %x3BB / "\" ) whitespace +/// forall = ( %x2200 / %x66.6f.72.61.6c.6c ) whitespace +/// arrow = ( %x2192 / "->" ) whitespace +/// +/// exponent = "e" [ "+" / "-" ] 1*DIGIT +/// +/// double-literal = [ "+" / "-" ] 1*DIGIT ( "." 1*DIGIT [ exponent ] / exponent) whitespace +/// +/// natural-literal-raw = 1*DIGIT +natural_literal_raw = _{ ASCII_DIGIT+ } +/// +/// integer-literal = ( "+" / "-" ) natural-literal-raw whitespace +/// +/// natural-literal = natural-literal-raw whitespace +natural_literal = { natural_literal_raw ~ whitespace } +/// +/// identifier = label [ at natural-literal-raw whitespace ] +/// +/// identifier-reserved-prefix = +/// reserved-raw 1*(ALPHA / DIGIT / "-" / "/" / "_") whitespace [ at natural-literal-raw whitespace ] +/// +/// identifier-reserved-namespaced-prefix = +/// reserved-namespaced-raw 1*(ALPHA / DIGIT / "-" / "/" / "_") whitespace [ at natural-literal-raw whitespace ] +/// +/// missing = missing-raw whitespace +/// +/// ; Printable characters other than " ()[]{}<>/\," +/// ; +/// ; Excluding those characters ensures that paths don't have to end with trailing +/// ; whitespace most of the time +/// path-character = +/// ; %x20 = " " +/// %x21 +/// ; %x22 = "\"" +/// ; %x23 = "#" +/// / %x24-27 +/// ; %x28 = "(" +/// ; %x29 = ")" +/// / %x2A-2B +/// ; %x2C = "," +/// / %x2D-2E +/// ; %x2F = "/" +/// / %x30-3B +/// ; %x3C = "<" +/// / %x3D +/// ; %x3E = ">" +/// ; %x3F = "?" +/// / %x40-5A +/// ; %x5B = "[" +/// ; %x5C = "\" +/// ; %x5D = "]" +/// / %x5E-7A +/// ; %x7B = "{" +/// / %x7C +/// ; %x7D = "}" +/// / %x7E +/// +/// quoted-path-character = +/// %x20-21 +/// ; %x22 = "\"" +/// / %x23-2E +/// ; %x2F = "/" +/// / %x30-10FFFF +/// +/// +/// path-component = "/" ( 1*path-character / %x22 1*quoted-path-character %x22 ) +/// +/// directory = *path-component +/// +/// file = path-component +/// +/// local-raw = +/// ".." directory file ; Relative path +/// / "." directory file ; Relative path +/// / "~" directory file ; Home-anchored path +/// ; NOTE: Backtrack if parsing this alternative fails +/// ; +/// ; This is because the first character of this alternative will be "/", but +/// ; if the second character is "/" or "\" then this should have been parsed +/// ; as an operator instead of a path +/// / directory file ; Absolute path +/// +/// local = local-raw whitespace +/// +/// ; `http[s]` URI grammar based on RFC7230 and RFC 3986 with some differences +/// ; noted below +/// +/// scheme = %x68.74.74.70 [ %x73 ] ; "http" [ "s" ] +/// +/// ; NOTE: This does not match the official grammar for a URI. Specifically, this +/// ; replaces `path-abempty` with `directory file` +/// http-raw = scheme "://" authority directory file [ "?" query ] [ "#" fragment ] +/// +/// ; NOTE: Backtrack if parsing the optional user info prefix fails +/// authority = [ userinfo "@" ] host [ ":" port ] +/// +/// userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) +/// +/// host = IP-literal / IPv4address / reg-name +/// +/// port = *DIGIT +/// +/// IP-literal = "[" ( IPv6address / IPvFuture ) "]" +/// +/// IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) +/// +/// ; NOTE: Backtrack when parsing each alternative +/// IPv6address = 6( h16 ":" ) ls32 +/// / "::" 5( h16 ":" ) ls32 +/// / [ h16 ] "::" 4( h16 ":" ) ls32 +/// / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32 +/// / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32 +/// / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32 +/// / [ *4( h16 ":" ) h16 ] "::" ls32 +/// / [ *5( h16 ":" ) h16 ] "::" h16 +/// / [ *6( h16 ":" ) h16 ] "::" +/// +/// h16 = 1*4HEXDIG +/// +/// ls32 = ( h16 ":" h16 ) / IPv4address +/// +/// IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet +/// +/// ; NOTE: Backtrack when parsing these alternatives and try them in reverse order +/// dec-octet = DIGIT ; 0-9 +/// / %x31-39 DIGIT ; 10-99 +/// / "1" 2DIGIT ; 100-199 +/// / "2" %x30-34 DIGIT ; 200-249 +/// / "25" %x30-35 ; 250-255 +/// +/// reg-name = *( unreserved / pct-encoded / sub-delims ) +/// +/// pchar = unreserved / pct-encoded / sub-delims / ":" / "@" +/// +/// query = *( pchar / "/" / "?" ) +/// +/// fragment = *( pchar / "/" / "?" ) +/// +/// pct-encoded = "%" HEXDIG HEXDIG +/// +/// unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" +/// +/// sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "=" +/// +/// http = +/// http-raw whitespace +/// [ using (import-hashed / open-parens import-hashed close-parens) ] +/// +/// ; Dhall supports unquoted environment variables that are Bash-compliant or +/// ; quoted environment variables that are POSIX-compliant +/// env = "env:" +/// ( bash-environment-variable +/// / %x22 posix-environment-variable %x22 +/// ) +/// whitespace +/// +/// ; Bash supports a restricted subset of POSIX environment variables. From the +/// ; Bash `man` page, an environment variable name is: +/// ; +/// ; > A word consisting only of alphanumeric characters and under-scores, and +/// ; > beginning with an alphabetic character or an under-score +/// bash-environment-variable = (ALPHA / "_") *(ALPHA / DIGIT / "_") +/// +/// ; The POSIX standard is significantly more flexible about legal environment +/// ; variable names, which can contain alerts (i.e. '\a'), whitespace, or +/// ; punctuation, for example. The POSIX standard says about environment variable +/// ; names: +/// ; +/// ; > The value of an environment variable is a string of characters. For a +/// ; > C-language program, an array of strings called the environment shall be made +/// ; > available when a process begins. The array is pointed to by the external +/// ; > variable environ, which is defined as: +/// ; > +/// ; > extern char **environ; +/// ; > +/// ; > These strings have the form name=value; names shall not contain the +/// ; > character '='. For values to be portable across systems conforming to IEEE +/// ; > Std 1003.1-2001, the value shall be composed of characters from the portable +/// ; > character set (except NUL and as indicated below). +/// ; +/// ; Note that the standard does not explicitly state that the name must have at +/// ; least one character, but `env` does not appear to support this and `env` +/// ; claims to be POSIX-compliant. To be safe, Dhall requires at least one +/// ; character like `env` +/// posix-environment-variable = 1*posix-environment-variable-character +/// +/// ; These are all the characters from the POSIX Portable Character Set except for +/// ; '\0' (NUL) and '='. Note that the POSIX standard does not explicitly state +/// ; that environment variable names cannot have NUL. However, this is implicit +/// ; in the fact that environment variables are passed to the program as +/// ; NUL-terminated `name=value` strings, which implies that the `name` portion of +/// ; the string cannot have NUL characters +/// posix-environment-variable-character = +/// %x5C ; '\' Beginning of escape sequence +/// ( %x22 ; '"' quotation mark U+0022 +/// / %x5C ; '\' reverse solidus U+005C +/// / %x61 ; 'a' alert U+0007 +/// / %x62 ; 'b' backspace U+0008 +/// / %x66 ; 'f' form feed U+000C +/// / %x6E ; 'n' line feed U+000A +/// / %x72 ; 'r' carriage return U+000D +/// / %x74 ; 't' tab U+0009 +/// / %x76 ; 'v' vertical tab U+000B +/// ) +/// ; Printable characters except double quote, backslash and equals +/// / %x20-21 +/// ; %x22 = '"' +/// / %x23-3C +/// ; %x3D = '=' +/// / %x3E-5B +/// ; %x5C = "\" +/// / %x5D-7E +/// +/// import-type = missing / local / http / env +/// +/// hash = %x73.68.61.32.35.36.3a 64HEXDIG whitespace ; "sha256:XXX...XXX" +/// +/// import-hashed = import-type [ hash ] +/// +/// ; "http://example.com" +/// ; "./foo/bar" +/// ; "env:FOO" +/// import = import-hashed [ as Text ] +/// +/// ; NOTE: Every rule past this point should only reference rules that end with +/// ; whitespace. This ensures consistent handling of whitespace in the absence of +/// ; a separate lexing step +/// +/// expression = +/// ; "\(x : a) -> b" +/// lambda open-parens label colon expression close-parens arrow expression +/// +/// ; "if a then b else c" +/// / if expression then expression else expression +/// +/// ; "let x : t = e1 in e2" +/// ; "let x = e1 in e2" +/// ; "let x = e1 let y = e2 in e3" +/// / 1*(let label [ colon expression ] equal expression) in expression +/// +/// ; "forall (x : a) -> b" +/// / forall open-parens label colon expression close-parens arrow expression +/// +/// ; "a -> b" +/// ; +/// ; NOTE: Backtrack if parsing this alternative fails +/// / operator-expression arrow expression +/// +/// / annotated-expression +expression = _{ annotated_expression } +/// +/// annotated-expression = +/// ; "merge e1 e2 : t" +/// ; "merge e1 e2" +/// merge import-expression import-expression [ colon application-expression ] +/// +/// ; "[] : List t" +/// ; "[] : Optional t" +/// ; "[x] : Optional t" +/// ; +/// ; NOTE: Backtrack if parsing this alternative fails since we can't tell +/// ; from the opening bracket whether or not this will be an empty list or +/// ; non-empty list +/// / open-bracket (empty-collection / non-empty-optional) +/// +/// ; "x : t" +/// / operator-expression (colon expression / "") +annotated_expression = _{ operator_expression ~ (colon ~ expression)? } +/// +/// empty-collection = close-bracket colon (List / Optional) import-expression +/// +/// non-empty-optional = expression close-bracket colon Optional import-expression +/// +/// operator-expression = import-alt-expression +operator_expression = _{ plus_expression } +/// +/// import-alt-expression = or-expression *(import-alt or-expression) +// import_alt_expression = { application_expression } +/// or-expression = plus-expression *(or plus-expression ) +/// plus-expression = text-append-expression *(plus text-append-expression ) +plus_expression = { times_expression ~ (plus ~ times_expression)* } +/// text-append-expression = list-append-expression *(text-append list-append-expression ) +/// list-append-expression = and-expression *(list-append and-expression ) +/// and-expression = combine-expression *(and combine-expression ) +/// combine-expression = prefer-expression *(combine prefer-expression ) +/// prefer-expression = combine-types-expression *(prefer combine-types-expression) +/// combine-types-expression = times-expression *(combine-types times-expression ) +/// times-expression = equal-expression *(times equal-expression ) +times_expression = { primitive_expression ~ (times ~ primitive_expression)* } +/// equal-expression = not-equal-expression *(double-equal not-equal-expression ) +/// not-equal-expression = application-expression *(not-equal application-expression ) +/// +/// ; Import expressions need to be separated by some whitespace, otherwise there +/// ; would be ambiguity: `./ab` could be interpreted as "import the file `./ab`", +/// ; or "apply the import `./a` to label `b`" +/// application-expression = +/// [ constructors / Some ] import-expression *(whitespace-chunk import-expression) +/// +/// import-expression = import / selector-expression +/// +/// ; `record.field` extracts one field of a record +/// ; +/// ; `record.{ field0, field1, field2 }` projects out several fields of a record +/// ; +/// ; NOTE: Backtrack when parsing the `*(dot ...)`. The reason why is that you +/// ; can't tell from parsing just the period whether "foo." will become "foo.bar" +/// ; (i.e. accessing field `bar` of the record `foo`) or `foo./bar` (i.e. applying +/// ; the function `foo` to the relative path `./bar`) +/// selector-expression = primitive-expression *(dot ( label / labels )) +/// +/// ; NOTE: Backtrack when parsing the first three alternatives (i.e. the numeric +/// ; literals). This is because they share leading characters in common +/// +/// ; NOTE: The reason why we have three different types of identifiers (that is: +/// ; identifier, identifier-reserved-prefix, identifier-reserved-namespaced-prefix) +/// ; is that it's the only way to parse correctly identifiers that start with reserved +/// ; words, other than using a lexer and use the longest match rule. +/// ; +/// ; Since reserved words can include themselves (e.g. 'List/build' includes 'List'), +/// ; we have to match the "namespaced" reserved words before the identifiers prefixed +/// ; by a reserved word. +/// primitive-expression = +/// ; "2.0" +/// double-literal +/// +/// ; "2" +/// / natural-literal +/// +/// ; "+2" +/// / integer-literal +/// +/// ; "-Infinity" +/// / "-" Infinity-raw whitespace +/// +/// ; '"ABC"' +/// / text-literal +/// +/// ; "{ foo = 1 , bar = True }" +/// ; "{ foo : Integer, bar : Bool }" +/// / open-brace record-type-or-literal close-brace +/// +/// ; "< Foo : Integer | Bar : Bool >" +/// ; "< Foo : Integer | Bar = True >" +/// / open-angle union-type-or-literal close-angle +/// +/// ; "[1, 2, 3]" +/// / non-empty-list-literal ; `annotated-expression` handles empty lists +/// +/// ; "List/foldWith" +/// / identifier-reserved-namespaced-prefix +/// +/// ; "List/head" +/// / reserved-namespaced +/// +/// ; "List/map" +/// ; "TypeDefinition" +/// / identifier-reserved-prefix +/// +/// ; "List" +/// / reserved +/// +/// ; "x" +/// ; "x@2" +/// / identifier +/// +/// ; "( e )" +/// / open-parens expression close-parens +primitive_expression = _{ + natural_literal + | open_parens ~ expression ~ close_parens +} +/// +/// labels = open-brace ( label *(comma label) / "" ) close-brace +/// +/// record-type-or-literal = +/// equal ; Empty record literal +/// / non-empty-record-type-or-literal +/// / "" ; Empty record type +/// +/// non-empty-record-type-or-literal = +/// label (non-empty-record-literal / non-empty-record-type) +/// +/// non-empty-record-type = colon expression *(comma label colon expression) +/// non-empty-record-literal = equal expression *(comma label equal expression) +/// +/// union-type-or-literal = +/// non-empty-union-type-or-literal +/// / "" ; Empty union type +/// +/// non-empty-union-type-or-literal = +/// label +/// ( equal expression *(bar label colon expression) +/// / colon expression (bar non-empty-union-type-or-literal / "") +/// ) +/// +/// non-empty-list-literal = open-bracket expression *(comma expression) close-bracket +/// +/// ; All expressions end with trailing whitespace. This just adds a final +/// ; whitespace prefix for the top-level of the program +/// complete-expression = whitespace expression + +complete_expression = _{ SOI ~ whitespace ~ expression ~ EOI } diff --git a/src/parser.rs b/src/parser.rs index 8416d9b..a0281f4 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -3,6 +3,7 @@ use lalrpop_util; use crate::grammar; use crate::grammar_util::BoxExpr; use crate::lexer::{Lexer, LexicalError, Tok}; +use crate::core::{bx, Expr}; pub type ParseError<'i> = lalrpop_util::ParseError, LexicalError>; @@ -10,9 +11,64 @@ pub fn parse_expr(s: &str) -> Result { grammar::ExprParser::new().parse(Lexer::new(s)) } +use pest::Parser; +use pest::error::Error; +use pest_derive::*; + +#[derive(Parser)] +#[grammar = "dhall.pest"] +struct DhallParser; + +use pest::iterators::Pair; +fn debug_pair(pair: Pair) { + fn aux(indent: usize, pair: Pair) { + let indent_str = "| ".repeat(indent); + println!(r#"{}{:?}: "{}""#, indent_str, pair.as_rule(), pair.as_str()); + for p in pair.into_inner() { + aux(indent+1, p); + } + } + aux(0, pair) +} + +pub fn parse_expr_pest(s: &str) -> Result> { + let parsed_expr = DhallParser::parse(Rule::complete_expression, s)?.next().unwrap(); + debug_pair(parsed_expr.clone()); + // println!("{}", parsed_expr.clone()); + + fn parse_pair(pair: Pair) -> BoxExpr { + match pair.as_rule() { + Rule::natural_literal => bx(Expr::NaturalLit(str::parse(pair.as_str().trim()).unwrap())), + Rule::plus_expression => { + let mut inner = pair.into_inner().map(parse_pair); + let first_expr = inner.next().unwrap(); + inner.fold(first_expr, |acc, e| bx(Expr::NaturalPlus(acc, e))) + } + Rule::times_expression => { + let mut inner = pair.into_inner().map(parse_pair); + let first_expr = inner.next().unwrap(); + inner.fold(first_expr, |acc, e| bx(Expr::NaturalTimes(acc, e))) + } + r => panic!("{:?}", r), + } + } + + Ok(parse_pair(parsed_expr)) +} + + #[test] fn test_parse() { use crate::core::Expr::*; + let expr = "((22 + 3) * 10)"; + match parse_expr_pest(expr) { + Err(e) => println!("{}", e), + ok => println!("{:?}", ok), + } + println!("{:?}", parse_expr(expr)); + assert_eq!(parse_expr_pest(expr).unwrap(), parse_expr(expr).unwrap()); + assert!(false); + println!("test {:?}", parse_expr("3 + 5 * 10")); assert!(parse_expr("22").is_ok()); assert!(parse_expr("(22)").is_ok()); @@ -32,4 +88,8 @@ fn test_parse() { println!("{:?}", parse_expr("foo.bar")); assert!(parse_expr("foo.bar").is_ok()); assert!(parse_expr("[] : List Bool").is_ok()); + + // println!("{:?}", parse_expr("< Left = True | Right : Natural >")); + // println!("{:?}", parse_expr(r#""bl${42}ah""#)); + // assert!(parse_expr("< Left = True | Right : Natural >").is_ok()); } diff --git a/tests/macros.rs b/tests/macros.rs index 3e8f111..4b6d0d7 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -11,10 +11,14 @@ macro_rules! include_test_strs_ab { #[macro_export] macro_rules! run_spec_test { (normalization, $path:expr) => { - let (expr_str, expected_str) = include_test_strs_ab!($path); - let expr = parser::parse_expr(&expr_str).unwrap(); - let expected = parser::parse_expr(&expected_str).unwrap(); - assert_eq!(normalize::<_, X, _>(&expr), normalize::<_, X, _>(&expected)); + // let (expr_str, expected_str) = include_test_strs_ab!($path); + // let expr = parser::parse_expr(&expr_str).unwrap(); + // let expected = parser::parse_expr(&expected_str).unwrap(); + // assert_eq!(normalize::<_, X, _>(&expr), normalize::<_, X, _>(&expected)); + }; + (parser, $path:expr) => { + let expr_str = include_test_str!(concat!($path, "A")); + parser::parse_expr(&expr_str).unwrap(); }; } @@ -23,6 +27,8 @@ macro_rules! make_spec_test { ($type:ident, $name:ident, $path:expr) => { #[test] #[allow(non_snake_case)] + #[allow(unused_variables)] + #[allow(unused_imports)] fn $name(){ use dhall::*; run_spec_test!($type, $path); diff --git a/tests/tests.rs b/tests/tests.rs index 2f59425..6a2ada8 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -144,7 +144,7 @@ make_spec_test!(normalization, spec_normalization_success_haskell_tutorial_acces // make_spec_test!(normalization, spec_normalization_success_remoteSystems, "normalization/success/remoteSystems"); // make_spec_test!(normalization, spec_normalization_success_simple_constructorsId, "normalization/success/simple/constructorsId"); // make_spec_test!(normalization, spec_normalization_success_simple_doubleShow, "normalization/success/simple/doubleShow"); -// make_spec_test!(normalization, spec_normalization_success_simple_integerShow, "normalization/success/simple/integerShow"); +make_spec_test!(normalization, spec_normalization_success_simple_integerShow, "normalization/success/simple/integerShow"); // make_spec_test!(normalization, spec_normalization_success_simple_integerToDouble, "normalization/success/simple/integerToDouble"); // make_spec_test!(normalization, spec_normalization_success_simple_letlet, "normalization/success/simple/letlet"); // make_spec_test!(normalization, spec_normalization_success_simple_listBuild, "normalization/success/simple/listBuild"); @@ -152,13 +152,62 @@ make_spec_test!(normalization, spec_normalization_success_haskell_tutorial_acces // make_spec_test!(normalization, spec_normalization_success_simple_naturalBuild, "normalization/success/simple/naturalBuild"); make_spec_test!(normalization, spec_normalization_success_simple_naturalPlus, "normalization/success/simple/naturalPlus"); make_spec_test!(normalization, spec_normalization_success_simple_naturalShow, "normalization/success/simple/naturalShow"); -// make_spec_test!(normalization, spec_normalization_success_simple_naturalToInteger, "normalization/success/simple/naturalToInteger"); +make_spec_test!(normalization, spec_normalization_success_simple_naturalToInteger, "normalization/success/simple/naturalToInteger"); // make_spec_test!(normalization, spec_normalization_success_simple_optionalBuild, "normalization/success/simple/optionalBuild"); -// make_spec_test!(normalization, spec_normalization_success_simple_optionalBuildFold, "normalization/success/simple/optionalBuildFold"); -// make_spec_test!(normalization, spec_normalization_success_simple_optionalFold, "normalization/success/simple/optionalFold"); +make_spec_test!(normalization, spec_normalization_success_simple_optionalBuildFold, "normalization/success/simple/optionalBuildFold"); +make_spec_test!(normalization, spec_normalization_success_simple_optionalFold, "normalization/success/simple/optionalFold"); // make_spec_test!(normalization, spec_normalization_success_simple_sortOperator, "normalization/success/simple/sortOperator"); -// make_spec_test!(normalization, spec_normalization_success_simplifications_and, "normalization/success/simplifications/and"); -// make_spec_test!(normalization, spec_normalization_success_simplifications_eq, "normalization/success/simplifications/eq"); -// make_spec_test!(normalization, spec_normalization_success_simplifications_ifThenElse, "normalization/success/simplifications/ifThenElse"); -// make_spec_test!(normalization, spec_normalization_success_simplifications_ne, "normalization/success/simplifications/ne"); -// make_spec_test!(normalization, spec_normalization_success_simplifications_or, "normalization/success/simplifications/or"); +make_spec_test!(normalization, spec_normalization_success_simplifications_and, "normalization/success/simplifications/and"); +make_spec_test!(normalization, spec_normalization_success_simplifications_eq, "normalization/success/simplifications/eq"); +make_spec_test!(normalization, spec_normalization_success_simplifications_ifThenElse, "normalization/success/simplifications/ifThenElse"); +make_spec_test!(normalization, spec_normalization_success_simplifications_ne, "normalization/success/simplifications/ne"); +make_spec_test!(normalization, spec_normalization_success_simplifications_or, "normalization/success/simplifications/or"); + + +make_spec_test!(parser, spec_parser_success_annotations, "parser/success/annotations"); +make_spec_test!(parser, spec_parser_success_asText, "parser/success/asText"); +make_spec_test!(parser, spec_parser_success_blockComment, "parser/success/blockComment"); +make_spec_test!(parser, spec_parser_success_builtins, "parser/success/builtins"); +make_spec_test!(parser, spec_parser_success_collectionImportType, "parser/success/collectionImportType"); +make_spec_test!(parser, spec_parser_success_constructors, "parser/success/constructors"); +make_spec_test!(parser, spec_parser_success_double, "parser/success/double"); +make_spec_test!(parser, spec_parser_success_doubleQuotedString, "parser/success/doubleQuotedString"); +make_spec_test!(parser, spec_parser_success_environmentVariables, "parser/success/environmentVariables"); +make_spec_test!(parser, spec_parser_success_escapedDoubleQuotedString, "parser/success/escapedDoubleQuotedString"); +make_spec_test!(parser, spec_parser_success_escapedSingleQuotedString, "parser/success/escapedSingleQuotedString"); +make_spec_test!(parser, spec_parser_success_fields, "parser/success/fields"); +make_spec_test!(parser, spec_parser_success_forall, "parser/success/forall"); +make_spec_test!(parser, spec_parser_success_functionType, "parser/success/functionType"); +make_spec_test!(parser, spec_parser_success_identifier, "parser/success/identifier"); +make_spec_test!(parser, spec_parser_success_ifThenElse, "parser/success/ifThenElse"); +make_spec_test!(parser, spec_parser_success_importAlt, "parser/success/importAlt"); +make_spec_test!(parser, spec_parser_success_interpolatedDoubleQuotedString, "parser/success/interpolatedDoubleQuotedString"); +make_spec_test!(parser, spec_parser_success_interpolatedSingleQuotedString, "parser/success/interpolatedSingleQuotedString"); +make_spec_test!(parser, spec_parser_success_label, "parser/success/label"); +make_spec_test!(parser, spec_parser_success_lambda, "parser/success/lambda"); +make_spec_test!(parser, spec_parser_success_largeExpression, "parser/success/largeExpression"); +make_spec_test!(parser, spec_parser_success_let, "parser/success/let"); +make_spec_test!(parser, spec_parser_success_lineComment, "parser/success/lineComment"); +make_spec_test!(parser, spec_parser_success_list, "parser/success/list"); +make_spec_test!(parser, spec_parser_success_merge, "parser/success/merge"); +make_spec_test!(parser, spec_parser_success_multilet, "parser/success/multilet"); +make_spec_test!(parser, spec_parser_success_natural, "parser/success/natural"); +make_spec_test!(parser, spec_parser_success_nestedBlockComment, "parser/success/nestedBlockComment"); +make_spec_test!(parser, spec_parser_success_operators, "parser/success/operators"); +make_spec_test!(parser, spec_parser_success_parenthesizeUsing, "parser/success/parenthesizeUsing"); +make_spec_test!(parser, spec_parser_success_pathTermination, "parser/success/pathTermination"); +make_spec_test!(parser, spec_parser_success_paths, "parser/success/paths"); +make_spec_test!(parser, spec_parser_success_quotedLabel, "parser/success/quotedLabel"); +make_spec_test!(parser, spec_parser_success_quotedPaths, "parser/success/quotedPaths"); +make_spec_test!(parser, spec_parser_success_record, "parser/success/record"); +make_spec_test!(parser, spec_parser_success_reservedPrefix, "parser/success/reservedPrefix"); +make_spec_test!(parser, spec_parser_success_singleQuotedString, "parser/success/singleQuotedString"); +make_spec_test!(parser, spec_parser_success_sort, "parser/success/sort"); +make_spec_test!(parser, spec_parser_success_template, "parser/success/template"); +make_spec_test!(parser, spec_parser_success_unicodeComment, "parser/success/unicodeComment"); +make_spec_test!(parser, spec_parser_success_unicodeDoubleQuotedString, "parser/success/unicodeDoubleQuotedString"); +make_spec_test!(parser, spec_parser_success_unicodePaths, "parser/success/unicodePaths"); +make_spec_test!(parser, spec_parser_success_union, "parser/success/union"); +make_spec_test!(parser, spec_parser_success_urls, "parser/success/urls"); +make_spec_test!(parser, spec_parser_success_whitespace, "parser/success/whitespace"); +make_spec_test!(parser, spec_parser_success_whitespaceBuffet, "parser/success/whitespaceBuffet"); -- cgit v1.2.3