swift.js 4.9 KB

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