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