From bea8f581186bb8d933357599468dd7d862ae847b Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Sat, 21 Dec 2019 18:29:14 +0800 Subject: Add macro 2.0 support in parser --- crates/ra_parser/src/grammar/items.rs | 28 +++++++++++++++++++++++++++ crates/ra_parser/src/syntax_kind/generated.rs | 8 +++++++- 2 files changed, 35 insertions(+), 1 deletion(-) (limited to 'crates/ra_parser') diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs index 370990e21..7e345f733 100644 --- a/crates/ra_parser/src/grammar/items.rs +++ b/crates/ra_parser/src/grammar/items.rs @@ -249,6 +249,11 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> { // } adt::struct_def(p, m); } + // test pub_macro_def + // pub macro m($:ident) {} + T![macro] => { + macro_def(p, m); + } IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => { // test union_items // union Foo {} @@ -379,6 +384,29 @@ pub(crate) fn mod_item_list(p: &mut Parser) { m.complete(p, ITEM_LIST); } +// test macro_def +// macro m { ($i:ident) => {} } +// macro m($i:ident) {} +fn macro_def(p: &mut Parser, m: Marker) { + p.expect(T![macro]); + p.expect(IDENT); + if p.at(T!['{']) { + token_tree(p); + } else if !p.at(T!['(']) { + p.error("unmatched `(`"); + } else { + let m = p.start(); + token_tree(p); + match p.current() { + T!['{'] | T!['['] | T!['('] => token_tree(p), + _ => p.error("expected `{`, `[`, `(`"), + } + m.complete(p, TOKEN_TREE); + } + + m.complete(p, MACRO_DEF); +} + fn macro_call(p: &mut Parser) -> BlockLike { assert!(paths::is_use_path_start(p)); paths::use_path(p); diff --git a/crates/ra_parser/src/syntax_kind/generated.rs b/crates/ra_parser/src/syntax_kind/generated.rs index fe0fcdb33..afe4ce51a 100644 --- a/crates/ra_parser/src/syntax_kind/generated.rs +++ b/crates/ra_parser/src/syntax_kind/generated.rs @@ -100,6 +100,7 @@ pub enum SyntaxKind { TRY_KW, BOX_KW, AWAIT_KW, + MACRO_KW, AUTO_KW, DEFAULT_KW, EXISTENTIAL_KW, @@ -136,6 +137,7 @@ pub enum SyntaxKind { TYPE_ALIAS_DEF, MACRO_CALL, TOKEN_TREE, + MACRO_DEF, PAREN_TYPE, TUPLE_TYPE, NEVER_TYPE, @@ -251,7 +253,7 @@ impl SyntaxKind { | SUPER_KW | IN_KW | WHERE_KW | FOR_KW | LOOP_KW | WHILE_KW | CONTINUE_KW | BREAK_KW | IF_KW | ELSE_KW | MATCH_KW | CONST_KW | STATIC_KW | MUT_KW | UNSAFE_KW | TYPE_KW | REF_KW | LET_KW | MOVE_KW | RETURN_KW | TRY_KW | BOX_KW | AWAIT_KW - | AUTO_KW | DEFAULT_KW | EXISTENTIAL_KW | UNION_KW => true, + | MACRO_KW | AUTO_KW | DEFAULT_KW | EXISTENTIAL_KW | UNION_KW => true, _ => false, } } @@ -314,6 +316,7 @@ impl SyntaxKind { "try" => TRY_KW, "box" => BOX_KW, "await" => AWAIT_KW, + "macro" => MACRO_KW, _ => return None, }; Some(kw) @@ -628,6 +631,9 @@ macro_rules! T { ( await ) => { $crate::SyntaxKind::AWAIT_KW }; + ( macro ) => { + $crate::SyntaxKind::MACRO_KW + }; ( auto ) => { $crate::SyntaxKind::AUTO_KW }; -- cgit v1.2.3