From cd814fdf8113bc801b735ed462ba142e98f1f81b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 31 Jul 2018 18:24:30 +0300 Subject: trait bounds --- src/parser/grammar/items/mod.rs | 11 ++++++++ src/parser/grammar/items/traits.rs | 7 +++++ src/parser/grammar/type_params.rs | 52 +++++++++++++++++++++----------------- 3 files changed, 47 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/parser/grammar/items/mod.rs b/src/parser/grammar/items/mod.rs index 12bcf7924..037cdca53 100644 --- a/src/parser/grammar/items/mod.rs +++ b/src/parser/grammar/items/mod.rs @@ -149,6 +149,17 @@ fn item(p: &mut Parser) { } } } + TRAIT_KW => { + traits::trait_item(p); + TRAIT_ITEM + } + // test auto_trait + // auto trait T {} + IDENT if p.at_contextual_kw("auto") && la == TRAIT_KW => { + p.bump_remap(AUTO_KW); + traits::trait_item(p); + TRAIT_ITEM + } IMPL_KW => { traits::impl_item(p); IMPL_ITEM diff --git a/src/parser/grammar/items/traits.rs b/src/parser/grammar/items/traits.rs index 7d657ced0..60158fe41 100644 --- a/src/parser/grammar/items/traits.rs +++ b/src/parser/grammar/items/traits.rs @@ -1,9 +1,16 @@ use super::*; +// test trait_item +// trait T: Hash + Clone where U: Copy {} pub(super) fn trait_item(p: &mut Parser) { assert!(p.at(TRAIT_KW)); p.bump(); name(p); + type_params::list(p); + if p.at(COLON) { + type_params::bounds(p); + } + type_params::where_clause(p); p.expect(L_CURLY); p.expect(R_CURLY); } diff --git a/src/parser/grammar/type_params.rs b/src/parser/grammar/type_params.rs index ba0d4bfe8..3648ab5f3 100644 --- a/src/parser/grammar/type_params.rs +++ b/src/parser/grammar/type_params.rs @@ -39,29 +39,8 @@ pub(super) fn list(p: &mut Parser) { assert!(p.at(IDENT)); let m = p.start(); p.bump(); - if p.eat(COLON) { - // test type_param_bounds - // struct S; - loop { - let has_paren = p.eat(L_PAREN); - p.eat(QUESTION); - if p.at(FOR_KW) { - //TODO - } - if p.at(LIFETIME) { - p.bump(); - } else if paths::is_path_start(p) { - paths::type_path(p); - } else { - break; - } - if has_paren { - p.expect(R_PAREN); - } - if !p.eat(PLUS) { - break; - } - } + if p.at(COLON) { + bounds(p); } // test type_param_default // struct S; @@ -73,6 +52,33 @@ pub(super) fn list(p: &mut Parser) { } } +// test type_param_bounds +// struct S; +pub(super) fn bounds(p: &mut Parser) { + assert!(p.at(COLON)); + p.bump(); + loop { + let has_paren = p.eat(L_PAREN); + p.eat(QUESTION); + if p.at(FOR_KW) { + //TODO + } + if p.at(LIFETIME) { + p.bump(); + } else if paths::is_path_start(p) { + paths::type_path(p); + } else { + break; + } + if has_paren { + p.expect(R_PAREN); + } + if !p.eat(PLUS) { + break; + } + } +} + pub(super) fn where_clause(p: &mut Parser) { if p.at(WHERE_KW) { let m = p.start(); -- cgit v1.2.3