rust.go

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