From b04d4a88d1ba7f04445e807b6a816930b1e9bbf2 Mon Sep 17 00:00:00 2001 From: roblabla Date: Sun, 22 Dec 2019 00:38:23 +0000 Subject: [PATCH] Parse const generics Fixes #1574 Fixes #2281 --- crates/ra_parser/src/grammar/type_params.rs | 11 +++++++ crates/ra_parser/src/syntax_kind/generated.rs | 1 + crates/ra_syntax/src/ast/generated.rs | 30 +++++++++++++++++++ crates/ra_syntax/src/grammar.ron | 5 ++++ .../parser/inline/ok/0147_const_param.rs | 1 + .../parser/inline/ok/0147_const_param.txt | 23 ++++++++++++++ 6 files changed, 71 insertions(+) create mode 100644 crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.rs create mode 100644 crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.txt diff --git a/crates/ra_parser/src/grammar/type_params.rs b/crates/ra_parser/src/grammar/type_params.rs index 34406b5bd5a..50e4900c31a 100644 --- a/crates/ra_parser/src/grammar/type_params.rs +++ b/crates/ra_parser/src/grammar/type_params.rs @@ -25,6 +25,7 @@ fn type_param_list(p: &mut Parser) { match p.current() { LIFETIME => lifetime_param(p, m), IDENT => type_param(p, m), + CONST_KW => type_const_param(p, m), _ => { m.abandon(p); p.err_and_bump("expected type parameter") @@ -62,6 +63,16 @@ fn type_param(p: &mut Parser, m: Marker) { m.complete(p, TYPE_PARAM); } +// test const_param +// struct S; +fn type_const_param(p: &mut Parser, m: Marker) { + assert!(p.at(CONST_KW)); + p.bump(T![const]); + name(p); + types::ascription(p); + m.complete(p, CONST_PARAM); +} + // test type_param_bounds // struct S; pub(super) fn bounds(p: &mut Parser) { diff --git a/crates/ra_parser/src/syntax_kind/generated.rs b/crates/ra_parser/src/syntax_kind/generated.rs index fe0fcdb33f2..bee1f113473 100644 --- a/crates/ra_parser/src/syntax_kind/generated.rs +++ b/crates/ra_parser/src/syntax_kind/generated.rs @@ -227,6 +227,7 @@ pub enum SyntaxKind { TYPE_PARAM_LIST, LIFETIME_PARAM, TYPE_PARAM, + CONST_PARAM, TYPE_ARG_LIST, LIFETIME_ARG, TYPE_ARG, diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs index 9dd6bd3eacb..b917f77fef1 100644 --- a/crates/ra_syntax/src/ast/generated.rs +++ b/crates/ra_syntax/src/ast/generated.rs @@ -551,6 +551,36 @@ impl ConstDef { } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct ConstParam { + pub(crate) syntax: SyntaxNode, +} +impl AstNode for ConstParam { + fn can_cast(kind: SyntaxKind) -> bool { + match kind { + CONST_PARAM => true, + _ => false, + } + } + fn cast(syntax: SyntaxNode) -> Option { + if Self::can_cast(syntax.kind()) { + Some(Self { syntax }) + } else { + None + } + } + fn syntax(&self) -> &SyntaxNode { + &self.syntax + } +} +impl ast::NameOwner for ConstParam {} +impl ast::AttrsOwner for ConstParam {} +impl ast::TypeAscriptionOwner for ConstParam {} +impl ConstParam { + pub fn default_val(&self) -> Option { + AstChildren::new(&self.syntax).next() + } +} +#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ContinueExpr { pub(crate) syntax: SyntaxNode, } diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron index 9ffa9095bc3..d6802b6fb21 100644 --- a/crates/ra_syntax/src/grammar.ron +++ b/crates/ra_syntax/src/grammar.ron @@ -243,6 +243,7 @@ Grammar( "TYPE_PARAM_LIST", "LIFETIME_PARAM", "TYPE_PARAM", + "CONST_PARAM", "TYPE_ARG_LIST", "LIFETIME_ARG", "TYPE_ARG", @@ -602,6 +603,10 @@ Grammar( options: [("default_type", "TypeRef")], traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner"], ), + "ConstParam": ( + options: [("default_val", "Expr")], + traits: ["NameOwner", "AttrsOwner", "TypeAscriptionOwner"], + ), "LifetimeParam": ( traits: ["AttrsOwner"], ), diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.rs b/crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.rs new file mode 100644 index 00000000000..8cdb3b70367 --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.rs @@ -0,0 +1 @@ +struct S; diff --git a/crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.txt b/crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.txt new file mode 100644 index 00000000000..f81de7bac7c --- /dev/null +++ b/crates/ra_syntax/test_data/parser/inline/ok/0147_const_param.txt @@ -0,0 +1,23 @@ +SOURCE_FILE@[0; 24) + STRUCT_DEF@[0; 23) + STRUCT_KW@[0; 6) "struct" + WHITESPACE@[6; 7) " " + NAME@[7; 8) + IDENT@[7; 8) "S" + TYPE_PARAM_LIST@[8; 22) + L_ANGLE@[8; 9) "<" + CONST_PARAM@[9; 21) + CONST_KW@[9; 14) "const" + WHITESPACE@[14; 15) " " + NAME@[15; 16) + IDENT@[15; 16) "N" + COLON@[16; 17) ":" + WHITESPACE@[17; 18) " " + PATH_TYPE@[18; 21) + PATH@[18; 21) + PATH_SEGMENT@[18; 21) + NAME_REF@[18; 21) + IDENT@[18; 21) "u32" + R_ANGLE@[21; 22) ">" + SEMI@[22; 23) ";" + WHITESPACE@[23; 24) "\n"