10280: internal: type argument parsing refactoring r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2021-09-19 08:54:02 +00:00 committed by GitHub
commit 1c81c36219
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 139 additions and 29 deletions

View File

@ -26,15 +26,12 @@ pub(super) fn opt_generic_arg_list(p: &mut Parser, colon_colon_required: bool) {
// test type_arg
// type A = B<'static, i32, 1, { 2 }, Item=u64, true, false>;
fn generic_arg(p: &mut Parser) {
let m = p.start();
match p.current() {
LIFETIME_IDENT => {
lifetime(p);
m.complete(p, LIFETIME_ARG);
}
LIFETIME_IDENT => lifetime_arg(p),
// test associated_type_bounds
// fn print_all<T: Iterator<Item, Item::Item, Item::<true>, Item: Display, Item<'a> = Item>>(printables: T) {}
IDENT if [T![<], T![=], T![:]].contains(&p.nth(1)) => {
let m = p.start();
let path_ty = p.start();
let path = p.start();
let path_seg = p.start();
@ -78,37 +75,22 @@ fn generic_arg(p: &mut Parser) {
}
}
}
T!['{'] => {
expressions::block_expr(p);
m.complete(p, CONST_ARG);
}
k if k.is_literal() => {
expressions::literal(p);
m.complete(p, CONST_ARG);
}
T![true] | T![false] => {
expressions::literal(p);
m.complete(p, CONST_ARG);
}
// test const_generic_negated_literal
// fn f() { S::<-1> }
T![-] => {
let lm = p.start();
p.bump(T![-]);
expressions::literal(p);
lm.complete(p, PREFIX_EXPR);
m.complete(p, CONST_ARG);
}
_ => {
types::type_(p);
m.complete(p, TYPE_ARG);
}
T!['{'] | T![true] | T![false] | T![-] => const_arg(p),
k if k.is_literal() => const_arg(p),
_ => type_arg(p),
}
}
fn lifetime_arg(p: &mut Parser) {
let m = p.start();
lifetime(p);
m.complete(p, LIFETIME_ARG);
}
pub(super) fn const_arg(p: &mut Parser) {
let m = p.start();
// FIXME: duplicates the code above
match p.current() {
T!['{'] => {
expressions::block_expr(p);
@ -137,3 +119,9 @@ pub(super) fn const_arg(p: &mut Parser) {
}
}
}
fn type_arg(p: &mut Parser) {
let m = p.start();
types::type_(p);
m.complete(p, TYPE_ARG);
}

View File

@ -0,0 +1,115 @@
SOURCE_FILE@0..90
FN@0..40
FN_KW@0..2 "fn"
WHITESPACE@2..3 " "
NAME@3..4
IDENT@3..4 "f"
PARAM_LIST@4..6
L_PAREN@4..5 "("
R_PAREN@5..6 ")"
WHITESPACE@6..7 " "
BLOCK_EXPR@7..40
L_CURLY@7..8 "{"
WHITESPACE@8..13 "\n "
EXPR_STMT@13..38
PATH_EXPR@13..37
PATH@13..37
PATH_SEGMENT@13..37
NAME_REF@13..14
IDENT@13..14 "S"
GENERIC_ARG_LIST@14..37
COLON2@14..16 "::"
L_ANGLE@16..17 "<"
TYPE_ARG@17..36
PATH_TYPE@17..36
PATH@17..28
PATH_SEGMENT@17..28
NAME_REF@17..21
IDENT@17..21 "Item"
GENERIC_ARG_LIST@21..28
COLON2@21..23 "::"
L_ANGLE@23..24 "<"
TYPE_ARG@24..27
PATH_TYPE@24..27
PATH@24..27
PATH_SEGMENT@24..27
NAME_REF@24..27
IDENT@24..27 "lol"
R_ANGLE@27..28 ">"
GENERIC_ARG_LIST@28..36
COLON2@28..30 "::"
L_ANGLE@30..31 "<"
TYPE_ARG@31..35
PATH_TYPE@31..35
PATH@31..35
PATH_SEGMENT@31..35
NAME_REF@31..35
IDENT@31..35 "nope"
R_ANGLE@35..36 ">"
R_ANGLE@36..37 ">"
SEMICOLON@37..38 ";"
WHITESPACE@38..39 "\n"
R_CURLY@39..40 "}"
WHITESPACE@40..42 "\n\n"
FN@42..89
FN_KW@42..44 "fn"
WHITESPACE@44..45 " "
NAME@45..46
IDENT@45..46 "g"
PARAM_LIST@46..48
L_PAREN@46..47 "("
R_PAREN@47..48 ")"
WHITESPACE@48..49 " "
BLOCK_EXPR@49..89
L_CURLY@49..50 "{"
WHITESPACE@50..55 "\n "
LET_STMT@55..76
LET_KW@55..58 "let"
WHITESPACE@58..59 " "
WILDCARD_PAT@59..60
UNDERSCORE@59..60 "_"
COLON@60..61 ":"
WHITESPACE@61..62 " "
PATH_TYPE@62..76
PATH@62..76
PATH@62..73
PATH_SEGMENT@62..73
NAME_REF@62..66
IDENT@62..66 "Item"
GENERIC_ARG_LIST@66..73
COLON2@66..68 "::"
L_ANGLE@68..69 "<"
TYPE_ARG@69..72
PATH_TYPE@69..72
PATH@69..72
PATH_SEGMENT@69..72
NAME_REF@69..72
IDENT@69..72 "lol"
R_ANGLE@72..73 ">"
COLON2@73..75 "::"
ERROR@75..76
L_ANGLE@75..76 "<"
EXPR_STMT@76..83
BIN_EXPR@76..83
PATH_EXPR@76..80
PATH@76..80
PATH_SEGMENT@76..80
NAME_REF@76..80
IDENT@76..80 "nope"
R_ANGLE@80..81 ">"
WHITESPACE@81..82 " "
ERROR@82..83
EQ@82..83 "="
WHITESPACE@83..84 " "
EXPR_STMT@84..87
TUPLE_EXPR@84..86
L_PAREN@84..85 "("
R_PAREN@85..86 ")"
SEMICOLON@86..87 ";"
WHITESPACE@87..88 "\n"
R_CURLY@88..89 "}"
WHITESPACE@89..90 "\n"
error 75..75: expected identifier
error 76..76: expected SEMICOLON
error 82..82: expected expression
error 83..83: expected SEMICOLON

View File

@ -0,0 +1,7 @@
fn f() {
S::<Item::<lol>::<nope>>;
}
fn g() {
let _: Item::<lol>::<nope> = ();
}