From b097090690f5edbe03f4aa9d042ba26c123699e4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 11 Feb 2018 00:46:17 +0300 Subject: G: more types --- src/parser/grammar/items/consts.rs | 2 +- src/parser/grammar/items/mod.rs | 2 +- src/parser/grammar/items/structs.rs | 4 +-- src/parser/grammar/type_params.rs | 2 +- src/parser/grammar/types.rs | 49 ++++++++++++++++++++++++++++++++----- src/syntax_kinds.rs | 8 ++++++ 6 files changed, 56 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/parser/grammar/items/consts.rs b/src/parser/grammar/items/consts.rs index 8117af706..5f3cf58c2 100644 --- a/src/parser/grammar/items/consts.rs +++ b/src/parser/grammar/items/consts.rs @@ -14,7 +14,7 @@ fn const_or_static(p: &mut Parser, kw: SyntaxKind) { p.eat(MUT_KW); // TODO: validator to forbid const mut name(p); p.expect(COLON); - types::type_ref(p); + types::ty(p); p.expect(EQ); expressions::expr(p); p.expect(SEMI); diff --git a/src/parser/grammar/items/mod.rs b/src/parser/grammar/items/mod.rs index 8bb821fb6..f1776e0e2 100644 --- a/src/parser/grammar/items/mod.rs +++ b/src/parser/grammar/items/mod.rs @@ -247,7 +247,7 @@ fn type_item(p: &mut Parser) { type_params::where_clause(p); p.expect(EQ); - types::type_ref(p); + types::ty(p); p.expect(SEMI); } diff --git a/src/parser/grammar/items/structs.rs b/src/parser/grammar/items/structs.rs index eca0d2e64..ad18fd270 100644 --- a/src/parser/grammar/items/structs.rs +++ b/src/parser/grammar/items/structs.rs @@ -89,7 +89,7 @@ fn named_fields(p: &mut Parser) { if p.at(IDENT) { name(p); p.expect(COLON); - types::type_ref(p); + types::ty(p); field.complete(p, NAMED_FIELD); } else { field.abandon(p); @@ -105,7 +105,7 @@ fn pos_fields(p: &mut Parser) { while !p.at(R_PAREN) && !p.at(EOF) { let pos_field = p.start(); visibility(p); - types::type_ref(p); + types::ty(p); pos_field.complete(p, POS_FIELD); if !p.at(R_PAREN) { diff --git a/src/parser/grammar/type_params.rs b/src/parser/grammar/type_params.rs index 8f62a471c..2462b260e 100644 --- a/src/parser/grammar/type_params.rs +++ b/src/parser/grammar/type_params.rs @@ -62,7 +62,7 @@ pub(super) fn list(p: &mut Parser) { } } if p.at(EQ) { - types::type_ref(p) + types::ty(p) } m.complete(p, TYPE_PARAM); } diff --git a/src/parser/grammar/types.rs b/src/parser/grammar/types.rs index c798edd08..71801d8ef 100644 --- a/src/parser/grammar/types.rs +++ b/src/parser/grammar/types.rs @@ -1,14 +1,51 @@ use super::*; -pub(super) fn type_ref(p: &mut Parser) { +pub(super) fn ty(p: &mut Parser) { match p.current() { - IDENT => p.bump(), - L_PAREN => { - p.bump(); - p.expect(R_PAREN); - } + L_PAREN => paren_or_tuple_ty(p), + IDENT => path_type(p), _ => { p.error("expected type"); } } } + +fn paren_or_tuple_ty(p: &mut Parser) { + assert!(p.at(L_PAREN)); + let m = p.start(); + p.bump(); + let mut n_types: u32 = 0; + let mut trailing_comma: bool = false; + while !p.at(EOF) && !p.at(R_PAREN) { + n_types += 1; + ty(p); + if p.eat(COMMA) { + trailing_comma = true; + } else { + trailing_comma = false; + break; + } + } + p.expect(R_PAREN); + + let kind = if n_types == 1 && !trailing_comma { + // test paren_type + // type T = (i32); + PAREN_TYPE + } else { + // test unit_type + // type T = (); + + // test singleton_tuple_type + // type T = (i32,); + TUPLE_TYPE + }; + m.complete(p, kind); +} + +fn path_type(p: &mut Parser) { + assert!(p.at(IDENT)); + let m = p.start(); + paths::type_path(p); + m.complete(p, PATH_TYPE); +} diff --git a/src/syntax_kinds.rs b/src/syntax_kinds.rs index 7450f9d6f..630d3d2a5 100644 --- a/src/syntax_kinds.rs +++ b/src/syntax_kinds.rs @@ -99,6 +99,10 @@ pub enum SyntaxKind { TRAIT_ITEM, IMPL_ITEM, TYPE_ITEM, + PAREN_TYPE, + TUPLE_TYPE, + NEVER_TYPE, + PATH_TYPE, EXTERN_BLOCK, ENUM_VARIANT, NAMED_FIELD, @@ -224,6 +228,10 @@ impl SyntaxKind { TRAIT_ITEM => &SyntaxInfo { name: "TRAIT_ITEM" }, IMPL_ITEM => &SyntaxInfo { name: "IMPL_ITEM" }, TYPE_ITEM => &SyntaxInfo { name: "TYPE_ITEM" }, + PAREN_TYPE => &SyntaxInfo { name: "PAREN_TYPE" }, + TUPLE_TYPE => &SyntaxInfo { name: "TUPLE_TYPE" }, + NEVER_TYPE => &SyntaxInfo { name: "NEVER_TYPE" }, + PATH_TYPE => &SyntaxInfo { name: "PATH_TYPE" }, EXTERN_BLOCK => &SyntaxInfo { name: "EXTERN_BLOCK" }, ENUM_VARIANT => &SyntaxInfo { name: "ENUM_VARIANT" }, NAMED_FIELD => &SyntaxInfo { name: "NAMED_FIELD" }, -- cgit v1.2.3