objectivec.go

  1package o
  2
  3import (
  4	. "github.com/alecthomas/chroma" // nolint
  5	"github.com/alecthomas/chroma/lexers/internal"
  6)
  7
  8// Objective-C lexer.
  9var ObjectiveC = internal.Register(MustNewLexer(
 10	&Config{
 11		Name:      "Objective-C",
 12		Aliases:   []string{"objective-c", "objectivec", "obj-c", "objc"},
 13		Filenames: []string{"*.m", "*.h"},
 14		MimeTypes: []string{"text/x-objective-c"},
 15	},
 16	Rules{
 17		"statements": {
 18			{`@"`, LiteralString, Push("string")},
 19			{`@(YES|NO)`, LiteralNumber, nil},
 20			{`@'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'`, LiteralStringChar, nil},
 21			{`@(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?`, LiteralNumberFloat, nil},
 22			{`@(\d+\.\d*|\.\d+|\d+[fF])[fF]?`, LiteralNumberFloat, nil},
 23			{`@0x[0-9a-fA-F]+[Ll]?`, LiteralNumberHex, nil},
 24			{`@0[0-7]+[Ll]?`, LiteralNumberOct, nil},
 25			{`@\d+[Ll]?`, LiteralNumberInteger, nil},
 26			{`@\(`, Literal, Push("literal_number")},
 27			{`@\[`, Literal, Push("literal_array")},
 28			{`@\{`, Literal, Push("literal_dictionary")},
 29			{Words(``, `\b`, `@selector`, `@private`, `@protected`, `@public`, `@encode`, `@synchronized`, `@try`, `@throw`, `@catch`, `@finally`, `@end`, `@property`, `@synthesize`, `__bridge`, `__bridge_transfer`, `__autoreleasing`, `__block`, `__weak`, `__strong`, `weak`, `strong`, `copy`, `retain`, `assign`, `unsafe_unretained`, `atomic`, `nonatomic`, `readonly`, `readwrite`, `setter`, `getter`, `typeof`, `in`, `out`, `inout`, `release`, `class`, `@dynamic`, `@optional`, `@required`, `@autoreleasepool`), Keyword, nil},
 30			{Words(``, `\b`, `id`, `instancetype`, `Class`, `IMP`, `SEL`, `BOOL`, `IBOutlet`, `IBAction`, `unichar`), KeywordType, nil},
 31			{`@(true|false|YES|NO)\n`, NameBuiltin, nil},
 32			{`(YES|NO|nil|self|super)\b`, NameBuiltin, nil},
 33			{`(Boolean|UInt8|SInt8|UInt16|SInt16|UInt32|SInt32)\b`, KeywordType, nil},
 34			{`(TRUE|FALSE)\b`, NameBuiltin, nil},
 35			{`(@interface|@implementation)(\s+)`, ByGroups(Keyword, Text), Push("#pop", "oc_classname")},
 36			{`(@class|@protocol)(\s+)`, ByGroups(Keyword, Text), Push("#pop", "oc_forward_classname")},
 37			{`@`, Punctuation, nil},
 38			{`(L?)(")`, ByGroups(LiteralStringAffix, LiteralString), Push("string")},
 39			{`(L?)(')(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])(')`, ByGroups(LiteralStringAffix, LiteralStringChar, LiteralStringChar, LiteralStringChar), nil},
 40			{`(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[LlUu]*`, LiteralNumberFloat, nil},
 41			{`(\d+\.\d*|\.\d+|\d+[fF])[fF]?`, LiteralNumberFloat, nil},
 42			{`0x[0-9a-fA-F]+[LlUu]*`, LiteralNumberHex, nil},
 43			{`0[0-7]+[LlUu]*`, LiteralNumberOct, nil},
 44			{`\d+[LlUu]*`, LiteralNumberInteger, nil},
 45			{`\*/`, Error, nil},
 46			{`[~!%^&*+=|?:<>/-]`, Operator, nil},
 47			{`[()\[\],.]`, Punctuation, nil},
 48			{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},
 49			{`(bool|int|long|float|short|double|char|unsigned|signed|void)\b`, KeywordType, nil},
 50			{Words(``, `\b`, `inline`, `_inline`, `__inline`, `naked`, `restrict`, `thread`, `typename`), KeywordReserved, nil},
 51			{`(__m(128i|128d|128|64))\b`, KeywordReserved, nil},
 52			{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},
 53			{`(true|false|NULL)\b`, NameBuiltin, nil},
 54			{`([a-zA-Z_]\w*)(\s*)(:)(?!:)`, ByGroups(NameLabel, Text, Punctuation), nil},
 55			{`[a-zA-Z_]\w*`, Name, nil},
 56		},
 57		"oc_classname": {
 58			{`([a-zA-Z$_][\w$]*)(\s*:\s*)([a-zA-Z$_][\w$]*)?(\s*)(\{)`, ByGroups(NameClass, Text, NameClass, Text, Punctuation), Push("#pop", "oc_ivars")},
 59			{`([a-zA-Z$_][\w$]*)(\s*:\s*)([a-zA-Z$_][\w$]*)?`, ByGroups(NameClass, Text, NameClass), Pop(1)},
 60			{`([a-zA-Z$_][\w$]*)(\s*)(\([a-zA-Z$_][\w$]*\))(\s*)(\{)`, ByGroups(NameClass, Text, NameLabel, Text, Punctuation), Push("#pop", "oc_ivars")},
 61			{`([a-zA-Z$_][\w$]*)(\s*)(\([a-zA-Z$_][\w$]*\))`, ByGroups(NameClass, Text, NameLabel), Pop(1)},
 62			{`([a-zA-Z$_][\w$]*)(\s*)(\{)`, ByGroups(NameClass, Text, Punctuation), Push("#pop", "oc_ivars")},
 63			{`([a-zA-Z$_][\w$]*)`, NameClass, Pop(1)},
 64		},
 65		"oc_forward_classname": {
 66			{`([a-zA-Z$_][\w$]*)(\s*,\s*)`, ByGroups(NameClass, Text), Push("oc_forward_classname")},
 67			{`([a-zA-Z$_][\w$]*)(\s*;?)`, ByGroups(NameClass, Text), Pop(1)},
 68		},
 69		"oc_ivars": {
 70			Include("whitespace"),
 71			Include("statements"),
 72			{`;`, Punctuation, nil},
 73			{`\{`, Punctuation, Push()},
 74			{`\}`, Punctuation, Pop(1)},
 75		},
 76		"root": {
 77			{`^([-+])(\s*)(\(.*?\))?(\s*)([a-zA-Z$_][\w$]*:?)`, ByGroups(Punctuation, Text, UsingSelf("root"), Text, NameFunction), Push("method")},
 78			Include("whitespace"),
 79			{`((?:[\w*\s])+?(?:\s|[*]))([a-zA-Z_]\w*)(\s*\([^;]*?\))([^;{]*)(\{)`, ByGroups(UsingSelf("root"), NameFunction, UsingSelf("root"), UsingSelf("root"), Punctuation), Push("function")},
 80			{`((?:[\w*\s])+?(?:\s|[*]))([a-zA-Z_]\w*)(\s*\([^;]*?\))([^;]*)(;)`, ByGroups(UsingSelf("root"), NameFunction, UsingSelf("root"), UsingSelf("root"), Punctuation), nil},
 81			Default(Push("statement")),
 82		},
 83		"method": {
 84			Include("whitespace"),
 85			{`,`, Punctuation, nil},
 86			{`\.\.\.`, Punctuation, nil},
 87			{`(\(.*?\))(\s*)([a-zA-Z$_][\w$]*)`, ByGroups(UsingSelf("root"), Text, NameVariable), nil},
 88			{`[a-zA-Z$_][\w$]*:`, NameFunction, nil},
 89			{`;`, Punctuation, Pop(1)},
 90			{`\{`, Punctuation, Push("function")},
 91			Default(Pop(1)),
 92		},
 93		"literal_number": {
 94			{`\(`, Punctuation, Push("literal_number_inner")},
 95			{`\)`, Literal, Pop(1)},
 96			Include("statement"),
 97		},
 98		"literal_number_inner": {
 99			{`\(`, Punctuation, Push()},
100			{`\)`, Punctuation, Pop(1)},
101			Include("statement"),
102		},
103		"literal_array": {
104			{`\[`, Punctuation, Push("literal_array_inner")},
105			{`\]`, Literal, Pop(1)},
106			Include("statement"),
107		},
108		"literal_array_inner": {
109			{`\[`, Punctuation, Push()},
110			{`\]`, Punctuation, Pop(1)},
111			Include("statement"),
112		},
113		"literal_dictionary": {
114			{`\}`, Literal, Pop(1)},
115			Include("statement"),
116		},
117		"whitespace": {
118			{`^#if\s+0`, CommentPreproc, Push("if0")},
119			{`^#`, CommentPreproc, Push("macro")},
120			{`^(\s*(?:/[*].*?[*]/\s*)?)(#if\s+0)`, ByGroups(UsingSelf("root"), CommentPreproc), Push("if0")},
121			{`^(\s*(?:/[*].*?[*]/\s*)?)(#)`, ByGroups(UsingSelf("root"), CommentPreproc), Push("macro")},
122			{`\n`, Text, nil},
123			{`\s+`, Text, nil},
124			{`\\\n`, Text, nil},
125			{`//(\n|[\w\W]*?[^\\]\n)`, CommentSingle, nil},
126			{`/(\\\n)?[*][\w\W]*?[*](\\\n)?/`, CommentMultiline, nil},
127			{`/(\\\n)?[*][\w\W]*`, CommentMultiline, nil},
128		},
129		"statement": {
130			Include("whitespace"),
131			Include("statements"),
132			{`[{}]`, Punctuation, nil},
133			{`;`, Punctuation, Pop(1)},
134		},
135		"function": {
136			Include("whitespace"),
137			Include("statements"),
138			{`;`, Punctuation, nil},
139			{`\{`, Punctuation, Push()},
140			{`\}`, Punctuation, Pop(1)},
141		},
142		"string": {
143			{`"`, LiteralString, Pop(1)},
144			{`\\([\\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},
145			{`[^\\"\n]+`, LiteralString, nil},
146			{`\\\n`, LiteralString, nil},
147			{`\\`, LiteralString, nil},
148		},
149		"macro": {
150			{`(include)(\s*(?:/[*].*?[*]/\s*)?)([^\n]+)`, ByGroups(CommentPreproc, Text, CommentPreprocFile), nil},
151			{`[^/\n]+`, CommentPreproc, nil},
152			{`/[*](.|\n)*?[*]/`, CommentMultiline, nil},
153			{`//.*?\n`, CommentSingle, Pop(1)},
154			{`/`, CommentPreproc, nil},
155			{`(?<=\\)\n`, CommentPreproc, nil},
156			{`\n`, CommentPreproc, Pop(1)},
157		},
158		"if0": {
159			{`^\s*#if.*?(?<!\\)\n`, CommentPreproc, Push()},
160			{`^\s*#el(?:se|if).*\n`, CommentPreproc, Pop(1)},
161			{`^\s*#endif.*?(?<!\\)\n`, CommentPreproc, Pop(1)},
162			{`.*?\n`, Comment, nil},
163		},
164	},
165))