rust-analyzer/crates/libsyntax2/src/grammar/params.rs

117 lines
2.8 KiB
Rust
Raw Normal View History

2018-07-31 20:13:08 +00:00
use super::*;
// test param_list
// fn a() {}
// fn b(x: i32) {}
// fn c(x: i32, ) {}
// fn d(x: i32, y: ()) {}
2018-07-31 20:16:07 +00:00
pub(super) fn param_list(p: &mut Parser) {
2018-08-08 15:13:30 +00:00
list_(p, Flavor::Normal)
}
// test param_list_opt_patterns
// fn foo<F: FnMut(&mut Foo<'a>)>(){}
pub(super) fn param_list_opt_patterns(p: &mut Parser) {
list_(p, Flavor::OptionalPattern)
2018-07-31 20:13:08 +00:00
}
2018-07-31 20:16:07 +00:00
pub(super) fn param_list_opt_types(p: &mut Parser) {
2018-08-08 15:13:30 +00:00
list_(p, Flavor::OptionalType)
}
#[derive(Clone, Copy, Eq, PartialEq)]
enum Flavor {
OptionalType,
OptionalPattern,
Normal,
2018-07-31 20:13:08 +00:00
}
2018-08-08 15:13:30 +00:00
impl Flavor {
fn type_required(self) -> bool {
match self {
Flavor::OptionalType => false,
_ => true,
}
}
}
fn list_(p: &mut Parser, flavor: Flavor) {
let (bra, ket) = if flavor.type_required() {
(L_PAREN, R_PAREN)
} else {
(PIPE, PIPE)
};
assert!(p.at(bra));
2018-07-31 20:13:08 +00:00
let m = p.start();
p.bump();
2018-08-08 15:13:30 +00:00
if flavor.type_required() {
2018-07-31 20:13:08 +00:00
self_param(p);
}
2018-08-08 15:13:30 +00:00
while !p.at(EOF) && !p.at(ket) {
value_parameter(p, flavor);
if !p.at(ket) {
2018-07-31 20:13:08 +00:00
p.expect(COMMA);
}
}
2018-08-08 15:13:30 +00:00
p.expect(ket);
2018-07-31 20:13:08 +00:00
m.complete(p, PARAM_LIST);
}
2018-08-08 15:13:30 +00:00
fn value_parameter(p: &mut Parser, flavor: Flavor) {
2018-07-31 20:13:08 +00:00
let m = p.start();
2018-08-08 15:34:26 +00:00
match flavor {
Flavor::OptionalType | Flavor::Normal => {
patterns::pattern(p);
if p.at(COLON) || flavor.type_required() {
types::ascription(p)
}
},
// test value_parameters_no_patterns
// type F = Box<Fn(a: i32, &b: &i32, &mut c: &i32, ())>;
Flavor::OptionalPattern => {
let la0 = p.current();
let la1 = p.nth(1);
let la2 = p.nth(2);
let la3 = p.nth(3);
if la0 == IDENT && la1 == COLON
|| la0 == AMP && la1 == IDENT && la2 == COLON
|| la0 == AMP && la1 == MUT_KW && la2 == IDENT && la3 == COLON {
patterns::pattern(p);
types::ascription(p);
} else {
types::type_(p);
}
},
2018-07-31 20:13:08 +00:00
}
2018-07-31 20:13:55 +00:00
m.complete(p, PARAM);
2018-07-31 20:13:08 +00:00
}
// test self_param
// impl S {
// fn a(self) {}
// fn b(&self,) {}
// fn c(&'a self,) {}
// fn d(&'a mut self, x: i32) {}
// }
fn self_param(p: &mut Parser) {
let la1 = p.nth(1);
let la2 = p.nth(2);
let la3 = p.nth(3);
let n_toks = match (p.current(), la1, la2, la3) {
(SELF_KW, _, _, _) => 1,
2018-08-05 15:18:02 +00:00
(AMP, SELF_KW, _, _) => 2,
(AMP, MUT_KW, SELF_KW, _) => 3,
(AMP, LIFETIME, SELF_KW, _) => 3,
(AMP, LIFETIME, MUT_KW, SELF_KW) => 4,
2018-07-31 20:13:08 +00:00
_ => return,
};
let m = p.start();
for _ in 0..n_toks {
p.bump();
}
m.complete(p, SELF_PARAM);
if !p.at(R_PAREN) {
p.expect(COMMA);
}
}