rust/crates/libsyntax2/src/grammar/patterns.rs

206 lines
4.4 KiB
Rust
Raw Normal View History

use super::*;
pub(super) fn pattern(p: &mut Parser) {
2018-08-08 07:05:33 -05:00
if let Some(lhs) = atom_pat(p) {
// test range_pat
// fn main() {
// match 92 { 0 ... 100 => () }
// }
if p.at(DOTDOTDOT) {
let m = lhs.precede(p);
p.bump();
atom_pat(p);
m.complete(p, RANGE_PAT);
}
}
}
fn atom_pat(p: &mut Parser) -> Option<CompletedMarker> {
2018-08-04 07:47:45 -05:00
let la0 = p.nth(0);
let la1 = p.nth(1);
if la0 == REF_KW || la0 == MUT_KW
|| (la0 == IDENT && !(la1 == COLONCOLON || la1 == L_PAREN || la1 == L_CURLY)) {
2018-08-08 07:05:33 -05:00
return Some(bind_pat(p, true));
2018-08-04 07:47:45 -05:00
}
if paths::is_path_start(p) {
2018-08-08 07:05:33 -05:00
return Some(path_pat(p));
2018-08-04 07:47:45 -05:00
}
2018-08-07 16:59:16 -05:00
// test literal_pattern
// fn main() {
// match () {
// 92 => (),
// 'c' => (),
// "hello" => (),
// }
// }
2018-08-08 07:05:33 -05:00
match expressions::literal(p) {
Some(m) => return Some(m),
None => (),
2018-08-07 16:59:16 -05:00
}
2018-08-08 07:05:33 -05:00
let m = match la0 {
UNDERSCORE => placeholder_pat(p),
2018-08-05 10:18:02 -05:00
AMP => ref_pat(p),
2018-08-07 06:41:03 -05:00
L_PAREN => tuple_pat(p),
2018-08-07 09:00:45 -05:00
L_BRACK => slice_pat(p),
2018-08-08 07:05:33 -05:00
_ => {
p.err_and_bump("expected pattern");
return None;
}
};
Some(m)
}
2018-08-04 07:47:45 -05:00
// test path_part
// fn foo() {
// let foo::Bar = ();
// let ::Bar = ();
// let Bar { .. } = ();
// let Bar(..) = ();
// }
2018-08-08 07:05:33 -05:00
fn path_pat(p: &mut Parser) -> CompletedMarker {
2018-08-13 10:23:14 -05:00
assert!(paths::is_path_start(p));
2018-08-04 07:47:45 -05:00
let m = p.start();
paths::expr_path(p);
let kind = match p.current() {
L_PAREN => {
tuple_pat_fields(p);
2018-08-07 06:41:03 -05:00
TUPLE_STRUCT_PAT
2018-08-04 07:47:45 -05:00
}
L_CURLY => {
struct_pat_fields(p);
STRUCT_PAT
}
_ => PATH_PAT
};
2018-08-08 07:05:33 -05:00
m.complete(p, kind)
2018-08-04 07:47:45 -05:00
}
// test tuple_pat_fields
// fn foo() {
// let S() = ();
// let S(_) = ();
// let S(_,) = ();
// let S(_, .. , x) = ();
// }
fn tuple_pat_fields(p: &mut Parser) {
assert!(p.at(L_PAREN));
p.bump();
while !p.at(EOF) && !p.at(R_PAREN) {
match p.current() {
DOTDOT => p.bump(),
_ => pattern(p),
}
if !p.at(R_PAREN) {
p.expect(COMMA);
}
}
p.expect(R_PAREN);
}
// test struct_pat_fields
// fn foo() {
// let S {} = ();
// let S { f, ref mut g } = ();
// let S { h: _, ..} = ();
// let S { h: _, } = ();
// }
fn struct_pat_fields(p: &mut Parser) {
assert!(p.at(L_CURLY));
p.bump();
while !p.at(EOF) && !p.at(R_CURLY) {
match p.current() {
DOTDOT => p.bump(),
IDENT if p.nth(1) == COLON => {
p.bump();
p.bump();
pattern(p);
}
2018-08-13 10:23:14 -05:00
_ => {
2018-08-13 10:30:56 -05:00
bind_pat(p, false);
2018-08-08 07:05:33 -05:00
}
2018-08-04 07:47:45 -05:00
}
if !p.at(R_CURLY) {
p.expect(COMMA);
}
}
p.expect(R_CURLY);
}
// test placeholder_pat
// fn main() { let _ = (); }
2018-08-08 07:05:33 -05:00
fn placeholder_pat(p: &mut Parser) -> CompletedMarker {
assert!(p.at(UNDERSCORE));
let m = p.start();
p.bump();
2018-08-08 07:05:33 -05:00
m.complete(p, PLACEHOLDER_PAT)
}
// test ref_pat
// fn main() {
// let &a = ();
// let &mut b = ();
// }
2018-08-08 07:05:33 -05:00
fn ref_pat(p: &mut Parser) -> CompletedMarker {
2018-08-05 10:18:02 -05:00
assert!(p.at(AMP));
let m = p.start();
p.bump();
p.eat(MUT_KW);
pattern(p);
2018-08-08 07:05:33 -05:00
m.complete(p, REF_PAT)
}
2018-08-07 06:41:03 -05:00
// test tuple_pat
// fn main() {
// let (a, b, ..) = ();
// }
2018-08-08 07:05:33 -05:00
fn tuple_pat(p: &mut Parser) -> CompletedMarker {
2018-08-07 06:41:03 -05:00
assert!(p.at(L_PAREN));
let m = p.start();
tuple_pat_fields(p);
2018-08-08 07:05:33 -05:00
m.complete(p, TUPLE_PAT)
2018-08-07 06:41:03 -05:00
}
2018-08-07 09:00:45 -05:00
// test slice_pat
// fn main() {
// let [a, b, ..] = [];
// }
2018-08-08 07:05:33 -05:00
fn slice_pat(p: &mut Parser) -> CompletedMarker {
2018-08-07 09:00:45 -05:00
assert!(p.at(L_BRACK));
let m = p.start();
p.bump();
while !p.at(EOF) && !p.at(R_BRACK) {
match p.current() {
DOTDOT => p.bump(),
_ => pattern(p),
}
if !p.at(R_BRACK) {
p.expect(COMMA);
}
}
p.expect(R_BRACK);
2018-08-08 07:05:33 -05:00
m.complete(p, SLICE_PAT)
2018-08-07 09:00:45 -05:00
}
// test bind_pat
// fn main() {
// let a = ();
2018-07-31 07:30:11 -05:00
// let mut b = ();
// let ref c = ();
// let ref mut d = ();
// let e @ _ = ();
// let ref mut f @ g @ _ = ();
// }
2018-08-08 07:05:33 -05:00
fn bind_pat(p: &mut Parser, with_at: bool) -> CompletedMarker {
let m = p.start();
2018-07-31 07:40:40 -05:00
p.eat(REF_KW);
p.eat(MUT_KW);
name(p);
2018-08-04 07:47:45 -05:00
if with_at && p.eat(AT) {
pattern(p);
}
2018-08-08 07:05:33 -05:00
m.complete(p, BIND_PAT)
}