swift.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*!---------------------------------------------------------------------------------------------
  2. * Copyright (C) David Owens II, owensd.io. All rights reserved.
  3. *--------------------------------------------------------------------------------------------*/
  4. define(["require", "exports"], function (require, exports) {
  5. 'use strict';
  6. Object.defineProperty(exports, "__esModule", { value: true });
  7. exports.conf = {
  8. comments: {
  9. lineComment: '//',
  10. blockComment: ['/*', '*/'],
  11. },
  12. brackets: [
  13. ['{', '}'],
  14. ['[', ']'],
  15. ['(', ')']
  16. ],
  17. autoClosingPairs: [
  18. { open: '{', close: '}' },
  19. { open: '[', close: ']' },
  20. { open: '(', close: ')' },
  21. { open: '"', close: '"' },
  22. { open: '\'', close: '\'' },
  23. { open: '`', close: '`' },
  24. ],
  25. surroundingPairs: [
  26. { open: '{', close: '}' },
  27. { open: '[', close: ']' },
  28. { open: '(', close: ')' },
  29. { open: '"', close: '"' },
  30. { open: '\'', close: '\'' },
  31. { open: '`', close: '`' },
  32. ]
  33. };
  34. exports.language = {
  35. defaultToken: '',
  36. tokenPostfix: '.swift',
  37. // TODO(owensd): Support the full range of unicode valid identifiers.
  38. identifier: /[a-zA-Z_][\w$]*/,
  39. // TODO(owensd): Support the @availability macro properly.
  40. attributes: [
  41. '@autoclosure', '@noescape', '@noreturn', '@NSApplicationMain', '@NSCopying', '@NSManaged',
  42. '@objc', '@UIApplicationMain', '@noreturn', '@availability', '@IBAction', '@IBDesignable', '@IBInspectable', '@IBOutlet'
  43. ],
  44. accessmodifiers: ['public', 'private', 'internal'],
  45. keywords: [
  46. '__COLUMN__', '__FILE__', '__FUNCTION__', '__LINE__', 'as', 'as!', 'as?', 'associativity', 'break', 'case', 'catch',
  47. 'class', 'continue', 'convenience', 'default', 'deinit', 'didSet', 'do', 'dynamic', 'dynamicType',
  48. 'else', 'enum', 'extension', 'fallthrough', 'final', 'for', 'func', 'get', 'guard', 'if', 'import', 'in', 'infix',
  49. 'init', 'inout', 'internal', 'is', 'lazy', 'left', 'let', 'mutating', 'nil', 'none', 'nonmutating', 'operator',
  50. 'optional', 'override', 'postfix', 'precedence', 'prefix', 'private', 'protocol', 'Protocol', 'public',
  51. 'repeat', 'required', 'return', 'right', 'self', 'Self', 'set', 'static', 'struct', 'subscript', 'super', 'switch',
  52. 'throw', 'throws', 'try', 'try!', 'Type', 'typealias', 'unowned', 'var', 'weak', 'where', 'while', 'willSet', 'FALSE', 'TRUE'
  53. ],
  54. symbols: /[=(){}\[\].,:;@#\_&\-<>`?!+*\\\/]/,
  55. // Moved . to operatorstart so it can be a delimiter
  56. operatorstart: /[\/=\-+!*%<>&|^~?\u00A1-\u00A7\u00A9\u00AB\u00AC\u00AE\u00B0-\u00B1\u00B6\u00BB\u00BF\u00D7\u00F7\u2016-\u2017\u2020-\u2027\u2030-\u203E\u2041-\u2053\u2055-\u205E\u2190-\u23FF\u2500-\u2775\u2794-\u2BFF\u2E00-\u2E7F\u3001-\u3003\u3008-\u3030]/,
  57. operatorend: /[\u0300-\u036F\u1DC0-\u1DFF\u20D0-\u20FF\uFE00-\uFE0F\uFE20-\uFE2F\uE0100-\uE01EF]/,
  58. operators: /(@operatorstart)((@operatorstart)|(@operatorend))*/,
  59. // TODO(owensd): These are borrowed from C#; need to validate correctness for Swift.
  60. escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
  61. tokenizer: {
  62. root: [
  63. { include: '@whitespace' },
  64. { include: '@comment' },
  65. { include: '@attribute' },
  66. { include: '@literal' },
  67. { include: '@keyword' },
  68. { include: '@invokedmethod' },
  69. { include: '@symbol' },
  70. ],
  71. whitespace: [
  72. [/\s+/, 'white'],
  73. [/"""/, 'string.quote', '@endDblDocString']
  74. ],
  75. endDblDocString: [
  76. [/[^"]+/, 'string'],
  77. [/\\"/, 'string'],
  78. [/"""/, 'string.quote', '@popall'],
  79. [/"/, 'string']
  80. ],
  81. symbol: [
  82. [/[{}()\[\]]/, '@brackets'],
  83. [/[<>](?!@symbols)/, '@brackets'],
  84. [/[.]/, 'delimiter'],
  85. [/@operators/, 'operator'],
  86. [/@symbols/, 'operator']
  87. ],
  88. comment: [
  89. [/\/\/\/.*$/, 'comment.doc'],
  90. [/\/\*\*/, 'comment.doc', '@commentdocbody'],
  91. [/\/\/.*$/, 'comment'],
  92. [/\/\*/, 'comment', '@commentbody']
  93. ],
  94. commentdocbody: [
  95. [/\/\*/, 'comment', '@commentbody'],
  96. [/\*\//, 'comment.doc', '@pop'],
  97. [/\:[a-zA-Z]+\:/, 'comment.doc.param'],
  98. [/./, 'comment.doc']
  99. ],
  100. commentbody: [
  101. [/\/\*/, 'comment', '@commentbody'],
  102. [/\*\//, 'comment', '@pop'],
  103. [/./, 'comment']
  104. ],
  105. attribute: [
  106. [/\@@identifier/, {
  107. cases: {
  108. '@attributes': 'keyword.control',
  109. '@default': ''
  110. }
  111. }]
  112. ],
  113. literal: [
  114. [/"/, { token: 'string.quote', next: '@stringlit' }],
  115. [/0[b]([01]_?)+/, 'number.binary'],
  116. [/0[o]([0-7]_?)+/, 'number.octal'],
  117. [/0[x]([0-9a-fA-F]_?)+([pP][\-+](\d_?)+)?/, 'number.hex'],
  118. [/(\d_?)*\.(\d_?)+([eE][\-+]?(\d_?)+)?/, 'number.float'],
  119. [/(\d_?)+/, 'number']
  120. ],
  121. stringlit: [
  122. [/\\\(/, { token: 'operator', next: '@interpolatedexpression' }],
  123. [/@escapes/, 'string'],
  124. [/\\./, 'string.escape.invalid'],
  125. [/"/, { token: 'string.quote', next: '@pop' }],
  126. [/./, 'string']
  127. ],
  128. interpolatedexpression: [
  129. [/\(/, { token: 'operator', next: '@interpolatedexpression' }],
  130. [/\)/, { token: 'operator', next: '@pop' }],
  131. { include: '@literal' },
  132. { include: '@keyword' },
  133. { include: '@symbol' }
  134. ],
  135. keyword: [
  136. [/`/, { token: 'operator', next: '@escapedkeyword' }],
  137. [/@identifier/, {
  138. cases: {
  139. '@keywords': 'keyword', '[A-Z][\a-zA-Z0-9$]*': 'type.identifier',
  140. '@default': 'identifier'
  141. }
  142. }]
  143. ],
  144. escapedkeyword: [
  145. [/`/, { token: 'operator', next: '@pop' }],
  146. [/./, 'identifier']
  147. ],
  148. // symbol: [
  149. // [ /@symbols/, 'operator' ],
  150. // [ /@operators/, 'operator' ]
  151. // ],
  152. invokedmethod: [
  153. [/([.])(@identifier)/, {
  154. cases: {
  155. '$2': ['delimeter', 'type.identifier'],
  156. '@default': ''
  157. }
  158. }],
  159. ]
  160. }
  161. };
  162. });