use sql_dialect_fmt_lexer::{Lexed, Token};
use sql_dialect_fmt_syntax::SyntaxKind;
pub(crate) struct Input<'a> {
all: Vec<Token<'a>>,
meaningful: Vec<usize>,
offsets: Vec<usize>,
total: usize,
}
impl<'a> Input<'a> {
pub(crate) fn new(lexed: Lexed<'a>) -> Self {
let all = lexed.tokens;
let mut offsets = Vec::with_capacity(all.len());
let mut meaningful = Vec::new();
let mut off = 0usize;
for (i, t) in all.iter().enumerate() {
offsets.push(off);
off += t.text.len();
if !t.kind.is_trivia() {
meaningful.push(i);
}
}
Input {
all,
meaningful,
offsets,
total: off,
}
}
pub(crate) fn all(&self) -> &[Token<'a>] {
&self.all
}
pub(crate) fn len(&self) -> usize {
self.meaningful.len()
}
pub(crate) fn kind(&self, pos: usize) -> SyntaxKind {
match self.meaningful.get(pos) {
Some(&i) => self.all[i].kind,
None => SyntaxKind::EOF,
}
}
pub(crate) fn text(&self, pos: usize) -> &'a str {
match self.meaningful.get(pos) {
Some(&i) => self.all[i].text,
None => "",
}
}
pub(crate) fn offset(&self, pos: usize) -> usize {
match self.meaningful.get(pos) {
Some(&i) => self.offsets[i],
None => self.total,
}
}
pub(crate) fn token_len(&self, pos: usize) -> usize {
match self.meaningful.get(pos) {
Some(&i) => self.all[i].text.len(),
None => 0,
}
}
}