aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_parser/src/grammar
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2019-12-21 10:47:47 +0000
committerGitHub <[email protected]>2019-12-21 10:47:47 +0000
commit90ef070db3dce0a7acb9cd11d0b0d72de13c9d79 (patch)
treec6fcc8c77fe4948b356e397fb5fe1f8e8ac39037 /crates/ra_parser/src/grammar
parent3ebf15c9b29b1fed6319d04f540ad48cd4bd6995 (diff)
parent4195c0e5f9a3db7646d4df28aa8c77a863c35759 (diff)
Merge #2628
2628: Add macro 2.0 support in parser r=matklad a=edwin0cheng This PR added a new syntax kind : `MACRO_DEF` and a keyword `MACRO_KW` there are two syntax for declarative macro 2.0 : 1. Normal : `macro m { ($i:ident) => {} }` , which handle similar to legacy one. 2. Call like: `macro m($i:ident) {}`, it produces a single token tree which have two child token trees : `($i:ident)` and `{}` Co-authored-by: Edwin Cheng <[email protected]>
Diffstat (limited to 'crates/ra_parser/src/grammar')
-rw-r--r--crates/ra_parser/src/grammar/items.rs30
1 files changed, 29 insertions, 1 deletions
diff --git a/crates/ra_parser/src/grammar/items.rs b/crates/ra_parser/src/grammar/items.rs
index 370990e21..3c717e5f9 100644
--- a/crates/ra_parser/src/grammar/items.rs
+++ b/crates/ra_parser/src/grammar/items.rs
@@ -33,7 +33,7 @@ pub(super) enum ItemFlavor {
33 33
34pub(super) const ITEM_RECOVERY_SET: TokenSet = token_set![ 34pub(super) const ITEM_RECOVERY_SET: TokenSet = token_set![
35 FN_KW, STRUCT_KW, ENUM_KW, IMPL_KW, TRAIT_KW, CONST_KW, STATIC_KW, LET_KW, MOD_KW, PUB_KW, 35 FN_KW, STRUCT_KW, ENUM_KW, IMPL_KW, TRAIT_KW, CONST_KW, STATIC_KW, LET_KW, MOD_KW, PUB_KW,
36 CRATE_KW, USE_KW 36 CRATE_KW, USE_KW, MACRO_KW
37]; 37];
38 38
39pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) { 39pub(super) fn item_or_macro(p: &mut Parser, stop_on_r_curly: bool, flavor: ItemFlavor) {
@@ -249,6 +249,11 @@ fn items_without_modifiers(p: &mut Parser, m: Marker) -> Result<(), Marker> {
249 // } 249 // }
250 adt::struct_def(p, m); 250 adt::struct_def(p, m);
251 } 251 }
252 // test pub_macro_def
253 // pub macro m($:ident) {}
254 T![macro] => {
255 macro_def(p, m);
256 }
252 IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => { 257 IDENT if p.at_contextual_kw("union") && p.nth(1) == IDENT => {
253 // test union_items 258 // test union_items
254 // union Foo {} 259 // union Foo {}
@@ -379,6 +384,29 @@ pub(crate) fn mod_item_list(p: &mut Parser) {
379 m.complete(p, ITEM_LIST); 384 m.complete(p, ITEM_LIST);
380} 385}
381 386
387// test macro_def
388// macro m { ($i:ident) => {} }
389// macro m($i:ident) {}
390fn macro_def(p: &mut Parser, m: Marker) {
391 p.expect(T![macro]);
392 p.expect(IDENT);
393 if p.at(T!['{']) {
394 token_tree(p);
395 } else if !p.at(T!['(']) {
396 p.error("unmatched `(`");
397 } else {
398 let m = p.start();
399 token_tree(p);
400 match p.current() {
401 T!['{'] | T!['['] | T!['('] => token_tree(p),
402 _ => p.error("expected `{`, `[`, `(`"),
403 }
404 m.complete(p, TOKEN_TREE);
405 }
406
407 m.complete(p, MACRO_DEF);
408}
409
382fn macro_call(p: &mut Parser) -> BlockLike { 410fn macro_call(p: &mut Parser) -> BlockLike {
383 assert!(paths::is_use_path_start(p)); 411 assert!(paths::is_use_path_start(p));
384 paths::use_path(p); 412 paths::use_path(p);