1 package r
2
3 import (
4 . "github.com/alecthomas/chroma"
5 "github.com/alecthomas/chroma/lexers/internal"
6 )
7
8
9 var Rust = internal.Register(MustNewLazyLexer(
10 &Config{
11 Name: "Rust",
12 Aliases: []string{"rust", "rs"},
13 Filenames: []string{"*.rs", "*.rs.in"},
14 MimeTypes: []string{"text/rust", "text/x-rust"},
15 EnsureNL: true,
16 },
17 rustRules,
18 ))
19
20 func rustRules() Rules {
21 return Rules{
22 "root": {
23 {`#![^[\r\n].*$`, CommentPreproc, nil},
24 Default(Push("base")),
25 },
26 "base": {
27 {`\n`, TextWhitespace, nil},
28 {`\s+`, TextWhitespace, nil},
29 {`//!.*?\n`, LiteralStringDoc, nil},
30 {`///(\n|[^/].*?\n)`, LiteralStringDoc, nil},
31 {`//(.*?)\n`, CommentSingle, nil},
32 {`/\*\*(\n|[^/*])`, LiteralStringDoc, Push("doccomment")},
33 {`/\*!`, LiteralStringDoc, Push("doccomment")},
34 {`/\*`, CommentMultiline, Push("comment")},
35 {`r#*"(?:\\.|[^\\;])*"#*`, LiteralString, nil},
36 {`"(?:\\.|[^\\"])*"`, LiteralString, nil},
37 {`\$([a-zA-Z_]\w*|\(,?|\),?|,?)`, CommentPreproc, nil},
38 {Words(``, `\b`, `as`, `async`, `await`, `box`, `const`, `crate`, `dyn`, `else`, `extern`, `for`, `if`, `impl`, `in`, `loop`, `match`, `move`, `mut`, `pub`, `ref`, `return`, `static`, `super`, `trait`, `unsafe`, `use`, `where`, `while`), Keyword, nil},
39 {Words(``, `\b`, `abstract`, `become`, `do`, `final`, `macro`, `override`, `priv`, `typeof`, `try`, `unsized`, `virtual`, `yield`), KeywordReserved, nil},
40 {`(true|false)\b`, KeywordConstant, nil},
41 {`self\b`, NameBuiltinPseudo, nil},
42 {`mod\b`, Keyword, Push("modname")},
43 {`let\b`, KeywordDeclaration, nil},
44 {`fn\b`, Keyword, Push("funcname")},
45 {`(struct|enum|type|union)\b`, Keyword, Push("typename")},
46 {`(default)(\s+)(type|fn)\b`, ByGroups(Keyword, Text, Keyword), nil},
47 {Words(``, `\b`, `u8`, `u16`, `u32`, `u64`, `u128`, `i8`, `i16`, `i32`, `i64`, `i128`, `usize`, `isize`, `f32`, `f64`, `char`, `str`, `bool`), KeywordType, nil},
48 {`[sS]elf\b`, NameBuiltinPseudo, nil},
49 {Words(``, `\b`, `Copy`, `Send`, `Sized`, `Sync`, `Unpin`, `Drop`, `Fn`, `FnMut`, `FnOnce`, `drop`, `Box`, `ToOwned`, `Clone`, `PartialEq`, `PartialOrd`, `Eq`, `Ord`, `AsRef`, `AsMut`, `Into`, `From`, `Default`, `Iterator`, `Extend`, `IntoIterator`, `DoubleEndedIterator`, `ExactSizeIterator`, `Option`, `Some`, `None`, `Result`, `Ok`, `Err`, `String`, `ToString`, `Vec`), NameBuiltin, nil},
50 {Words(``, `!`, `asm`, `assert`, `assert_eq`, `assert_ne`, `cfg`, `column`, `compile_error`, `concat`, `concat_idents`, `dbg`, `debug_assert`, `debug_assert_eq`, `debug_assert_ne`, `env`, `eprint`, `eprintln`, `file`, `format`, `format_args`, `format_args_nl`, `global_asm`, `include`, `include_bytes`, `include_str`, `is_aarch64_feature_detected`, `is_arm_feature_detected`, `is_mips64_feature_detected`, `is_mips_feature_detected`, `is_powerpc64_feature_detected`, `is_powerpc_feature_detected`, `is_x86_feature_detected`, `line`, `llvm_asm`, `log_syntax`, `macro_rules`, `matches`, `module_path`, `option_env`, `panic`, `print`, `println`, `stringify`, `thread_local`, `todo`, `trace_macros`, `unimplemented`, `unreachable`, `vec`, `write`, `writeln`), NameFunctionMagic, nil},
51 {`::\b`, Text, nil},
52 {`(?::|->)`, Text, Push("typename")},
53 {`(break|continue)(\b\s*)(\'[A-Za-z_]\w*)?`, ByGroups(Keyword, TextWhitespace, NameLabel), nil},
54 {`'(\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0|\\u\{[0-9a-fA-F]{1,6}\}|.)'`, LiteralStringChar, nil},
55 {`b'(\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\0|\\u\{[0-9a-fA-F]{1,6}\}|.)'`, LiteralStringChar, nil},
56 {`0b[01_]+`, LiteralNumberBin, Push("number_lit")},
57 {`0o[0-7_]+`, LiteralNumberOct, Push("number_lit")},
58 {`0[xX][0-9a-fA-F_]+`, LiteralNumberHex, Push("number_lit")},
59 {`[0-9][0-9_]*(\.[0-9_]+[eE][+\-]?[0-9_]+|\.[0-9_]*(?!\.)|[eE][+\-]?[0-9_]+)`, LiteralNumberFloat, Push("number_lit")},
60 {`[0-9][0-9_]*`, LiteralNumberInteger, Push("number_lit")},
61 {`b"`, LiteralString, Push("bytestring")},
62 {`(?s)b?r(#*)".*?"\1`, LiteralString, nil},
63 {`'`, Operator, Push("lifetime")},
64 {`\.\.=?`, Operator, nil},
65 {`[{}()\[\],.;]`, Punctuation, nil},
66 {`[+\-*/%&|<>^!~@=:?]`, Operator, nil},
67 {`(r#)?[a-zA-Z_]\w*`, Name, nil},
68 {`r#[a-zA-Z_]\w*`, Name, nil},
69 {`#!?\[`, CommentPreproc, Push("attribute[")},
70 {`#`, Text, nil},
71 },
72 "comment": {
73 {`[^*/]+`, CommentMultiline, nil},
74 {`/\*`, CommentMultiline, Push()},
75 {`\*/`, CommentMultiline, Pop(1)},
76 {`[*/]`, CommentMultiline, nil},
77 },
78 "doccomment": {
79 {`[^*/]+`, LiteralStringDoc, nil},
80 {`/\*`, LiteralStringDoc, Push()},
81 {`\*/`, LiteralStringDoc, Pop(1)},
82 {`[*/]`, LiteralStringDoc, nil},
83 },
84 "modname": {
85 {`\s+`, Text, nil},
86 {`[a-zA-Z_]\w*`, NameNamespace, Pop(1)},
87 Default(Pop(1)),
88 },
89 "funcname": {
90 {`\s+`, Text, nil},
91 {`[a-zA-Z_]\w*`, NameFunction, Pop(1)},
92 Default(Pop(1)),
93 },
94 "typename": {
95 {`\s+`, Text, nil},
96 {`&`, KeywordPseudo, nil},
97 {`'`, Operator, Push("lifetime")},
98 {Words(``, `\b`, `Copy`, `Send`, `Sized`, `Sync`, `Unpin`, `Drop`, `Fn`, `FnMut`, `FnOnce`, `drop`, `Box`, `ToOwned`, `Clone`, `PartialEq`, `PartialOrd`, `Eq`, `Ord`, `AsRef`, `AsMut`, `Into`, `From`, `Default`, `Iterator`, `Extend`, `IntoIterator`, `DoubleEndedIterator`, `ExactSizeIterator`, `Option`, `Some`, `None`, `Result`, `Ok`, `Err`, `String`, `ToString`, `Vec`), NameBuiltin, nil},
99 {Words(``, `\b`, `u8`, `u16`, `u32`, `u64`, `u128`, `i8`, `i16`, `i32`, `i64`, `i128`, `usize`, `isize`, `f32`, `f64`, `char`, `str`, `bool`), KeywordType, nil},
100 {`[a-zA-Z_]\w*`, NameClass, Pop(1)},
101 Default(Pop(1)),
102 },
103 "lifetime": {
104 {`(static|_)`, NameBuiltin, nil},
105 {`[a-zA-Z_]+\w*`, NameAttribute, nil},
106 Default(Pop(1)),
107 },
108 "number_lit": {
109 {`[ui](8|16|32|64|size)`, Keyword, Pop(1)},
110 {`f(32|64)`, Keyword, Pop(1)},
111 Default(Pop(1)),
112 },
113 "string": {
114 {`"`, LiteralString, Pop(1)},
115 {`\\['"\\nrt]|\\x[0-7][0-9a-fA-F]|\\0|\\u\{[0-9a-fA-F]{1,6}\}`, LiteralStringEscape, nil},
116 {`[^\\"]+`, LiteralString, nil},
117 {`\\`, LiteralString, nil},
118 },
119 "bytestring": {
120 {`\\x[89a-fA-F][0-9a-fA-F]`, LiteralStringEscape, nil},
121 Include("string"),
122 },
123 "attribute_common": {
124 {`"`, LiteralString, Push("string")},
125 {`\[`, CommentPreproc, Push("attribute[")},
126 },
127 "attribute[": {
128 Include("attribute_common"),
129 {`\]`, CommentPreproc, Pop(1)},
130 {`[^"\]\[]+`, CommentPreproc, nil},
131 },
132 }
133 }
134
View as plain text