mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
mbe: handle multi-character separator
This commit is contained in:
parent
767351fb87
commit
a7d411425c
3 changed files with 26 additions and 32 deletions
|
@ -321,8 +321,8 @@ struct MatchState<'t> {
|
||||||
/// The KleeneOp of this sequence if we are in a repetition.
|
/// The KleeneOp of this sequence if we are in a repetition.
|
||||||
sep_kind: Option<RepeatKind>,
|
sep_kind: Option<RepeatKind>,
|
||||||
|
|
||||||
/// Number of tokens of separator parsed
|
/// Whether we already matched separator token.
|
||||||
sep_parsed: Option<usize>,
|
sep_matched: bool,
|
||||||
|
|
||||||
/// Matched meta variables bindings
|
/// Matched meta variables bindings
|
||||||
bindings: BindingsIdx,
|
bindings: BindingsIdx,
|
||||||
|
@ -387,7 +387,7 @@ fn match_loop_inner<'t>(
|
||||||
None => {
|
None => {
|
||||||
// We are at or past the end of the matcher of `item`.
|
// We are at or past the end of the matcher of `item`.
|
||||||
if let Some(up) = &item.up {
|
if let Some(up) = &item.up {
|
||||||
if item.sep_parsed.is_none() {
|
if !item.sep_matched {
|
||||||
// Get the `up` matcher
|
// Get the `up` matcher
|
||||||
let mut new_pos = (**up).clone();
|
let mut new_pos = (**up).clone();
|
||||||
new_pos.bindings = bindings_builder.copy(&new_pos.bindings);
|
new_pos.bindings = bindings_builder.copy(&new_pos.bindings);
|
||||||
|
@ -401,14 +401,17 @@ fn match_loop_inner<'t>(
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we need a separator.
|
// Check if we need a separator.
|
||||||
// We check the separator one by one
|
if item.sep.is_some() && !item.sep_matched {
|
||||||
let sep_idx = item.sep_parsed.unwrap_or(0);
|
|
||||||
let sep_len = item.sep.as_ref().map_or(0, Separator::tt_count);
|
|
||||||
if item.sep.is_some() && sep_idx != sep_len {
|
|
||||||
let sep = item.sep.as_ref().unwrap();
|
let sep = item.sep.as_ref().unwrap();
|
||||||
if src.clone().expect_separator(sep, sep_idx) {
|
let mut fork = src.clone();
|
||||||
|
if fork.expect_separator(sep) {
|
||||||
|
// HACK: here we use `meta_result` to pass `TtIter` back to caller because
|
||||||
|
// it might have been advanced multiple times. `ValueResult` is
|
||||||
|
// insignificant.
|
||||||
|
item.meta_result = Some((fork, ValueResult::ok(None)));
|
||||||
item.dot.next();
|
item.dot.next();
|
||||||
item.sep_parsed = Some(sep_idx + 1);
|
// item.sep_parsed = Some(sep_len);
|
||||||
|
item.sep_matched = true;
|
||||||
try_push!(next_items, item);
|
try_push!(next_items, item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -416,7 +419,7 @@ fn match_loop_inner<'t>(
|
||||||
// and try to match again UNLESS we are only allowed to have _one_ repetition.
|
// and try to match again UNLESS we are only allowed to have _one_ repetition.
|
||||||
else if item.sep_kind != Some(RepeatKind::ZeroOrOne) {
|
else if item.sep_kind != Some(RepeatKind::ZeroOrOne) {
|
||||||
item.dot = item.dot.reset();
|
item.dot = item.dot.reset();
|
||||||
item.sep_parsed = None;
|
item.sep_matched = false;
|
||||||
bindings_builder.push_default(&mut item.bindings);
|
bindings_builder.push_default(&mut item.bindings);
|
||||||
cur_items.push(item);
|
cur_items.push(item);
|
||||||
}
|
}
|
||||||
|
@ -451,7 +454,7 @@ fn match_loop_inner<'t>(
|
||||||
up: Some(Box::new(item)),
|
up: Some(Box::new(item)),
|
||||||
sep: separator.clone(),
|
sep: separator.clone(),
|
||||||
sep_kind: Some(*kind),
|
sep_kind: Some(*kind),
|
||||||
sep_parsed: None,
|
sep_matched: false,
|
||||||
bindings: bindings_builder.alloc(),
|
bindings: bindings_builder.alloc(),
|
||||||
meta_result: None,
|
meta_result: None,
|
||||||
is_error: false,
|
is_error: false,
|
||||||
|
@ -592,7 +595,7 @@ fn match_loop(pattern: &MetaTemplate, src: &tt::Subtree) -> Match {
|
||||||
up: None,
|
up: None,
|
||||||
sep: None,
|
sep: None,
|
||||||
sep_kind: None,
|
sep_kind: None,
|
||||||
sep_parsed: None,
|
sep_matched: false,
|
||||||
bindings: bindings_builder.alloc(),
|
bindings: bindings_builder.alloc(),
|
||||||
is_error: false,
|
is_error: false,
|
||||||
meta_result: None,
|
meta_result: None,
|
||||||
|
@ -864,14 +867,14 @@ impl<'a> Iterator for OpDelimitedIter<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> TtIter<'a> {
|
impl<'a> TtIter<'a> {
|
||||||
fn expect_separator(&mut self, separator: &Separator, idx: usize) -> bool {
|
fn expect_separator(&mut self, separator: &Separator) -> bool {
|
||||||
let mut fork = self.clone();
|
let mut fork = self.clone();
|
||||||
let ok = match separator {
|
let ok = match separator {
|
||||||
Separator::Ident(lhs) if idx == 0 => match fork.expect_ident_or_underscore() {
|
Separator::Ident(lhs) => match fork.expect_ident_or_underscore() {
|
||||||
Ok(rhs) => rhs.text == lhs.text,
|
Ok(rhs) => rhs.text == lhs.text,
|
||||||
Err(_) => false,
|
Err(_) => false,
|
||||||
},
|
},
|
||||||
Separator::Literal(lhs) if idx == 0 => match fork.expect_literal() {
|
Separator::Literal(lhs) => match fork.expect_literal() {
|
||||||
Ok(rhs) => match rhs {
|
Ok(rhs) => match rhs {
|
||||||
tt::Leaf::Literal(rhs) => rhs.text == lhs.text,
|
tt::Leaf::Literal(rhs) => rhs.text == lhs.text,
|
||||||
tt::Leaf::Ident(rhs) => rhs.text == lhs.text,
|
tt::Leaf::Ident(rhs) => rhs.text == lhs.text,
|
||||||
|
@ -879,11 +882,14 @@ impl<'a> TtIter<'a> {
|
||||||
},
|
},
|
||||||
Err(_) => false,
|
Err(_) => false,
|
||||||
},
|
},
|
||||||
Separator::Puncts(lhss) if idx < lhss.len() => match fork.expect_single_punct() {
|
Separator::Puncts(lhs) => match fork.expect_glued_punct() {
|
||||||
Ok(rhs) => rhs.char == lhss[idx].char,
|
Ok(rhs) => {
|
||||||
|
let lhs = lhs.iter().map(|it| it.char);
|
||||||
|
let rhs = rhs.iter().map(|it| it.char);
|
||||||
|
lhs.eq(rhs)
|
||||||
|
}
|
||||||
Err(_) => false,
|
Err(_) => false,
|
||||||
},
|
},
|
||||||
_ => false,
|
|
||||||
};
|
};
|
||||||
if ok {
|
if ok {
|
||||||
*self = fork;
|
*self = fork;
|
||||||
|
|
|
@ -110,16 +110,6 @@ impl PartialEq for Separator {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Separator {
|
|
||||||
pub(crate) fn tt_count(&self) -> usize {
|
|
||||||
match self {
|
|
||||||
Separator::Literal(_) => 1,
|
|
||||||
Separator::Ident(_) => 1,
|
|
||||||
Separator::Puncts(it) => it.len(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
enum Mode {
|
enum Mode {
|
||||||
Pattern,
|
Pattern,
|
||||||
|
|
|
@ -112,10 +112,9 @@ impl<'a> TtIter<'a> {
|
||||||
|
|
||||||
match (first.char, second.char, third.map(|it| it.char)) {
|
match (first.char, second.char, third.map(|it| it.char)) {
|
||||||
('.', '.', Some('.' | '=')) | ('<', '<', Some('=')) | ('>', '>', Some('=')) => {
|
('.', '.', Some('.' | '=')) | ('<', '<', Some('=')) | ('>', '>', Some('=')) => {
|
||||||
let puncts = smallvec![first, second.clone(), third.unwrap().clone()];
|
|
||||||
let _ = self.next().unwrap();
|
let _ = self.next().unwrap();
|
||||||
let _ = self.next().unwrap();
|
let _ = self.next().unwrap();
|
||||||
Ok(puncts)
|
Ok(smallvec![first, second.clone(), third.unwrap().clone()])
|
||||||
}
|
}
|
||||||
('-' | '!' | '*' | '/' | '&' | '%' | '^' | '+' | '<' | '=' | '>' | '|', '=', _)
|
('-' | '!' | '*' | '/' | '&' | '%' | '^' | '+' | '<' | '=' | '>' | '|', '=', _)
|
||||||
| ('-' | '=' | '>', '>', _)
|
| ('-' | '=' | '>', '>', _)
|
||||||
|
@ -125,9 +124,8 @@ impl<'a> TtIter<'a> {
|
||||||
| ('&', '&', _)
|
| ('&', '&', _)
|
||||||
| ('<', '<', _)
|
| ('<', '<', _)
|
||||||
| ('|', '|', _) => {
|
| ('|', '|', _) => {
|
||||||
let puncts = smallvec![first, second.clone()];
|
|
||||||
let _ = self.next().unwrap();
|
let _ = self.next().unwrap();
|
||||||
Ok(puncts)
|
Ok(smallvec![first, second.clone()])
|
||||||
}
|
}
|
||||||
_ => Ok(smallvec![first]),
|
_ => Ok(smallvec![first]),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue