1 package c
2
3 import (
4 . "github.com/alecthomas/chroma"
5 "github.com/alecthomas/chroma/lexers/internal"
6 )
7
8
9 var C = internal.Register(MustNewLazyLexer(
10 &Config{
11 Name: "C",
12 Aliases: []string{"c"},
13 Filenames: []string{"*.c", "*.h", "*.idc", "*.x[bp]m"},
14 MimeTypes: []string{"text/x-chdr", "text/x-csrc", "image/x-xbitmap", "image/x-xpixmap"},
15 EnsureNL: true,
16 },
17 cRules,
18 ))
19
20 func cRules() Rules {
21 return Rules{
22 "whitespace": {
23 {`^#if\s+0`, CommentPreproc, Push("if0")},
24 {`^#`, CommentPreproc, Push("macro")},
25 {`^(\s*(?:/[*].*?[*]/\s*)?)(#if\s+0)`, ByGroups(UsingSelf("root"), CommentPreproc), Push("if0")},
26 {`^(\s*(?:/[*].*?[*]/\s*)?)(#)`, ByGroups(UsingSelf("root"), CommentPreproc), Push("macro")},
27 {`\n`, Text, nil},
28 {`\s+`, Text, nil},
29 {`\\\n`, Text, nil},
30 {`//(\n|[\w\W]*?[^\\]\n)`, CommentSingle, nil},
31 {`/(\\\n)?[*][\w\W]*?[*](\\\n)?/`, CommentMultiline, nil},
32 {`/(\\\n)?[*][\w\W]*`, CommentMultiline, nil},
33 },
34 "statements": {
35 {`(L?)(")`, ByGroups(LiteralStringAffix, LiteralString), Push("string")},
36 {`(L?)(')(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])(')`, ByGroups(LiteralStringAffix, LiteralStringChar, LiteralStringChar, LiteralStringChar), nil},
37 {`(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*`, LiteralNumberFloat, nil},
38 {`(\d+\.\d*|\.\d+|\d+[fF])[fF]?`, LiteralNumberFloat, nil},
39 {`0x[0-9a-fA-F]+[LlUu]*`, LiteralNumberHex, nil},
40 {`0[0-7]+[LlUu]*`, LiteralNumberOct, nil},
41 {`\d+[LlUu]*`, LiteralNumberInteger, nil},
42 {`\*/`, Error, nil},
43 {`[~!%^&*+=|?:<>/-]`, Operator, nil},
44 {`[()\[\],.]`, Punctuation, nil},
45 {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},
46 {`(bool|int|long|float|short|double|char((8|16|32)_t)?|unsigned|signed|void|u?int(_fast|_least|)(8|16|32|64)_t)\b`, KeywordType, nil},
47 {Words(``, `\b`, `inline`, `_inline`, `__inline`, `naked`, `restrict`, `thread`, `typename`), KeywordReserved, nil},
48 {`(__m(128i|128d|128|64))\b`, KeywordReserved, nil},
49 {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},
50 {`(true|false|NULL)\b`, NameBuiltin, nil},
51 {`([a-zA-Z_]\w*)(\s*)(:)(?!:)`, ByGroups(NameLabel, Text, Punctuation), nil},
52 {`[a-zA-Z_]\w*`, Name, nil},
53 },
54 "root": {
55 Include("whitespace"),
56 {`((?:[\w*\s])+?(?:\s|[*]))([a-zA-Z_]\w*)(\s*\([^;]*?\))([^;{]*)(\{)`, ByGroups(UsingSelf("root"), NameFunction, UsingSelf("root"), UsingSelf("root"), Punctuation), Push("function")},
57 {`((?:[\w*\s])+?(?:\s|[*]))([a-zA-Z_]\w*)(\s*\([^;]*?\))([^;]*)(;)`, ByGroups(UsingSelf("root"), NameFunction, UsingSelf("root"), UsingSelf("root"), Punctuation), nil},
58 Default(Push("statement")),
59 },
60 "statement": {
61 Include("whitespace"),
62 Include("statements"),
63 {`[{}]`, Punctuation, nil},
64 {`;`, Punctuation, Pop(1)},
65 },
66 "function": {
67 Include("whitespace"),
68 Include("statements"),
69 {`;`, Punctuation, nil},
70 {`\{`, Punctuation, Push()},
71 {`\}`, Punctuation, Pop(1)},
72 },
73 "string": {
74 {`"`, LiteralString, Pop(1)},
75 {`\\([\\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},
76 {`[^\\"\n]+`, LiteralString, nil},
77 {`\\\n`, LiteralString, nil},
78 {`\\`, LiteralString, nil},
79 },
80 "macro": {
81 {`(include)(\s*(?:/[*].*?[*]/\s*)?)([^\n]+)`, ByGroups(CommentPreproc, Text, CommentPreprocFile), nil},
82 {`[^/\n]+`, CommentPreproc, nil},
83 {`/[*](.|\n)*?[*]/`, CommentMultiline, nil},
84 {`//.*?\n`, CommentSingle, Pop(1)},
85 {`/`, CommentPreproc, nil},
86 {`(?<=\\)\n`, CommentPreproc, nil},
87 {`\n`, CommentPreproc, Pop(1)},
88 },
89 "if0": {
90 {`^\s*#if.*?(?<!\\)\n`, CommentPreproc, Push()},
91 {`^\s*#el(?:se|if).*\n`, CommentPreproc, Pop(1)},
92 {`^\s*#endif.*?(?<!\\)\n`, CommentPreproc, Pop(1)},
93 {`.*?\n`, Comment, nil},
94 },
95 }
96 }
97
View as plain text