From 892acc5b36552995515f91d2bc14ae82f81d7b8d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 31 Jul 2018 18:03:50 +0300 Subject: impl items --- src/grammar.ron | 1 + src/parser/grammar/items/traits.rs | 11 +++++++++++ src/parser/grammar/mod.rs | 24 ++++++++++++++++++++++++ src/syntax_kinds/generated.rs | 2 ++ 4 files changed, 38 insertions(+) (limited to 'src') diff --git a/src/grammar.ron b/src/grammar.ron index fca29f1ef..e10e5aaf4 100644 --- a/src/grammar.ron +++ b/src/grammar.ron @@ -158,6 +158,7 @@ Grammar( "TYPE_ARG_LIST", "PARAM_LIST", + "SELF_PARAM", "ARG_LIST", ] ) diff --git a/src/parser/grammar/items/traits.rs b/src/parser/grammar/items/traits.rs index 812cacfb7..7d657ced0 100644 --- a/src/parser/grammar/items/traits.rs +++ b/src/parser/grammar/items/traits.rs @@ -29,6 +29,17 @@ pub(super) fn impl_item(p: &mut Parser) { } type_params::where_clause(p); p.expect(L_CURLY); + + // test impl_item_items + // impl F { + // type A = i32; + // const B: i32 = 92; + // fn foo() {} + // fn bar(&self) {} + // } + while !p.at(EOF) && !p.at(R_CURLY) { + item(p); + } p.expect(R_CURLY); } diff --git a/src/parser/grammar/mod.rs b/src/parser/grammar/mod.rs index 498b45d44..b8847fb68 100644 --- a/src/parser/grammar/mod.rs +++ b/src/parser/grammar/mod.rs @@ -104,6 +104,7 @@ fn fn_value_parameters(p: &mut Parser) { assert!(p.at(L_PAREN)); let m = p.start(); p.bump(); + self_param(p); while !p.at(EOF) && !p.at(R_PAREN) { value_parameter(p); if !p.at(R_PAREN) { @@ -120,6 +121,29 @@ fn fn_value_parameters(p: &mut Parser) { types::type_(p); m.complete(p, VALUE_PARAMETER); } + + // test self_param + // impl S { + // fn a(self) {} + // fn b(&self,) {} + // fn c(&mut self, x: i32) {} + // } + fn self_param(p: &mut Parser) { + let la1 = p.nth(1); + let la2 = p.nth(2); + let n_toks = match (p.current(), la1, la2) { + (SELF_KW, _, _) => 1, + (AMPERSAND, SELF_KW, _) => 2, + (AMPERSAND, MUT_KW, SELF_KW) => 3, + _ => 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); + } + } } fn fn_ret_type(p: &mut Parser) { diff --git a/src/syntax_kinds/generated.rs b/src/syntax_kinds/generated.rs index 699f5282e..0603f53fd 100644 --- a/src/syntax_kinds/generated.rs +++ b/src/syntax_kinds/generated.rs @@ -145,6 +145,7 @@ pub enum SyntaxKind { TYPE_PARAM_LIST, TYPE_ARG_LIST, PARAM_LIST, + SELF_PARAM, ARG_LIST, // Technical SyntaxKinds: they appear temporally during parsing, // but never end up in the final tree @@ -298,6 +299,7 @@ impl SyntaxKind { TYPE_PARAM_LIST => &SyntaxInfo { name: "TYPE_PARAM_LIST" }, TYPE_ARG_LIST => &SyntaxInfo { name: "TYPE_ARG_LIST" }, PARAM_LIST => &SyntaxInfo { name: "PARAM_LIST" }, + SELF_PARAM => &SyntaxInfo { name: "SELF_PARAM" }, ARG_LIST => &SyntaxInfo { name: "ARG_LIST" }, TOMBSTONE => &SyntaxInfo { name: "TOMBSTONE" }, EOF => &SyntaxInfo { name: "EOF" }, -- cgit v1.2.3