From 713ece847de3c36e6658102beb917dcd9aeb3494 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 12 Dec 2023 13:57:47 -0300 Subject: [PATCH] Playing with Pest (again) --- pestgroupstest/Cargo.lock | 212 ++++++++++++++++++++++++++++++++++++ pestgroupstest/Cargo.toml | 10 ++ pestgroupstest/README.md | 3 + pestgroupstest/src/csv.pest | 7 ++ pestgroupstest/src/main.rs | 29 +++++ 5 files changed, 261 insertions(+) create mode 100644 pestgroupstest/Cargo.lock create mode 100644 pestgroupstest/Cargo.toml create mode 100644 pestgroupstest/README.md create mode 100644 pestgroupstest/src/csv.pest create mode 100644 pestgroupstest/src/main.rs diff --git a/pestgroupstest/Cargo.lock b/pestgroupstest/Cargo.lock new file mode 100644 index 0000000..11fe2e7 --- /dev/null +++ b/pestgroupstest/Cargo.lock @@ -0,0 +1,212 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cpufeatures" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce420fe07aecd3e67c5f910618fe65e94158f6dcc0adf44e00d69ce2bdfe0fd0" +dependencies = [ + "libc", +] + +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "libc" +version = "0.2.151" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "302d7ab3130588088d277783b1e2d2e10c9e9e4a16dd9050e6ec93fb3e7048f4" + +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "pest" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae9cee2a55a544be8b89dc6848072af97a20f2422603c10865be2a42b580fff5" +dependencies = [ + "memchr", + "thiserror", + "ucd-trie", +] + +[[package]] +name = "pest_derive" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81d78524685f5ef2a3b3bd1cafbc9fcabb036253d9b1463e726a91cd16e2dfc2" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68bd1206e71118b5356dae5ddc61c8b11e28b09ef6a31acbd15ea48a28e0c227" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pest_meta" +version = "2.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7c747191d4ad9e4a4ab9c8798f1e82a39affe7ef9648390b7e5548d18e099de6" +dependencies = [ + "once_cell", + "pest", + "sha2", +] + +[[package]] +name = "pestgroupstest" +version = "0.1.0" +dependencies = [ + "pest", + "pest_derive", +] + +[[package]] +name = "proc-macro2" +version = "1.0.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + +[[package]] +name = "syn" +version = "2.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13fa70a4ee923979ffb522cacce59d34421ebdea5625e1073c4326ef9d2dd42e" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "thiserror" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.50" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + +[[package]] +name = "ucd-trie" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9" + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "version_check" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" diff --git a/pestgroupstest/Cargo.toml b/pestgroupstest/Cargo.toml new file mode 100644 index 0000000..19a9c71 --- /dev/null +++ b/pestgroupstest/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "pestgroupstest" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +pest = "2.7.5" +pest_derive = "2.7.5" diff --git a/pestgroupstest/README.md b/pestgroupstest/README.md new file mode 100644 index 0000000..343a28d --- /dev/null +++ b/pestgroupstest/README.md @@ -0,0 +1,3 @@ +# PestGroupTest + +Testing using Pest to grab groups of values diff --git a/pestgroupstest/src/csv.pest b/pestgroupstest/src/csv.pest new file mode 100644 index 0000000..875e5ba --- /dev/null +++ b/pestgroupstest/src/csv.pest @@ -0,0 +1,7 @@ +csv = { SOI ~ line ~ EOI } + +line = _{ (value | group)+ } +value = @{ (ASCII_ALPHANUMERIC | ".")+ } +group = { "(" ~ line ~ ")" } + +WHITESPACE = _{ (" " | "\t") } diff --git a/pestgroupstest/src/main.rs b/pestgroupstest/src/main.rs new file mode 100644 index 0000000..325a8e9 --- /dev/null +++ b/pestgroupstest/src/main.rs @@ -0,0 +1,29 @@ +use pest::Parser; +use pest_derive::Parser; + +#[derive(Parser)] +#[grammar = "csv.pest"] +struct CsvParser; + +fn main() { + let line = "(1 2 3) 123 123 (123 (123))"; + let mut parser = CsvParser::parse(Rule::csv, &line).unwrap(); + let _csv = parser.next().unwrap(); + let worker = _csv + .into_inner() + .filter_map(|pair| extract(pair)) + .collect::>(); + println!("{:?}", worker); +} + +fn extract(pair: pest::iterators::Pair<'_, Rule>) -> Option> { + match pair.as_rule() { + Rule::value => Some(vec![pair.as_str()]), + Rule::group => { + let local = pair.into_inner().filter_map(|pair| extract(pair)).flatten().collect::>(); + // println!("{:?}", local); + Some(local) + } + _ => None, + } +}