summaryrefslogtreecommitdiff
path: root/src/parser_state.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser_state.rs')
-rw-r--r--src/parser_state.rs45
1 files changed, 43 insertions, 2 deletions
diff --git a/src/parser_state.rs b/src/parser_state.rs
index f58de00..f665bf2 100644
--- a/src/parser_state.rs
+++ b/src/parser_state.rs
@@ -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("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: &'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()
@@ -660,7 +701,7 @@ impl<'i, R: RuleType> ParserState<'i, R> {
/// `Box<ParserState>` if successful, or `Err` with the updated `Box<ParserState>` otherwise.
///
/// # Caution
- /// The provided `range` is intepreted as inclusive.
+ /// The provided `range` is interpreted as inclusive.
///
/// # Examples
///