aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/parsing/input.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-02-20 19:52:32 +0000
committerAleksey Kladov <[email protected]>2019-02-20 19:52:32 +0000
commitcce23fddba4241202ebd29cce44db4ce9a08793a (patch)
tree6f1385feff4b4cdfaac1fb9b93abad4757fc6918 /crates/ra_syntax/src/parsing/input.rs
parent2acb21e8f72896c7a2855ca6042d0ee1870d8643 (diff)
flattern module structure
Diffstat (limited to 'crates/ra_syntax/src/parsing/input.rs')
-rw-r--r--crates/ra_syntax/src/parsing/input.rs72
1 files changed, 72 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/parsing/input.rs b/crates/ra_syntax/src/parsing/input.rs
new file mode 100644
index 000000000..0f1810df5
--- /dev/null
+++ b/crates/ra_syntax/src/parsing/input.rs
@@ -0,0 +1,72 @@
1use crate::{
2 SyntaxKind, SyntaxKind::EOF, TextRange, TextUnit,
3 parsing::{
4 TokenPos, TokenSource,
5 lexer::Token,
6 },
7};
8
9impl<'t> TokenSource for ParserInput<'t> {
10 fn token_kind(&self, pos: TokenPos) -> SyntaxKind {
11 let idx = pos.0 as usize;
12 if !(idx < self.tokens.len()) {
13 return EOF;
14 }
15 self.tokens[idx].kind
16 }
17 fn is_token_joint_to_next(&self, pos: TokenPos) -> bool {
18 let idx_curr = pos.0 as usize;
19 let idx_next = pos.0 as usize + 1;
20 if !(idx_next < self.tokens.len()) {
21 return true;
22 }
23 self.start_offsets[idx_curr] + self.tokens[idx_curr].len == self.start_offsets[idx_next]
24 }
25 fn is_keyword(&self, pos: TokenPos, kw: &str) -> bool {
26 let idx = pos.0 as usize;
27 if !(idx < self.tokens.len()) {
28 return false;
29 }
30 let range = TextRange::offset_len(self.start_offsets[idx], self.tokens[idx].len);
31
32 self.text[range] == *kw
33 }
34}
35
36pub(crate) struct ParserInput<'t> {
37 text: &'t str,
38 /// start position of each token(expect whitespace and comment)
39 /// ```non-rust
40 /// struct Foo;
41 /// ^------^---
42 /// | | ^-
43 /// 0 7 10
44 /// ```
45 /// (token, start_offset): `[(struct, 0), (Foo, 7), (;, 10)]`
46 start_offsets: Vec<TextUnit>,
47 /// non-whitespace/comment tokens
48 /// ```non-rust
49 /// struct Foo {}
50 /// ^^^^^^ ^^^ ^^
51 /// ```
52 /// tokens: `[struct, Foo, {, }]`
53 tokens: Vec<Token>,
54}
55
56impl<'t> ParserInput<'t> {
57 /// Generate input from tokens(expect comment and whitespace).
58 pub fn new(text: &'t str, raw_tokens: &'t [Token]) -> ParserInput<'t> {
59 let mut tokens = Vec::new();
60 let mut start_offsets = Vec::new();
61 let mut len = 0.into();
62 for &token in raw_tokens.iter() {
63 if !token.kind.is_trivia() {
64 tokens.push(token);
65 start_offsets.push(len);
66 }
67 len += token.len;
68 }
69
70 ParserInput { text, start_offsets, tokens }
71 }
72}