1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
use {Token, TextUnit, SyntaxKind};
use syntax_kinds::*;
pub(crate) enum Event {
Start { kind: SyntaxKind },
Finish,
Token {
kind: SyntaxKind,
n_raw_tokens: u8,
}
}
pub(crate) fn parse<'t>(text: &'t str, raw_tokens: &'t [Token]) -> Vec<Event> {
let mut parser = Parser::new(text, raw_tokens);
parse_file(&mut parser);
parser.events
}
struct Parser<'t> {
text: &'t str,
raw_tokens: &'t [Token],
non_ws_tokens: Vec<(usize, TextUnit)>,
pos: usize,
events: Vec<Event>,
}
impl<'t> Parser<'t> {
fn new(text: &'t str, raw_tokens: &'t [Token]) -> Parser<'t> {
let mut non_ws_tokens = Vec::new();
let mut len = TextUnit::new(0);
for (idx, &token) in raw_tokens.iter().enumerate() {
match token.kind {
WHITESPACE | COMMENT => (),
_ => non_ws_tokens.push((idx, len)),
}
len += token.len;
}
Parser {
text,
raw_tokens,
non_ws_tokens,
pos: 0,
events: Vec::new(),
}
}
fn start(&mut self, kind: SyntaxKind) {
self.event(Event::Start { kind });
}
fn finish(&mut self) {
self.event(Event::Finish);
}
fn event(&mut self, event: Event) {
self.events.push(event)
}
}
fn parse_file(p: &mut Parser) {
p.start(FILE);
p.finish();
}
|