diff options
Diffstat (limited to 'crates/ra_syntax/src/parser_impl')
-rw-r--r-- | crates/ra_syntax/src/parser_impl/input.rs | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/crates/ra_syntax/src/parser_impl/input.rs b/crates/ra_syntax/src/parser_impl/input.rs index ac6d900d8..083a7aa15 100644 --- a/crates/ra_syntax/src/parser_impl/input.rs +++ b/crates/ra_syntax/src/parser_impl/input.rs | |||
@@ -4,11 +4,26 @@ use std::ops::{Add, AddAssign}; | |||
4 | 4 | ||
5 | pub(crate) struct ParserInput<'t> { | 5 | pub(crate) struct ParserInput<'t> { |
6 | text: &'t str, | 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)]` | ||
7 | start_offsets: Vec<TextUnit>, | 15 | start_offsets: Vec<TextUnit>, |
8 | tokens: Vec<Token>, // non-whitespace tokens | 16 | /// non-whitespace/comment tokens |
17 | /// ```non-rust | ||
18 | /// struct Foo {} | ||
19 | /// ^^^^^^ ^^^ ^^ | ||
20 | /// ``` | ||
21 | /// tokens: `[struct, Foo, {, }]` | ||
22 | tokens: Vec<Token>, | ||
9 | } | 23 | } |
10 | 24 | ||
11 | impl<'t> ParserInput<'t> { | 25 | impl<'t> ParserInput<'t> { |
26 | /// Generate input from tokens(expect comment and whitespace). | ||
12 | pub fn new(text: &'t str, raw_tokens: &'t [Token]) -> ParserInput<'t> { | 27 | pub fn new(text: &'t str, raw_tokens: &'t [Token]) -> ParserInput<'t> { |
13 | let mut tokens = Vec::new(); | 28 | let mut tokens = Vec::new(); |
14 | let mut start_offsets = Vec::new(); | 29 | let mut start_offsets = Vec::new(); |
@@ -28,6 +43,7 @@ impl<'t> ParserInput<'t> { | |||
28 | } | 43 | } |
29 | } | 44 | } |
30 | 45 | ||
46 | /// Get the syntax kind of token at given input position. | ||
31 | pub fn kind(&self, pos: InputPosition) -> SyntaxKind { | 47 | pub fn kind(&self, pos: InputPosition) -> SyntaxKind { |
32 | let idx = pos.0 as usize; | 48 | let idx = pos.0 as usize; |
33 | if !(idx < self.tokens.len()) { | 49 | if !(idx < self.tokens.len()) { |
@@ -36,7 +52,8 @@ impl<'t> ParserInput<'t> { | |||
36 | self.tokens[idx].kind | 52 | self.tokens[idx].kind |
37 | } | 53 | } |
38 | 54 | ||
39 | pub fn len(&self, pos: InputPosition) -> TextUnit { | 55 | /// Get the length of a token at given input position. |
56 | pub fn token_len(&self, pos: InputPosition) -> TextUnit { | ||
40 | let idx = pos.0 as usize; | 57 | let idx = pos.0 as usize; |
41 | if !(idx < self.tokens.len()) { | 58 | if !(idx < self.tokens.len()) { |
42 | return 0.into(); | 59 | return 0.into(); |
@@ -44,7 +61,8 @@ impl<'t> ParserInput<'t> { | |||
44 | self.tokens[idx].len | 61 | self.tokens[idx].len |
45 | } | 62 | } |
46 | 63 | ||
47 | pub fn start(&self, pos: InputPosition) -> TextUnit { | 64 | /// Get the start position of a taken at given input position. |
65 | pub fn token_start_at(&self, pos: InputPosition) -> TextUnit { | ||
48 | let idx = pos.0 as usize; | 66 | let idx = pos.0 as usize; |
49 | if !(idx < self.tokens.len()) { | 67 | if !(idx < self.tokens.len()) { |
50 | return 0.into(); | 68 | return 0.into(); |
@@ -52,7 +70,8 @@ impl<'t> ParserInput<'t> { | |||
52 | self.start_offsets[idx] | 70 | self.start_offsets[idx] |
53 | } | 71 | } |
54 | 72 | ||
55 | pub fn text(&self, pos: InputPosition) -> &'t str { | 73 | /// Get the raw text of a toen at given input position. |
74 | pub fn token_text(&self, pos: InputPosition) -> &'t str { | ||
56 | let idx = pos.0 as usize; | 75 | let idx = pos.0 as usize; |
57 | if !(idx < self.tokens.len()) { | 76 | if !(idx < self.tokens.len()) { |
58 | return ""; | 77 | return ""; |