aboutsummaryrefslogtreecommitdiff
path: root/crates/libsyntax2/src/parser_impl/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/libsyntax2/src/parser_impl/mod.rs')
-rw-r--r--crates/libsyntax2/src/parser_impl/mod.rs194
1 files changed, 0 insertions, 194 deletions
diff --git a/crates/libsyntax2/src/parser_impl/mod.rs b/crates/libsyntax2/src/parser_impl/mod.rs
deleted file mode 100644
index b343b404f..000000000
--- a/crates/libsyntax2/src/parser_impl/mod.rs
+++ /dev/null
@@ -1,194 +0,0 @@
1mod event;
2mod input;
3
4use std::cell::Cell;
5
6use {
7 lexer::Token,
8 parser_api::Parser,
9 parser_impl::{
10 event::{process, Event},
11 input::{InputPosition, ParserInput},
12 },
13 TextUnit,
14};
15
16use SyntaxKind::{self, EOF, TOMBSTONE};
17
18pub(crate) trait Sink<'a> {
19 type Tree;
20
21 fn new(text: &'a str) -> Self;
22
23 fn leaf(&mut self, kind: SyntaxKind, len: TextUnit);
24 fn start_internal(&mut self, kind: SyntaxKind);
25 fn finish_internal(&mut self);
26 fn error(&mut self, err: String);
27 fn finish(self) -> Self::Tree;
28}
29
30/// Parse a sequence of tokens into the representative node tree
31pub(crate) fn parse_with<'a, S: Sink<'a>>(
32 text: &'a str,
33 tokens: &[Token],
34 parser: fn(&mut Parser),
35) -> S::Tree {
36 let events = {
37 let input = input::ParserInput::new(text, tokens);
38 let parser_impl = ParserImpl::new(&input);
39 let mut parser_api = Parser(parser_impl);
40 parser(&mut parser_api);
41 parser_api.0.into_events()
42 };
43 let mut sink = S::new(text);
44 process(&mut sink, tokens, events);
45 sink.finish()
46}
47
48/// Implementation details of `Parser`, extracted
49/// to a separate struct in order not to pollute
50/// the public API of the `Parser`.
51pub(crate) struct ParserImpl<'t> {
52 inp: &'t ParserInput<'t>,
53
54 pos: InputPosition,
55 events: Vec<Event>,
56 steps: Cell<u32>,
57}
58
59impl<'t> ParserImpl<'t> {
60 pub(crate) fn new(inp: &'t ParserInput<'t>) -> ParserImpl<'t> {
61 ParserImpl {
62 inp,
63
64 pos: InputPosition::new(),
65 events: Vec::new(),
66 steps: Cell::new(0),
67 }
68 }
69
70 pub(crate) fn into_events(self) -> Vec<Event> {
71 assert_eq!(self.nth(0), EOF);
72 self.events
73 }
74
75 pub(super) fn next2(&self) -> Option<(SyntaxKind, SyntaxKind)> {
76 let c1 = self.inp.kind(self.pos);
77 let c2 = self.inp.kind(self.pos + 1);
78 if self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos) {
79 Some((c1, c2))
80 } else {
81 None
82 }
83 }
84
85 pub(super) fn next3(&self) -> Option<(SyntaxKind, SyntaxKind, SyntaxKind)> {
86 let c1 = self.inp.kind(self.pos);
87 let c2 = self.inp.kind(self.pos + 1);
88 let c3 = self.inp.kind(self.pos + 2);
89 if self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos)
90 && self.inp.start(self.pos + 2) == self.inp.start(self.pos + 1) + self.inp.len(self.pos + 1){
91 Some((c1, c2, c3))
92 } else {
93 None
94 }
95 }
96
97 pub(super) fn nth(&self, n: u32) -> SyntaxKind {
98 let steps = self.steps.get();
99 if steps > 10_000_000 {
100 panic!("the parser seems stuck");
101 }
102 self.steps.set(steps + 1);
103
104 self.inp.kind(self.pos + n)
105 }
106
107 pub(super) fn at_kw(&self, t: &str) -> bool {
108 self.inp.text(self.pos) == t
109 }
110
111 pub(super) fn start(&mut self) -> u32 {
112 let pos = self.events.len() as u32;
113 self.event(Event::Start {
114 kind: TOMBSTONE,
115 forward_parent: None,
116 });
117 pos
118 }
119
120 pub(super) fn bump(&mut self) {
121 let kind = self.nth(0);
122 if kind == EOF {
123 return;
124 }
125 self.do_bump(kind, 1);
126 }
127
128 pub(super) fn bump_remap(&mut self, kind: SyntaxKind) {
129 if self.nth(0) == EOF {
130 // TODO: panic!?
131 return;
132 }
133 self.do_bump(kind, 1);
134 }
135
136 pub(super) fn bump_compound(&mut self, kind: SyntaxKind, n: u8) {
137 self.do_bump(kind, n);
138 }
139
140 fn do_bump(&mut self, kind: SyntaxKind, n_raw_tokens: u8) {
141 self.pos += u32::from(n_raw_tokens);
142 self.event(Event::Token {
143 kind,
144 n_raw_tokens,
145 });
146 }
147
148 pub(super) fn error(&mut self, msg: String) {
149 self.event(Event::Error { msg })
150 }
151
152 pub(super) fn complete(&mut self, pos: u32, kind: SyntaxKind) {
153 match self.events[pos as usize] {
154 Event::Start {
155 kind: ref mut slot, ..
156 } => {
157 *slot = kind;
158 }
159 _ => unreachable!(),
160 }
161 self.event(Event::Finish);
162 }
163
164 pub(super) fn abandon(&mut self, pos: u32) {
165 let idx = pos as usize;
166 if idx == self.events.len() - 1 {
167 match self.events.pop() {
168 Some(Event::Start {
169 kind: TOMBSTONE,
170 forward_parent: None,
171 }) => (),
172 _ => unreachable!(),
173 }
174 }
175 }
176
177 pub(super) fn precede(&mut self, pos: u32) -> u32 {
178 let new_pos = self.start();
179 match self.events[pos as usize] {
180 Event::Start {
181 ref mut forward_parent,
182 ..
183 } => {
184 *forward_parent = Some(new_pos - pos);
185 }
186 _ => unreachable!(),
187 }
188 new_pos
189 }
190
191 fn event(&mut self, event: Event) {
192 self.events.push(event)
193 }
194}