Skip to content

Commit

Permalink
Rust: Generalize declaritive and function-like macros (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicHorrorDev committed Jan 30, 2023
1 parent 55b0e59 commit f5f48e2
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lexers/embedded/rust.xml
Expand Up @@ -218,9 +218,6 @@
<rule pattern="(DoubleEndedIterator|ExactSizeIterator|IntoIterator|PartialOrd|PartialEq|ToString|Iterator|ToOwned|Default|Result|String|FnOnce|Extend|Option|FnMut|Unpin|Sized|AsRef|AsMut|Clone|None|From|Into|Sync|drop|Send|Drop|Copy|Some|Ord|Err|Box|Vec|Eq|Ok|Fn)\b">
<token type="NameBuiltin"/>
</rule>
<rule pattern="(is_powerpc64_feature_detected|is_aarch64_feature_detected|is_powerpc_feature_detected|is_mips64_feature_detected|is_mips_feature_detected|is_x86_feature_detected|is_arm_feature_detected|debug_assert_ne|debug_assert_eq|format_args_nl|concat_idents|unimplemented|include_bytes|compile_error|debug_assert|thread_local|trace_macros|macro_rules|format_args|module_path|unreachable|include_str|option_env|global_asm|log_syntax|stringify|assert_ne|assert_eq|llvm_asm|eprintln|include|matches|println|writeln|format|column|assert|concat|eprint|write|panic|print|file|todo|line|env|dbg|vec|cfg|asm)!">
<token type="NameFunctionMagic"/>
</rule>
<rule pattern="::\b">
<token type="Text"/>
</rule>
Expand Down Expand Up @@ -297,6 +294,13 @@
<rule pattern="\b(r#)?_?([A-Z][A-Z0-9_]*){2,}\b">
<token type="NameConstant"/>
</rule>
<rule pattern="([a-zA-Z_]\w*!)(\s*)(\(|\[|\{)">
<bygroups>
<token type="NameFunctionMagic"/>
<token type="TextWhitespace"/>
<token type="Punctuation"/>
</bygroups>
</rule>
<rule pattern="(r#)?[a-zA-Z_]\w*">
<token type="Name"/>
</rule>
Expand Down
14 changes: 14 additions & 0 deletions lexers/testdata/rust/proc_macros.actual
@@ -0,0 +1,14 @@
use smallvec::{smallvec, SmallVec};

lazy_static::lazy_static! {
static ref ONE: usize = 1;
}

fn main() {
pretty_assertions::assert_eq!(*ONE, *ONE);
let _: SmallVec<[_; 16]> = smallvec![1, 2, 3];

// And some builtin ones
println!("Hello, world!");
panic!();
}
92 changes: 92 additions & 0 deletions lexers/testdata/rust/proc_macros.expected
@@ -0,0 +1,92 @@
[
{"type":"Keyword","value":"use"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"smallvec"},
{"type":"Text","value":"::"},
{"type":"Punctuation","value":"{"},
{"type":"Name","value":"smallvec"},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"SmallVec"},
{"type":"Punctuation","value":"};"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"Name","value":"lazy_static"},
{"type":"Text","value":"::"},
{"type":"NameFunctionMagic","value":"lazy_static!"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Keyword","value":"static"},
{"type":"TextWhitespace","value":" "},
{"type":"Keyword","value":"ref"},
{"type":"TextWhitespace","value":" "},
{"type":"NameConstant","value":"ONE"},
{"type":"Text","value":": "},
{"type":"KeywordType","value":"usize"},
{"type":"Text","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n\n"},
{"type":"Keyword","value":"fn"},
{"type":"Text","value":" "},
{"type":"NameFunction","value":"main"},
{"type":"Punctuation","value":"()"},
{"type":"TextWhitespace","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"TextWhitespace","value":"\n "},
{"type":"Name","value":"pretty_assertions"},
{"type":"Text","value":"::"},
{"type":"NameFunctionMagic","value":"assert_eq!"},
{"type":"Punctuation","value":"("},
{"type":"Operator","value":"*"},
{"type":"NameConstant","value":"ONE"},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"*"},
{"type":"NameConstant","value":"ONE"},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n "},
{"type":"KeywordDeclaration","value":"let"},
{"type":"TextWhitespace","value":" "},
{"type":"Name","value":"_"},
{"type":"Text","value":": "},
{"type":"NameClass","value":"SmallVec"},
{"type":"Operator","value":"\u003c"},
{"type":"Punctuation","value":"["},
{"type":"Name","value":"_"},
{"type":"Punctuation","value":";"},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralNumberInteger","value":"16"},
{"type":"Punctuation","value":"]"},
{"type":"Operator","value":"\u003e"},
{"type":"TextWhitespace","value":" "},
{"type":"Operator","value":"="},
{"type":"TextWhitespace","value":" "},
{"type":"NameFunctionMagic","value":"smallvec!"},
{"type":"Punctuation","value":"["},
{"type":"LiteralNumberInteger","value":"1"},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralNumberInteger","value":"2"},
{"type":"Punctuation","value":","},
{"type":"TextWhitespace","value":" "},
{"type":"LiteralNumberInteger","value":"3"},
{"type":"Punctuation","value":"];"},
{"type":"TextWhitespace","value":"\n\n "},
{"type":"CommentSingle","value":"// And some builtin ones\n"},
{"type":"TextWhitespace","value":" "},
{"type":"NameFunctionMagic","value":"println!"},
{"type":"Punctuation","value":"("},
{"type":"LiteralString","value":"\"Hello, world!\""},
{"type":"Punctuation","value":");"},
{"type":"TextWhitespace","value":"\n "},
{"type":"NameFunctionMagic","value":"panic!"},
{"type":"Punctuation","value":"();"},
{"type":"TextWhitespace","value":"\n"},
{"type":"Punctuation","value":"}"},
{"type":"TextWhitespace","value":"\n"}
]

0 comments on commit f5f48e2

Please sign in to comment.