nu_protocol/engine/bracket_table.rs
1use crate::Span;
2
3/// Where the brackets of one source file close.
4///
5/// nu-parser's lexer builds one of these per file in a [`StateWorkingSet`](super::StateWorkingSet)
6/// and uses it to jump from an opening `(`, `[` or `{` straight past its closer instead of scanning
7/// the bytes in between. An entry is recorded only where scanning would find exactly that closer,
8/// so jumping never changes what the lexer produces.
9#[derive(Debug, Clone)]
10pub struct BracketTable {
11 /// The file's span, in the same global coordinates as every other [`Span`].
12 pub covered_span: Span,
13 /// For the byte at `covered_span.start + i`: if it is an opening bracket closed at
14 /// `covered_span.start + j`, then `close[i] == j + 1`; otherwise `close[i] == 0`.
15 pub close: Box<[u32]>,
16}
17
18impl BracketTable {
19 /// The position of the closer of the opening bracket at `open`, if it is known.
20 #[inline]
21 pub fn close_of(&self, open: usize) -> Option<usize> {
22 let index = open.checked_sub(self.covered_span.start)?;
23 match self.close.get(index) {
24 Some(&close) if close != 0 => Some(self.covered_span.start + close as usize - 1),
25 _ => None,
26 }
27 }
28}