...

Source file src/github.com/alecthomas/chroma/lexers/o/objectivec.go

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

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

View as plain text