c.go

 1package c
 2
 3import (
 4	. "github.com/alecthomas/chroma" // nolint
 5	"github.com/alecthomas/chroma/lexers/internal"
 6)
 7
 8// C lexer.
 9var C = internal.Register(MustNewLexer(
10	&Config{
11		Name:      "C",
12		Aliases:   []string{"c"},
13		Filenames: []string{"*.c", "*.h", "*.idc"},
14		MimeTypes: []string{"text/x-chdr", "text/x-csrc"},
15	},
16	Rules{
17		"whitespace": {
18			{`^#if\s+0`, CommentPreproc, Push("if0")},
19			{`^#`, CommentPreproc, Push("macro")},
20			{`^(\s*(?:/[*].*?[*]/\s*)?)(#if\s+0)`, ByGroups(UsingSelf("root"), CommentPreproc), Push("if0")},
21			{`^(\s*(?:/[*].*?[*]/\s*)?)(#)`, ByGroups(UsingSelf("root"), CommentPreproc), Push("macro")},
22			{`\n`, Text, nil},
23			{`\s+`, Text, nil},
24			{`\\\n`, Text, nil},
25			{`//(\n|[\w\W]*?[^\\]\n)`, CommentSingle, nil},
26			{`/(\\\n)?[*][\w\W]*?[*](\\\n)?/`, CommentMultiline, nil},
27			{`/(\\\n)?[*][\w\W]*`, CommentMultiline, nil},
28		},
29		"statements": {
30			{`(L?)(")`, ByGroups(LiteralStringAffix, LiteralString), Push("string")},
31			{`(L?)(')(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])(')`, ByGroups(LiteralStringAffix, LiteralStringChar, LiteralStringChar, LiteralStringChar), nil},
32			{`(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*`, LiteralNumberFloat, nil},
33			{`(\d+\.\d*|\.\d+|\d+[fF])[fF]?`, LiteralNumberFloat, nil},
34			{`0x[0-9a-fA-F]+[LlUu]*`, LiteralNumberHex, nil},
35			{`0[0-7]+[LlUu]*`, LiteralNumberOct, nil},
36			{`\d+[LlUu]*`, LiteralNumberInteger, nil},
37			{`\*/`, Error, nil},
38			{`[~!%^&*+=|?:<>/-]`, Operator, nil},
39			{`[()\[\],.]`, Punctuation, nil},
40			{Words(``, `\b`, `asm`, `auto`, `break`, `case`, `const`, `continue`, `default`, `do`, `else`, `enum`, `extern`, `for`, `goto`, `if`, `register`, `restricted`, `return`, `sizeof`, `static`, `struct`, `switch`, `typedef`, `union`, `volatile`, `while`), Keyword, nil},
41			{`(bool|int|long|float|short|double|char|unsigned|signed|void)\b`, KeywordType, nil},
42			{Words(``, `\b`, `inline`, `_inline`, `__inline`, `naked`, `restrict`, `thread`, `typename`), KeywordReserved, nil},
43			{`(__m(128i|128d|128|64))\b`, KeywordReserved, nil},
44			{Words(`__`, `\b`, `asm`, `int8`, `based`, `except`, `int16`, `stdcall`, `cdecl`, `fastcall`, `int32`, `declspec`, `finally`, `int64`, `try`, `leave`, `wchar_t`, `w64`, `unaligned`, `raise`, `noop`, `identifier`, `forceinline`, `assume`), KeywordReserved, nil},
45			{`(true|false|NULL)\b`, NameBuiltin, 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			{`((?:[\w*\s])+?(?:\s|[*]))([a-zA-Z_]\w*)(\s*\([^;]*?\))([^;{]*)(\{)`, ByGroups(UsingSelf("root"), NameFunction, UsingSelf("root"), UsingSelf("root"), Punctuation), Push("function")},
52			{`((?:[\w*\s])+?(?:\s|[*]))([a-zA-Z_]\w*)(\s*\([^;]*?\))([^;]*)(;)`, ByGroups(UsingSelf("root"), NameFunction, UsingSelf("root"), UsingSelf("root"), Punctuation), nil},
53			Default(Push("statement")),
54		},
55		"statement": {
56			Include("whitespace"),
57			Include("statements"),
58			{`[{}]`, Punctuation, nil},
59			{`;`, Punctuation, Pop(1)},
60		},
61		"function": {
62			Include("whitespace"),
63			Include("statements"),
64			{`;`, Punctuation, nil},
65			{`\{`, Punctuation, Push()},
66			{`\}`, Punctuation, Pop(1)},
67		},
68		"string": {
69			{`"`, LiteralString, Pop(1)},
70			{`\\([\\abfnrtv"\']|x[a-fA-F0-9]{2,4}|u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8}|[0-7]{1,3})`, LiteralStringEscape, nil},
71			{`[^\\"\n]+`, LiteralString, nil},
72			{`\\\n`, LiteralString, nil},
73			{`\\`, LiteralString, nil},
74		},
75		"macro": {
76			{`(include)(\s*(?:/[*].*?[*]/\s*)?)([^\n]+)`, ByGroups(CommentPreproc, Text, CommentPreprocFile), nil},
77			{`[^/\n]+`, CommentPreproc, nil},
78			{`/[*](.|\n)*?[*]/`, CommentMultiline, nil},
79			{`//.*?\n`, CommentSingle, Pop(1)},
80			{`/`, CommentPreproc, nil},
81			{`(?<=\\)\n`, CommentPreproc, nil},
82			{`\n`, CommentPreproc, Pop(1)},
83		},
84		"if0": {
85			{`^\s*#if.*?(?<!\\)\n`, CommentPreproc, Push()},
86			{`^\s*#el(?:se|if).*\n`, CommentPreproc, Pop(1)},
87			{`^\s*#endif.*?(?<!\\)\n`, CommentPreproc, Pop(1)},
88			{`.*?\n`, Comment, nil},
89		},
90	},
91))