]> git.lizzy.rs Git - rust.git/blob - crates/syntax/src/parsing/text_token_source.rs
Switch parser to use tokens
[rust.git] / crates / syntax / src / parsing / text_token_source.rs
1 //! See `TextTokenSource` docs.
2
3 use parser::TokenSource;
4
5 use crate::{parsing::lexer::Token, SyntaxKind::EOF, TextRange, TextSize};
6
7 /// Implementation of `parser::TokenSource` that takes tokens from source code text.
8 pub(crate) struct TextTokenSource<'t> {
9     text: &'t str,
10     /// token and its start position (non-whitespace/comment tokens)
11     /// ```non-rust
12     ///  struct Foo;
13     ///  ^------^--^-
14     ///  |      |    \________
15     ///  |      \____         \
16     ///  |           \         |
17     ///  (struct, 0) (Foo, 7) (;, 10)
18     /// ```
19     /// `[(struct, 0), (Foo, 7), (;, 10)]`
20     token_offset_pairs: Vec<(Token, TextSize)>,
21
22     /// Current token and position
23     curr: (parser::Token, usize),
24 }
25
26 impl<'t> TokenSource for TextTokenSource<'t> {
27     fn current(&self) -> parser::Token {
28         self.curr.0
29     }
30
31     fn lookahead_nth(&self, n: usize) -> parser::Token {
32         mk_token(self.curr.1 + n, &self.token_offset_pairs)
33     }
34
35     fn bump(&mut self) {
36         if self.curr.0.kind == EOF {
37             return;
38         }
39
40         let pos = self.curr.1 + 1;
41         self.curr = (mk_token(pos, &self.token_offset_pairs), pos);
42     }
43
44     fn is_keyword(&self, kw: &str) -> bool {
45         self.token_offset_pairs
46             .get(self.curr.1)
47             .map_or(false, |(token, offset)| &self.text[TextRange::at(*offset, token.len)] == kw)
48     }
49 }
50
51 fn mk_token(pos: usize, token_offset_pairs: &[(Token, TextSize)]) -> parser::Token {
52     let (kind, is_jointed_to_next) = match token_offset_pairs.get(pos) {
53         Some((token, offset)) => (
54             token.kind,
55             token_offset_pairs
56                 .get(pos + 1)
57                 .map_or(false, |(_, next_offset)| offset + token.len == *next_offset),
58         ),
59         None => (EOF, false),
60     };
61     parser::Token { kind, is_jointed_to_next }
62 }
63
64 impl<'t> TextTokenSource<'t> {
65     /// Generate input from tokens(expect comment and whitespace).
66     pub(crate) fn new(text: &'t str, raw_tokens: &'t [Token]) -> TextTokenSource<'t> {
67         let token_offset_pairs: Vec<_> = raw_tokens
68             .iter()
69             .filter_map({
70                 let mut len = 0.into();
71                 move |token| {
72                     let pair = if token.kind.is_trivia() { None } else { Some((*token, len)) };
73                     len += token.len;
74                     pair
75                 }
76             })
77             .collect();
78
79         let first = mk_token(0, &token_offset_pairs);
80         TextTokenSource { text, token_offset_pairs, curr: (first, 0) }
81     }
82 }