mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
cleanup dollar handling in expressions
This commit is contained in:
parent
1140a83c1b
commit
b9d9db83d1
3 changed files with 51 additions and 96 deletions
|
@ -14,20 +14,17 @@ const EXPR_FIRST: TokenSet = LHS_FIRST;
|
||||||
|
|
||||||
pub(super) fn expr(p: &mut Parser) -> BlockLike {
|
pub(super) fn expr(p: &mut Parser) -> BlockLike {
|
||||||
let r = Restrictions { forbid_structs: false, prefer_stmt: false };
|
let r = Restrictions { forbid_structs: false, prefer_stmt: false };
|
||||||
let mut dollar_lvl = 0;
|
expr_bp(p, r, 1).1
|
||||||
expr_bp(p, r, 1, &mut dollar_lvl).1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn expr_stmt(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) {
|
pub(super) fn expr_stmt(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) {
|
||||||
let r = Restrictions { forbid_structs: false, prefer_stmt: true };
|
let r = Restrictions { forbid_structs: false, prefer_stmt: true };
|
||||||
let mut dollar_lvl = 0;
|
expr_bp(p, r, 1)
|
||||||
expr_bp(p, r, 1, &mut dollar_lvl)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expr_no_struct(p: &mut Parser) {
|
fn expr_no_struct(p: &mut Parser) {
|
||||||
let r = Restrictions { forbid_structs: true, prefer_stmt: false };
|
let r = Restrictions { forbid_structs: true, prefer_stmt: false };
|
||||||
let mut dollar_lvl = 0;
|
expr_bp(p, r, 1);
|
||||||
expr_bp(p, r, 1, &mut dollar_lvl);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// test block
|
// test block
|
||||||
|
@ -257,23 +254,8 @@ fn current_op(p: &Parser) -> (u8, SyntaxKind) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parses expression with binding power of at least bp.
|
// Parses expression with binding power of at least bp.
|
||||||
fn expr_bp(
|
fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option<CompletedMarker>, BlockLike) {
|
||||||
p: &mut Parser,
|
let mut lhs = match lhs(p, r) {
|
||||||
r: Restrictions,
|
|
||||||
mut bp: u8,
|
|
||||||
dollar_lvl: &mut usize,
|
|
||||||
) -> (Option<CompletedMarker>, BlockLike) {
|
|
||||||
// `newly_dollar_open` is a flag indicated that dollar is just closed after lhs, e.g.
|
|
||||||
// `$1$ + a`
|
|
||||||
// We use this flag to skip handling it.
|
|
||||||
let mut newly_dollar_open = if p.at_l_dollar() {
|
|
||||||
*dollar_lvl += p.eat_l_dollars();
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut lhs = match lhs(p, r, dollar_lvl) {
|
|
||||||
Some((lhs, blocklike)) => {
|
Some((lhs, blocklike)) => {
|
||||||
// test stmt_bin_expr_ambiguity
|
// test stmt_bin_expr_ambiguity
|
||||||
// fn foo() {
|
// fn foo() {
|
||||||
|
@ -289,15 +271,6 @@ fn expr_bp(
|
||||||
};
|
};
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if *dollar_lvl > 0 && p.at_r_dollar() {
|
|
||||||
*dollar_lvl -= p.eat_r_dollars(*dollar_lvl);
|
|
||||||
if !newly_dollar_open {
|
|
||||||
// We "pump" bp for make it highest priority
|
|
||||||
bp = 255;
|
|
||||||
}
|
|
||||||
newly_dollar_open = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
let is_range = p.at(T![..]) || p.at(T![..=]);
|
let is_range = p.at(T![..]) || p.at(T![..=]);
|
||||||
let (op_bp, op) = current_op(p);
|
let (op_bp, op) = current_op(p);
|
||||||
if op_bp < bp {
|
if op_bp < bp {
|
||||||
|
@ -306,7 +279,7 @@ fn expr_bp(
|
||||||
let m = lhs.precede(p);
|
let m = lhs.precede(p);
|
||||||
p.bump(op);
|
p.bump(op);
|
||||||
|
|
||||||
expr_bp(p, r, op_bp + 1, dollar_lvl);
|
expr_bp(p, r, op_bp + 1);
|
||||||
lhs = m.complete(p, if is_range { RANGE_EXPR } else { BIN_EXPR });
|
lhs = m.complete(p, if is_range { RANGE_EXPR } else { BIN_EXPR });
|
||||||
}
|
}
|
||||||
(Some(lhs), BlockLike::NotBlock)
|
(Some(lhs), BlockLike::NotBlock)
|
||||||
|
@ -314,11 +287,7 @@ fn expr_bp(
|
||||||
|
|
||||||
const LHS_FIRST: TokenSet = atom::ATOM_EXPR_FIRST.union(token_set![AMP, STAR, EXCL, DOT, MINUS]);
|
const LHS_FIRST: TokenSet = atom::ATOM_EXPR_FIRST.union(token_set![AMP, STAR, EXCL, DOT, MINUS]);
|
||||||
|
|
||||||
fn lhs(
|
fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> {
|
||||||
p: &mut Parser,
|
|
||||||
r: Restrictions,
|
|
||||||
dollar_lvl: &mut usize,
|
|
||||||
) -> Option<(CompletedMarker, BlockLike)> {
|
|
||||||
let m;
|
let m;
|
||||||
let kind = match p.current() {
|
let kind = match p.current() {
|
||||||
// test ref_expr
|
// test ref_expr
|
||||||
|
@ -351,7 +320,7 @@ fn lhs(
|
||||||
m = p.start();
|
m = p.start();
|
||||||
p.bump(op);
|
p.bump(op);
|
||||||
if p.at_ts(EXPR_FIRST) {
|
if p.at_ts(EXPR_FIRST) {
|
||||||
expr_bp(p, r, 2, dollar_lvl);
|
expr_bp(p, r, 2);
|
||||||
}
|
}
|
||||||
return Some((m.complete(p, RANGE_EXPR), BlockLike::NotBlock));
|
return Some((m.complete(p, RANGE_EXPR), BlockLike::NotBlock));
|
||||||
}
|
}
|
||||||
|
@ -367,7 +336,7 @@ fn lhs(
|
||||||
return Some(postfix_expr(p, lhs, blocklike, !(r.prefer_stmt && blocklike.is_block())));
|
return Some(postfix_expr(p, lhs, blocklike, !(r.prefer_stmt && blocklike.is_block())));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
expr_bp(p, r, 255, dollar_lvl);
|
expr_bp(p, r, 255);
|
||||||
Some((m.complete(p, kind), BlockLike::NotBlock))
|
Some((m.complete(p, kind), BlockLike::NotBlock))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -69,6 +69,7 @@ pub(super) fn atom_expr(p: &mut Parser, r: Restrictions) -> Option<(CompletedMar
|
||||||
let done = match p.current() {
|
let done = match p.current() {
|
||||||
T!['('] => tuple_expr(p),
|
T!['('] => tuple_expr(p),
|
||||||
T!['['] => array_expr(p),
|
T!['['] => array_expr(p),
|
||||||
|
L_DOLLAR => meta_var_expr(p),
|
||||||
T![|] => lambda_expr(p),
|
T![|] => lambda_expr(p),
|
||||||
T![move] if la == T![|] => lambda_expr(p),
|
T![move] if la == T![|] => lambda_expr(p),
|
||||||
T![async] if la == T![|] || (la == T![move] && p.nth(2) == T![|]) => lambda_expr(p),
|
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 {
|
||||||
}
|
}
|
||||||
m.complete(p, BOX_EXPR)
|
m.complete(p, BOX_EXPR)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Expression from `$var` macro expansion, wrapped in dollars
|
||||||
|
fn meta_var_expr(p: &mut Parser) -> CompletedMarker {
|
||||||
|
assert!(p.at(L_DOLLAR));
|
||||||
|
let m = p.start();
|
||||||
|
p.bump(L_DOLLAR);
|
||||||
|
let (completed, _is_block) =
|
||||||
|
expr_bp(p, Restrictions { forbid_structs: false, prefer_stmt: false }, 1);
|
||||||
|
|
||||||
|
match (completed, p.current()) {
|
||||||
|
(Some(it), R_DOLLAR) => {
|
||||||
|
p.bump(R_DOLLAR);
|
||||||
|
m.abandon(p);
|
||||||
|
it
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
while !p.at(R_DOLLAR) {
|
||||||
|
p.bump_any()
|
||||||
|
}
|
||||||
|
p.bump(R_DOLLAR);
|
||||||
|
m.complete(p, ERROR)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -5,7 +5,7 @@ use drop_bomb::DropBomb;
|
||||||
use crate::{
|
use crate::{
|
||||||
event::Event,
|
event::Event,
|
||||||
ParseError,
|
ParseError,
|
||||||
SyntaxKind::{self, EOF, ERROR, TOMBSTONE},
|
SyntaxKind::{self, EOF, ERROR, L_DOLLAR, R_DOLLAR, TOMBSTONE},
|
||||||
TokenSet, TokenSource, T,
|
TokenSet, TokenSource, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -211,19 +211,26 @@ impl<'t> Parser<'t> {
|
||||||
|
|
||||||
/// Create an error node and consume the next token.
|
/// Create an error node and consume the next token.
|
||||||
pub(crate) fn err_recover(&mut self, message: &str, recovery: TokenSet) {
|
pub(crate) fn err_recover(&mut self, message: &str, recovery: TokenSet) {
|
||||||
if self.at(T!['{']) || self.at(T!['}']) || self.at_ts(recovery) {
|
match self.current() {
|
||||||
|
T!['{'] | T!['}'] | L_DOLLAR | R_DOLLAR => {
|
||||||
self.error(message);
|
self.error(message);
|
||||||
} else {
|
return;
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.at_ts(recovery) {
|
||||||
|
self.error(message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let m = self.start();
|
let m = self.start();
|
||||||
self.error(message);
|
self.error(message);
|
||||||
self.bump_any();
|
self.bump_any();
|
||||||
m.complete(self, ERROR);
|
m.complete(self, ERROR);
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_bump(&mut self, kind: SyntaxKind, n_raw_tokens: u8) {
|
fn do_bump(&mut self, kind: SyntaxKind, n_raw_tokens: u8) {
|
||||||
// self.eat_dollars();
|
|
||||||
|
|
||||||
for _ in 0..n_raw_tokens {
|
for _ in 0..n_raw_tokens {
|
||||||
self.token_source.bump();
|
self.token_source.bump();
|
||||||
}
|
}
|
||||||
|
@ -234,52 +241,6 @@ impl<'t> Parser<'t> {
|
||||||
fn push_event(&mut self, event: Event) {
|
fn push_event(&mut self, event: Event) {
|
||||||
self.events.push(event)
|
self.events.push(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn eat_l_dollars(&mut self) -> usize {
|
|
||||||
let mut ate_count = 0;
|
|
||||||
loop {
|
|
||||||
match self.token_source.current().kind {
|
|
||||||
k @ SyntaxKind::L_DOLLAR => {
|
|
||||||
self.token_source.bump();
|
|
||||||
self.push_event(Event::Token { kind: k, n_raw_tokens: 1 });
|
|
||||||
ate_count += 1;
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return ate_count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn eat_r_dollars(&mut self, max_count: usize) -> usize {
|
|
||||||
let mut ate_count = 0;
|
|
||||||
loop {
|
|
||||||
match self.token_source.current().kind {
|
|
||||||
k @ SyntaxKind::R_DOLLAR => {
|
|
||||||
self.token_source.bump();
|
|
||||||
self.push_event(Event::Token { kind: k, n_raw_tokens: 1 });
|
|
||||||
ate_count += 1;
|
|
||||||
|
|
||||||
if max_count >= ate_count {
|
|
||||||
return ate_count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
return ate_count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn at_l_dollar(&self) -> bool {
|
|
||||||
let kind = self.token_source.current().kind;
|
|
||||||
(kind == SyntaxKind::L_DOLLAR)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn at_r_dollar(&self) -> bool {
|
|
||||||
let kind = self.token_source.current().kind;
|
|
||||||
(kind == SyntaxKind::R_DOLLAR)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// See `Parser::start`.
|
/// See `Parser::start`.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue