aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_syntax/src/parser_impl.rs13
-rw-r--r--crates/ra_syntax/src/parser_impl/input.rs27
2 files changed, 30 insertions, 10 deletions
diff --git a/crates/ra_syntax/src/parser_impl.rs b/crates/ra_syntax/src/parser_impl.rs
index d4032a6d9..ce321aecb 100644
--- a/crates/ra_syntax/src/parser_impl.rs
+++ b/crates/ra_syntax/src/parser_impl.rs
@@ -64,7 +64,6 @@ pub(crate) fn parse_with<S: Sink>(
64/// the public API of the `Parser`. 64/// the public API of the `Parser`.
65pub(crate) struct ParserImpl<'t> { 65pub(crate) struct ParserImpl<'t> {
66 inp: &'t ParserInput<'t>, 66 inp: &'t ParserInput<'t>,
67
68 pos: InputPosition, 67 pos: InputPosition,
69 events: Vec<Event>, 68 events: Vec<Event>,
70 steps: Cell<u32>, 69 steps: Cell<u32>,
@@ -74,7 +73,6 @@ impl<'t> ParserImpl<'t> {
74 pub(crate) fn new(inp: &'t ParserInput<'t>) -> ParserImpl<'t> { 73 pub(crate) fn new(inp: &'t ParserInput<'t>) -> ParserImpl<'t> {
75 ParserImpl { 74 ParserImpl {
76 inp, 75 inp,
77
78 pos: InputPosition::new(), 76 pos: InputPosition::new(),
79 events: Vec::new(), 77 events: Vec::new(),
80 steps: Cell::new(0), 78 steps: Cell::new(0),
@@ -89,7 +87,9 @@ impl<'t> ParserImpl<'t> {
89 pub(super) fn next2(&self) -> Option<(SyntaxKind, SyntaxKind)> { 87 pub(super) fn next2(&self) -> Option<(SyntaxKind, SyntaxKind)> {
90 let c1 = self.inp.kind(self.pos); 88 let c1 = self.inp.kind(self.pos);
91 let c2 = self.inp.kind(self.pos + 1); 89 let c2 = self.inp.kind(self.pos + 1);
92 if self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos) { 90 if self.inp.token_start_at(self.pos + 1)
91 == self.inp.token_start_at(self.pos) + self.inp.len(self.pos)
92 {
93 Some((c1, c2)) 93 Some((c1, c2))
94 } else { 94 } else {
95 None 95 None
@@ -100,9 +100,10 @@ impl<'t> ParserImpl<'t> {
100 let c1 = self.inp.kind(self.pos); 100 let c1 = self.inp.kind(self.pos);
101 let c2 = self.inp.kind(self.pos + 1); 101 let c2 = self.inp.kind(self.pos + 1);
102 let c3 = self.inp.kind(self.pos + 2); 102 let c3 = self.inp.kind(self.pos + 2);
103 if self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos) 103 if self.inp.token_start_at(self.pos + 1)
104 && self.inp.start(self.pos + 2) 104 == self.inp.token_start_at(self.pos) + self.inp.len(self.pos)
105 == self.inp.start(self.pos + 1) + self.inp.len(self.pos + 1) 105 && self.inp.token_start_at(self.pos + 2)
106 == self.inp.token_start_at(self.pos + 1) + self.inp.len(self.pos + 1)
106 { 107 {
107 Some((c1, c2, c3)) 108 Some((c1, c2, c3))
108 } else { 109 } else {
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
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 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 "";