...

Source file src/github.com/alecthomas/chroma/lexers/m/metal.go

Documentation: github.com/alecthomas/chroma/lexers/m

     1  package m
     2  
     3  import (
     4  	. "github.com/alecthomas/chroma" // nolint
     5  	"github.com/alecthomas/chroma/lexers/internal"
     6  )
     7  
     8  // Metal lexer.
     9  var Metal = internal.Register(MustNewLazyLexer(
    10  	&Config{
    11  		Name:      "Metal",
    12  		Aliases:   []string{"metal"},
    13  		Filenames: []string{"*.metal"},
    14  		MimeTypes: []string{"text/x-metal"},
    15  		EnsureNL:  true,
    16  	},
    17  	metalRules,
    18  ))
    19  
    20  func metalRules() Rules {
    21  	return Rules{
    22  		"statements": {
    23  			{Words(``, `\b`, `namespace`, `operator`, `template`, `this`, `using`, `constexpr`), Keyword, nil},
    24  			{`(enum)\b(\s+)(class)\b(\s*)`, ByGroups(Keyword, Text, Keyword, Text), Push("classname")},
    25  			{`(class|struct|enum|union)\b(\s*)`, ByGroups(Keyword, Text), Push("classname")},
    26  			{`\[\[.+\]\]`, NameAttribute, nil},
    27  			{`(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*`, LiteralNumberFloat, nil},
    28  			{`(\d+\.\d*|\.\d+|\d+[fF])[fF]?`, LiteralNumberFloat, nil},
    29  			{`0[xX]([0-9A-Fa-f]('?[0-9A-Fa-f]+)*)[LlUu]*`, LiteralNumberHex, nil},
    30  			{`0('?[0-7]+)+[LlUu]*`, LiteralNumberOct, nil},
    31  			{`0[Bb][01]('?[01]+)*[LlUu]*`, LiteralNumberBin, nil},
    32  			{`[0-9]('?[0-9]+)*[LlUu]*`, LiteralNumberInteger, nil},
    33  			{`\*/`, Error, nil},
    34  			{`[~!%^&*+=|?:<>/-]`, Operator, nil},
    35  			{`[()\[\],.]`, Punctuation, nil},
    36  			{Words(``, `\b`, `break`, `case`, `const`, `continue`, `do`, `else`, `enum`, `extern`, `for`, `if`, `return`, `sizeof`, `static`, `struct`, `switch`, `typedef`, `union`, `while`), Keyword, nil},
    37  			{`(bool|float|half|long|ptrdiff_t|size_t|unsigned|u?char|u?int((8|16|32|64)_t)?|u?short)\b`, KeywordType, nil},
    38  			{`(bool|float|half|u?(char|int|long|short))(2|3|4)\b`, KeywordType, nil},
    39  			{`packed_(float|half|long|u?(char|int|short))(2|3|4)\b`, KeywordType, nil},
    40  			{`(float|half)(2|3|4)x(2|3|4)\b`, KeywordType, nil},
    41  			{`atomic_u?int\b`, KeywordType, nil},
    42  			{`(rg?(8|16)(u|s)norm|rgba(8|16)(u|s)norm|srgba8unorm|rgb10a2|rg11b10f|rgb9e5)\b`, KeywordType, nil},
    43  			{`(array|depth(2d|cube)(_array)?|depth2d_ms(_array)?|sampler|texture_buffer|texture(1|2)d(_array)?|texture2d_ms(_array)?|texture3d|texturecube(_array)?|uniform|visible_function_table)\b`, KeywordType, nil},
    44  			{`(true|false|NULL)\b`, NameBuiltin, nil},
    45  			{Words(``, `\b`, `device`, `constant`, `ray_data`, `thread`, `threadgroup`, `threadgroup_imageblock`), Keyword, nil},
    46  			{`([a-zA-Z_]\w*)(\s*)(:)(?!:)`, ByGroups(NameLabel, Text, Punctuation), nil},
    47  			{`[a-zA-Z_]\w*`, Name, nil},
    48  		},
    49  		"root": {
    50  			Include("whitespace"),
    51  			{`(fragment|kernel|vertex)?((?:[\w*\s])+?(?:\s|[*]))([a-zA-Z_]\w*)(\s*\([^;]*?\))([^;{]*)(\{)`, ByGroups(Keyword, UsingSelf("root"), NameFunction, UsingSelf("root"), UsingSelf("root"), Punctuation), Push("function")},
    52  			{`(fragment|kernel|vertex)?((?:[\w*\s])+?(?:\s|[*]))([a-zA-Z_]\w*)(\s*\([^;]*?\))([^;]*)(;)`, ByGroups(Keyword, UsingSelf("root"), NameFunction, UsingSelf("root"), UsingSelf("root"), Punctuation), nil},
    53  			Default(Push("statement")),
    54  		},
    55  		"classname": {
    56  			{`(\[\[.+\]\])(\s*)`, ByGroups(NameAttribute, Text), nil},
    57  			{`[a-zA-Z_]\w*`, NameClass, Pop(1)},
    58  			{`\s*(?=[>{])`, Text, Pop(1)},
    59  		},
    60  		"whitespace": {
    61  			{`^#if\s+0`, CommentPreproc, Push("if0")},
    62  			{`^#`, CommentPreproc, Push("macro")},
    63  			{`^(\s*(?:/[*].*?[*]/\s*)?)(#if\s+0)`, ByGroups(UsingSelf("root"), CommentPreproc), Push("if0")},
    64  			{`^(\s*(?:/[*].*?[*]/\s*)?)(#)`, ByGroups(UsingSelf("root"), CommentPreproc), Push("macro")},
    65  			{`\n`, Text, nil},
    66  			{`\s+`, Text, nil},
    67  			{`\\\n`, Text, nil},
    68  			{`//(\n|[\w\W]*?[^\\]\n)`, CommentSingle, nil},
    69  			{`/(\\\n)?[*][\w\W]*?[*](\\\n)?/`, CommentMultiline, nil},
    70  			{`/(\\\n)?[*][\w\W]*`, CommentMultiline, nil},
    71  		},
    72  		"statement": {
    73  			Include("whitespace"),
    74  			Include("statements"),
    75  			{`[{]`, Punctuation, Push("root")},
    76  			{`[;}]`, Punctuation, Pop(1)},
    77  		},
    78  		"function": {
    79  			Include("whitespace"),
    80  			Include("statements"),
    81  			{`;`, Punctuation, nil},
    82  			{`\{`, Punctuation, Push()},
    83  			{`\}`, Punctuation, Pop(1)},
    84  		},
    85  		"macro": {
    86  			{`(include)(\s*(?:/[*].*?[*]/\s*)?)([^\n]+)`, ByGroups(CommentPreproc, Text, CommentPreprocFile), nil},
    87  			{`[^/\n]+`, CommentPreproc, nil},
    88  			{`/[*](.|\n)*?[*]/`, CommentMultiline, nil},
    89  			{`//.*?\n`, CommentSingle, Pop(1)},
    90  			{`/`, CommentPreproc, nil},
    91  			{`(?<=\\)\n`, CommentPreproc, nil},
    92  			{`\n`, CommentPreproc, Pop(1)},
    93  		},
    94  		"if0": {
    95  			{`^\s*#if.*?(?<!\\)\n`, CommentPreproc, Push()},
    96  			{`^\s*#el(?:se|if).*\n`, CommentPreproc, Pop(1)},
    97  			{`^\s*#endif.*?(?<!\\)\n`, CommentPreproc, Pop(1)},
    98  			{`.*?\n`, Comment, nil},
    99  		},
   100  	}
   101  }
   102  

View as plain text