aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/parser_impl/input.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-04 04:36:40 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-04 04:36:40 +0000
commit6295bbe6ec9741538307cd619ada1c9566f99a5d (patch)
tree9a004a531fce65c4a01e2d243dff638fb76ae373 /crates/ra_syntax/src/parser_impl/input.rs
parent2fcc6bdafa1caffd4ddf98968ee967e9fb091cc0 (diff)
parent58139c558aa085588264ba659b8483a036c1da0e (diff)
Merge #391
391: docing parser methods r=csmoe a=csmoe Co-authored-by: csmoe <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/parser_impl/input.rs')
-rw-r--r--crates/ra_syntax/src/parser_impl/input.rs27
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..7fde5b3ab 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
5pub(crate) struct ParserInput<'t> { 5pub(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
11impl<'t> ParserInput<'t> { 25impl<'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 token 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 "";