aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/parser_impl/input.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-02-20 12:47:32 +0000
committerAleksey Kladov <[email protected]>2019-02-20 12:47:32 +0000
commit5222b8aba3b1c2c68706aacf6869423a8e4fe6d5 (patch)
treec8a6e999b8ac5f1f29bde86a2e0b3a53466bb369 /crates/ra_syntax/src/parser_impl/input.rs
parent9d0cda4bc84350961f3884e75a1c20e62c449ede (diff)
move all parsing related bits to a separate module
Diffstat (limited to 'crates/ra_syntax/src/parser_impl/input.rs')
-rw-r--r--crates/ra_syntax/src/parser_impl/input.rs101
1 files changed, 0 insertions, 101 deletions
diff --git a/crates/ra_syntax/src/parser_impl/input.rs b/crates/ra_syntax/src/parser_impl/input.rs
deleted file mode 100644
index 616a26fdc..000000000
--- a/crates/ra_syntax/src/parser_impl/input.rs
+++ /dev/null
@@ -1,101 +0,0 @@
1use crate::{lexer::Token, SyntaxKind, SyntaxKind::EOF, TextRange, TextUnit};
2
3use std::ops::{Add, AddAssign};
4
5pub(crate) struct ParserInput<'t> {
6 text: &'t str,
7 /// start position of each token(expect whitespace and comment)
8 /// ```non-rust
9 /// struct Foo;
10 /// ^------^---
11 /// | | ^-
12 /// 0 7 10
13 /// ```
14 /// (token, start_offset): `[(struct, 0), (Foo, 7), (;, 10)]`
15 start_offsets: Vec<TextUnit>,
16 /// non-whitespace/comment tokens
17 /// ```non-rust
18 /// struct Foo {}
19 /// ^^^^^^ ^^^ ^^
20 /// ```
21 /// tokens: `[struct, Foo, {, }]`
22 tokens: Vec<Token>,
23}
24
25impl<'t> ParserInput<'t> {
26 /// Generate input from tokens(expect comment and whitespace).
27 pub fn new(text: &'t str, raw_tokens: &'t [Token]) -> ParserInput<'t> {
28 let mut tokens = Vec::new();
29 let mut start_offsets = Vec::new();
30 let mut len = 0.into();
31 for &token in raw_tokens.iter() {
32 if !token.kind.is_trivia() {
33 tokens.push(token);
34 start_offsets.push(len);
35 }
36 len += token.len;
37 }
38
39 ParserInput { text, start_offsets, tokens }
40 }
41
42 /// Get the syntax kind of token at given input position.
43 pub fn kind(&self, pos: InputPosition) -> SyntaxKind {
44 let idx = pos.0 as usize;
45 if !(idx < self.tokens.len()) {
46 return EOF;
47 }
48 self.tokens[idx].kind
49 }
50
51 /// Get the length of a token at given input position.
52 pub fn token_len(&self, pos: InputPosition) -> TextUnit {
53 let idx = pos.0 as usize;
54 if !(idx < self.tokens.len()) {
55 return 0.into();
56 }
57 self.tokens[idx].len
58 }
59
60 /// Get the start position of a taken at given input position.
61 pub fn token_start_at(&self, pos: InputPosition) -> TextUnit {
62 let idx = pos.0 as usize;
63 if !(idx < self.tokens.len()) {
64 return 0.into();
65 }
66 self.start_offsets[idx]
67 }
68
69 /// Get the raw text of a token at given input position.
70 pub fn token_text(&self, pos: InputPosition) -> &'t str {
71 let idx = pos.0 as usize;
72 if !(idx < self.tokens.len()) {
73 return "";
74 }
75 let range = TextRange::offset_len(self.start_offsets[idx], self.tokens[idx].len);
76 &self.text[range]
77 }
78}
79
80#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
81pub(crate) struct InputPosition(u32);
82
83impl InputPosition {
84 pub fn new() -> Self {
85 InputPosition(0)
86 }
87}
88
89impl Add<u32> for InputPosition {
90 type Output = InputPosition;
91
92 fn add(self, rhs: u32) -> InputPosition {
93 InputPosition(self.0 + rhs)
94 }
95}
96
97impl AddAssign<u32> for InputPosition {
98 fn add_assign(&mut self, rhs: u32) {
99 self.0 += rhs
100 }
101}