aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/parsing/parser_impl
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/parsing/parser_impl')
-rw-r--r--crates/ra_syntax/src/parsing/parser_impl/input.rs69
1 files changed, 31 insertions, 38 deletions
diff --git a/crates/ra_syntax/src/parsing/parser_impl/input.rs b/crates/ra_syntax/src/parsing/parser_impl/input.rs
index 275d94918..8ebbd3825 100644
--- a/crates/ra_syntax/src/parsing/parser_impl/input.rs
+++ b/crates/ra_syntax/src/parsing/parser_impl/input.rs
@@ -1,10 +1,40 @@
1use crate::{ 1use crate::{
2 SyntaxKind, SyntaxKind::EOF, TextRange, TextUnit, 2 SyntaxKind, SyntaxKind::EOF, TextRange, TextUnit,
3 parsing::lexer::Token, 3 parsing::{
4 parser_impl::TokenSource,
5 lexer::Token,
6 },
4}; 7};
5 8
6use std::ops::{Add, AddAssign}; 9use std::ops::{Add, AddAssign};
7 10
11impl<'t> TokenSource for ParserInput<'t> {
12 fn token_kind(&self, pos: InputPosition) -> SyntaxKind {
13 let idx = pos.0 as usize;
14 if !(idx < self.tokens.len()) {
15 return EOF;
16 }
17 self.tokens[idx].kind
18 }
19 fn is_token_joint_to_next(&self, pos: InputPosition) -> bool {
20 let idx_curr = pos.0 as usize;
21 let idx_next = pos.0 as usize;
22 if !(idx_next < self.tokens.len()) {
23 return true;
24 }
25 self.start_offsets[idx_curr] + self.tokens[idx_curr].len == self.start_offsets[idx_next]
26 }
27 fn is_keyword(&self, pos: InputPosition, kw: &str) -> bool {
28 let idx = pos.0 as usize;
29 if !(idx < self.tokens.len()) {
30 return false;
31 }
32 let range = TextRange::offset_len(self.start_offsets[idx], self.tokens[idx].len);
33
34 self.text[range] == *kw
35 }
36}
37
8pub(crate) struct ParserInput<'t> { 38pub(crate) struct ParserInput<'t> {
9 text: &'t str, 39 text: &'t str,
10 /// start position of each token(expect whitespace and comment) 40 /// start position of each token(expect whitespace and comment)
@@ -41,43 +71,6 @@ impl<'t> ParserInput<'t> {
41 71
42 ParserInput { text, start_offsets, tokens } 72 ParserInput { text, start_offsets, tokens }
43 } 73 }
44
45 /// Get the syntax kind of token at given input position.
46 pub fn kind(&self, pos: InputPosition) -> SyntaxKind {
47 let idx = pos.0 as usize;
48 if !(idx < self.tokens.len()) {
49 return EOF;
50 }
51 self.tokens[idx].kind
52 }
53
54 /// Get the length of a token at given input position.
55 pub fn token_len(&self, pos: InputPosition) -> TextUnit {
56 let idx = pos.0 as usize;
57 if !(idx < self.tokens.len()) {
58 return 0.into();
59 }
60 self.tokens[idx].len
61 }
62
63 /// Get the start position of a taken at given input position.
64 pub fn token_start_at(&self, pos: InputPosition) -> TextUnit {
65 let idx = pos.0 as usize;
66 if !(idx < self.tokens.len()) {
67 return 0.into();
68 }
69 self.start_offsets[idx]
70 }
71
72 /// Get the raw text of a token at given input position.
73 pub fn token_text(&self, pos: InputPosition) -> &'t str {
74 let idx = pos.0 as usize;
75 if !(idx < self.tokens.len()) {
76 return "";
77 }
78 let range = TextRange::offset_len(self.start_offsets[idx], self.tokens[idx].len);
79 &self.text[range]
80 }
81} 74}
82 75
83#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] 76#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]