mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 20:42:04 +00:00
Merge #1138
1138: Add L_DOLLAR and R_DOLLAR r=matklad a=edwin0cheng As discussion in issue https://github.com/rust-analyzer/rust-analyzer/issues/1132 and PR #1125 , this PR add 2 `Syntax::Kind` : `L_DOLLAR` and `R_DOLLAR` for representing `Delimiter::None` in mbe and proc_marco. By design, It should not affect the final syntax tree, and will be discard in `TreeSink`. My original idea is handling these 2 tokens case by case, but i found that they will appear in every place in the parser (imagine `tt` matcher). So this PR only handle it in `Parser::do_bump` and `Parser::start`, although It will not fix the `expr` matcher executing order problem in original idea. Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
This commit is contained in:
commit
5d35f284f5
13 changed files with 561 additions and 244 deletions
|
@ -53,6 +53,18 @@ pub(crate) fn path(p: &mut Parser) {
|
|||
paths::type_path(p);
|
||||
}
|
||||
|
||||
pub(crate) fn expr(p: &mut Parser) {
|
||||
expressions::expr(p);
|
||||
}
|
||||
|
||||
pub(crate) fn type_(p: &mut Parser) {
|
||||
types::type_(p)
|
||||
}
|
||||
|
||||
pub(crate) fn pattern(p: &mut Parser) {
|
||||
patterns::pattern(p)
|
||||
}
|
||||
|
||||
pub(crate) fn reparser(
|
||||
node: SyntaxKind,
|
||||
first_child: Option<SyntaxKind>,
|
||||
|
|
|
@ -8,17 +8,20 @@ const EXPR_FIRST: TokenSet = LHS_FIRST;
|
|||
|
||||
pub(super) fn expr(p: &mut Parser) -> BlockLike {
|
||||
let r = Restrictions { forbid_structs: false, prefer_stmt: false };
|
||||
expr_bp(p, r, 1).1
|
||||
let mut dollar_lvl = 0;
|
||||
expr_bp(p, r, 1, &mut dollar_lvl).1
|
||||
}
|
||||
|
||||
pub(super) fn expr_stmt(p: &mut Parser) -> (Option<CompletedMarker>, BlockLike) {
|
||||
let r = Restrictions { forbid_structs: false, prefer_stmt: true };
|
||||
expr_bp(p, r, 1)
|
||||
let mut dollar_lvl = 0;
|
||||
expr_bp(p, r, 1, &mut dollar_lvl)
|
||||
}
|
||||
|
||||
fn expr_no_struct(p: &mut Parser) {
|
||||
let r = Restrictions { forbid_structs: true, prefer_stmt: false };
|
||||
expr_bp(p, r, 1);
|
||||
let mut dollar_lvl = 0;
|
||||
expr_bp(p, r, 1, &mut dollar_lvl);
|
||||
}
|
||||
|
||||
// test block
|
||||
|
@ -206,8 +209,23 @@ fn current_op(p: &Parser) -> (u8, Op) {
|
|||
}
|
||||
|
||||
// Parses expression with binding power of at least bp.
|
||||
fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option<CompletedMarker>, BlockLike) {
|
||||
let mut lhs = match lhs(p, r) {
|
||||
fn expr_bp(
|
||||
p: &mut Parser,
|
||||
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 = false;
|
||||
|
||||
if p.at_l_dollar() {
|
||||
*dollar_lvl += p.eat_l_dollars();
|
||||
newly_dollar_open = true;
|
||||
}
|
||||
|
||||
let mut lhs = match lhs(p, r, dollar_lvl) {
|
||||
Some((lhs, blocklike)) => {
|
||||
// test stmt_bin_expr_ambiguity
|
||||
// fn foo() {
|
||||
|
@ -223,6 +241,15 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option<CompletedMarker>,
|
|||
};
|
||||
|
||||
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.current() == DOTDOT || p.current() == DOTDOTEQ;
|
||||
let (op_bp, op) = current_op(p);
|
||||
if op_bp < bp {
|
||||
|
@ -235,7 +262,8 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option<CompletedMarker>,
|
|||
p.bump_compound(kind, n);
|
||||
}
|
||||
}
|
||||
expr_bp(p, r, op_bp + 1);
|
||||
|
||||
expr_bp(p, r, op_bp + 1, dollar_lvl);
|
||||
lhs = m.complete(p, if is_range { RANGE_EXPR } else { BIN_EXPR });
|
||||
}
|
||||
(Some(lhs), BlockLike::NotBlock)
|
||||
|
@ -244,7 +272,11 @@ fn expr_bp(p: &mut Parser, r: Restrictions, bp: u8) -> (Option<CompletedMarker>,
|
|||
const LHS_FIRST: TokenSet =
|
||||
atom::ATOM_EXPR_FIRST.union(token_set![AMP, STAR, EXCL, DOTDOT, DOTDOTEQ, MINUS]);
|
||||
|
||||
fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)> {
|
||||
fn lhs(
|
||||
p: &mut Parser,
|
||||
r: Restrictions,
|
||||
dollar_lvl: &mut usize,
|
||||
) -> Option<(CompletedMarker, BlockLike)> {
|
||||
let m;
|
||||
let kind = match p.current() {
|
||||
// test ref_expr
|
||||
|
@ -275,7 +307,7 @@ fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)>
|
|||
m = p.start();
|
||||
p.bump();
|
||||
if p.at_ts(EXPR_FIRST) {
|
||||
expr_bp(p, r, 2);
|
||||
expr_bp(p, r, 2, dollar_lvl);
|
||||
}
|
||||
return Some((m.complete(p, RANGE_EXPR), BlockLike::NotBlock));
|
||||
}
|
||||
|
@ -287,7 +319,7 @@ fn lhs(p: &mut Parser, r: Restrictions) -> Option<(CompletedMarker, BlockLike)>
|
|||
));
|
||||
}
|
||||
};
|
||||
expr_bp(p, r, 255);
|
||||
expr_bp(p, r, 255, dollar_lvl);
|
||||
Some((m.complete(p, kind), BlockLike::NotBlock))
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ pub(super) const PATTERN_FIRST: TokenSet = expressions::LITERAL_FIRST
|
|||
.union(token_set![REF_KW, MUT_KW, L_PAREN, L_BRACK, AMP, UNDERSCORE, MINUS]);
|
||||
|
||||
pub(super) fn pattern(p: &mut Parser) {
|
||||
pattern_r(p, PAT_RECOVERY_SET)
|
||||
pattern_r(p, PAT_RECOVERY_SET);
|
||||
}
|
||||
|
||||
/// Parses a pattern list separated by pipes `|`
|
||||
|
|
|
@ -53,20 +53,39 @@ pub trait TreeSink {
|
|||
fn error(&mut self, error: ParseError);
|
||||
}
|
||||
|
||||
/// Parse given tokens into the given sink as a rust file.
|
||||
pub fn parse(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) {
|
||||
fn parse_from_tokens<F>(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink, f: F)
|
||||
where
|
||||
F: FnOnce(&mut parser::Parser),
|
||||
{
|
||||
let mut p = parser::Parser::new(token_source);
|
||||
grammar::root(&mut p);
|
||||
f(&mut p);
|
||||
let events = p.finish();
|
||||
event::process(tree_sink, events);
|
||||
}
|
||||
|
||||
/// Parse given tokens into the given sink as a rust file.
|
||||
pub fn parse(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) {
|
||||
parse_from_tokens(token_source, tree_sink, grammar::root);
|
||||
}
|
||||
|
||||
/// Parse given tokens into the given sink as a path
|
||||
pub fn parse_path(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) {
|
||||
let mut p = parser::Parser::new(token_source);
|
||||
grammar::path(&mut p);
|
||||
let events = p.finish();
|
||||
event::process(tree_sink, events);
|
||||
parse_from_tokens(token_source, tree_sink, grammar::path);
|
||||
}
|
||||
|
||||
/// Parse given tokens into the given sink as a expression
|
||||
pub fn parse_expr(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) {
|
||||
parse_from_tokens(token_source, tree_sink, grammar::expr);
|
||||
}
|
||||
|
||||
/// Parse given tokens into the given sink as a ty
|
||||
pub fn parse_ty(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) {
|
||||
parse_from_tokens(token_source, tree_sink, grammar::type_);
|
||||
}
|
||||
|
||||
/// Parse given tokens into the given sink as a pattern
|
||||
pub fn parse_pat(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) {
|
||||
parse_from_tokens(token_source, tree_sink, grammar::pattern);
|
||||
}
|
||||
|
||||
/// A parsing function for a specific braced-block.
|
||||
|
|
|
@ -45,8 +45,9 @@ impl<'t> Parser<'t> {
|
|||
///
|
||||
/// Useful for parsing things like `>>`.
|
||||
pub(crate) fn current2(&self) -> Option<(SyntaxKind, SyntaxKind)> {
|
||||
let c1 = self.token_source.token_kind(self.token_pos);
|
||||
let c2 = self.token_source.token_kind(self.token_pos + 1);
|
||||
let c1 = self.nth(0);
|
||||
let c2 = self.nth(1);
|
||||
|
||||
if self.token_source.is_token_joint_to_next(self.token_pos) {
|
||||
Some((c1, c2))
|
||||
} else {
|
||||
|
@ -59,9 +60,9 @@ impl<'t> Parser<'t> {
|
|||
///
|
||||
/// Useful for parsing things like `=>>`.
|
||||
pub(crate) fn current3(&self) -> Option<(SyntaxKind, SyntaxKind, SyntaxKind)> {
|
||||
let c1 = self.token_source.token_kind(self.token_pos);
|
||||
let c2 = self.token_source.token_kind(self.token_pos + 1);
|
||||
let c3 = self.token_source.token_kind(self.token_pos + 2);
|
||||
let c1 = self.nth(0);
|
||||
let c2 = self.nth(1);
|
||||
let c3 = self.nth(2);
|
||||
if self.token_source.is_token_joint_to_next(self.token_pos)
|
||||
&& self.token_source.is_token_joint_to_next(self.token_pos + 1)
|
||||
{
|
||||
|
@ -77,7 +78,23 @@ impl<'t> Parser<'t> {
|
|||
let steps = self.steps.get();
|
||||
assert!(steps <= 10_000_000, "the parser seems stuck");
|
||||
self.steps.set(steps + 1);
|
||||
self.token_source.token_kind(self.token_pos + n)
|
||||
|
||||
// It is beecause the Dollar will appear between nth
|
||||
// Following code skips through it
|
||||
let mut non_dollars_count = 0;
|
||||
let mut i = 0;
|
||||
|
||||
loop {
|
||||
let kind = self.token_source.token_kind(self.token_pos + i);
|
||||
i += 1;
|
||||
|
||||
match kind {
|
||||
EOF => return EOF,
|
||||
SyntaxKind::L_DOLLAR | SyntaxKind::R_DOLLAR => {}
|
||||
_ if non_dollars_count == n => return kind,
|
||||
_ => non_dollars_count += 1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks if the current token is `kind`.
|
||||
|
@ -180,6 +197,7 @@ impl<'t> Parser<'t> {
|
|||
}
|
||||
|
||||
fn do_bump(&mut self, kind: SyntaxKind, n_raw_tokens: u8) {
|
||||
self.eat_dollars();
|
||||
self.token_pos += usize::from(n_raw_tokens);
|
||||
self.push_event(Event::Token { kind, n_raw_tokens });
|
||||
}
|
||||
|
@ -187,6 +205,66 @@ impl<'t> Parser<'t> {
|
|||
fn push_event(&mut self, event: Event) {
|
||||
self.events.push(event)
|
||||
}
|
||||
|
||||
fn eat_dollars(&mut self) {
|
||||
loop {
|
||||
match self.token_source.token_kind(self.token_pos) {
|
||||
k @ SyntaxKind::L_DOLLAR | k @ SyntaxKind::R_DOLLAR => {
|
||||
self.token_pos += 1;
|
||||
self.push_event(Event::Token { kind: k, n_raw_tokens: 1 });
|
||||
}
|
||||
_ => {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn eat_l_dollars(&mut self) -> usize {
|
||||
let mut ate_count = 0;
|
||||
loop {
|
||||
match self.token_source.token_kind(self.token_pos) {
|
||||
k @ SyntaxKind::L_DOLLAR => {
|
||||
self.token_pos += 1;
|
||||
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.token_kind(self.token_pos) {
|
||||
k @ SyntaxKind::R_DOLLAR => {
|
||||
self.token_pos += 1;
|
||||
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.token_kind(self.token_pos);
|
||||
(kind == SyntaxKind::L_DOLLAR)
|
||||
}
|
||||
|
||||
pub(crate) fn at_r_dollar(&self) -> bool {
|
||||
let kind = self.token_source.token_kind(self.token_pos);
|
||||
(kind == SyntaxKind::R_DOLLAR)
|
||||
}
|
||||
}
|
||||
|
||||
/// See `Parser::start`.
|
||||
|
|
|
@ -120,6 +120,8 @@ pub enum SyntaxKind {
|
|||
LIFETIME,
|
||||
COMMENT,
|
||||
SHEBANG,
|
||||
L_DOLLAR,
|
||||
R_DOLLAR,
|
||||
SOURCE_FILE,
|
||||
STRUCT_DEF,
|
||||
ENUM_DEF,
|
||||
|
@ -477,6 +479,8 @@ impl SyntaxKind {
|
|||
LIFETIME => &SyntaxInfo { name: "LIFETIME" },
|
||||
COMMENT => &SyntaxInfo { name: "COMMENT" },
|
||||
SHEBANG => &SyntaxInfo { name: "SHEBANG" },
|
||||
L_DOLLAR => &SyntaxInfo { name: "L_DOLLAR" },
|
||||
R_DOLLAR => &SyntaxInfo { name: "R_DOLLAR" },
|
||||
SOURCE_FILE => &SyntaxInfo { name: "SOURCE_FILE" },
|
||||
STRUCT_DEF => &SyntaxInfo { name: "STRUCT_DEF" },
|
||||
ENUM_DEF => &SyntaxInfo { name: "ENUM_DEF" },
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue