|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.objective |
|
4 ~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for Objective-C family languages. |
|
7 |
|
8 :copyright: Copyright 2006-2014 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 import re |
|
13 |
|
14 from pygments.lexer import RegexLexer, include, bygroups, using, this, words, \ |
|
15 inherit, default |
|
16 from pygments.token import Text, Keyword, Name, String, Operator, \ |
|
17 Number, Punctuation, Literal, Comment |
|
18 |
|
19 from pygments.lexers.c_cpp import CLexer, CppLexer |
|
20 |
|
21 __all__ = ['ObjectiveCLexer', 'ObjectiveCppLexer', 'LogosLexer', 'SwiftLexer'] |
|
22 |
|
23 |
|
24 def objective(baselexer): |
|
25 """ |
|
26 Generate a subclass of baselexer that accepts the Objective-C syntax |
|
27 extensions. |
|
28 """ |
|
29 |
|
30 # Have to be careful not to accidentally match JavaDoc/Doxygen syntax here, |
|
31 # since that's quite common in ordinary C/C++ files. It's OK to match |
|
32 # JavaDoc/Doxygen keywords that only apply to Objective-C, mind. |
|
33 # |
|
34 # The upshot of this is that we CANNOT match @class or @interface |
|
35 _oc_keywords = re.compile(r'@(?:end|implementation|protocol)') |
|
36 |
|
37 # Matches [ <ws>? identifier <ws> ( identifier <ws>? ] | identifier? : ) |
|
38 # (note the identifier is *optional* when there is a ':'!) |
|
39 _oc_message = re.compile(r'\[\s*[a-zA-Z_]\w*\s+' |
|
40 r'(?:[a-zA-Z_]\w*\s*\]|' |
|
41 r'(?:[a-zA-Z_]\w*)?:)') |
|
42 |
|
43 class GeneratedObjectiveCVariant(baselexer): |
|
44 """ |
|
45 Implements Objective-C syntax on top of an existing C family lexer. |
|
46 """ |
|
47 |
|
48 tokens = { |
|
49 'statements': [ |
|
50 (r'@"', String, 'string'), |
|
51 (r'@(YES|NO)', Number), |
|
52 (r"@'(\\.|\\[0-7]{1,3}|\\x[a-fA-F0-9]{1,2}|[^\\\'\n])'", String.Char), |
|
53 (r'@(\d+\.\d*|\.\d+|\d+)[eE][+-]?\d+[lL]?', Number.Float), |
|
54 (r'@(\d+\.\d*|\.\d+|\d+[fF])[fF]?', Number.Float), |
|
55 (r'@0x[0-9a-fA-F]+[Ll]?', Number.Hex), |
|
56 (r'@0[0-7]+[Ll]?', Number.Oct), |
|
57 (r'@\d+[Ll]?', Number.Integer), |
|
58 (r'@\(', Literal, 'literal_number'), |
|
59 (r'@\[', Literal, 'literal_array'), |
|
60 (r'@\{', Literal, 'literal_dictionary'), |
|
61 (words(( |
|
62 '@selector', '@private', '@protected', '@public', '@encode', |
|
63 '@synchronized', '@try', '@throw', '@catch', '@finally', |
|
64 '@end', '@property', '@synthesize', '__bridge', '__bridge_transfer', |
|
65 '__autoreleasing', '__block', '__weak', '__strong', 'weak', 'strong', |
|
66 'copy', 'retain', 'assign', 'unsafe_unretained', 'atomic', 'nonatomic', |
|
67 'readonly', 'readwrite', 'setter', 'getter', 'typeof', 'in', |
|
68 'out', 'inout', 'release', 'class', '@dynamic', '@optional', |
|
69 '@required', '@autoreleasepool'), suffix=r'\b'), |
|
70 Keyword), |
|
71 (words(('id', 'instancetype', 'Class', 'IMP', 'SEL', 'BOOL', |
|
72 'IBOutlet', 'IBAction', 'unichar'), suffix=r'\b'), |
|
73 Keyword.Type), |
|
74 (r'@(true|false|YES|NO)\n', Name.Builtin), |
|
75 (r'(YES|NO|nil|self|super)\b', Name.Builtin), |
|
76 # Carbon types |
|
77 (r'(Boolean|UInt8|SInt8|UInt16|SInt16|UInt32|SInt32)\b', Keyword.Type), |
|
78 # Carbon built-ins |
|
79 (r'(TRUE|FALSE)\b', Name.Builtin), |
|
80 (r'(@interface|@implementation)(\s+)', bygroups(Keyword, Text), |
|
81 ('#pop', 'oc_classname')), |
|
82 (r'(@class|@protocol)(\s+)', bygroups(Keyword, Text), |
|
83 ('#pop', 'oc_forward_classname')), |
|
84 # @ can also prefix other expressions like @{...} or @(...) |
|
85 (r'@', Punctuation), |
|
86 inherit, |
|
87 ], |
|
88 'oc_classname': [ |
|
89 # interface definition that inherits |
|
90 ('([a-zA-Z$_][\w$]*)(\s*:\s*)([a-zA-Z$_][\w$]*)?(\s*)(\{)', |
|
91 bygroups(Name.Class, Text, Name.Class, Text, Punctuation), |
|
92 ('#pop', 'oc_ivars')), |
|
93 ('([a-zA-Z$_][\w$]*)(\s*:\s*)([a-zA-Z$_][\w$]*)?', |
|
94 bygroups(Name.Class, Text, Name.Class), '#pop'), |
|
95 # interface definition for a category |
|
96 ('([a-zA-Z$_][\w$]*)(\s*)(\([a-zA-Z$_][\w$]*\))(\s*)(\{)', |
|
97 bygroups(Name.Class, Text, Name.Label, Text, Punctuation), |
|
98 ('#pop', 'oc_ivars')), |
|
99 ('([a-zA-Z$_][\w$]*)(\s*)(\([a-zA-Z$_][\w$]*\))', |
|
100 bygroups(Name.Class, Text, Name.Label), '#pop'), |
|
101 # simple interface / implementation |
|
102 ('([a-zA-Z$_][\w$]*)(\s*)(\{)', |
|
103 bygroups(Name.Class, Text, Punctuation), ('#pop', 'oc_ivars')), |
|
104 ('([a-zA-Z$_][\w$]*)', Name.Class, '#pop') |
|
105 ], |
|
106 'oc_forward_classname': [ |
|
107 ('([a-zA-Z$_][\w$]*)(\s*,\s*)', |
|
108 bygroups(Name.Class, Text), 'oc_forward_classname'), |
|
109 ('([a-zA-Z$_][\w$]*)(\s*;?)', |
|
110 bygroups(Name.Class, Text), '#pop') |
|
111 ], |
|
112 'oc_ivars': [ |
|
113 include('whitespace'), |
|
114 include('statements'), |
|
115 (';', Punctuation), |
|
116 (r'\{', Punctuation, '#push'), |
|
117 (r'\}', Punctuation, '#pop'), |
|
118 ], |
|
119 'root': [ |
|
120 # methods |
|
121 (r'^([-+])(\s*)' # method marker |
|
122 r'(\(.*?\))?(\s*)' # return type |
|
123 r'([a-zA-Z$_][\w$]*:?)', # begin of method name |
|
124 bygroups(Punctuation, Text, using(this), |
|
125 Text, Name.Function), |
|
126 'method'), |
|
127 inherit, |
|
128 ], |
|
129 'method': [ |
|
130 include('whitespace'), |
|
131 # TODO unsure if ellipses are allowed elsewhere, see |
|
132 # discussion in Issue 789 |
|
133 (r',', Punctuation), |
|
134 (r'\.\.\.', Punctuation), |
|
135 (r'(\(.*?\))(\s*)([a-zA-Z$_][\w$]*)', |
|
136 bygroups(using(this), Text, Name.Variable)), |
|
137 (r'[a-zA-Z$_][\w$]*:', Name.Function), |
|
138 (';', Punctuation, '#pop'), |
|
139 (r'\{', Punctuation, 'function'), |
|
140 default('#pop'), |
|
141 ], |
|
142 'literal_number': [ |
|
143 (r'\(', Punctuation, 'literal_number_inner'), |
|
144 (r'\)', Literal, '#pop'), |
|
145 include('statement'), |
|
146 ], |
|
147 'literal_number_inner': [ |
|
148 (r'\(', Punctuation, '#push'), |
|
149 (r'\)', Punctuation, '#pop'), |
|
150 include('statement'), |
|
151 ], |
|
152 'literal_array': [ |
|
153 (r'\[', Punctuation, 'literal_array_inner'), |
|
154 (r'\]', Literal, '#pop'), |
|
155 include('statement'), |
|
156 ], |
|
157 'literal_array_inner': [ |
|
158 (r'\[', Punctuation, '#push'), |
|
159 (r'\]', Punctuation, '#pop'), |
|
160 include('statement'), |
|
161 ], |
|
162 'literal_dictionary': [ |
|
163 (r'\}', Literal, '#pop'), |
|
164 include('statement'), |
|
165 ], |
|
166 } |
|
167 |
|
168 def analyse_text(text): |
|
169 if _oc_keywords.search(text): |
|
170 return 1.0 |
|
171 elif '@"' in text: # strings |
|
172 return 0.8 |
|
173 elif re.search('@[0-9]+', text): |
|
174 return 0.7 |
|
175 elif _oc_message.search(text): |
|
176 return 0.8 |
|
177 return 0 |
|
178 |
|
179 def get_tokens_unprocessed(self, text): |
|
180 from pygments.lexers._cocoa_builtins import COCOA_INTERFACES, \ |
|
181 COCOA_PROTOCOLS, COCOA_PRIMITIVES |
|
182 |
|
183 for index, token, value in \ |
|
184 baselexer.get_tokens_unprocessed(self, text): |
|
185 if token is Name or token is Name.Class: |
|
186 if value in COCOA_INTERFACES or value in COCOA_PROTOCOLS \ |
|
187 or value in COCOA_PRIMITIVES: |
|
188 token = Name.Builtin.Pseudo |
|
189 |
|
190 yield index, token, value |
|
191 |
|
192 return GeneratedObjectiveCVariant |
|
193 |
|
194 |
|
195 class ObjectiveCLexer(objective(CLexer)): |
|
196 """ |
|
197 For Objective-C source code with preprocessor directives. |
|
198 """ |
|
199 |
|
200 name = 'Objective-C' |
|
201 aliases = ['objective-c', 'objectivec', 'obj-c', 'objc'] |
|
202 filenames = ['*.m', '*.h'] |
|
203 mimetypes = ['text/x-objective-c'] |
|
204 priority = 0.05 # Lower than C |
|
205 |
|
206 |
|
207 class ObjectiveCppLexer(objective(CppLexer)): |
|
208 """ |
|
209 For Objective-C++ source code with preprocessor directives. |
|
210 """ |
|
211 |
|
212 name = 'Objective-C++' |
|
213 aliases = ['objective-c++', 'objectivec++', 'obj-c++', 'objc++'] |
|
214 filenames = ['*.mm', '*.hh'] |
|
215 mimetypes = ['text/x-objective-c++'] |
|
216 priority = 0.05 # Lower than C++ |
|
217 |
|
218 |
|
219 class LogosLexer(ObjectiveCppLexer): |
|
220 """ |
|
221 For Logos + Objective-C source code with preprocessor directives. |
|
222 |
|
223 .. versionadded:: 1.6 |
|
224 """ |
|
225 |
|
226 name = 'Logos' |
|
227 aliases = ['logos'] |
|
228 filenames = ['*.x', '*.xi', '*.xm', '*.xmi'] |
|
229 mimetypes = ['text/x-logos'] |
|
230 priority = 0.25 |
|
231 |
|
232 tokens = { |
|
233 'statements': [ |
|
234 (r'(%orig|%log)\b', Keyword), |
|
235 (r'(%c)\b(\()(\s*)([a-zA-Z$_][\w$]*)(\s*)(\))', |
|
236 bygroups(Keyword, Punctuation, Text, Name.Class, Text, Punctuation)), |
|
237 (r'(%init)\b(\()', |
|
238 bygroups(Keyword, Punctuation), 'logos_init_directive'), |
|
239 (r'(%init)(?=\s*;)', bygroups(Keyword)), |
|
240 (r'(%hook|%group)(\s+)([a-zA-Z$_][\w$]+)', |
|
241 bygroups(Keyword, Text, Name.Class), '#pop'), |
|
242 (r'(%subclass)(\s+)', bygroups(Keyword, Text), |
|
243 ('#pop', 'logos_classname')), |
|
244 inherit, |
|
245 ], |
|
246 'logos_init_directive': [ |
|
247 ('\s+', Text), |
|
248 (',', Punctuation, ('logos_init_directive', '#pop')), |
|
249 ('([a-zA-Z$_][\w$]*)(\s*)(=)(\s*)([^);]*)', |
|
250 bygroups(Name.Class, Text, Punctuation, Text, Text)), |
|
251 ('([a-zA-Z$_][\w$]*)', Name.Class), |
|
252 ('\)', Punctuation, '#pop'), |
|
253 ], |
|
254 'logos_classname': [ |
|
255 ('([a-zA-Z$_][\w$]*)(\s*:\s*)([a-zA-Z$_][\w$]*)?', |
|
256 bygroups(Name.Class, Text, Name.Class), '#pop'), |
|
257 ('([a-zA-Z$_][\w$]*)', Name.Class, '#pop') |
|
258 ], |
|
259 'root': [ |
|
260 (r'(%subclass)(\s+)', bygroups(Keyword, Text), |
|
261 'logos_classname'), |
|
262 (r'(%hook|%group)(\s+)([a-zA-Z$_][\w$]+)', |
|
263 bygroups(Keyword, Text, Name.Class)), |
|
264 (r'(%config)(\s*\(\s*)(\w+)(\s*=\s*)(.*?)(\s*\)\s*)', |
|
265 bygroups(Keyword, Text, Name.Variable, Text, String, Text)), |
|
266 (r'(%ctor)(\s*)(\{)', bygroups(Keyword, Text, Punctuation), |
|
267 'function'), |
|
268 (r'(%new)(\s*)(\()(\s*.*?\s*)(\))', |
|
269 bygroups(Keyword, Text, Keyword, String, Keyword)), |
|
270 (r'(\s*)(%end)(\s*)', bygroups(Text, Keyword, Text)), |
|
271 inherit, |
|
272 ], |
|
273 } |
|
274 |
|
275 _logos_keywords = re.compile(r'%(?:hook|ctor|init|c\()') |
|
276 |
|
277 def analyse_text(text): |
|
278 if LogosLexer._logos_keywords.search(text): |
|
279 return 1.0 |
|
280 return 0 |
|
281 |
|
282 |
|
283 class SwiftLexer(RegexLexer): |
|
284 """ |
|
285 For `Swift <https://developer.apple.com/swift/>`_ source. |
|
286 |
|
287 .. versionadded:: 2.0 |
|
288 """ |
|
289 name = 'Swift' |
|
290 filenames = ['*.swift'] |
|
291 aliases = ['swift'] |
|
292 mimetypes = ['text/x-swift'] |
|
293 |
|
294 tokens = { |
|
295 'root': [ |
|
296 # Whitespace and Comments |
|
297 (r'\n', Text), |
|
298 (r'\s+', Text), |
|
299 (r'//', Comment.Single, 'comment-single'), |
|
300 (r'/\*', Comment.Multiline, 'comment-multi'), |
|
301 (r'#(if|elseif|else|endif)\b', Comment.Preproc, 'preproc'), |
|
302 |
|
303 # Keywords |
|
304 include('keywords'), |
|
305 |
|
306 # Global Types |
|
307 (words(( |
|
308 'Array', 'AutoreleasingUnsafeMutablePointer', 'BidirectionalReverseView', |
|
309 'Bit', 'Bool', 'CFunctionPointer', 'COpaquePointer', 'CVaListPointer', |
|
310 'Character', 'ClosedInterval', 'CollectionOfOne', 'ContiguousArray', |
|
311 'Dictionary', 'DictionaryGenerator', 'DictionaryIndex', 'Double', |
|
312 'EmptyCollection', 'EmptyGenerator', 'EnumerateGenerator', |
|
313 'EnumerateSequence', 'FilterCollectionView', |
|
314 'FilterCollectionViewIndex', 'FilterGenerator', 'FilterSequenceView', |
|
315 'Float', 'Float80', 'FloatingPointClassification', 'GeneratorOf', |
|
316 'GeneratorOfOne', 'GeneratorSequence', 'HalfOpenInterval', 'HeapBuffer', |
|
317 'HeapBufferStorage', 'ImplicitlyUnwrappedOptional', 'IndexingGenerator', |
|
318 'Int', 'Int16', 'Int32', 'Int64', 'Int8', 'LazyBidirectionalCollection', |
|
319 'LazyForwardCollection', 'LazyRandomAccessCollection', |
|
320 'LazySequence', 'MapCollectionView', 'MapSequenceGenerator', |
|
321 'MapSequenceView', 'MirrorDisposition', 'ObjectIdentifier', 'OnHeap', |
|
322 'Optional', 'PermutationGenerator', 'QuickLookObject', |
|
323 'RandomAccessReverseView', 'Range', 'RangeGenerator', 'RawByte', 'Repeat', |
|
324 'ReverseBidirectionalIndex', 'ReverseRandomAccessIndex', 'SequenceOf', |
|
325 'SinkOf', 'Slice', 'StaticString', 'StrideThrough', 'StrideThroughGenerator', |
|
326 'StrideTo', 'StrideToGenerator', 'String', 'UInt', 'UInt16', 'UInt32', |
|
327 'UInt64', 'UInt8', 'UTF16', 'UTF32', 'UTF8', 'UnicodeDecodingResult', |
|
328 'UnicodeScalar', 'Unmanaged', 'UnsafeBufferPointer', |
|
329 'UnsafeBufferPointerGenerator', 'UnsafeMutableBufferPointer', |
|
330 'UnsafeMutablePointer', 'UnsafePointer', 'Zip2', 'ZipGenerator2', |
|
331 # Protocols |
|
332 'AbsoluteValuable', 'AnyObject', 'ArrayLiteralConvertible', |
|
333 'BidirectionalIndexType', 'BitwiseOperationsType', |
|
334 'BooleanLiteralConvertible', 'BooleanType', 'CVarArgType', |
|
335 'CollectionType', 'Comparable', 'DebugPrintable', |
|
336 'DictionaryLiteralConvertible', 'Equatable', |
|
337 'ExtendedGraphemeClusterLiteralConvertible', |
|
338 'ExtensibleCollectionType', 'FloatLiteralConvertible', |
|
339 'FloatingPointType', 'ForwardIndexType', 'GeneratorType', 'Hashable', |
|
340 'IntegerArithmeticType', 'IntegerLiteralConvertible', 'IntegerType', |
|
341 'IntervalType', 'MirrorType', 'MutableCollectionType', 'MutableSliceable', |
|
342 'NilLiteralConvertible', 'OutputStreamType', 'Printable', |
|
343 'RandomAccessIndexType', 'RangeReplaceableCollectionType', |
|
344 'RawOptionSetType', 'RawRepresentable', 'Reflectable', 'SequenceType', |
|
345 'SignedIntegerType', 'SignedNumberType', 'SinkType', 'Sliceable', |
|
346 'Streamable', 'Strideable', 'StringInterpolationConvertible', |
|
347 'StringLiteralConvertible', 'UnicodeCodecType', |
|
348 'UnicodeScalarLiteralConvertible', 'UnsignedIntegerType', |
|
349 '_ArrayBufferType', '_BidirectionalIndexType', '_CocoaStringType', |
|
350 '_CollectionType', '_Comparable', '_ExtensibleCollectionType', |
|
351 '_ForwardIndexType', '_Incrementable', '_IntegerArithmeticType', |
|
352 '_IntegerType', '_ObjectiveCBridgeable', '_RandomAccessIndexType', |
|
353 '_RawOptionSetType', '_SequenceType', '_Sequence_Type', |
|
354 '_SignedIntegerType', '_SignedNumberType', '_Sliceable', '_Strideable', |
|
355 '_SwiftNSArrayRequiredOverridesType', '_SwiftNSArrayType', |
|
356 '_SwiftNSCopyingType', '_SwiftNSDictionaryRequiredOverridesType', |
|
357 '_SwiftNSDictionaryType', '_SwiftNSEnumeratorType', |
|
358 '_SwiftNSFastEnumerationType', '_SwiftNSStringRequiredOverridesType', |
|
359 '_SwiftNSStringType', '_UnsignedIntegerType', |
|
360 # Variables |
|
361 'C_ARGC', 'C_ARGV', 'Process', |
|
362 # Typealiases |
|
363 'Any', 'AnyClass', 'BooleanLiteralType', 'CBool', 'CChar', 'CChar16', |
|
364 'CChar32', 'CDouble', 'CFloat', 'CInt', 'CLong', 'CLongLong', 'CShort', |
|
365 'CSignedChar', 'CUnsignedInt', 'CUnsignedLong', 'CUnsignedShort', |
|
366 'CWideChar', 'ExtendedGraphemeClusterType', 'Float32', 'Float64', |
|
367 'FloatLiteralType', 'IntMax', 'IntegerLiteralType', 'StringLiteralType', |
|
368 'UIntMax', 'UWord', 'UnicodeScalarType', 'Void', 'Word', |
|
369 # Foundation/Cocoa |
|
370 'NSErrorPointer', 'NSObjectProtocol', 'Selector'), suffix=r'\b'), |
|
371 Name.Builtin), |
|
372 # Functions |
|
373 (words(( |
|
374 'abs', 'advance', 'alignof', 'alignofValue', 'assert', 'assertionFailure', |
|
375 'contains', 'count', 'countElements', 'debugPrint', 'debugPrintln', |
|
376 'distance', 'dropFirst', 'dropLast', 'dump', 'enumerate', 'equal', |
|
377 'extend', 'fatalError', 'filter', 'find', 'first', 'getVaList', 'indices', |
|
378 'insert', 'isEmpty', 'join', 'last', 'lazy', 'lexicographicalCompare', |
|
379 'map', 'max', 'maxElement', 'min', 'minElement', 'numericCast', 'overlaps', |
|
380 'partition', 'precondition', 'preconditionFailure', 'prefix', 'print', |
|
381 'println', 'reduce', 'reflect', 'removeAll', 'removeAtIndex', 'removeLast', |
|
382 'removeRange', 'reverse', 'sizeof', 'sizeofValue', 'sort', 'sorted', |
|
383 'splice', 'split', 'startsWith', 'stride', 'strideof', 'strideofValue', |
|
384 'suffix', 'swap', 'toDebugString', 'toString', 'transcode', |
|
385 'underestimateCount', 'unsafeAddressOf', 'unsafeBitCast', 'unsafeDowncast', |
|
386 'withExtendedLifetime', 'withUnsafeMutablePointer', |
|
387 'withUnsafeMutablePointers', 'withUnsafePointer', 'withUnsafePointers', |
|
388 'withVaList'), suffix=r'\b'), |
|
389 Name.Builtin.Pseudo), |
|
390 |
|
391 # Implicit Block Variables |
|
392 (r'\$\d+', Name.Variable), |
|
393 |
|
394 # Binary Literal |
|
395 (r'0b[01_]+', Number.Bin), |
|
396 # Octal Literal |
|
397 (r'0o[0-7_]+', Number.Oct), |
|
398 # Hexadecimal Literal |
|
399 (r'0x[0-9a-fA-F_]+', Number.Hex), |
|
400 # Decimal Literal |
|
401 (r'[0-9][0-9_]*(\.[0-9_]+[eE][+\-]?[0-9_]+|' |
|
402 r'\.[0-9_]*|[eE][+\-]?[0-9_]+)', Number.Float), |
|
403 (r'[0-9][0-9_]*', Number.Integer), |
|
404 # String Literal |
|
405 (r'"', String, 'string'), |
|
406 |
|
407 # Operators and Punctuation |
|
408 (r'[(){}\[\].,:;=@#`?]|->|[<&?](?=\w)|(?<=\w)[>!?]', Punctuation), |
|
409 (r'[/=\-+!*%<>&|^?~]+', Operator), |
|
410 |
|
411 # Identifier |
|
412 (r'[a-zA-Z_]\w*', Name) |
|
413 ], |
|
414 'keywords': [ |
|
415 (words(( |
|
416 'break', 'case', 'continue', 'default', 'do', 'else', |
|
417 'fallthrough', 'for', 'if', 'in', 'return', 'switch', 'where', |
|
418 'while'), suffix=r'\b'), |
|
419 Keyword), |
|
420 (r'@availability\([^)]+\)', Keyword.Reserved), |
|
421 (words(( |
|
422 'associativity', 'convenience', 'dynamic', 'didSet', 'final', |
|
423 'get', 'infix', 'inout', 'lazy', 'left', 'mutating', 'none', |
|
424 'nonmutating', 'optional', 'override', 'postfix', 'precedence', |
|
425 'prefix', 'Protocol', 'required', 'right', 'set', 'Type', |
|
426 'unowned', 'weak', 'willSet', '@availability', '@autoclosure', |
|
427 '@noreturn', '@NSApplicationMain', '@NSCopying', '@NSManaged', |
|
428 '@objc', '@UIApplicationMain', '@IBAction', '@IBDesignable', |
|
429 '@IBInspectable', '@IBOutlet'), suffix=r'\b'), |
|
430 Keyword.Reserved), |
|
431 (r'(as|dynamicType|false|is|nil|self|Self|super|true|__COLUMN__' |
|
432 r'|__FILE__|__FUNCTION__|__LINE__|_)\b', Keyword.Constant), |
|
433 (r'import\b', Keyword.Declaration, 'module'), |
|
434 (r'(class|enum|extension|struct|protocol)(\s+)([a-zA-Z_]\w*)', |
|
435 bygroups(Keyword.Declaration, Text, Name.Class)), |
|
436 (r'(func)(\s+)([a-zA-Z_]\w*)', |
|
437 bygroups(Keyword.Declaration, Text, Name.Function)), |
|
438 (r'(var|let)(\s+)([a-zA-Z_]\w*)', bygroups(Keyword.Declaration, |
|
439 Text, Name.Variable)), |
|
440 (words(( |
|
441 'class', 'deinit', 'enum', 'extension', 'func', 'import', 'init', |
|
442 'internal', 'let', 'operator', 'private', 'protocol', 'public', |
|
443 'static', 'struct', 'subscript', 'typealias', 'var'), suffix=r'\b'), |
|
444 Keyword.Declaration) |
|
445 ], |
|
446 'comment': [ |
|
447 (r':param: [a-zA-Z_]\w*|:returns?:|(FIXME|MARK|TODO):', |
|
448 Comment.Special) |
|
449 ], |
|
450 |
|
451 # Nested |
|
452 'comment-single': [ |
|
453 (r'\n', Text, '#pop'), |
|
454 include('comment'), |
|
455 (r'[^\n]', Comment.Single) |
|
456 ], |
|
457 'comment-multi': [ |
|
458 include('comment'), |
|
459 (r'[^*/]', Comment.Multiline), |
|
460 (r'/\*', Comment.Multiline, '#push'), |
|
461 (r'\*/', Comment.Multiline, '#pop'), |
|
462 (r'[*/]', Comment.Multiline) |
|
463 ], |
|
464 'module': [ |
|
465 (r'\n', Text, '#pop'), |
|
466 (r'[a-zA-Z_]\w*', Name.Class), |
|
467 include('root') |
|
468 ], |
|
469 'preproc': [ |
|
470 (r'\n', Text, '#pop'), |
|
471 include('keywords'), |
|
472 (r'[A-Za-z]\w*', Comment.Preproc), |
|
473 include('root') |
|
474 ], |
|
475 'string': [ |
|
476 (r'\\\(', String.Interpol, 'string-intp'), |
|
477 (r'"', String, '#pop'), |
|
478 (r"""\\['"\\nrt]|\\x[0-9a-fA-F]{2}|\\[0-7]{1,3}""" |
|
479 r"""|\\u[0-9a-fA-F]{4}|\\U[0-9a-fA-F]{8}""", String.Escape), |
|
480 (r'[^\\"]+', String), |
|
481 (r'\\', String) |
|
482 ], |
|
483 'string-intp': [ |
|
484 (r'\(', String.Interpol, '#push'), |
|
485 (r'\)', String.Interpol, '#pop'), |
|
486 include('root') |
|
487 ] |
|
488 } |
|
489 |
|
490 def get_tokens_unprocessed(self, text): |
|
491 from pygments.lexers._cocoa_builtins import COCOA_INTERFACES, \ |
|
492 COCOA_PROTOCOLS, COCOA_PRIMITIVES |
|
493 |
|
494 for index, token, value in \ |
|
495 RegexLexer.get_tokens_unprocessed(self, text): |
|
496 if token is Name or token is Name.Class: |
|
497 if value in COCOA_INTERFACES or value in COCOA_PROTOCOLS \ |
|
498 or value in COCOA_PRIMITIVES: |
|
499 token = Name.Builtin.Pseudo |
|
500 |
|
501 yield index, token, value |