summaryrefslogtreecommitdiff
path: root/src/iterators/pair.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/iterators/pair.rs')
-rw-r--r--src/iterators/pair.rs54
1 files changed, 40 insertions, 14 deletions
diff --git a/src/iterators/pair.rs b/src/iterators/pair.rs
index 88844a3..891b905 100644
--- a/src/iterators/pair.rs
+++ b/src/iterators/pair.rs
@@ -7,20 +7,25 @@
// option. All files in the project carrying such notice may not be copied,
// modified, or distributed except according to those terms.
-use std::fmt;
-use std::hash::{Hash, Hasher};
-use std::ptr;
-use std::rc::Rc;
-use std::str;
+use alloc::format;
+use alloc::rc::Rc;
+#[cfg(feature = "pretty-print")]
+use alloc::string::String;
+use alloc::vec::Vec;
+use core::fmt;
+use core::hash::{Hash, Hasher};
+use core::ptr;
+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};
-use span::{self, Span};
-use RuleType;
+use crate::span::{self, Span};
+use crate::RuleType;
/// A matching pair of [`Token`]s and everything between them.
///
@@ -39,6 +44,7 @@ pub struct Pair<'i, R> {
input: &'i str,
/// Token index into `queue`.
start: usize,
+ line_index: Rc<LineIndex>,
}
/// # Safety
@@ -47,12 +53,14 @@ pub struct Pair<'i, R> {
pub unsafe fn new<R: RuleType>(
queue: Rc<Vec<QueueableToken<R>>>,
input: &str,
+ line_index: Rc<LineIndex>,
start: usize,
-) -> Pair<R> {
+) -> Pair<'_, R> {
Pair {
queue,
input,
start,
+ line_index,
}
}
@@ -198,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`.
@@ -237,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 {
@@ -259,12 +279,18 @@ 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,
+ )
}
}
impl<'i, R: RuleType> fmt::Debug for Pair<'i, R> {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Pair")
.field("rule", &self.as_rule())
.field("span", &self.as_span())
@@ -274,7 +300,7 @@ impl<'i, R: RuleType> fmt::Debug for Pair<'i, R> {
}
impl<'i, R: RuleType> fmt::Display for Pair<'i, R> {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let rule = self.as_rule();
let start = self.pos(self.start);
let end = self.pos(self.pair());
@@ -343,8 +369,8 @@ impl<'i, R: RuleType> ::serde::Serialize for Pair<'i, R> {
#[cfg(test)]
mod tests {
- use macros::tests::*;
- use parser::Parser;
+ use crate::macros::tests::*;
+ use crate::parser::Parser;
#[test]
#[cfg(feature = "pretty-print")]