diff options
Diffstat (limited to 'src/parser_impl/input.rs')
-rw-r--r-- | src/parser_impl/input.rs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/parser_impl/input.rs b/src/parser_impl/input.rs new file mode 100644 index 000000000..db76364b2 --- /dev/null +++ b/src/parser_impl/input.rs | |||
@@ -0,0 +1,71 @@ | |||
1 | use {lexer::Token, SyntaxKind, SyntaxKind::EOF, TextRange, TextUnit}; | ||
2 | |||
3 | use std::ops::{Add, AddAssign}; | ||
4 | |||
5 | pub(crate) struct ParserInput<'t> { | ||
6 | text: &'t str, | ||
7 | start_offsets: Vec<TextUnit>, | ||
8 | tokens: Vec<Token>, // non-whitespace tokens | ||
9 | } | ||
10 | |||
11 | impl<'t> ParserInput<'t> { | ||
12 | pub fn new(text: &'t str, raw_tokens: &'t [Token]) -> ParserInput<'t> { | ||
13 | let mut tokens = Vec::new(); | ||
14 | let mut start_offsets = Vec::new(); | ||
15 | let mut len = 0.into(); | ||
16 | for &token in raw_tokens.iter() { | ||
17 | if !token.kind.is_trivia() { | ||
18 | tokens.push(token); | ||
19 | start_offsets.push(len); | ||
20 | } | ||
21 | len += token.len; | ||
22 | } | ||
23 | |||
24 | ParserInput { | ||
25 | text, | ||
26 | start_offsets, | ||
27 | tokens, | ||
28 | } | ||
29 | } | ||
30 | |||
31 | pub fn kind(&self, pos: InputPosition) -> SyntaxKind { | ||
32 | let idx = pos.0 as usize; | ||
33 | if !(idx < self.tokens.len()) { | ||
34 | return EOF; | ||
35 | } | ||
36 | self.tokens[idx].kind | ||
37 | } | ||
38 | |||
39 | #[allow(unused)] | ||
40 | pub fn text(&self, pos: InputPosition) -> &'t str { | ||
41 | let idx = pos.0 as usize; | ||
42 | if !(idx < self.tokens.len()) { | ||
43 | return ""; | ||
44 | } | ||
45 | let range = TextRange::offset_len(self.start_offsets[idx], self.tokens[idx].len); | ||
46 | &self.text[range] | ||
47 | } | ||
48 | } | ||
49 | |||
50 | #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] | ||
51 | pub(crate) struct InputPosition(u32); | ||
52 | |||
53 | impl InputPosition { | ||
54 | pub fn new() -> Self { | ||
55 | InputPosition(0) | ||
56 | } | ||
57 | } | ||
58 | |||
59 | impl Add<u32> for InputPosition { | ||
60 | type Output = InputPosition; | ||
61 | |||
62 | fn add(self, rhs: u32) -> InputPosition { | ||
63 | InputPosition(self.0 + rhs) | ||
64 | } | ||
65 | } | ||
66 | |||
67 | impl AddAssign<u32> for InputPosition { | ||
68 | fn add_assign(&mut self, rhs: u32) { | ||
69 | self.0 += rhs | ||
70 | } | ||
71 | } | ||