Skip to content

Commit

Permalink
do not compute line offsets after the last token
Browse files Browse the repository at this point in the history
Previously when constructing the root Pairs instance line offsets were
computed for the full input string. If the parse stopped before EOI this
can require a substantial amount of unnecessary work.
  • Loading branch information
wabain committed Jun 29, 2024
1 parent f090ec1 commit 48500ef
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pest/src/iterators/flat_pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ pub struct FlatPairs<'i, R> {
pub fn new<'i, R: RuleType>(
queue: Rc<Vec<QueueableToken<'i, R>>>,
input: &'i str,
line_index: Rc<LineIndex>,
start: usize,
end: usize,
) -> FlatPairs<'i, R> {
FlatPairs {
queue,
input,
line_index: Rc::new(LineIndex::new(input)),
line_index,
start,
end,
}
Expand Down
20 changes: 18 additions & 2 deletions pest/src/iterators/pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@ pub fn new<'i, R: RuleType>(
) -> Pairs<'i, R> {
let line_index = match line_index {
Some(line_index) => line_index,
None => Rc::new(LineIndex::new(input)),
None => {
let last_input_pos = queue
.last()
.map(|token| match *token {
QueueableToken::Start { input_pos, .. }
| QueueableToken::End { input_pos, .. } => input_pos,
})
.unwrap_or(0);

Rc::new(LineIndex::new(&input[..last_input_pos]))
}
};

let mut pairs_count = 0;
Expand Down Expand Up @@ -205,7 +215,13 @@ impl<'i, R: RuleType> Pairs<'i, R> {
/// ```
#[inline]
pub fn flatten(self) -> FlatPairs<'i, R> {
flat_pairs::new(self.queue, self.input, self.start, self.end)
flat_pairs::new(
self.queue,
self.input,
self.line_index,
self.start,
self.end,
)
}

/// Finds the first pair that has its node or branch tagged with the provided
Expand Down

0 comments on commit 48500ef

Please sign in to comment.