aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/parser_impl.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/parser_impl.rs')
-rw-r--r--crates/ra_syntax/src/parser_impl.rs71
1 files changed, 43 insertions, 28 deletions
diff --git a/crates/ra_syntax/src/parser_impl.rs b/crates/ra_syntax/src/parser_impl.rs
index cb6e370ac..01a51cd8d 100644
--- a/crates/ra_syntax/src/parser_impl.rs
+++ b/crates/ra_syntax/src/parser_impl.rs
@@ -22,10 +22,21 @@ use crate::SyntaxKind::{self, EOF, TOMBSTONE};
22pub(crate) trait Sink { 22pub(crate) trait Sink {
23 type Tree; 23 type Tree;
24 24
25 /// Adds new leaf to the current branch.
25 fn leaf(&mut self, kind: SyntaxKind, text: SmolStr); 26 fn leaf(&mut self, kind: SyntaxKind, text: SmolStr);
26 fn start_internal(&mut self, kind: SyntaxKind); 27
27 fn finish_internal(&mut self); 28 /// Start new branch and make it current.
29 fn start_branch(&mut self, kind: SyntaxKind);
30
31 /// Finish current branch and restore previous
32 /// branch as current.
33 fn finish_branch(&mut self);
34
28 fn error(&mut self, error: SyntaxError); 35 fn error(&mut self, error: SyntaxError);
36
37 /// Complete tree building. Make sure that
38 /// `start_branch` and `finish_branch` calls
39 /// are paired!
29 fn finish(self) -> Self::Tree; 40 fn finish(self) -> Self::Tree;
30} 41}
31 42
@@ -52,8 +63,7 @@ pub(crate) fn parse_with<S: Sink>(
52/// to a separate struct in order not to pollute 63/// to a separate struct in order not to pollute
53/// the public API of the `Parser`. 64/// the public API of the `Parser`.
54pub(crate) struct ParserImpl<'t> { 65pub(crate) struct ParserImpl<'t> {
55 inp: &'t ParserInput<'t>, 66 parser_input: &'t ParserInput<'t>,
56
57 pos: InputPosition, 67 pos: InputPosition,
58 events: Vec<Event>, 68 events: Vec<Event>,
59 steps: Cell<u32>, 69 steps: Cell<u32>,
@@ -62,8 +72,7 @@ pub(crate) struct ParserImpl<'t> {
62impl<'t> ParserImpl<'t> { 72impl<'t> ParserImpl<'t> {
63 pub(crate) fn new(inp: &'t ParserInput<'t>) -> ParserImpl<'t> { 73 pub(crate) fn new(inp: &'t ParserInput<'t>) -> ParserImpl<'t> {
64 ParserImpl { 74 ParserImpl {
65 inp, 75 parser_input: inp,
66
67 pos: InputPosition::new(), 76 pos: InputPosition::new(),
68 events: Vec::new(), 77 events: Vec::new(),
69 steps: Cell::new(0), 78 steps: Cell::new(0),
@@ -76,9 +85,11 @@ impl<'t> ParserImpl<'t> {
76 } 85 }
77 86
78 pub(super) fn next2(&self) -> Option<(SyntaxKind, SyntaxKind)> { 87 pub(super) fn next2(&self) -> Option<(SyntaxKind, SyntaxKind)> {
79 let c1 = self.inp.kind(self.pos); 88 let c1 = self.parser_input.kind(self.pos);
80 let c2 = self.inp.kind(self.pos + 1); 89 let c2 = self.parser_input.kind(self.pos + 1);
81 if self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos) { 90 if self.parser_input.token_start_at(self.pos + 1)
91 == self.parser_input.token_start_at(self.pos) + self.parser_input.token_len(self.pos)
92 {
82 Some((c1, c2)) 93 Some((c1, c2))
83 } else { 94 } else {
84 None 95 None
@@ -86,12 +97,14 @@ impl<'t> ParserImpl<'t> {
86 } 97 }
87 98
88 pub(super) fn next3(&self) -> Option<(SyntaxKind, SyntaxKind, SyntaxKind)> { 99 pub(super) fn next3(&self) -> Option<(SyntaxKind, SyntaxKind, SyntaxKind)> {
89 let c1 = self.inp.kind(self.pos); 100 let c1 = self.parser_input.kind(self.pos);
90 let c2 = self.inp.kind(self.pos + 1); 101 let c2 = self.parser_input.kind(self.pos + 1);
91 let c3 = self.inp.kind(self.pos + 2); 102 let c3 = self.parser_input.kind(self.pos + 2);
92 if self.inp.start(self.pos + 1) == self.inp.start(self.pos) + self.inp.len(self.pos) 103 if self.parser_input.token_start_at(self.pos + 1)
93 && self.inp.start(self.pos + 2) 104 == self.parser_input.token_start_at(self.pos) + self.parser_input.token_len(self.pos)
94 == self.inp.start(self.pos + 1) + self.inp.len(self.pos + 1) 105 && self.parser_input.token_start_at(self.pos + 2)
106 == self.parser_input.token_start_at(self.pos + 1)
107 + self.parser_input.token_len(self.pos + 1)
95 { 108 {
96 Some((c1, c2, c3)) 109 Some((c1, c2, c3))
97 } else { 110 } else {
@@ -99,29 +112,27 @@ impl<'t> ParserImpl<'t> {
99 } 112 }
100 } 113 }
101 114
115 /// Get the syntax kind of the nth token.
102 pub(super) fn nth(&self, n: u32) -> SyntaxKind { 116 pub(super) fn nth(&self, n: u32) -> SyntaxKind {
103 let steps = self.steps.get(); 117 let steps = self.steps.get();
104 if steps > 10_000_000 { 118 assert!(steps <= 10_000_000, "the parser seems stuck");
105 panic!("the parser seems stuck");
106 }
107 self.steps.set(steps + 1); 119 self.steps.set(steps + 1);
108 120
109 self.inp.kind(self.pos + n) 121 self.parser_input.kind(self.pos + n)
110 } 122 }
111 123
112 pub(super) fn at_kw(&self, t: &str) -> bool { 124 pub(super) fn at_kw(&self, t: &str) -> bool {
113 self.inp.text(self.pos) == t 125 self.parser_input.token_text(self.pos) == t
114 } 126 }
115 127
128 /// Start parsing right behind the last event.
116 pub(super) fn start(&mut self) -> u32 { 129 pub(super) fn start(&mut self) -> u32 {
117 let pos = self.events.len() as u32; 130 let pos = self.events.len() as u32;
118 self.event(Event::Start { 131 self.push_event(Event::tombstone());
119 kind: TOMBSTONE,
120 forward_parent: None,
121 });
122 pos 132 pos
123 } 133 }
124 134
135 /// Advances the parser by one token unconditionally.
125 pub(super) fn bump(&mut self) { 136 pub(super) fn bump(&mut self) {
126 let kind = self.nth(0); 137 let kind = self.nth(0);
127 if kind == EOF { 138 if kind == EOF {
@@ -144,15 +155,17 @@ impl<'t> ParserImpl<'t> {
144 155
145 fn do_bump(&mut self, kind: SyntaxKind, n_raw_tokens: u8) { 156 fn do_bump(&mut self, kind: SyntaxKind, n_raw_tokens: u8) {
146 self.pos += u32::from(n_raw_tokens); 157 self.pos += u32::from(n_raw_tokens);
147 self.event(Event::Token { kind, n_raw_tokens }); 158 self.push_event(Event::Token { kind, n_raw_tokens });
148 } 159 }
149 160
161 /// Append one Error event to the back of events.
150 pub(super) fn error(&mut self, msg: String) { 162 pub(super) fn error(&mut self, msg: String) {
151 self.event(Event::Error { 163 self.push_event(Event::Error {
152 msg: ParseError(msg), 164 msg: ParseError(msg),
153 }) 165 })
154 } 166 }
155 167
168 /// Complete an event with appending a `Finish` event.
156 pub(super) fn complete(&mut self, pos: u32, kind: SyntaxKind) { 169 pub(super) fn complete(&mut self, pos: u32, kind: SyntaxKind) {
157 match self.events[pos as usize] { 170 match self.events[pos as usize] {
158 Event::Start { 171 Event::Start {
@@ -162,9 +175,10 @@ impl<'t> ParserImpl<'t> {
162 } 175 }
163 _ => unreachable!(), 176 _ => unreachable!(),
164 } 177 }
165 self.event(Event::Finish); 178 self.push_event(Event::Finish);
166 } 179 }
167 180
181 /// Ignore the dummy `Start` event.
168 pub(super) fn abandon(&mut self, pos: u32) { 182 pub(super) fn abandon(&mut self, pos: u32) {
169 let idx = pos as usize; 183 let idx = pos as usize;
170 if idx == self.events.len() - 1 { 184 if idx == self.events.len() - 1 {
@@ -178,6 +192,7 @@ impl<'t> ParserImpl<'t> {
178 } 192 }
179 } 193 }
180 194
195 /// Save the relative distance of a completed event to its forward_parent.
181 pub(super) fn precede(&mut self, pos: u32) -> u32 { 196 pub(super) fn precede(&mut self, pos: u32) -> u32 {
182 let new_pos = self.start(); 197 let new_pos = self.start();
183 match self.events[pos as usize] { 198 match self.events[pos as usize] {
@@ -192,7 +207,7 @@ impl<'t> ParserImpl<'t> {
192 new_pos 207 new_pos
193 } 208 }
194 209
195 fn event(&mut self, event: Event) { 210 fn push_event(&mut self, event: Event) {
196 self.events.push(event) 211 self.events.push(event)
197 } 212 }
198} 213}