swift.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Language: Swift
  3. Author: Chris Eidhof <[email protected]>
  4. Contributors: Nate Cook <[email protected]>, Alexander Lichter <[email protected]>
  5. Category: system
  6. */
  7. function(hljs) {
  8. var SWIFT_KEYWORDS = {
  9. keyword: '__COLUMN__ __FILE__ __FUNCTION__ __LINE__ as as! as? associativity ' +
  10. 'break case catch class continue convenience default defer deinit didSet do ' +
  11. 'dynamic dynamicType else enum extension fallthrough false final for func ' +
  12. 'get guard if import in indirect infix init inout internal is lazy left let ' +
  13. 'mutating nil none nonmutating operator optional override postfix precedence ' +
  14. 'prefix private protocol Protocol public repeat required rethrows return ' +
  15. 'right self Self set static struct subscript super switch throw throws true ' +
  16. 'try try! try? Type typealias unowned var weak where while willSet',
  17. literal: 'true false nil',
  18. built_in: 'abs advance alignof alignofValue anyGenerator assert assertionFailure ' +
  19. 'bridgeFromObjectiveC bridgeFromObjectiveCUnconditional bridgeToObjectiveC ' +
  20. 'bridgeToObjectiveCUnconditional c contains count countElements countLeadingZeros ' +
  21. 'debugPrint debugPrintln distance dropFirst dropLast dump encodeBitsAsWords ' +
  22. 'enumerate equal fatalError filter find getBridgedObjectiveCType getVaList ' +
  23. 'indices insertionSort isBridgedToObjectiveC isBridgedVerbatimToObjectiveC ' +
  24. 'isUniquelyReferenced isUniquelyReferencedNonObjC join lazy lexicographicalCompare ' +
  25. 'map max maxElement min minElement numericCast overlaps partition posix ' +
  26. 'precondition preconditionFailure print println quickSort readLine reduce reflect ' +
  27. 'reinterpretCast reverse roundUpToAlignment sizeof sizeofValue sort split ' +
  28. 'startsWith stride strideof strideofValue swap toString transcode ' +
  29. 'underestimateCount unsafeAddressOf unsafeBitCast unsafeDowncast unsafeUnwrap ' +
  30. 'unsafeReflect withExtendedLifetime withObjectAtPlusZero withUnsafePointer ' +
  31. 'withUnsafePointerToObject withUnsafeMutablePointer withUnsafeMutablePointers ' +
  32. 'withUnsafePointer withUnsafePointers withVaList zip'
  33. };
  34. var TYPE = {
  35. className: 'type',
  36. begin: '\\b[A-Z][\\w\u00C0-\u02B8\']*',
  37. relevance: 0
  38. };
  39. var BLOCK_COMMENT = hljs.COMMENT(
  40. '/\\*',
  41. '\\*/',
  42. {
  43. contains: ['self']
  44. }
  45. );
  46. var SUBST = {
  47. className: 'subst',
  48. begin: /\\\(/, end: '\\)',
  49. keywords: SWIFT_KEYWORDS,
  50. contains: [] // assigned later
  51. };
  52. var NUMBERS = {
  53. className: 'number',
  54. begin: '\\b([\\d_]+(\\.[\\deE_]+)?|0x[a-fA-F0-9_]+(\\.[a-fA-F0-9p_]+)?|0b[01_]+|0o[0-7_]+)\\b',
  55. relevance: 0
  56. };
  57. var QUOTE_STRING_MODE = hljs.inherit(hljs.QUOTE_STRING_MODE, {
  58. contains: [SUBST, hljs.BACKSLASH_ESCAPE]
  59. });
  60. SUBST.contains = [NUMBERS];
  61. return {
  62. keywords: SWIFT_KEYWORDS,
  63. contains: [
  64. QUOTE_STRING_MODE,
  65. hljs.C_LINE_COMMENT_MODE,
  66. BLOCK_COMMENT,
  67. TYPE,
  68. NUMBERS,
  69. {
  70. className: 'function',
  71. beginKeywords: 'func', end: '{', excludeEnd: true,
  72. contains: [
  73. hljs.inherit(hljs.TITLE_MODE, {
  74. begin: /[A-Za-z$_][0-9A-Za-z$_]*/
  75. }),
  76. {
  77. begin: /</, end: />/
  78. },
  79. {
  80. className: 'params',
  81. begin: /\(/, end: /\)/, endsParent: true,
  82. keywords: SWIFT_KEYWORDS,
  83. contains: [
  84. 'self',
  85. NUMBERS,
  86. QUOTE_STRING_MODE,
  87. hljs.C_BLOCK_COMMENT_MODE,
  88. {begin: ':'} // relevance booster
  89. ],
  90. illegal: /["']/
  91. }
  92. ],
  93. illegal: /\[|%/
  94. },
  95. {
  96. className: 'class',
  97. beginKeywords: 'struct protocol class extension enum',
  98. keywords: SWIFT_KEYWORDS,
  99. end: '\\{',
  100. excludeEnd: true,
  101. contains: [
  102. hljs.inherit(hljs.TITLE_MODE, {begin: /[A-Za-z$_][\u00C0-\u02B80-9A-Za-z$_]*/})
  103. ]
  104. },
  105. {
  106. className: 'meta', // @attributes
  107. begin: '(@warn_unused_result|@exported|@lazy|@noescape|' +
  108. '@NSCopying|@NSManaged|@objc|@convention|@required|' +
  109. '@noreturn|@IBAction|@IBDesignable|@IBInspectable|@IBOutlet|' +
  110. '@infix|@prefix|@postfix|@autoclosure|@testable|@available|' +
  111. '@nonobjc|@NSApplicationMain|@UIApplicationMain)'
  112. },
  113. {
  114. beginKeywords: 'import', end: /$/,
  115. contains: [hljs.C_LINE_COMMENT_MODE, BLOCK_COMMENT]
  116. }
  117. ]
  118. };
  119. }