From cb8632d87ca0ad4e686452b72c0c0c01cd7a869b Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 29 Apr 2021 03:07:53 +0200 Subject: Parse const param defaults --- crates/parser/src/grammar/type_args.rs | 32 ++++++++++++++++++++++++++++++++ crates/parser/src/grammar/type_params.rs | 10 ++++++++++ 2 files changed, 42 insertions(+) (limited to 'crates/parser') diff --git a/crates/parser/src/grammar/type_args.rs b/crates/parser/src/grammar/type_args.rs index 56266b8d4..be36cad17 100644 --- a/crates/parser/src/grammar/type_args.rs +++ b/crates/parser/src/grammar/type_args.rs @@ -25,6 +25,38 @@ pub(super) fn opt_generic_arg_list(p: &mut Parser, colon_colon_required: bool) { m.complete(p, GENERIC_ARG_LIST); } +pub(super) fn const_arg(p: &mut Parser) { + let m = p.start(); + // FIXME: duplicates the code below + match p.current() { + T!['{'] => { + expressions::block_expr(p); + m.complete(p, CONST_ARG); + } + k if k.is_literal() => { + expressions::literal(p); + m.complete(p, CONST_ARG); + } + T![true] | T![false] => { + expressions::literal(p); + m.complete(p, CONST_ARG); + } + T![-] => { + let lm = p.start(); + p.bump(T![-]); + expressions::literal(p); + lm.complete(p, PREFIX_EXPR); + m.complete(p, CONST_ARG); + } + _ => { + let lm = p.start(); + paths::use_path(p); + lm.complete(p, PATH_EXPR); + m.complete(p, CONST_ARG); + } + } +} + // test type_arg // type A = B<'static, i32, 1, { 2 }, Item=u64, true, false>; fn generic_arg(p: &mut Parser) { diff --git a/crates/parser/src/grammar/type_params.rs b/crates/parser/src/grammar/type_params.rs index 3de5248da..b1f979281 100644 --- a/crates/parser/src/grammar/type_params.rs +++ b/crates/parser/src/grammar/type_params.rs @@ -70,6 +70,16 @@ fn const_param(p: &mut Parser, m: Marker) { p.bump(T![const]); name(p); types::ascription(p); + + // test const_param_defaults + // struct A; + // struct B; + // struct C; + if p.at(T![=]) { + p.bump(T![=]); + type_args::const_arg(p); + } + m.complete(p, CONST_PARAM); } -- cgit v1.2.3