diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:57:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:57:31 +0000 |
commit | dc0db358abe19481e475e10c32149b53370f1a1c (patch) | |
tree | ab8ce99c4b255ce46f99ef402c27916055b899ee /vendor/pest/src/parser_state.rs | |
parent | Releasing progress-linux version 1.71.1+dfsg1-2~progress7.99u1. (diff) | |
download | rustc-dc0db358abe19481e475e10c32149b53370f1a1c.tar.xz rustc-dc0db358abe19481e475e10c32149b53370f1a1c.zip |
Merging upstream version 1.72.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/pest/src/parser_state.rs')
-rw-r--r-- | vendor/pest/src/parser_state.rs | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/vendor/pest/src/parser_state.rs b/vendor/pest/src/parser_state.rs index f58de00c8..5a10b420b 100644 --- a/vendor/pest/src/parser_state.rs +++ b/vendor/pest/src/parser_state.rs @@ -7,7 +7,7 @@ // option. All files in the project carrying such notice may not be copied, // modified, or distributed except according to those terms. -use alloc::borrow::ToOwned; +use alloc::borrow::{Cow, ToOwned}; use alloc::boxed::Box; use alloc::rc::Rc; use alloc::vec; @@ -128,7 +128,7 @@ impl CallLimitTracker { #[derive(Debug)] pub struct ParserState<'i, R: RuleType> { position: Position<'i>, - queue: Vec<QueueableToken<R>>, + queue: Vec<QueueableToken<'i, R>>, lookahead: Lookahead, pos_attempts: Vec<R>, neg_attempts: Vec<R>, @@ -345,6 +345,7 @@ impl<'i, R: RuleType> ParserState<'i, R> { new_state.queue.push(QueueableToken::End { start_token_index: index, rule, + tag: None, input_pos: new_pos, }); } @@ -373,6 +374,46 @@ impl<'i, R: RuleType> ParserState<'i, R> { } } + /// Tag current node + /// + /// # Examples + /// + /// Try to recognize the one specified in a set of characters + /// + /// ``` + /// use pest::{state, ParseResult, ParserState, iterators::Pair}; + /// #[allow(non_camel_case_types)] + /// #[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] + /// enum Rule { + /// character, + /// } + /// fn mark_c(state: Box<ParserState<Rule>>) -> ParseResult<Box<ParserState<Rule>>> { + /// state.sequence(|state| { + /// character(state) + /// .and_then(|state| character(state)) + /// .and_then(|state| character(state)) + /// .and_then(|state| state.tag_node(std::borrow::Cow::Borrowed("c"))) + /// .and_then(|state| character(state)) + /// }) + /// } + /// fn character(state: Box<ParserState<Rule>>) -> ParseResult<Box<ParserState<Rule>>> { + /// state.rule(Rule::character, |state| state.match_range('a'..'z')) + /// } + /// + /// let input = "abcd"; + /// let pairs = state(input, mark_c).unwrap(); + /// // find all node tag as `c` + /// let find: Vec<Pair<Rule>> = pairs.filter(|s| s.as_node_tag() == Some("c")).collect(); + /// assert_eq!(find[0].as_str(), "c") + /// ``` + #[inline] + pub fn tag_node(mut self: Box<Self>, tag: Cow<'i, str>) -> ParseResult<Box<Self>> { + if let Some(QueueableToken::End { tag: old, .. }) = self.queue.last_mut() { + *old = Some(tag) + } + Ok(self) + } + fn attempts_at(&self, pos: usize) -> usize { if self.attempt_pos == pos { self.pos_attempts.len() + self.neg_attempts.len() |