[d24f17c] | 1 | 'use strict'
|
---|
| 2 |
|
---|
| 3 | module.exports = swift
|
---|
| 4 | swift.displayName = 'swift'
|
---|
| 5 | swift.aliases = []
|
---|
| 6 | function swift(Prism) {
|
---|
| 7 | Prism.languages.swift = {
|
---|
| 8 | comment: {
|
---|
| 9 | // Nested comments are supported up to 2 levels
|
---|
| 10 | pattern:
|
---|
| 11 | /(^|[^\\:])(?:\/\/.*|\/\*(?:[^/*]|\/(?!\*)|\*(?!\/)|\/\*(?:[^*]|\*(?!\/))*\*\/)*\*\/)/,
|
---|
| 12 | lookbehind: true,
|
---|
| 13 | greedy: true
|
---|
| 14 | },
|
---|
| 15 | 'string-literal': [
|
---|
| 16 | // https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html
|
---|
| 17 | {
|
---|
| 18 | pattern: RegExp(
|
---|
| 19 | /(^|[^"#])/.source +
|
---|
| 20 | '(?:' + // single-line string
|
---|
| 21 | /"(?:\\(?:\((?:[^()]|\([^()]*\))*\)|\r\n|[^(])|[^\\\r\n"])*"/
|
---|
| 22 | .source +
|
---|
| 23 | '|' + // multi-line string
|
---|
| 24 | /"""(?:\\(?:\((?:[^()]|\([^()]*\))*\)|[^(])|[^\\"]|"(?!""))*"""/
|
---|
| 25 | .source +
|
---|
| 26 | ')' +
|
---|
| 27 | /(?!["#])/.source
|
---|
| 28 | ),
|
---|
| 29 | lookbehind: true,
|
---|
| 30 | greedy: true,
|
---|
| 31 | inside: {
|
---|
| 32 | interpolation: {
|
---|
| 33 | pattern: /(\\\()(?:[^()]|\([^()]*\))*(?=\))/,
|
---|
| 34 | lookbehind: true,
|
---|
| 35 | inside: null // see below
|
---|
| 36 | },
|
---|
| 37 | 'interpolation-punctuation': {
|
---|
| 38 | pattern: /^\)|\\\($/,
|
---|
| 39 | alias: 'punctuation'
|
---|
| 40 | },
|
---|
| 41 | punctuation: /\\(?=[\r\n])/,
|
---|
| 42 | string: /[\s\S]+/
|
---|
| 43 | }
|
---|
| 44 | },
|
---|
| 45 | {
|
---|
| 46 | pattern: RegExp(
|
---|
| 47 | /(^|[^"#])(#+)/.source +
|
---|
| 48 | '(?:' + // single-line string
|
---|
| 49 | /"(?:\\(?:#+\((?:[^()]|\([^()]*\))*\)|\r\n|[^#])|[^\\\r\n])*?"/
|
---|
| 50 | .source +
|
---|
| 51 | '|' + // multi-line string
|
---|
| 52 | /"""(?:\\(?:#+\((?:[^()]|\([^()]*\))*\)|[^#])|[^\\])*?"""/.source +
|
---|
| 53 | ')' +
|
---|
| 54 | '\\2'
|
---|
| 55 | ),
|
---|
| 56 | lookbehind: true,
|
---|
| 57 | greedy: true,
|
---|
| 58 | inside: {
|
---|
| 59 | interpolation: {
|
---|
| 60 | pattern: /(\\#+\()(?:[^()]|\([^()]*\))*(?=\))/,
|
---|
| 61 | lookbehind: true,
|
---|
| 62 | inside: null // see below
|
---|
| 63 | },
|
---|
| 64 | 'interpolation-punctuation': {
|
---|
| 65 | pattern: /^\)|\\#+\($/,
|
---|
| 66 | alias: 'punctuation'
|
---|
| 67 | },
|
---|
| 68 | string: /[\s\S]+/
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | ],
|
---|
| 72 | directive: {
|
---|
| 73 | // directives with conditions
|
---|
| 74 | pattern: RegExp(
|
---|
| 75 | /#/.source +
|
---|
| 76 | '(?:' +
|
---|
| 77 | (/(?:elseif|if)\b/.source +
|
---|
| 78 | '(?:[ \t]*' + // This regex is a little complex. It's equivalent to this:
|
---|
| 79 | // (?:![ \t]*)?(?:\b\w+\b(?:[ \t]*<round>)?|<round>)(?:[ \t]*(?:&&|\|\|))?
|
---|
| 80 | // where <round> is a general parentheses expression.
|
---|
| 81 | /(?:![ \t]*)?(?:\b\w+\b(?:[ \t]*\((?:[^()]|\([^()]*\))*\))?|\((?:[^()]|\([^()]*\))*\))(?:[ \t]*(?:&&|\|\|))?/
|
---|
| 82 | .source +
|
---|
| 83 | ')+') +
|
---|
| 84 | '|' +
|
---|
| 85 | /(?:else|endif)\b/.source +
|
---|
| 86 | ')'
|
---|
| 87 | ),
|
---|
| 88 | alias: 'property',
|
---|
| 89 | inside: {
|
---|
| 90 | 'directive-name': /^#\w+/,
|
---|
| 91 | boolean: /\b(?:false|true)\b/,
|
---|
| 92 | number: /\b\d+(?:\.\d+)*\b/,
|
---|
| 93 | operator: /!|&&|\|\||[<>]=?/,
|
---|
| 94 | punctuation: /[(),]/
|
---|
| 95 | }
|
---|
| 96 | },
|
---|
| 97 | literal: {
|
---|
| 98 | pattern:
|
---|
| 99 | /#(?:colorLiteral|column|dsohandle|file(?:ID|Literal|Path)?|function|imageLiteral|line)\b/,
|
---|
| 100 | alias: 'constant'
|
---|
| 101 | },
|
---|
| 102 | 'other-directive': {
|
---|
| 103 | pattern: /#\w+\b/,
|
---|
| 104 | alias: 'property'
|
---|
| 105 | },
|
---|
| 106 | attribute: {
|
---|
| 107 | pattern: /@\w+/,
|
---|
| 108 | alias: 'atrule'
|
---|
| 109 | },
|
---|
| 110 | 'function-definition': {
|
---|
| 111 | pattern: /(\bfunc\s+)\w+/,
|
---|
| 112 | lookbehind: true,
|
---|
| 113 | alias: 'function'
|
---|
| 114 | },
|
---|
| 115 | label: {
|
---|
| 116 | // https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID141
|
---|
| 117 | pattern:
|
---|
| 118 | /\b(break|continue)\s+\w+|\b[a-zA-Z_]\w*(?=\s*:\s*(?:for|repeat|while)\b)/,
|
---|
| 119 | lookbehind: true,
|
---|
| 120 | alias: 'important'
|
---|
| 121 | },
|
---|
| 122 | keyword:
|
---|
| 123 | /\b(?:Any|Protocol|Self|Type|actor|as|assignment|associatedtype|associativity|async|await|break|case|catch|class|continue|convenience|default|defer|deinit|didSet|do|dynamic|else|enum|extension|fallthrough|fileprivate|final|for|func|get|guard|higherThan|if|import|in|indirect|infix|init|inout|internal|is|isolated|lazy|left|let|lowerThan|mutating|none|nonisolated|nonmutating|open|operator|optional|override|postfix|precedencegroup|prefix|private|protocol|public|repeat|required|rethrows|return|right|safe|self|set|some|static|struct|subscript|super|switch|throw|throws|try|typealias|unowned|unsafe|var|weak|where|while|willSet)\b/,
|
---|
| 124 | boolean: /\b(?:false|true)\b/,
|
---|
| 125 | nil: {
|
---|
| 126 | pattern: /\bnil\b/,
|
---|
| 127 | alias: 'constant'
|
---|
| 128 | },
|
---|
| 129 | 'short-argument': /\$\d+\b/,
|
---|
| 130 | omit: {
|
---|
| 131 | pattern: /\b_\b/,
|
---|
| 132 | alias: 'keyword'
|
---|
| 133 | },
|
---|
| 134 | number:
|
---|
| 135 | /\b(?:[\d_]+(?:\.[\de_]+)?|0x[a-f0-9_]+(?:\.[a-f0-9p_]+)?|0b[01_]+|0o[0-7_]+)\b/i,
|
---|
| 136 | // A class name must start with an upper-case letter and be either 1 letter long or contain a lower-case letter.
|
---|
| 137 | 'class-name': /\b[A-Z](?:[A-Z_\d]*[a-z]\w*)?\b/,
|
---|
| 138 | function: /\b[a-z_]\w*(?=\s*\()/i,
|
---|
| 139 | constant: /\b(?:[A-Z_]{2,}|k[A-Z][A-Za-z_]+)\b/,
|
---|
| 140 | // Operators are generic in Swift. Developers can even create new operators (e.g. +++).
|
---|
| 141 | // https://docs.swift.org/swift-book/ReferenceManual/zzSummaryOfTheGrammar.html#ID481
|
---|
| 142 | // This regex only supports ASCII operators.
|
---|
| 143 | operator: /[-+*/%=!<>&|^~?]+|\.[.\-+*/%=!<>&|^~?]+/,
|
---|
| 144 | punctuation: /[{}[\]();,.:\\]/
|
---|
| 145 | }
|
---|
| 146 | Prism.languages.swift['string-literal'].forEach(function (rule) {
|
---|
| 147 | rule.inside['interpolation'].inside = Prism.languages.swift
|
---|
| 148 | })
|
---|
| 149 | }
|
---|