2019-02-20 06:47:32 -06:00
|
|
|
use crate::{
|
|
|
|
SyntaxKind, SyntaxKind::EOF, TextRange, TextUnit,
|
|
|
|
parsing::lexer::Token,
|
|
|
|
};
|
2018-02-04 05:35:59 -06:00
|
|
|
|
|
|
|
use std::ops::{Add, AddAssign};
|
|
|
|
|
|
|
|
pub(crate) struct ParserInput<'t> {
|
|
|
|
text: &'t str,
|
2018-12-31 07:30:37 -06:00
|
|
|
/// start position of each token(expect whitespace and comment)
|
|
|
|
/// ```non-rust
|
|
|
|
/// struct Foo;
|
|
|
|
/// ^------^---
|
|
|
|
/// | | ^-
|
|
|
|
/// 0 7 10
|
|
|
|
/// ```
|
|
|
|
/// (token, start_offset): `[(struct, 0), (Foo, 7), (;, 10)]`
|
2018-02-04 05:35:59 -06:00
|
|
|
start_offsets: Vec<TextUnit>,
|
2018-12-31 07:30:37 -06:00
|
|
|
/// non-whitespace/comment tokens
|
|
|
|
/// ```non-rust
|
|
|
|
/// struct Foo {}
|
|
|
|
/// ^^^^^^ ^^^ ^^
|
|
|
|
/// ```
|
|
|
|
/// tokens: `[struct, Foo, {, }]`
|
|
|
|
tokens: Vec<Token>,
|
2018-02-04 05:35:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'t> ParserInput<'t> {
|
2018-12-31 07:30:37 -06:00
|
|
|
/// Generate input from tokens(expect comment and whitespace).
|
2018-02-04 05:35:59 -06:00
|
|
|
pub fn new(text: &'t str, raw_tokens: &'t [Token]) -> ParserInput<'t> {
|
|
|
|
let mut tokens = Vec::new();
|
|
|
|
let mut start_offsets = Vec::new();
|
2018-07-28 05:07:10 -05:00
|
|
|
let mut len = 0.into();
|
2018-02-04 05:35:59 -06:00
|
|
|
for &token in raw_tokens.iter() {
|
2018-07-29 07:16:07 -05:00
|
|
|
if !token.kind.is_trivia() {
|
2018-02-04 05:35:59 -06:00
|
|
|
tokens.push(token);
|
|
|
|
start_offsets.push(len);
|
|
|
|
}
|
|
|
|
len += token.len;
|
|
|
|
}
|
|
|
|
|
2019-02-08 05:49:43 -06:00
|
|
|
ParserInput { text, start_offsets, tokens }
|
2018-02-04 05:35:59 -06:00
|
|
|
}
|
|
|
|
|
2018-12-31 07:30:37 -06:00
|
|
|
/// Get the syntax kind of token at given input position.
|
2018-02-04 05:35:59 -06:00
|
|
|
pub fn kind(&self, pos: InputPosition) -> SyntaxKind {
|
|
|
|
let idx = pos.0 as usize;
|
|
|
|
if !(idx < self.tokens.len()) {
|
|
|
|
return EOF;
|
|
|
|
}
|
|
|
|
self.tokens[idx].kind
|
|
|
|
}
|
|
|
|
|
2018-12-31 07:30:37 -06:00
|
|
|
/// Get the length of a token at given input position.
|
|
|
|
pub fn token_len(&self, pos: InputPosition) -> TextUnit {
|
2018-08-05 08:09:25 -05:00
|
|
|
let idx = pos.0 as usize;
|
|
|
|
if !(idx < self.tokens.len()) {
|
|
|
|
return 0.into();
|
|
|
|
}
|
|
|
|
self.tokens[idx].len
|
|
|
|
}
|
|
|
|
|
2018-12-31 07:30:37 -06:00
|
|
|
/// Get the start position of a taken at given input position.
|
|
|
|
pub fn token_start_at(&self, pos: InputPosition) -> TextUnit {
|
2018-08-05 08:09:25 -05:00
|
|
|
let idx = pos.0 as usize;
|
|
|
|
if !(idx < self.tokens.len()) {
|
|
|
|
return 0.into();
|
|
|
|
}
|
|
|
|
self.start_offsets[idx]
|
|
|
|
}
|
|
|
|
|
2019-01-01 02:09:51 -06:00
|
|
|
/// Get the raw text of a token at given input position.
|
2018-12-31 07:30:37 -06:00
|
|
|
pub fn token_text(&self, pos: InputPosition) -> &'t str {
|
2018-02-04 05:35:59 -06:00
|
|
|
let idx = pos.0 as usize;
|
|
|
|
if !(idx < self.tokens.len()) {
|
|
|
|
return "";
|
|
|
|
}
|
2018-07-28 05:07:10 -05:00
|
|
|
let range = TextRange::offset_len(self.start_offsets[idx], self.tokens[idx].len);
|
2018-02-04 05:35:59 -06:00
|
|
|
&self.text[range]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
|
|
|
|
pub(crate) struct InputPosition(u32);
|
|
|
|
|
|
|
|
impl InputPosition {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
InputPosition(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Add<u32> for InputPosition {
|
|
|
|
type Output = InputPosition;
|
|
|
|
|
|
|
|
fn add(self, rhs: u32) -> InputPosition {
|
|
|
|
InputPosition(self.0 + rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AddAssign<u32> for InputPosition {
|
|
|
|
fn add_assign(&mut self, rhs: u32) {
|
|
|
|
self.0 += rhs
|
|
|
|
}
|
|
|
|
}
|