From 9835e2ae736235810b4ea1c162ca5e65c547e770 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 18 May 2024 04:49:50 +0200 Subject: Merging upstream version 1.71.1+dfsg1. Signed-off-by: Daniel Baumann --- vendor/pest/.cargo-checksum.json | 2 +- vendor/pest/Cargo.lock | 49 +++++++-------- vendor/pest/Cargo.toml | 10 +--- vendor/pest/_README.md | 25 ++++++++ vendor/pest/src/iterators/flat_pairs.rs | 43 ++++++++++++- vendor/pest/src/iterators/line_index.rs | 91 ++++++++++++++++++++++++++++ vendor/pest/src/iterators/mod.rs | 1 + vendor/pest/src/iterators/pair.rs | 26 +++++++- vendor/pest/src/iterators/pairs.rs | 60 ++++++++++++++++++- vendor/pest/src/lib.rs | 1 + vendor/pest/src/macros.rs | 2 + vendor/pest/src/parser_state.rs | 2 +- vendor/pest/src/position.rs | 103 ++++++++++++-------------------- vendor/pest/src/pratt_parser.rs | 26 ++++---- 14 files changed, 318 insertions(+), 123 deletions(-) create mode 100644 vendor/pest/src/iterators/line_index.rs (limited to 'vendor/pest') diff --git a/vendor/pest/.cargo-checksum.json b/vendor/pest/.cargo-checksum.json index 2890a9340..d757c410a 100644 --- a/vendor/pest/.cargo-checksum.json +++ b/vendor/pest/.cargo-checksum.json @@ -1 +1 @@ -{"files":{"Cargo.lock":"f8597669578e461a24cee07cd0fdec146fe8c8b0000d1efecb2092777ca60c53","Cargo.toml":"cde69a02acc2c12c7ee7a0adf17cdfec45d0580af71d0122ff3c8640b5cebbd3","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","_README.md":"190b4279777e3cd3c5adf3258114b89be536fc647d819620ada15be39d2259cc","examples/parens.rs":"2b022b5ed1fbc9b41a4359e3b79648317415217768f88234c3e3efc0db34bcce","src/error.rs":"7db24da89a3811cc4372a04cbc2dcfb7642a0ccf2702cfe5c027cfa45cb18ebd","src/iterators/flat_pairs.rs":"6884ebff7052fdd7ee90470b0621745363cad0bfb78f67e75c0b8a8a29ef0e20","src/iterators/mod.rs":"dfb9cc6d6c0c96f7365ec69500128e3547c0b15dd4e85a5d36e125649244d57a","src/iterators/pair.rs":"b4eb0a842ad7bbb643f1cc9950438e825f4ea0a8b298ff12283302694cffd97a","src/iterators/pairs.rs":"6b7cadf1f49b845e6cf959ebab1f8e05e28eb70b533d15a059b2bb74a08aa492","src/iterators/queueable_token.rs":"b6c4fd1d43437ea7c6cab45717c1fb66f71fc91aaf3ed9cf9f5686fd8ccfa8f9","src/iterators/tokens.rs":"487eb495f3713df56ae33fb664624f3d396d7f6d4aa872ede33413d561c5d21c","src/lib.rs":"727bca108ff5a01a6ce81e7028753f134d847123993b0ceffce850ec3ba7e45e","src/macros.rs":"18ae0d9b31701662b8b17784b63fc33f1277493b7b4513db937ba9e8020ef57f","src/parser.rs":"3c453a737eac42b113c91dba459c49d8417fcc46aa2d8d3d3297d29de079cb42","src/parser_state.rs":"ccdb519ddf27caf3dafd81a3d547cf8fde3b6d29e066c687eeeecbf1e5ae8e5f","src/position.rs":"be59dc9e949caca76f19b85e0da67a2dbe1438b262de3e9d33d81749b47af5ce","src/pratt_parser.rs":"0dfb3ffd9ac368bb7c70b8d911078583144d5ed3aaad13cfa03d7bb45c765360","src/prec_climber.rs":"e068f7abe105d39f56ba057e32c32450793b4180d243640cb1008c61f588ce48","src/span.rs":"ec467df8a54982bf747c55c43f825c349787353409bac1e6cddde461f8dfae26","src/stack.rs":"97b6fe59e60941c1388fa5fecc2a4fbf78e1cc7f1a3a7c4848cb68ae9d187628","src/token.rs":"8c3a09c7f9a5c059f94900cd8c16b3eb17f893154f395e2f1cfa1a472cfd2ba6","src/unicode/binary.rs":"16dbd15ca792f80ce470f58480dd3f69a71785f781e6bd83eb49a6763ab13e09","src/unicode/category.rs":"6d9ed866aceb905f30b79248d46820762674ffc67661df3cbf4a2d1d7d3aa374","src/unicode/mod.rs":"e45485c756fa9205571e4c5ba7c3a86793dc8868c2761ba3991abcc059ff5907","src/unicode/script.rs":"50fa653d154d00925119f836796d18249997b57655188f3c87dc3ddadd74df43","tests/calculator.rs":"b6045cf150ed62c14d848c8f439065e943f68efd088f62f5ff8fedceff435d7a","tests/json.rs":"74d222d9255d211d835e8215a74a002ba1ce8259d0d1106dc63c2bfd4c087ee5"},"package":"0f6e86fb9e7026527a0d46bc308b841d73170ef8f443e1807f6ef88526a816d4"} \ No newline at end of file +{"files":{"Cargo.lock":"0136178e8376039afab5a8cb6987cb1f67f3b0dcd65baa0466fc5171c17695a8","Cargo.toml":"fdfa43fbe47568456de975a22602558fb1b7c31402c79198e25577400721ceb4","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","_README.md":"647dbcab0b7ed9837cc066cb3731aa2db7fbecb0e9aa187995a0a7a03af3938d","examples/parens.rs":"2b022b5ed1fbc9b41a4359e3b79648317415217768f88234c3e3efc0db34bcce","src/error.rs":"7db24da89a3811cc4372a04cbc2dcfb7642a0ccf2702cfe5c027cfa45cb18ebd","src/iterators/flat_pairs.rs":"2ff507a2db033fed38711cbe5fa9a72f115ff24c427a94aa3dd2725c736fcea0","src/iterators/line_index.rs":"19729b5da43527a52910908d67f406f137bced5ab535e879a26032f5b97f9f48","src/iterators/mod.rs":"b8ac4e586c7b49403c694ceded242a9ed9c499d995e3db2df19b27aaff3d4e4d","src/iterators/pair.rs":"dbe805e6aba154c9275ae7beb70c7508acb426daad688dd5f26ab39a0b27ca97","src/iterators/pairs.rs":"5245b5f7c2997e34874114e57c008db9f51519d7cd32a3a0c9729bc9a37bf795","src/iterators/queueable_token.rs":"b6c4fd1d43437ea7c6cab45717c1fb66f71fc91aaf3ed9cf9f5686fd8ccfa8f9","src/iterators/tokens.rs":"487eb495f3713df56ae33fb664624f3d396d7f6d4aa872ede33413d561c5d21c","src/lib.rs":"ceecf1e44c6e4cb44383d9e41d93d6744d6924d41d6fd2826757415ca9dea2d4","src/macros.rs":"95637b87ecad9db34e3b23985d32ef56a0ea1f99f729281f9c2afe8057d0ea61","src/parser.rs":"3c453a737eac42b113c91dba459c49d8417fcc46aa2d8d3d3297d29de079cb42","src/parser_state.rs":"91cc987cf6fdbf449969a4ee3818990e3c26d9423c384b5c1cac5849784b8d2e","src/position.rs":"b0418c3db0836d44429d2d4659bf1335150ab22251f0205cb23e38449d2ed8b5","src/pratt_parser.rs":"d2df2a965e1dab123a1a2d3065c4c2c37c5b96d607464be442534bde86face31","src/prec_climber.rs":"e068f7abe105d39f56ba057e32c32450793b4180d243640cb1008c61f588ce48","src/span.rs":"ec467df8a54982bf747c55c43f825c349787353409bac1e6cddde461f8dfae26","src/stack.rs":"97b6fe59e60941c1388fa5fecc2a4fbf78e1cc7f1a3a7c4848cb68ae9d187628","src/token.rs":"8c3a09c7f9a5c059f94900cd8c16b3eb17f893154f395e2f1cfa1a472cfd2ba6","src/unicode/binary.rs":"16dbd15ca792f80ce470f58480dd3f69a71785f781e6bd83eb49a6763ab13e09","src/unicode/category.rs":"6d9ed866aceb905f30b79248d46820762674ffc67661df3cbf4a2d1d7d3aa374","src/unicode/mod.rs":"e45485c756fa9205571e4c5ba7c3a86793dc8868c2761ba3991abcc059ff5907","src/unicode/script.rs":"50fa653d154d00925119f836796d18249997b57655188f3c87dc3ddadd74df43","tests/calculator.rs":"b6045cf150ed62c14d848c8f439065e943f68efd088f62f5ff8fedceff435d7a","tests/json.rs":"74d222d9255d211d835e8215a74a002ba1ce8259d0d1106dc63c2bfd4c087ee5"},"package":"7b1403e8401ad5dedea73c626b99758535b342502f8d1e361f4a2dd952749122"} \ No newline at end of file diff --git a/vendor/pest/Cargo.lock b/vendor/pest/Cargo.lock index 49bba4a85..4014d8a18 100644 --- a/vendor/pest/Cargo.lock +++ b/vendor/pest/Cargo.lock @@ -2,17 +2,11 @@ # It is not intended for manual editing. version = 3 -[[package]] -name = "bytecount" -version = "0.6.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c676a478f63e9fa2dd5368a42f28bba0d6c560b775f38583c8bbaa7fcd67c9c" - [[package]] name = "itoa" -version = "1.0.5" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440" +checksum = "453ad9f582a441959e5f0d088b02ce04cfe8d51a8eaf077f12ac6d3e94164ca6" [[package]] name = "memchr" @@ -22,9 +16,8 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "pest" -version = "2.5.2" +version = "2.5.7" dependencies = [ - "bytecount", "memchr", "serde", "serde_json", @@ -34,39 +27,39 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.49" +version = "1.0.54" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "57a8eca9f9c4ffde41714334dee777596264c7825420f521abc92b5b5deb63a5" +checksum = "e472a104799c74b514a57226160104aa483546de37e839ec50e3c2e41dd87534" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.23" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8856d8364d252a14d474036ea1358d63c9e6965c8e5c1885c18f73d70bff9c7b" +checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" dependencies = [ "proc-macro2", ] [[package]] name = "ryu" -version = "1.0.12" +version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" +checksum = "f91339c0467de62360649f8d3e185ca8de4224ff281f66000de5eb2a77a79041" [[package]] name = "serde" -version = "1.0.151" +version = "1.0.159" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97fed41fc1a24994d044e6db6935e69511a1153b52c15eb42493b26fa87feba0" +checksum = "3c04e8343c3daeec41f58990b9d77068df31209f2af111e059e9fe9646693065" [[package]] name = "serde_json" -version = "1.0.91" +version = "1.0.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883" +checksum = "d721eca97ac802aa7777b701877c8004d950fc142651367300d21c1cc0194744" dependencies = [ "itoa", "ryu", @@ -75,9 +68,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.107" +version = "2.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f4064b5b16e03ae50984a5a8ed5d4f8803e6bc1fd170a3cda91a1be4b18e3f5" +checksum = "79d9531f94112cfc3e4c8f5f02cb2b58f72c97b7efd85f70203cc6d8efda5927" dependencies = [ "proc-macro2", "quote", @@ -86,18 +79,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.38" +version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a9cd18aa97d5c45c6603caea1da6628790b37f7a34b6ca89522331c5180fed0" +checksum = "978c9a314bd8dc99be594bc3c175faaa9794be04a5a5e153caba6915336cebac" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.38" +version = "1.0.40" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fb327af4685e4d03fa8cbcf1716380da910eeb2bb8be417e7f9fd3fb164f36f" +checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f" dependencies = [ "proc-macro2", "quote", @@ -112,6 +105,6 @@ checksum = "9e79c4d996edb816c91e4308506774452e55e95c3c9de07b6729e17e15a5ef81" [[package]] name = "unicode-ident" -version = "1.0.6" +version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" +checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4" diff --git a/vendor/pest/Cargo.toml b/vendor/pest/Cargo.toml index 29e54b46f..9b477863b 100644 --- a/vendor/pest/Cargo.toml +++ b/vendor/pest/Cargo.toml @@ -13,7 +13,7 @@ edition = "2021" rust-version = "1.56" name = "pest" -version = "2.5.2" +version = "2.5.7" authors = ["Dragoș Tiselice "] description = "The Elegant Parser" homepage = "https://pest.rs/" @@ -29,10 +29,6 @@ categories = ["parsing"] license = "MIT/Apache-2.0" repository = "https://github.com/pest-parser/pest" -[dependencies.bytecount] -version = "0.6" -optional = true - [dependencies.memchr] version = "2" optional = true @@ -56,10 +52,6 @@ default-features = false [features] const_prec_climber = [] default = ["std"] -fast-line-col = [ - "memchr", - "bytecount", -] pretty-print = [ "serde", "serde_json", diff --git a/vendor/pest/_README.md b/vendor/pest/_README.md index da30ab74e..cb8055eb0 100644 --- a/vendor/pest/_README.md +++ b/vendor/pest/_README.md @@ -167,6 +167,8 @@ mod b { ## Projects using pest +You can find more projects and ecosystem tools in the [awesome-pest](https://github.com/pest-parser/awesome-pest) repo. + * [pest_meta](https://github.com/pest-parser/pest/blob/master/meta/src/grammar.pest) (bootstrapped) * [AshPaper](https://github.com/shnewto/ashpaper) * [brain](https://github.com/brain-lang/brain) @@ -203,6 +205,29 @@ mod b { This library should always compile with default features on **Rust 1.56.1** or **Rust 1.61** with `const_prec_climber`. +## no_std support + +The `pest` and `pest_derive` crates can be built without the Rust standard +library and target embedded environments. To do so, you need to disable +their default features. In your `Cargo.toml`, you can specify it as follows: + +```toml +[dependencies] +# ... +pest = { version = "2", default-features = false } +pest_derive = { version = "2", default-features = false } +``` + +If you want to build these crates in the pest repository's workspace, you can +pass the `--no-default-features` flag to `cargo` and specify these crates using +the `--package` (`-p`) flag. For example: + +```bash +$ cargo build --target thumbv7em-none-eabihf --no-default-features -p pest +$ cargo bootstrap +$ cargo build --target thumbv7em-none-eabihf --no-default-features -p pest_derive +``` + ## Special thanks A special round of applause goes to prof. Marius Minea for his guidance and all diff --git a/vendor/pest/src/iterators/flat_pairs.rs b/vendor/pest/src/iterators/flat_pairs.rs index 6d310272e..52a207406 100644 --- a/vendor/pest/src/iterators/flat_pairs.rs +++ b/vendor/pest/src/iterators/flat_pairs.rs @@ -11,6 +11,7 @@ use alloc::rc::Rc; use alloc::vec::Vec; use core::fmt; +use super::line_index::LineIndex; use super::pair::{self, Pair}; use super::queueable_token::QueueableToken; use super::tokens::{self, Tokens}; @@ -28,6 +29,7 @@ pub struct FlatPairs<'i, R> { input: &'i str, start: usize, end: usize, + line_index: Rc, } /// # Safety @@ -42,6 +44,7 @@ pub unsafe fn new( FlatPairs { queue, input, + line_index: Rc::new(LineIndex::new(input)), start, end, } @@ -107,8 +110,14 @@ impl<'i, R: RuleType> Iterator for FlatPairs<'i, R> { return None; } - let pair = unsafe { pair::new(Rc::clone(&self.queue), self.input, self.start) }; - + let pair = unsafe { + pair::new( + Rc::clone(&self.queue), + self.input, + Rc::clone(&self.line_index), + self.start, + ) + }; self.next_start(); Some(pair) @@ -123,7 +132,14 @@ impl<'i, R: RuleType> DoubleEndedIterator for FlatPairs<'i, R> { self.next_start_from_end(); - let pair = unsafe { pair::new(Rc::clone(&self.queue), self.input, self.end) }; + let pair = unsafe { + pair::new( + Rc::clone(&self.queue), + self.input, + Rc::clone(&self.line_index), + self.end, + ) + }; Some(pair) } @@ -142,6 +158,7 @@ impl<'i, R: Clone> Clone for FlatPairs<'i, R> { FlatPairs { queue: Rc::clone(&self.queue), input: self.input, + line_index: Rc::clone(&self.line_index), start: self.start, end: self.end, } @@ -177,4 +194,24 @@ mod tests { vec![Rule::c, Rule::b, Rule::a] ); } + + #[test] + fn test_line_col() { + let mut pairs = AbcParser::parse(Rule::a, "abcNe\nabcde").unwrap().flatten(); + + let pair = pairs.next().unwrap(); + assert_eq!(pair.as_str(), "abc"); + assert_eq!(pair.line_col(), (1, 1)); + assert_eq!(pair.line_col(), pair.as_span().start_pos().line_col()); + + let pair = pairs.next().unwrap(); + assert_eq!(pair.as_str(), "b"); + assert_eq!(pair.line_col(), (1, 2)); + assert_eq!(pair.line_col(), pair.as_span().start_pos().line_col()); + + let pair = pairs.next().unwrap(); + assert_eq!(pair.as_str(), "e"); + assert_eq!(pair.line_col(), (1, 5)); + assert_eq!(pair.line_col(), pair.as_span().start_pos().line_col()); + } } diff --git a/vendor/pest/src/iterators/line_index.rs b/vendor/pest/src/iterators/line_index.rs new file mode 100644 index 000000000..54871e1bd --- /dev/null +++ b/vendor/pest/src/iterators/line_index.rs @@ -0,0 +1,91 @@ +//! `LineIndex` to make a line_offsets, each item is an byte offset (start from 0) of the beginning of the line. +//! +//! For example, the text: `"hello 你好\nworld"`, the line_offsets will store `[0, 13]`. +//! +//! Then `line_col` with a offset just need to find the line index by binary search. +//! +//! Inspired by rust-analyzer's `LineIndex`: +//! +use alloc::vec::Vec; + +#[derive(Clone)] +pub struct LineIndex { + /// Offset (bytes) the the beginning of each line, zero-based + line_offsets: Vec, +} + +impl LineIndex { + pub fn new(text: &str) -> LineIndex { + let mut line_offsets: Vec = alloc::vec![0]; + + let mut offset = 0; + + for c in text.chars() { + offset += c.len_utf8(); + if c == '\n' { + line_offsets.push(offset); + } + } + + LineIndex { line_offsets } + } + + /// Returns (line, col) of pos. + /// + /// The pos is a byte offset, start from 0, e.g. "ab" is 2, "你好" is 6 + pub fn line_col(&self, input: &str, pos: usize) -> (usize, usize) { + let line = self.line_offsets.partition_point(|&it| it <= pos) - 1; + let first_offset = self.line_offsets[line]; + + // Get line str from original input, then we can get column offset + let line_str = &input[first_offset..pos]; + let col = line_str.chars().count(); + + (line + 1, col + 1) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[allow(clippy::zero_prefixed_literal)] + #[test] + fn test_line_index() { + let text = "hello 你好 A🎈C\nworld"; + let table = [ + (00, 1, 1, 'h'), + (01, 1, 2, 'e'), + (02, 1, 3, 'l'), + (03, 1, 4, 'l'), + (04, 1, 5, 'o'), + (05, 1, 6, ' '), + (06, 1, 7, '你'), + (09, 1, 8, '好'), + (12, 1, 9, ' '), + (13, 1, 10, 'A'), + (14, 1, 11, '🎈'), + (18, 1, 12, 'C'), + (19, 1, 13, '\n'), + (20, 2, 1, 'w'), + (21, 2, 2, 'o'), + (22, 2, 3, 'r'), + (23, 2, 4, 'l'), + (24, 2, 5, 'd'), + ]; + + let index = LineIndex::new(text); + for &(offset, line, col, c) in table.iter() { + let res = index.line_col(text, offset); + assert_eq!( + (res.0, res.1), + (line, col), + "Expected: ({}, {}, {}, {:?})", + offset, + line, + col, + c + ); + } + } +} diff --git a/vendor/pest/src/iterators/mod.rs b/vendor/pest/src/iterators/mod.rs index 1a7896371..7f81019ac 100644 --- a/vendor/pest/src/iterators/mod.rs +++ b/vendor/pest/src/iterators/mod.rs @@ -10,6 +10,7 @@ //! Types and iterators for parser output. mod flat_pairs; +mod line_index; mod pair; pub(crate) mod pairs; mod queueable_token; diff --git a/vendor/pest/src/iterators/pair.rs b/vendor/pest/src/iterators/pair.rs index 62c95e037..891b90595 100644 --- a/vendor/pest/src/iterators/pair.rs +++ b/vendor/pest/src/iterators/pair.rs @@ -20,6 +20,7 @@ use core::str; #[cfg(feature = "pretty-print")] use serde::ser::SerializeStruct; +use super::line_index::LineIndex; use super::pairs::{self, Pairs}; use super::queueable_token::QueueableToken; use super::tokens::{self, Tokens}; @@ -43,6 +44,7 @@ pub struct Pair<'i, R> { input: &'i str, /// Token index into `queue`. start: usize, + line_index: Rc, } /// # Safety @@ -51,12 +53,14 @@ pub struct Pair<'i, R> { pub unsafe fn new( queue: Rc>>, input: &str, + line_index: Rc, start: usize, ) -> Pair<'_, R> { Pair { queue, input, start, + line_index, } } @@ -202,7 +206,13 @@ impl<'i, R: RuleType> Pair<'i, R> { pub fn into_inner(self) -> Pairs<'i, R> { let pair = self.pair(); - pairs::new(self.queue, self.input, self.start + 1, pair) + pairs::new( + self.queue, + self.input, + Some(self.line_index), + self.start + 1, + pair, + ) } /// Returns the `Tokens` for the `Pair`. @@ -241,6 +251,12 @@ impl<'i, R: RuleType> Pair<'i, R> { ::serde_json::to_string_pretty(self).expect("Failed to pretty-print Pair to json.") } + /// Returns the `line`, `col` of this pair start. + pub fn line_col(&self) -> (usize, usize) { + let pos = self.pos(self.start); + self.line_index.line_col(self.input, pos) + } + fn pair(&self) -> usize { match self.queue[self.start] { QueueableToken::Start { @@ -263,7 +279,13 @@ impl<'i, R: RuleType> Pairs<'i, R> { /// Create a new `Pairs` iterator containing just the single `Pair`. pub fn single(pair: Pair<'i, R>) -> Self { let end = pair.pair(); - pairs::new(pair.queue, pair.input, pair.start, end) + pairs::new( + pair.queue, + pair.input, + Some(pair.line_index), + pair.start, + end, + ) } } diff --git a/vendor/pest/src/iterators/pairs.rs b/vendor/pest/src/iterators/pairs.rs index e478cebf2..c21a7fae1 100644 --- a/vendor/pest/src/iterators/pairs.rs +++ b/vendor/pest/src/iterators/pairs.rs @@ -20,6 +20,7 @@ use core::str; use serde::ser::SerializeStruct; use super::flat_pairs::{self, FlatPairs}; +use super::line_index::LineIndex; use super::pair::{self, Pair}; use super::queueable_token::QueueableToken; use super::tokens::{self, Tokens}; @@ -36,19 +37,27 @@ pub struct Pairs<'i, R> { input: &'i str, start: usize, end: usize, + line_index: Rc, } pub fn new( queue: Rc>>, input: &str, + line_index: Option>, start: usize, end: usize, ) -> Pairs<'_, R> { + let line_index = match line_index { + Some(line_index) => line_index, + None => Rc::new(LineIndex::new(input)), + }; + Pairs { queue, input, start, end, + line_index, } } @@ -181,7 +190,14 @@ impl<'i, R: RuleType> Pairs<'i, R> { #[inline] pub fn peek(&self) -> Option> { if self.start < self.end { - Some(unsafe { pair::new(Rc::clone(&self.queue), self.input, self.start) }) + Some(unsafe { + pair::new( + Rc::clone(&self.queue), + self.input, + Rc::clone(&self.line_index), + self.start, + ) + }) } else { None } @@ -226,6 +242,7 @@ impl<'i, R: RuleType> Iterator for Pairs<'i, R> { fn next(&mut self) -> Option { let pair = self.peek()?; + self.start = self.pair() + 1; Some(pair) } @@ -239,7 +256,14 @@ impl<'i, R: RuleType> DoubleEndedIterator for Pairs<'i, R> { self.end = self.pair_from_end(); - let pair = unsafe { pair::new(Rc::clone(&self.queue), self.input, self.end) }; + let pair = unsafe { + pair::new( + Rc::clone(&self.queue), + self.input, + Rc::clone(&self.line_index), + self.end, + ) + }; Some(pair) } @@ -423,4 +447,36 @@ mod tests { vec![Rule::c, Rule::a] ); } + + #[test] + fn test_line_col() { + let mut pairs = AbcParser::parse(Rule::a, "abc\nefgh").unwrap(); + let pair = pairs.next().unwrap(); + assert_eq!(pair.as_str(), "abc"); + assert_eq!(pair.line_col(), (1, 1)); + + let pair = pairs.next().unwrap(); + assert_eq!(pair.as_str(), "e"); + assert_eq!(pair.line_col(), (2, 1)); + + let pair = pairs.next().unwrap(); + assert_eq!(pair.as_str(), "fgh"); + assert_eq!(pair.line_col(), (2, 2)); + } + + #[test] + fn test_rev_iter_line_col() { + let mut pairs = AbcParser::parse(Rule::a, "abc\nefgh").unwrap().rev(); + let pair = pairs.next().unwrap(); + assert_eq!(pair.as_str(), "fgh"); + assert_eq!(pair.line_col(), (2, 2)); + + let pair = pairs.next().unwrap(); + assert_eq!(pair.as_str(), "e"); + assert_eq!(pair.line_col(), (2, 1)); + + let pair = pairs.next().unwrap(); + assert_eq!(pair.as_str(), "abc"); + assert_eq!(pair.line_col(), (1, 1)); + } } diff --git a/vendor/pest/src/lib.rs b/vendor/pest/src/lib.rs index 5cf2f9e26..fa4df2007 100644 --- a/vendor/pest/src/lib.rs +++ b/vendor/pest/src/lib.rs @@ -360,6 +360,7 @@ pub mod prec_climber; mod span; mod stack; mod token; + #[doc(hidden)] pub mod unicode; diff --git a/vendor/pest/src/macros.rs b/vendor/pest/src/macros.rs index 0ca6d9e0d..1b83f388b 100644 --- a/vendor/pest/src/macros.rs +++ b/vendor/pest/src/macros.rs @@ -329,6 +329,7 @@ pub mod tests { a, b, c, + d, } pub struct AbcParser; @@ -345,6 +346,7 @@ pub mod tests { .skip(1) }) .and_then(|s| s.skip(1).unwrap().rule(Rule::c, |s| s.match_string("e"))) + .and_then(|s| s.optional(|s| s.rule(Rule::d, |s| s.match_string("fgh")))) }) } } diff --git a/vendor/pest/src/parser_state.rs b/vendor/pest/src/parser_state.rs index 609de5557..f58de00c8 100644 --- a/vendor/pest/src/parser_state.rs +++ b/vendor/pest/src/parser_state.rs @@ -157,7 +157,7 @@ where match f(state) { Ok(state) => { let len = state.queue.len(); - Ok(pairs::new(Rc::new(state.queue), input, 0, len)) + Ok(pairs::new(Rc::new(state.queue), input, None, 0, len)) } Err(mut state) => { let variant = if state.reached_call_limit() { diff --git a/vendor/pest/src/position.rs b/vendor/pest/src/position.rs index f91f8291e..465ff9766 100644 --- a/vendor/pest/src/position.rs +++ b/vendor/pest/src/position.rs @@ -116,6 +116,9 @@ impl<'i> Position<'i> { /// Returns the line and column number of this `Position`. /// + /// This is an O(n) operation, where n is the number of chars in the input. + /// You better use [`pair.line_col()`](struct.Pair.html#method.line_col) instead. + /// /// # Examples /// /// ``` @@ -135,14 +138,43 @@ impl<'i> Position<'i> { if self.pos > self.input.len() { panic!("position out of bounds"); } - #[cfg(feature = "fast-line-col")] - { - fast_line_col(self.input, self.pos) - } - #[cfg(not(feature = "fast-line-col"))] - { - original_line_col(self.input, self.pos) + let mut pos = self.pos; + let slice = &self.input[..pos]; + let mut chars = slice.chars().peekable(); + + let mut line_col = (1, 1); + + while pos != 0 { + match chars.next() { + Some('\r') => { + if let Some(&'\n') = chars.peek() { + chars.next(); + + if pos == 1 { + pos -= 1; + } else { + pos -= 2; + } + + line_col = (line_col.0 + 1, 1); + } else { + pos -= 1; + line_col = (line_col.0, line_col.1 + 1); + } + } + Some('\n') => { + pos -= 1; + line_col = (line_col.0 + 1, 1); + } + Some(c) => { + pos -= c.len_utf8(); + line_col = (line_col.0, line_col.1 + 1); + } + None => unreachable!(), + } } + + line_col } /// Returns the entire line of the input that contains this `Position`. @@ -455,63 +487,6 @@ impl<'i> Hash for Position<'i> { } } -#[inline] -#[cfg(not(feature = "fast-line-col"))] -fn original_line_col(input: &str, mut pos: usize) -> (usize, usize) { - // Position's pos is always a UTF-8 border. - let slice = &input[..pos]; - let mut chars = slice.chars().peekable(); - - let mut line_col = (1, 1); - - while pos != 0 { - match chars.next() { - Some('\r') => { - if let Some(&'\n') = chars.peek() { - chars.next(); - - if pos == 1 { - pos -= 1; - } else { - pos -= 2; - } - - line_col = (line_col.0 + 1, 1); - } else { - pos -= 1; - line_col = (line_col.0, line_col.1 + 1); - } - } - Some('\n') => { - pos -= 1; - line_col = (line_col.0 + 1, 1); - } - Some(c) => { - pos -= c.len_utf8(); - line_col = (line_col.0, line_col.1 + 1); - } - None => unreachable!(), - } - } - - line_col -} - -#[inline] -#[cfg(feature = "fast-line-col")] -fn fast_line_col(input: &str, pos: usize) -> (usize, usize) { - // Position's pos is always a UTF-8 border. - let slice = &input[..pos]; - - let prec_ln = memchr::memrchr(b'\n', slice.as_bytes()); - if let Some(prec_nl_pos) = prec_ln { - let lines = bytecount::count(slice[..=prec_nl_pos].as_bytes(), b'\n') + 1; - (lines, slice[prec_nl_pos..].chars().count()) - } else { - (1, slice.chars().count() + 1) - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/vendor/pest/src/pratt_parser.rs b/vendor/pest/src/pratt_parser.rs index be75f7fa4..76ffdf755 100644 --- a/vendor/pest/src/pratt_parser.rs +++ b/vendor/pest/src/pratt_parser.rs @@ -235,10 +235,10 @@ impl PrattParser { } /// Maps primary expressions with a closure `primary`. - pub fn map_primary<'pratt, 'i, X, T>( + pub fn map_primary<'pratt, 'a, 'i, X, T>( &'pratt self, primary: X, - ) -> PrattParserMap<'pratt, 'i, R, X, T> + ) -> PrattParserMap<'pratt, 'a, 'i, R, X, T> where X: FnMut(Pair<'i, R>) -> T, R: 'pratt, @@ -254,29 +254,29 @@ impl PrattParser { } } -type PrefixFn<'i, R, T> = Box, T) -> T + 'i>; -type PostfixFn<'i, R, T> = Box) -> T + 'i>; -type InfixFn<'i, R, T> = Box, T) -> T + 'i>; +type PrefixFn<'a, 'i, R, T> = Box, T) -> T + 'a>; +type PostfixFn<'a, 'i, R, T> = Box) -> T + 'a>; +type InfixFn<'a, 'i, R, T> = Box, T) -> T + 'a>; /// Product of calling [`map_primary`] on [`PrattParser`], defines how expressions should /// be mapped. /// /// [`map_primary`]: struct.PrattParser.html#method.map_primary /// [`PrattParser`]: struct.PrattParser.html -pub struct PrattParserMap<'pratt, 'i, R, F, T> +pub struct PrattParserMap<'pratt, 'a, 'i, R, F, T> where R: RuleType, F: FnMut(Pair<'i, R>) -> T, { pratt: &'pratt PrattParser, primary: F, - prefix: Option>, - postfix: Option>, - infix: Option>, + prefix: Option>, + postfix: Option>, + infix: Option>, phantom: PhantomData, } -impl<'pratt, 'i, R, F, T> PrattParserMap<'pratt, 'i, R, F, T> +impl<'pratt, 'a, 'i, R, F, T> PrattParserMap<'pratt, 'a, 'i, R, F, T> where R: RuleType + 'pratt, F: FnMut(Pair<'i, R>) -> T, @@ -284,7 +284,7 @@ where /// Maps prefix operators with closure `prefix`. pub fn map_prefix(mut self, prefix: X) -> Self where - X: FnMut(Pair<'i, R>, T) -> T + 'i, + X: FnMut(Pair<'i, R>, T) -> T + 'a, { self.prefix = Some(Box::new(prefix)); self @@ -293,7 +293,7 @@ where /// Maps postfix operators with closure `postfix`. pub fn map_postfix(mut self, postfix: X) -> Self where - X: FnMut(T, Pair<'i, R>) -> T + 'i, + X: FnMut(T, Pair<'i, R>) -> T + 'a, { self.postfix = Some(Box::new(postfix)); self @@ -302,7 +302,7 @@ where /// Maps infix operators with a closure `infix`. pub fn map_infix(mut self, infix: X) -> Self where - X: FnMut(T, Pair<'i, R>, T) -> T + 'i, + X: FnMut(T, Pair<'i, R>, T) -> T + 'a, { self.infix = Some(Box::new(infix)); self -- cgit v1.2.3