Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support macros 2.0 #235

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ module.exports = grammar({
_declaration_statement: $ => choice(
$.const_item,
$.macro_invocation,
$.macro_rules,
$.macro_definition,
$.empty_statement,
$.attribute_item,
Expand All @@ -159,7 +160,7 @@ module.exports = grammar({

// Section - Macro definitions

macro_definition: $ => {
macro_rules: $ => {
const rules = seq(
repeat(seq($.macro_rule, ';')),
optional($.macro_rule),
Expand All @@ -179,12 +180,44 @@ module.exports = grammar({
);
},

macro_definition: $ => {
const rules = seq(
repeat(seq($.macro_rule, ',')),
optional($.macro_rule),
);

return seq(
optional($.visibility_modifier),
'macro',
field('name', choice(
$.identifier,
$._reserved_identifier,
)),
choice(
seq($.macro_def_args, $.macro_def_body),
seq('{', rules, '}'),
),
);
},

macro_rule: $ => seq(
field('left', $.token_tree_pattern),
'=>',
field('right', $.token_tree),
),

macro_def_args: $ => seq(
'(',
field('left', repeat($._token_pattern)),
')',
),

macro_def_body: $ => seq(
'{',
field('right', repeat($._tokens)),
'}',
),

_token_pattern: $ => choice(
$.token_tree_pattern,
$.token_repetition_pattern,
Expand Down
156 changes: 155 additions & 1 deletion src/grammar.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading