swift.js 5.3 KB

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