From a077533513de5018e227c740a470e18652b63172 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 23 Aug 2018 19:07:33 +0300 Subject: Allow arbitrary self-types --- crates/libsyntax2/src/grammar/params.rs | 39 ++++++++---- .../tests/data/parser/inline/0058_type_arg.rs | 2 +- .../tests/data/parser/inline/0058_type_arg.txt | 8 +-- .../data/parser/inline/0104_arb_self_types.rs | 4 ++ .../data/parser/inline/0104_arb_self_types.txt | 69 ++++++++++++++++++++++ 5 files changed, 104 insertions(+), 18 deletions(-) create mode 100644 crates/libsyntax2/tests/data/parser/inline/0104_arb_self_types.rs create mode 100644 crates/libsyntax2/tests/data/parser/inline/0104_arb_self_types.txt (limited to 'crates') diff --git a/crates/libsyntax2/src/grammar/params.rs b/crates/libsyntax2/src/grammar/params.rs index 32e905cb2..7e58e8713 100644 --- a/crates/libsyntax2/src/grammar/params.rs +++ b/crates/libsyntax2/src/grammar/params.rs @@ -94,20 +94,33 @@ fn value_parameter(p: &mut Parser, flavor: Flavor) { // 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, - (AMP, SELF_KW, _, _) => 2, - (AMP, MUT_KW, SELF_KW, _) => 3, - (AMP, LIFETIME, SELF_KW, _) => 3, - (AMP, LIFETIME, MUT_KW, SELF_KW) => 4, - _ => return, - }; - let m = p.start(); - for _ in 0..n_toks { + let m; + if p.at(SELF_KW) { + m = p.start(); p.bump(); + // test arb_self_types + // impl S { + // fn a(self: &Self) {} + // fn b(self: Box) {} + // } + if p.at(COLON) { + types::ascription(p); + } + } else { + let la1 = p.nth(1); + let la2 = p.nth(2); + let la3 = p.nth(3); + let n_toks = match (p.current(), la1, la2, la3) { + (AMP, SELF_KW, _, _) => 2, + (AMP, MUT_KW, SELF_KW, _) => 3, + (AMP, LIFETIME, SELF_KW, _) => 3, + (AMP, LIFETIME, MUT_KW, SELF_KW) => 4, + _ => return, + }; + m = p.start(); + for _ in 0..n_toks { + p.bump(); + } } m.complete(p, SELF_PARAM); if !p.at(R_PAREN) { diff --git a/crates/libsyntax2/tests/data/parser/inline/0058_type_arg.rs b/crates/libsyntax2/tests/data/parser/inline/0058_type_arg.rs index 385c43131..f0c8cc3a8 100644 --- a/crates/libsyntax2/tests/data/parser/inline/0058_type_arg.rs +++ b/crates/libsyntax2/tests/data/parser/inline/0058_type_arg.rs @@ -1 +1 @@ -type A = B<'static, i32, Item=u64>; +type A = B<'static, i32, Item=u64> diff --git a/crates/libsyntax2/tests/data/parser/inline/0058_type_arg.txt b/crates/libsyntax2/tests/data/parser/inline/0058_type_arg.txt index 6fc29c03d..297456ec4 100644 --- a/crates/libsyntax2/tests/data/parser/inline/0058_type_arg.txt +++ b/crates/libsyntax2/tests/data/parser/inline/0058_type_arg.txt @@ -1,5 +1,5 @@ -FILE@[0; 36) - TYPE_DEF@[0; 35) +FILE@[0; 35) + TYPE_DEF@[0; 34) TYPE_KW@[0; 4) WHITESPACE@[4; 5) NAME@[5; 6) @@ -36,5 +36,5 @@ FILE@[0; 36) NAME_REF@[30; 33) IDENT@[30; 33) "u64" R_ANGLE@[33; 34) - SEMI@[34; 35) - WHITESPACE@[35; 36) + err: `expected SEMI` + WHITESPACE@[34; 35) diff --git a/crates/libsyntax2/tests/data/parser/inline/0104_arb_self_types.rs b/crates/libsyntax2/tests/data/parser/inline/0104_arb_self_types.rs new file mode 100644 index 000000000..2bf2a69f6 --- /dev/null +++ b/crates/libsyntax2/tests/data/parser/inline/0104_arb_self_types.rs @@ -0,0 +1,4 @@ +impl S { + fn a(self: &Self) {} + fn b(self: Box) {} +} diff --git a/crates/libsyntax2/tests/data/parser/inline/0104_arb_self_types.txt b/crates/libsyntax2/tests/data/parser/inline/0104_arb_self_types.txt new file mode 100644 index 000000000..41e3505fe --- /dev/null +++ b/crates/libsyntax2/tests/data/parser/inline/0104_arb_self_types.txt @@ -0,0 +1,69 @@ +FILE@[0; 65) + IMPL_ITEM@[0; 64) + IMPL_KW@[0; 4) + WHITESPACE@[4; 5) + PATH_TYPE@[5; 6) + PATH@[5; 6) + PATH_SEGMENT@[5; 6) + NAME_REF@[5; 6) + IDENT@[5; 6) "S" + WHITESPACE@[6; 7) + L_CURLY@[7; 8) + WHITESPACE@[8; 13) + FN_DEF@[13; 33) + FN_KW@[13; 15) + WHITESPACE@[15; 16) + NAME@[16; 17) + IDENT@[16; 17) "a" + PARAM_LIST@[17; 30) + L_PAREN@[17; 18) + SELF_PARAM@[18; 29) + SELF_KW@[18; 22) + COLON@[22; 23) + WHITESPACE@[23; 24) + REFERENCE_TYPE@[24; 29) + AMP@[24; 25) + PATH_TYPE@[25; 29) + PATH@[25; 29) + PATH_SEGMENT@[25; 29) + NAME_REF@[25; 29) + IDENT@[25; 29) "Self" + R_PAREN@[29; 30) + WHITESPACE@[30; 31) + BLOCK_EXPR@[31; 33) + L_CURLY@[31; 32) + R_CURLY@[32; 33) + WHITESPACE@[33; 38) + FN_DEF@[38; 62) + FN_KW@[38; 40) + WHITESPACE@[40; 41) + NAME@[41; 42) + IDENT@[41; 42) "b" + PARAM_LIST@[42; 59) + L_PAREN@[42; 43) + SELF_PARAM@[43; 58) + SELF_KW@[43; 47) + COLON@[47; 48) + WHITESPACE@[48; 49) + PATH_TYPE@[49; 58) + PATH@[49; 58) + PATH_SEGMENT@[49; 58) + NAME_REF@[49; 52) + IDENT@[49; 52) "Box" + TYPE_ARG_LIST@[52; 58) + L_ANGLE@[52; 53) + TYPE_ARG@[53; 57) + PATH_TYPE@[53; 57) + PATH@[53; 57) + PATH_SEGMENT@[53; 57) + NAME_REF@[53; 57) + IDENT@[53; 57) "Self" + R_ANGLE@[57; 58) + R_PAREN@[58; 59) + WHITESPACE@[59; 60) + BLOCK_EXPR@[60; 62) + L_CURLY@[60; 61) + R_CURLY@[61; 62) + WHITESPACE@[62; 63) + R_CURLY@[63; 64) + WHITESPACE@[64; 65) -- cgit v1.2.3