Files
rust/crates/parser/src/grammar/generic_params.rs

237 lines
5.2 KiB
Rust
Raw Normal View History

use super::*;
2020-08-13 17:58:35 +02:00
pub(super) fn opt_generic_param_list(p: &mut Parser) {
2021-09-18 15:46:28 +03:00
if p.at(T![<]) {
generic_param_list(p);
}
}
2021-09-18 15:46:28 +03:00
// test generic_param_list
// fn f<T: Clone>() {}
2020-08-13 17:58:35 +02:00
fn generic_param_list(p: &mut Parser) {
2019-05-15 15:35:47 +03:00
assert!(p.at(T![<]));
let m = p.start();
2019-09-19 15:51:46 -04:00
p.bump(T![<]);
2019-05-15 15:35:47 +03:00
while !p.at(EOF) && !p.at(T![>]) {
2021-09-18 15:56:26 +03:00
generic_param(p);
2019-05-15 15:35:47 +03:00
if !p.at(T![>]) && !p.expect(T![,]) {
break;
}
}
2019-05-15 15:35:47 +03:00
p.expect(T![>]);
m.complete(p, GENERIC_PARAM_LIST);
}
2021-09-18 15:56:26 +03:00
fn generic_param(p: &mut Parser) {
let m = p.start();
// test generic_param_attribute
// fn foo<#[lt_attr] 'a, #[t_attr] T>() {}
attributes::outer_attrs(p);
match p.current() {
LIFETIME_IDENT => lifetime_param(p, m),
IDENT => type_param(p, m),
T![const] => const_param(p, m),
_ => {
m.abandon(p);
p.err_and_bump("expected type parameter");
2021-09-18 15:56:26 +03:00
}
}
}
2021-09-18 15:46:28 +03:00
// test lifetime_param
// fn f<'a: 'b>() {}
fn lifetime_param(p: &mut Parser, m: Marker) {
2020-12-15 19:23:51 +01:00
assert!(p.at(LIFETIME_IDENT));
lifetime(p);
2019-05-15 15:35:47 +03:00
if p.at(T![:]) {
lifetime_bounds(p);
}
m.complete(p, LIFETIME_PARAM);
}
2021-09-18 15:46:28 +03:00
// test type_param
// fn f<T: Clone>() {}
fn type_param(p: &mut Parser, m: Marker) {
assert!(p.at(IDENT));
name(p);
2019-05-15 15:35:47 +03:00
if p.at(T![:]) {
bounds(p);
}
2019-05-15 15:35:47 +03:00
if p.at(T![=]) {
2021-09-18 15:46:28 +03:00
// test type_param_default
// struct S<T = i32>;
2019-09-19 15:51:46 -04:00
p.bump(T![=]);
types::type_(p);
}
m.complete(p, TYPE_PARAM);
}
// test const_param
// struct S<const N: u32>;
2020-08-13 17:58:35 +02:00
fn const_param(p: &mut Parser, m: Marker) {
p.bump(T![const]);
name(p);
if p.at(T![:]) {
types::ascription(p);
} else {
p.error("missing type for const parameter");
}
2021-04-29 03:07:53 +02:00
if p.at(T![=]) {
// test const_param_default_literal
2021-09-18 15:46:28 +03:00
// struct A<const N: i32 = -1>;
2021-04-29 03:07:53 +02:00
p.bump(T![=]);
// test const_param_default_expression
// struct A<const N: i32 = { 1 }>;
// test const_param_default_path
// struct A<const N: i32 = i32::MAX>;
generic_args::const_arg_expr(p);
2021-04-29 03:07:53 +02:00
}
m.complete(p, CONST_PARAM);
}
2018-08-08 19:26:38 +03:00
fn lifetime_bounds(p: &mut Parser) {
2019-05-15 15:35:47 +03:00
assert!(p.at(T![:]));
2019-09-19 15:51:46 -04:00
p.bump(T![:]);
2020-12-15 19:23:51 +01:00
while p.at(LIFETIME_IDENT) {
lifetime(p);
2019-05-15 15:35:47 +03:00
if !p.eat(T![+]) {
2018-08-08 19:26:38 +03:00
break;
}
}
}
2021-09-18 15:46:28 +03:00
// test type_param_bounds
2021-10-19 14:15:47 +02:00
// struct S<T: 'a + ?Sized + (Copy) + ~const Drop>;
2021-09-18 15:46:28 +03:00
pub(super) fn bounds(p: &mut Parser) {
assert!(p.at(T![:]));
p.bump(T![:]);
bounds_without_colon(p);
}
pub(super) fn bounds_without_colon(p: &mut Parser) {
let m = p.start();
bounds_without_colon_m(p, m);
}
pub(super) fn bounds_without_colon_m(p: &mut Parser, marker: Marker) -> CompletedMarker {
while type_bound(p) {
2019-05-15 15:35:47 +03:00
if !p.eat(T![+]) {
2018-07-31 18:24:30 +03:00
break;
}
}
marker.complete(p, TYPE_BOUND_LIST)
}
fn type_bound(p: &mut Parser) -> bool {
let m = p.start();
2019-05-15 15:35:47 +03:00
let has_paren = p.eat(T!['(']);
match p.current() {
2020-12-15 19:23:51 +01:00
LIFETIME_IDENT => lifetime(p),
T![for] => types::for_type(p, false),
2021-10-19 14:15:47 +02:00
current => {
match current {
T![?] => p.bump_any(),
T![~] => {
p.bump_any();
p.expect(T![const]);
}
_ => (),
}
if paths::is_use_path_start(p) {
types::path_type_(p, false);
} else {
m.abandon(p);
return false;
}
}
}
if has_paren {
2019-05-15 15:35:47 +03:00
p.expect(T![')']);
}
m.complete(p, TYPE_BOUND);
true
2018-07-31 18:24:30 +03:00
}
2018-08-08 19:26:38 +03:00
// test where_clause
// fn foo()
// where
// 'a: 'b + 'c,
// T: Clone + Copy + 'static,
// Iterator::Item: 'a,
// <T as Iterator>::Item: 'a
2018-08-08 19:26:38 +03:00
// {}
2018-08-24 02:14:10 +03:00
pub(super) fn opt_where_clause(p: &mut Parser) {
2019-05-15 15:35:47 +03:00
if !p.at(T![where]) {
2018-08-08 19:26:38 +03:00
return;
}
let m = p.start();
2019-09-19 15:51:46 -04:00
p.bump(T![where]);
while is_where_predicate(p) {
where_predicate(p);
2019-05-15 15:35:47 +03:00
let comma = p.eat(T![,]);
2021-09-18 15:46:28 +03:00
match p.current() {
T!['{'] | T![;] | T![=] => break,
_ => (),
}
if !comma {
p.error("expected comma");
2018-08-08 19:26:38 +03:00
}
}
2018-08-08 19:26:38 +03:00
m.complete(p, WHERE_CLAUSE);
2021-09-18 15:46:28 +03:00
fn is_where_predicate(p: &mut Parser) -> bool {
match p.current() {
LIFETIME_IDENT => true,
T![impl] => false,
token => types::TYPE_FIRST.contains(token),
}
}
}
2018-08-08 19:26:38 +03:00
fn where_predicate(p: &mut Parser) {
let m = p.start();
match p.current() {
2020-12-15 19:23:51 +01:00
LIFETIME_IDENT => {
lifetime(p);
2019-05-15 15:35:47 +03:00
if p.at(T![:]) {
bounds(p);
} else {
p.error("expected colon");
}
}
2019-05-24 01:48:44 +03:00
T![impl] => {
p.error("expected lifetime or type");
}
_ => {
if p.at(T![for]) {
2021-09-18 15:46:28 +03:00
// test where_pred_for
// fn for_trait<F>()
// where
// for<'a> F: Fn(&'a str)
// { }
types::for_binder(p);
}
types::type_(p);
2019-05-15 15:35:47 +03:00
if p.at(T![:]) {
bounds(p);
} else {
p.error("expected colon");
}
2018-08-13 18:23:14 +03:00
}
}
2018-08-08 19:26:38 +03:00
m.complete(p, WHERE_PRED);
}