prism-swift.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. Prism.languages.swift = {
  2. 'comment': {
  3. // Nested comments are supported up to 2 levels
  4. pattern: /(^|[^\\:])(?:\/\/.*|\/\*(?:[^/*]|\/(?!\*)|\*(?!\/)|\/\*(?:[^*]|\*(?!\/))*\*\/)*\*\/)/,
  5. lookbehind: true,
  6. greedy: true
  7. },
  8. 'string-literal': [
  9. // https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html
  10. {
  11. pattern: RegExp(
  12. /(^|[^"#])/.source
  13. + '(?:'
  14. // single-line string
  15. + /"(?:\\(?:\((?:[^()]|\([^()]*\))*\)|\r\n|[^(])|[^\\\r\n"])*"/.source
  16. + '|'
  17. // multi-line string
  18. + /"""(?:\\(?:\((?:[^()]|\([^()]*\))*\)|[^(])|[^\\"]|"(?!""))*"""/.source
  19. + ')'
  20. + /(?!["#])/.source
  21. ),
  22. lookbehind: true,
  23. greedy: true,
  24. inside: {
  25. 'interpolation': {
  26. pattern: /(\\\()(?:[^()]|\([^()]*\))*(?=\))/,
  27. lookbehind: true,
  28. inside: null // see below
  29. },
  30. 'interpolation-punctuation': {
  31. pattern: /^\)|\\\($/,
  32. alias: 'punctuation'
  33. },
  34. 'punctuation': /\\(?=[\r\n])/,
  35. 'string': /[\s\S]+/
  36. }
  37. },
  38. {
  39. pattern: RegExp(
  40. /(^|[^"#])(#+)/.source
  41. + '(?:'
  42. // single-line string
  43. + /"(?:\\(?:#+\((?:[^()]|\([^()]*\))*\)|\r\n|[^#])|[^\\\r\n])*?"/.source
  44. + '|'
  45. // multi-line string
  46. + /"""(?:\\(?:#+\((?:[^()]|\([^()]*\))*\)|[^#])|[^\\])*?"""/.source
  47. + ')'
  48. + '\\2'
  49. ),
  50. lookbehind: true,
  51. greedy: true,
  52. inside: {
  53. 'interpolation': {
  54. pattern: /(\\#+\()(?:[^()]|\([^()]*\))*(?=\))/,
  55. lookbehind: true,
  56. inside: null // see below
  57. },
  58. 'interpolation-punctuation': {
  59. pattern: /^\)|\\#+\($/,
  60. alias: 'punctuation'
  61. },
  62. 'string': /[\s\S]+/
  63. }
  64. },
  65. ],
  66. 'directive': {
  67. // directives with conditions
  68. pattern: RegExp(
  69. /#/.source
  70. + '(?:'
  71. + (
  72. /(?:elseif|if)\b/.source
  73. + '(?:[ \t]*'
  74. // This regex is a little complex. It's equivalent to this:
  75. // (?:![ \t]*)?(?:\b\w+\b(?:[ \t]*<round>)?|<round>)(?:[ \t]*(?:&&|\|\|))?
  76. // where <round> is a general parentheses expression.
  77. + /(?:![ \t]*)?(?:\b\w+\b(?:[ \t]*\((?:[^()]|\([^()]*\))*\))?|\((?:[^()]|\([^()]*\))*\))(?:[ \t]*(?:&&|\|\|))?/.source
  78. + ')+'
  79. )
  80. + '|'
  81. + /(?:else|endif)\b/.source
  82. + ')'
  83. ),
  84. alias: 'property',
  85. inside: {
  86. 'directive-name': /^#\w+/,
  87. 'boolean': /\b(?:false|true)\b/,
  88. 'number': /\b\d+(?:\.\d+)*\b/,
  89. 'operator': /!|&&|\|\||[<>]=?/,
  90. 'punctuation': /[(),]/
  91. }
  92. },
  93. 'literal': {
  94. pattern: /#(?:colorLiteral|column|dsohandle|file(?:ID|Literal|Path)?|function|imageLiteral|line)\b/,
  95. alias: 'constant'
  96. },
  97. 'other-directive': {
  98. pattern: /#\w+\b/,
  99. alias: 'property'
  100. },
  101. 'attribute': {
  102. pattern: /@\w+/,
  103. alias: 'atrule'
  104. },
  105. 'function-definition': {
  106. pattern: /(\bfunc\s+)\w+/,
  107. lookbehind: true,
  108. alias: 'function'
  109. },
  110. 'label': {
  111. // https://docs.swift.org/swift-book/LanguageGuide/ControlFlow.html#ID141
  112. pattern: /\b(break|continue)\s+\w+|\b[a-zA-Z_]\w*(?=\s*:\s*(?:for|repeat|while)\b)/,
  113. lookbehind: true,
  114. alias: 'important'
  115. },
  116. 'keyword': /\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/,
  117. 'boolean': /\b(?:false|true)\b/,
  118. 'nil': {
  119. pattern: /\bnil\b/,
  120. alias: 'constant'
  121. },
  122. 'short-argument': /\$\d+\b/,
  123. 'omit': {
  124. pattern: /\b_\b/,
  125. alias: 'keyword'
  126. },
  127. 'number': /\b(?:[\d_]+(?:\.[\de_]+)?|0x[a-f0-9_]+(?:\.[a-f0-9p_]+)?|0b[01_]+|0o[0-7_]+)\b/i,
  128. // A class name must start with an upper-case letter and be either 1 letter long or contain a lower-case letter.
  129. 'class-name': /\b[A-Z](?:[A-Z_\d]*[a-z]\w*)?\b/,
  130. 'function': /\b[a-z_]\w*(?=\s*\()/i,
  131. 'constant': /\b(?:[A-Z_]{2,}|k[A-Z][A-Za-z_]+)\b/,
  132. // Operators are generic in Swift. Developers can even create new operators (e.g. +++).
  133. // https://docs.swift.org/swift-book/ReferenceManual/zzSummaryOfTheGrammar.html#ID481
  134. // This regex only supports ASCII operators.
  135. 'operator': /[-+*/%=!<>&|^~?]+|\.[.\-+*/%=!<>&|^~?]+/,
  136. 'punctuation': /[{}[\]();,.:\\]/
  137. };
  138. Prism.languages.swift['string-literal'].forEach(function (rule) {
  139. rule.inside['interpolation'].inside = Prism.languages.swift;
  140. });