Skip to content

Commit

Permalink
update fragment specifiers/allow spaces in metavariable declarations
Browse files Browse the repository at this point in the history
- Add `pat_param` specifier from 2021 edition
- Add `expr_2021` specifier from 2024 edition: https://doc.rust-lang.org/edition-guide/rust-2024/macro-fragment-specifiers.html
  - This specifier is currently unstable but the name seems to be confirmed: rust-lang/rust#123742 (comment)
- Require a word boundary (`\b`) at the end of the specifier
- Replace `[A-Za-z0-9_]` with `\w`
- Allow spaces before and after the `:` in metavariable declarations
- Add tests for the new specifiers and spacing
  • Loading branch information
youngspe committed Nov 11, 2024
1 parent c7670c5 commit 01dc6be
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
4 changes: 2 additions & 2 deletions syntaxes/rust.tmLanguage.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
{
"comment": "macro type metavariables",
"name": "meta.macro.metavariable.type.rust",
"match": "(\\$)((crate)|([A-Z][A-Za-z0-9_]*))((:)(block|expr|ident|item|lifetime|literal|meta|path?|stmt|tt|ty|vis))?",
"match": "(\\$)((crate)|([A-Z]\\w*))(\\s*(:)\\s*(block|expr(?:_2021)?|ident|item|lifetime|literal|meta|pat(?:_param)?|path|stmt|tt|ty|vis)\\b)?",
"captures": {
"1": {
"name": "keyword.operator.macro.dollar.rust"
Expand All @@ -77,7 +77,7 @@
{
"comment": "macro metavariables",
"name": "meta.macro.metavariable.rust",
"match": "(\\$)([a-z][A-Za-z0-9_]*)((:)(block|expr|ident|item|lifetime|literal|meta|path?|stmt|tt|ty|vis))?",
"match": "(\\$)([a-z]\\w*)(\\s*(:)\\s*(block|expr(?:_2021)?|ident|item|lifetime|literal|meta|pat(?:_param)?|path|stmt|tt|ty|vis)\\b)?",
"captures": {
"1": {
"name": "keyword.operator.macro.dollar.rust"
Expand Down
4 changes: 2 additions & 2 deletions syntaxes/rust.tmLanguage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ patterns:
-
comment: macro type metavariables
name: meta.macro.metavariable.type.rust
match: (\$)((crate)|([A-Z][A-Za-z0-9_]*))((:)(block|expr|ident|item|lifetime|literal|meta|path?|stmt|tt|ty|vis))?
match: (\$)((crate)|([A-Z]\w*))(\s*(:)\s*(block|expr(?:_2021)?|ident|item|lifetime|literal|meta|pat(?:_param)?|path|stmt|tt|ty|vis)\b)?
captures:
1:
name: keyword.operator.macro.dollar.rust
Expand All @@ -46,7 +46,7 @@ patterns:
-
comment: macro metavariables
name: meta.macro.metavariable.rust
match: (\$)([a-z][A-Za-z0-9_]*)((:)(block|expr|ident|item|lifetime|literal|meta|path?|stmt|tt|ty|vis))?
match: (\$)([a-z]\w*)(\s*(:)\s*(block|expr(?:_2021)?|ident|item|lifetime|literal|meta|pat(?:_param)?|path|stmt|tt|ty|vis)\b)?
captures:
1:
name: keyword.operator.macro.dollar.rust
Expand Down
41 changes: 41 additions & 0 deletions test/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,44 @@ let x6 = 1.123E-12;
// ^ keyword.operator.exponent.rust
// ^ keyword.operator.exponent.sign.rust
// ^^ constant.numeric.decimal.exponent.mantissa.rust

// macro metavarables
macro_rules! metavariable_test {
($var:tt $Type:ty $var : tt $Type :ty) => {};
// ^^^^^^^ ^^^^^^^^^ meta.macro.metavariable.rust
// ^^^^^^^^ ^^^^^^^^^ meta.macro.metavariable.type.rust
// ^ ^ ^ ^ keyword.operator.macro.dollar.rust
// ^^^ ^^^ variable.other.metavariable.name.rust
// ^^^^ ^^^^ entity.name.type.metavariable.rust
// ^ ^ ^ ^ keyword.operator.key-value.rust
// ^^ ^^ ^^ ^^ variable.other.metavariable.specifier.rust
($var:pat_param $Var:pat_param) => {};
// ^^^^^^^^^^^^^^ meta.macro.metavariable.rust
// ^^^^^^^^^^^^^^ meta.macro.metavariable.type.rust
// ^ ^ keyword.operator.macro.dollar.rust
// ^^^ variable.other.metavariable.name.rust
// ^^^ entity.name.type.metavariable.rust
// ^ ^ keyword.operator.key-value.rust
// ^^^^^^^^^ ^^^^^^^^^ variable.other.metavariable.specifier.rust
($var: expr_2021 $Var: expr_2021) => {};
// ^^^^^^^^^^^^^^^ meta.macro.metavariable.rust
// ^^^^^^^^^^^^^^^ meta.macro.metavariable.type.rust
// ^ ^ keyword.operator.macro.dollar.rust
// ^^^ variable.other.metavariable.name.rust
// ^^^ entity.name.type.metavariable.rust
// ^ ^ keyword.operator.key-value.rust
// ^^^^^^^^^ ^^^^^^^^^ variable.other.metavariable.specifier.rust
() => { $var $Type $crate };
// ^^^^ meta.macro.metavariable.rust
// ^^^^^ ^^^^^^ meta.macro.metavariable.type.rust
// ^ ^ ^ - meta.macro.metavariable.rust meta.macro.metavariable.type.rust
// ^ ^ ^ keyword.operator.macro.dollar.rust
// ^^^ variable.other.metavariable.name.rust
// ^^^^ entity.name.type.metavariable.rust
// ^^^^^ keyword.other.crate.rust
() => { $var: not_a_specifier };
// ^^^^ meta.macro.metavariable.rust
// ^ keyword.operator.macro.dollar.rust
// ^^^ variable.other.metavariable.name.rust
// ^^^^^^^^^^^^^^^^^ - meta.macro.metavariable.rust
}

0 comments on commit 01dc6be

Please sign in to comment.