diff options
Diffstat (limited to 'crates/ra_syntax/src/parser_impl.rs')
-rw-r--r-- | crates/ra_syntax/src/parser_impl.rs | 55 |
1 files changed, 29 insertions, 26 deletions
diff --git a/crates/ra_syntax/src/parser_impl.rs b/crates/ra_syntax/src/parser_impl.rs index ce321aecb..01a51cd8d 100644 --- a/crates/ra_syntax/src/parser_impl.rs +++ b/crates/ra_syntax/src/parser_impl.rs | |||
@@ -63,7 +63,7 @@ pub(crate) fn parse_with<S: Sink>( | |||
63 | /// to a separate struct in order not to pollute | 63 | /// to a separate struct in order not to pollute |
64 | /// the public API of the `Parser`. | 64 | /// the public API of the `Parser`. |
65 | pub(crate) struct ParserImpl<'t> { | 65 | pub(crate) struct ParserImpl<'t> { |
66 | inp: &'t ParserInput<'t>, | 66 | parser_input: &'t ParserInput<'t>, |
67 | pos: InputPosition, | 67 | pos: InputPosition, |
68 | events: Vec<Event>, | 68 | events: Vec<Event>, |
69 | steps: Cell<u32>, | 69 | steps: Cell<u32>, |
@@ -72,7 +72,7 @@ pub(crate) struct ParserImpl<'t> { | |||
72 | impl<'t> ParserImpl<'t> { | 72 | impl<'t> ParserImpl<'t> { |
73 | pub(crate) fn new(inp: &'t ParserInput<'t>) -> ParserImpl<'t> { | 73 | pub(crate) fn new(inp: &'t ParserInput<'t>) -> ParserImpl<'t> { |
74 | ParserImpl { | 74 | ParserImpl { |
75 | inp, | 75 | parser_input: inp, |
76 | pos: InputPosition::new(), | 76 | pos: InputPosition::new(), |
77 | events: Vec::new(), | 77 | events: Vec::new(), |
78 | steps: Cell::new(0), | 78 | steps: Cell::new(0), |
@@ -85,10 +85,10 @@ impl<'t> ParserImpl<'t> { | |||
85 | } | 85 | } |
86 | 86 | ||
87 | pub(super) fn next2(&self) -> Option<(SyntaxKind, SyntaxKind)> { | 87 | pub(super) fn next2(&self) -> Option<(SyntaxKind, SyntaxKind)> { |
88 | let c1 = self.inp.kind(self.pos); | 88 | let c1 = self.parser_input.kind(self.pos); |
89 | let c2 = self.inp.kind(self.pos + 1); | 89 | let c2 = self.parser_input.kind(self.pos + 1); |
90 | if self.inp.token_start_at(self.pos + 1) | 90 | if self.parser_input.token_start_at(self.pos + 1) |
91 | == self.inp.token_start_at(self.pos) + self.inp.len(self.pos) | 91 | == self.parser_input.token_start_at(self.pos) + self.parser_input.token_len(self.pos) |
92 | { | 92 | { |
93 | Some((c1, c2)) | 93 | Some((c1, c2)) |
94 | } else { | 94 | } else { |
@@ -97,13 +97,14 @@ impl<'t> ParserImpl<'t> { | |||
97 | } | 97 | } |
98 | 98 | ||
99 | pub(super) fn next3(&self) -> Option<(SyntaxKind, SyntaxKind, SyntaxKind)> { | 99 | pub(super) fn next3(&self) -> Option<(SyntaxKind, SyntaxKind, SyntaxKind)> { |
100 | let c1 = self.inp.kind(self.pos); | 100 | let c1 = self.parser_input.kind(self.pos); |
101 | let c2 = self.inp.kind(self.pos + 1); | 101 | let c2 = self.parser_input.kind(self.pos + 1); |
102 | let c3 = self.inp.kind(self.pos + 2); | 102 | let c3 = self.parser_input.kind(self.pos + 2); |
103 | if self.inp.token_start_at(self.pos + 1) | 103 | if self.parser_input.token_start_at(self.pos + 1) |
104 | == self.inp.token_start_at(self.pos) + self.inp.len(self.pos) | 104 | == self.parser_input.token_start_at(self.pos) + self.parser_input.token_len(self.pos) |
105 | && self.inp.token_start_at(self.pos + 2) | 105 | && self.parser_input.token_start_at(self.pos + 2) |
106 | == self.inp.token_start_at(self.pos + 1) + self.inp.len(self.pos + 1) | 106 | == self.parser_input.token_start_at(self.pos + 1) |
107 | + self.parser_input.token_len(self.pos + 1) | ||
107 | { | 108 | { |
108 | Some((c1, c2, c3)) | 109 | Some((c1, c2, c3)) |
109 | } else { | 110 | } else { |
@@ -111,29 +112,27 @@ impl<'t> ParserImpl<'t> { | |||
111 | } | 112 | } |
112 | } | 113 | } |
113 | 114 | ||
115 | /// Get the syntax kind of the nth token. | ||
114 | pub(super) fn nth(&self, n: u32) -> SyntaxKind { | 116 | pub(super) fn nth(&self, n: u32) -> SyntaxKind { |
115 | let steps = self.steps.get(); | 117 | let steps = self.steps.get(); |
116 | if steps > 10_000_000 { | 118 | assert!(steps <= 10_000_000, "the parser seems stuck"); |
117 | panic!("the parser seems stuck"); | ||
118 | } | ||
119 | self.steps.set(steps + 1); | 119 | self.steps.set(steps + 1); |
120 | 120 | ||
121 | self.inp.kind(self.pos + n) | 121 | self.parser_input.kind(self.pos + n) |
122 | } | 122 | } |
123 | 123 | ||
124 | pub(super) fn at_kw(&self, t: &str) -> bool { | 124 | pub(super) fn at_kw(&self, t: &str) -> bool { |
125 | self.inp.text(self.pos) == t | 125 | self.parser_input.token_text(self.pos) == t |
126 | } | 126 | } |
127 | 127 | ||
128 | /// Start parsing right behind the last event. | ||
128 | pub(super) fn start(&mut self) -> u32 { | 129 | pub(super) fn start(&mut self) -> u32 { |
129 | let pos = self.events.len() as u32; | 130 | let pos = self.events.len() as u32; |
130 | self.event(Event::Start { | 131 | self.push_event(Event::tombstone()); |
131 | kind: TOMBSTONE, | ||
132 | forward_parent: None, | ||
133 | }); | ||
134 | pos | 132 | pos |
135 | } | 133 | } |
136 | 134 | ||
135 | /// Advances the parser by one token unconditionally. | ||
137 | pub(super) fn bump(&mut self) { | 136 | pub(super) fn bump(&mut self) { |
138 | let kind = self.nth(0); | 137 | let kind = self.nth(0); |
139 | if kind == EOF { | 138 | if kind == EOF { |
@@ -156,15 +155,17 @@ impl<'t> ParserImpl<'t> { | |||
156 | 155 | ||
157 | fn do_bump(&mut self, kind: SyntaxKind, n_raw_tokens: u8) { | 156 | fn do_bump(&mut self, kind: SyntaxKind, n_raw_tokens: u8) { |
158 | self.pos += u32::from(n_raw_tokens); | 157 | self.pos += u32::from(n_raw_tokens); |
159 | self.event(Event::Token { kind, n_raw_tokens }); | 158 | self.push_event(Event::Token { kind, n_raw_tokens }); |
160 | } | 159 | } |
161 | 160 | ||
161 | /// Append one Error event to the back of events. | ||
162 | pub(super) fn error(&mut self, msg: String) { | 162 | pub(super) fn error(&mut self, msg: String) { |
163 | self.event(Event::Error { | 163 | self.push_event(Event::Error { |
164 | msg: ParseError(msg), | 164 | msg: ParseError(msg), |
165 | }) | 165 | }) |
166 | } | 166 | } |
167 | 167 | ||
168 | /// Complete an event with appending a `Finish` event. | ||
168 | pub(super) fn complete(&mut self, pos: u32, kind: SyntaxKind) { | 169 | pub(super) fn complete(&mut self, pos: u32, kind: SyntaxKind) { |
169 | match self.events[pos as usize] { | 170 | match self.events[pos as usize] { |
170 | Event::Start { | 171 | Event::Start { |
@@ -174,9 +175,10 @@ impl<'t> ParserImpl<'t> { | |||
174 | } | 175 | } |
175 | _ => unreachable!(), | 176 | _ => unreachable!(), |
176 | } | 177 | } |
177 | self.event(Event::Finish); | 178 | self.push_event(Event::Finish); |
178 | } | 179 | } |
179 | 180 | ||
181 | /// Ignore the dummy `Start` event. | ||
180 | pub(super) fn abandon(&mut self, pos: u32) { | 182 | pub(super) fn abandon(&mut self, pos: u32) { |
181 | let idx = pos as usize; | 183 | let idx = pos as usize; |
182 | if idx == self.events.len() - 1 { | 184 | if idx == self.events.len() - 1 { |
@@ -190,6 +192,7 @@ impl<'t> ParserImpl<'t> { | |||
190 | } | 192 | } |
191 | } | 193 | } |
192 | 194 | ||
195 | /// Save the relative distance of a completed event to its forward_parent. | ||
193 | pub(super) fn precede(&mut self, pos: u32) -> u32 { | 196 | pub(super) fn precede(&mut self, pos: u32) -> u32 { |
194 | let new_pos = self.start(); | 197 | let new_pos = self.start(); |
195 | match self.events[pos as usize] { | 198 | match self.events[pos as usize] { |
@@ -204,7 +207,7 @@ impl<'t> ParserImpl<'t> { | |||
204 | new_pos | 207 | new_pos |
205 | } | 208 | } |
206 | 209 | ||
207 | fn event(&mut self, event: Event) { | 210 | fn push_event(&mut self, event: Event) { |
208 | self.events.push(event) | 211 | self.events.push(event) |
209 | } | 212 | } |
210 | } | 213 | } |