aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_parser/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-09-10 19:28:27 +0100
committerAleksey Kladov <[email protected]>2019-09-10 19:28:27 +0100
commitb9d9db83d1ac85e83e018333f7aa4c5c5616e82b (patch)
treed486002e8f5a4866716d7640b26562e340b7a347 /crates/ra_parser/src
parent1140a83c1b393cdcd18e42d5d816fd8be348b059 (diff)
cleanup dollar handling in expressions
Diffstat (limited to 'crates/ra_parser/src')
-rw-r--r--crates/ra_parser/src/grammar/expressions.rs49
-rw-r--r--crates/ra_parser/src/grammar/expressions/atom.rs25
-rw-r--r--crates/ra_parser/src/parser.rs73
3 files changed, 51 insertions, 96 deletions
diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs
index ea04b9458..1dd9a586c 100644
--- a/crates/ra_parser/src/grammar/expressions.rs
+++ b/crates/ra_parser/src/grammar/expressions.rs
@@ -14,20 +14,17 @@ const EXPR_FIRST: TokenSet = LHS_FIRST;
14 14
15pub(super) fn expr(p: &mut Parser) -> BlockLike { 15pub(super) fn expr(p: &mut Parser) -> BlockLike {
16 let r = Restrictions { forbid_structs: false, prefer_stmt: false }; 16 let r = Restrictions { forbid_structs: false, prefer_stmt: false };
17 let mut dollar_lvl = 0; 17 expr_bp(p, r, 1).1
18 expr_bp(p, r, 1, &mut dollar_lvl).1
19} 18}
20 19
21pub(super) fn expr_stmt(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) { 20pub(super) fn expr_stmt(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) {
22 let r = Restrictions { forbid_structs: false, prefer_stmt: true }; 21 let r = Restrictions { forbid_structs: false, prefer_stmt: true };
23 let mut dollar_lvl = 0; 22 expr_bp(p, r, 1)
24 expr_bp(p, r, 1, &mut dollar_lvl)
25} 23}
26 24
27fn expr_no_struct(p: &mut Parser) { 25fn expr_no_struct(p: &mut Parser) {
28 let r = Restrictions { forbid_structs: true, prefer_stmt: false }; 26 let r = Restrictions { forbid_structs: true, prefer_stmt: false };
29 let mut dollar_lvl = 0; 27 expr_bp(p, r, 1);
30 expr_bp(p, r, 1, &mut dollar_lvl);
31} 28}
32 29
33// test block 30// test block
@@ -257,23 +254,8 @@ fn current_op(p: &Parser) -> (u8, SyntaxKind) {
257} 254}
258 255
259// Parses expression with binding power of at least bp. 256// Parses expression with binding power of at least bp.
260fn expr_bp( 257fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option<CompletedMarker>, BlockLike) {
261 p: &mut Parser, 258 let mut lhs = match lhs(p, r) {
262 r: Restrictions,
263 mut bp: u8,
264 dollar_lvl: &mut usize,
265) -> (Option<CompletedMarker>, BlockLike) {
266 // `newly_dollar_open` is a flag indicated that dollar is just closed after lhs, e.g.
267 // `$1$ + a`
268 // We use this flag to skip handling it.
269 let mut newly_dollar_open = if p.at_l_dollar() {
270 *dollar_lvl += p.eat_l_dollars();
271 true
272 } else {
273 false
274 };
275
276 let mut lhs = match lhs(p, r, dollar_lvl) {
277 Some((lhs, blocklike)) => { 259 Some((lhs, blocklike)) => {
278 // test stmt_bin_expr_ambiguity 260 // test stmt_bin_expr_ambiguity
279 // fn foo() { 261 // fn foo() {
@@ -289,15 +271,6 @@ fn expr_bp(
289 }; 271 };
290 272
291 loop { 273 loop {
292 if *dollar_lvl > 0 && p.at_r_dollar() {
293 *dollar_lvl -= p.eat_r_dollars(*dollar_lvl);
294 if !newly_dollar_open {
295 // We "pump" bp for make it highest priority
296 bp = 255;
297 }
298 newly_dollar_open = false;
299 }
300
301 let is_range = p.at(T![..]) || p.at(T![..=]); 274 let is_range = p.at(T![..]) || p.at(T![..=]);
302 let (op_bp, op) = current_op(p); 275 let (op_bp, op) = current_op(p);
303 if op_bp < bp { 276 if op_bp < bp {
@@ -306,7 +279,7 @@ fn expr_bp(
306 let m = lhs.precede(p); 279 let m = lhs.precede(p);
307 p.bump(op); 280 p.bump(op);
308 281
309 expr_bp(p, r, op_bp + 1, dollar_lvl); 282 expr_bp(p, r, op_bp + 1);
310 lhs = m.complete(p, if is_range { RANGE_EXPR } else { BIN_EXPR }); 283 lhs = m.complete(p, if is_range { RANGE_EXPR } else { BIN_EXPR });
311 } 284 }
312 (Some(lhs), BlockLike::NotBlock) 285 (Some(lhs), BlockLike::NotBlock)
@@ -314,11 +287,7 @@ fn expr_bp(
314 287
315const LHS_FIRST: TokenSet = atom::ATOM_EXPR_FIRST.union(token_set![AMP, STAR, EXCL, DOT, MINUS]); 288const LHS_FIRST: TokenSet = atom::ATOM_EXPR_FIRST.union(token_set![AMP, STAR, EXCL, DOT, MINUS]);
316 289
317fn lhs( 290fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> {
318 p: &mut Parser,
319 r: Restrictions,
320 dollar_lvl: &mut usize,
321) -> Option<(CompletedMarker, BlockLike)> {
322 let m; 291 let m;
323 let kind = match p.current() { 292 let kind = match p.current() {
324 // test ref_expr 293 // test ref_expr
@@ -351,7 +320,7 @@ fn lhs(
351 m = p.start(); 320 m = p.start();
352 p.bump(op); 321 p.bump(op);
353 if p.at_ts(EXPR_FIRST) { 322 if p.at_ts(EXPR_FIRST) {
354 expr_bp(p, r, 2, dollar_lvl); 323 expr_bp(p, r, 2);
355 } 324 }
356 return Some((m.complete(p, RANGE_EXPR), BlockLike::NotBlock)); 325 return Some((m.complete(p, RANGE_EXPR), BlockLike::NotBlock));
357 } 326 }
@@ -367,7 +336,7 @@ fn lhs(
367 return Some(postfix_expr(p, lhs, blocklike, !(r.prefer_stmt && blocklike.is_block()))); 336 return Some(postfix_expr(p, lhs, blocklike, !(r.prefer_stmt && blocklike.is_block())));
368 } 337 }
369 }; 338 };
370 expr_bp(p, r, 255, dollar_lvl); 339 expr_bp(p, r, 255);
371 Some((m.complete(p, kind), BlockLike::NotBlock)) 340 Some((m.complete(p, kind), BlockLike::NotBlock))
372} 341}
373 342
diff --git a/crates/ra_parser/src/grammar/expressions/atom.rs b/crates/ra_parser/src/grammar/expressions/atom.rs
index cea79cf6f..6e295fbf9 100644
--- a/crates/ra_parser/src/grammar/expressions/atom.rs
+++ b/crates/ra_parser/src/grammar/expressions/atom.rs
@@ -69,6 +69,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
69 let done = match p.current() { 69 let done = match p.current() {
70 T!['('] => tuple_expr(p), 70 T!['('] => tuple_expr(p),
71 T!['['] => array_expr(p), 71 T!['['] => array_expr(p),
72 L_DOLLAR => meta_var_expr(p),
72 T![|] => lambda_expr(p), 73 T![|] => lambda_expr(p),
73 T![move] if la == T![|] => lambda_expr(p), 74 T![move] if la == T![|] => lambda_expr(p),
74 T![async] if la == T![|] || (la == T![move] && p.nth(2) == T![|]) => lambda_expr(p), 75 T![async] if la == T![|] || (la == T![move] && p.nth(2) == T![|]) => lambda_expr(p),
@@ -554,3 +555,27 @@ fn box_expr(p: &mut Parser, m: Option<Marker>) -> CompletedMarker {
554 } 555 }
555 m.complete(p, BOX_EXPR) 556 m.complete(p, BOX_EXPR)
556} 557}
558
559/// Expression from `$var` macro expansion, wrapped in dollars
560fn meta_var_expr(p: &mut Parser) -> CompletedMarker {
561 assert!(p.at(L_DOLLAR));
562 let m = p.start();
563 p.bump(L_DOLLAR);
564 let (completed, _is_block) =
565 expr_bp(p, Restrictions { forbid_structs: false, prefer_stmt: false }, 1);
566
567 match (completed, p.current()) {
568 (Some(it), R_DOLLAR) => {
569 p.bump(R_DOLLAR);
570 m.abandon(p);
571 it
572 }
573 _ => {
574 while !p.at(R_DOLLAR) {
575 p.bump_any()
576 }
577 p.bump(R_DOLLAR);
578 m.complete(p, ERROR)
579 }
580 }
581}
diff --git a/crates/ra_parser/src/parser.rs b/crates/ra_parser/src/parser.rs
index e7281123b..a2ac363fb 100644
--- a/crates/ra_parser/src/parser.rs
+++ b/crates/ra_parser/src/parser.rs
@@ -5,7 +5,7 @@ use drop_bomb::DropBomb;
5use crate::{ 5use crate::{
6 event::Event, 6 event::Event,
7 ParseError, 7 ParseError,
8 SyntaxKind::{self, EOF, ERROR, TOMBSTONE}, 8 SyntaxKind::{self, EOF, ERROR, L_DOLLAR, R_DOLLAR, TOMBSTONE},
9 TokenSet, TokenSource, T, 9 TokenSet, TokenSource, T,
10}; 10};
11 11
@@ -211,19 +211,26 @@ impl<'t> Parser<'t> {
211 211
212 /// Create an error node and consume the next token. 212 /// Create an error node and consume the next token.
213 pub(crate) fn err_recover(&mut self, message: &str, recovery: TokenSet) { 213 pub(crate) fn err_recover(&mut self, message: &str, recovery: TokenSet) {
214 if self.at(T!['{']) || self.at(T!['}']) || self.at_ts(recovery) { 214 match self.current() {
215 self.error(message); 215 T!['{'] | T!['}'] | L_DOLLAR | R_DOLLAR => {
216 } else { 216 self.error(message);
217 let m = self.start(); 217 return;
218 }
219 _ => (),
220 }
221
222 if self.at_ts(recovery) {
218 self.error(message); 223 self.error(message);
219 self.bump_any(); 224 return;
220 m.complete(self, ERROR); 225 }
221 }; 226
227 let m = self.start();
228 self.error(message);
229 self.bump_any();
230 m.complete(self, ERROR);
222 } 231 }
223 232
224 fn do_bump(&mut self, kind: SyntaxKind, n_raw_tokens: u8) { 233 fn do_bump(&mut self, kind: SyntaxKind, n_raw_tokens: u8) {
225 // self.eat_dollars();
226
227 for _ in 0..n_raw_tokens { 234 for _ in 0..n_raw_tokens {
228 self.token_source.bump(); 235 self.token_source.bump();
229 } 236 }
@@ -234,52 +241,6 @@ impl<'t> Parser<'t> {
234 fn push_event(&mut self, event: Event) { 241 fn push_event(&mut self, event: Event) {
235 self.events.push(event) 242 self.events.push(event)
236 } 243 }
237
238 pub(crate) fn eat_l_dollars(&mut self) -> usize {
239 let mut ate_count = 0;
240 loop {
241 match self.token_source.current().kind {
242 k @ SyntaxKind::L_DOLLAR => {
243 self.token_source.bump();
244 self.push_event(Event::Token { kind: k, n_raw_tokens: 1 });
245 ate_count += 1;
246 }
247 _ => {
248 return ate_count;
249 }
250 }
251 }
252 }
253
254 pub(crate) fn eat_r_dollars(&mut self, max_count: usize) -> usize {
255 let mut ate_count = 0;
256 loop {
257 match self.token_source.current().kind {
258 k @ SyntaxKind::R_DOLLAR => {
259 self.token_source.bump();
260 self.push_event(Event::Token { kind: k, n_raw_tokens: 1 });
261 ate_count += 1;
262
263 if max_count >= ate_count {
264 return ate_count;
265 }
266 }
267 _ => {
268 return ate_count;
269 }
270 }
271 }
272 }
273
274 pub(crate) fn at_l_dollar(&self) -> bool {
275 let kind = self.token_source.current().kind;
276 (kind == SyntaxKind::L_DOLLAR)
277 }
278
279 pub(crate) fn at_r_dollar(&self) -> bool {
280 let kind = self.token_source.current().kind;
281 (kind == SyntaxKind::R_DOLLAR)
282 }
283} 244}
284 245
285/// See `Parser::start`. 246/// See `Parser::start`.