From 2ef16a4121ad497e7fb290445ffe644b6b8ceae6 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 10 Feb 2018 12:35:40 +0300 Subject: G: type item --- src/parser/grammar/items/mod.rs | 80 +++++++++++++++++++++++++-------------- src/parser/grammar/type_params.rs | 3 ++ src/parser/grammar/types.rs | 11 +++++- src/syntax_kinds.rs | 5 +++ 4 files changed, 70 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/parser/grammar/items/mod.rs b/src/parser/grammar/items/mod.rs index b73628ec0..ffe86fa97 100644 --- a/src/parser/grammar/items/mod.rs +++ b/src/parser/grammar/items/mod.rs @@ -150,7 +150,14 @@ fn item(p: &mut Parser) { } } } - + FN_KW => { + fn_item(p); + FN_ITEM + } + TYPE_KW => { + type_item(p); + TYPE_ITEM + } MOD_KW => { mod_item(p); MOD_ITEM @@ -163,10 +170,6 @@ fn item(p: &mut Parser) { structs::enum_item(p); ENUM_ITEM } - FN_KW => { - fn_item(p); - FN_ITEM - } L_CURLY => { item.abandon(p); error_block(p, "expected item"); @@ -203,29 +206,6 @@ fn extern_block(p: &mut Parser) { p.expect(R_CURLY); } -fn mod_item(p: &mut Parser) { - assert!(p.at(MOD_KW)); - p.bump(); - - if p.expect(IDENT) && !p.eat(SEMI) { - if p.expect(L_CURLY) { - mod_contents(p, true); - p.expect(R_CURLY); - } - } -} - -fn abi(p: &mut Parser) { - assert!(p.at(EXTERN_KW)); - let abi = p.start(); - p.bump(); - match p.current() { - STRING | RAW_STRING => p.bump(), - _ => (), - } - abi.complete(p, ABI); -} - fn fn_item(p: &mut Parser) { assert!(p.at(FN_KW)); p.bump(); @@ -248,3 +228,47 @@ fn fn_item(p: &mut Parser) { p.expect(R_PAREN); } } + +// test type_item +// type Foo = Bar; +fn type_item(p: &mut Parser) { + assert!(p.at(TYPE_KW)); + p.bump(); + + p.expect(IDENT); + + // test type_item_type_params + // type Result = (); + type_params::list(p); + + // test type_item_where_clause + // type Foo where Foo: Copy = (); + type_params::where_clause(p); + + p.expect(EQ); + types::type_ref(p); + p.expect(SEMI); +} + +fn mod_item(p: &mut Parser) { + assert!(p.at(MOD_KW)); + p.bump(); + + if p.expect(IDENT) && !p.eat(SEMI) { + if p.expect(L_CURLY) { + mod_contents(p, true); + p.expect(R_CURLY); + } + } +} + +fn abi(p: &mut Parser) { + assert!(p.at(EXTERN_KW)); + let abi = p.start(); + p.bump(); + match p.current() { + STRING | RAW_STRING => p.bump(), + _ => (), + } + abi.complete(p, ABI); +} diff --git a/src/parser/grammar/type_params.rs b/src/parser/grammar/type_params.rs index 12c9a5362..73c3cf8b8 100644 --- a/src/parser/grammar/type_params.rs +++ b/src/parser/grammar/type_params.rs @@ -71,5 +71,8 @@ pub(super) fn list(p: &mut Parser) { pub(super) fn where_clause(p: &mut Parser) { if p.at(WHERE_KW) { p.bump(); + p.expect(IDENT); + p.expect(COLON); + p.expect(IDENT); } } diff --git a/src/parser/grammar/types.rs b/src/parser/grammar/types.rs index 1a3d44a0a..c798edd08 100644 --- a/src/parser/grammar/types.rs +++ b/src/parser/grammar/types.rs @@ -1,5 +1,14 @@ use super::*; pub(super) fn type_ref(p: &mut Parser) { - p.expect(IDENT); + match p.current() { + IDENT => p.bump(), + L_PAREN => { + p.bump(); + p.expect(R_PAREN); + } + _ => { + p.error("expected type"); + } + } } diff --git a/src/syntax_kinds.rs b/src/syntax_kinds.rs index 27bc1cafa..501b940bb 100644 --- a/src/syntax_kinds.rs +++ b/src/syntax_kinds.rs @@ -83,6 +83,7 @@ pub enum SyntaxKind { STATIC_KW, MUT_KW, UNSAFE_KW, + TYPE_KW, AUTO_KW, DEFAULT_KW, UNION_KW, @@ -97,6 +98,7 @@ pub enum SyntaxKind { CONST_ITEM, TRAIT_ITEM, IMPL_ITEM, + TYPE_ITEM, EXTERN_BLOCK, ENUM_VARIANT, NAMED_FIELD, @@ -203,6 +205,7 @@ impl SyntaxKind { STATIC_KW => &SyntaxInfo { name: "STATIC_KW" }, MUT_KW => &SyntaxInfo { name: "MUT_KW" }, UNSAFE_KW => &SyntaxInfo { name: "UNSAFE_KW" }, + TYPE_KW => &SyntaxInfo { name: "TYPE_KW" }, AUTO_KW => &SyntaxInfo { name: "AUTO_KW" }, DEFAULT_KW => &SyntaxInfo { name: "DEFAULT_KW" }, UNION_KW => &SyntaxInfo { name: "UNION_KW" }, @@ -217,6 +220,7 @@ impl SyntaxKind { CONST_ITEM => &SyntaxInfo { name: "CONST_ITEM" }, TRAIT_ITEM => &SyntaxInfo { name: "TRAIT_ITEM" }, IMPL_ITEM => &SyntaxInfo { name: "IMPL_ITEM" }, + TYPE_ITEM => &SyntaxInfo { name: "TYPE_ITEM" }, EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" }, ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" }, NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" }, @@ -268,6 +272,7 @@ pub(crate) fn ident_to_keyword(ident: &str) -> Option { "static" => Some(STATIC_KW), "mut" => Some(MUT_KW), "unsafe" => Some(UNSAFE_KW), + "type" => Some(TYPE_KW), _ => None, } } -- cgit v1.2.3