swift.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*!---------------------------------------------------------------------------------------------
  2. * Copyright (C) David Owens II, owensd.io. All rights reserved.
  3. *--------------------------------------------------------------------------------------------*/
  4. export var conf = {
  5. comments: {
  6. lineComment: '//',
  7. blockComment: ['/*', '*/']
  8. },
  9. brackets: [
  10. ['{', '}'],
  11. ['[', ']'],
  12. ['(', ')']
  13. ],
  14. autoClosingPairs: [
  15. { open: '{', close: '}' },
  16. { open: '[', close: ']' },
  17. { open: '(', close: ')' },
  18. { open: '"', close: '"' },
  19. { open: "'", close: "'" },
  20. { open: '`', close: '`' }
  21. ],
  22. surroundingPairs: [
  23. { open: '{', close: '}' },
  24. { open: '[', close: ']' },
  25. { open: '(', close: ')' },
  26. { open: '"', close: '"' },
  27. { open: "'", close: "'" },
  28. { open: '`', close: '`' }
  29. ]
  30. };
  31. export var language = {
  32. defaultToken: '',
  33. tokenPostfix: '.swift',
  34. // TODO(owensd): Support the full range of unicode valid identifiers.
  35. identifier: /[a-zA-Z_][\w$]*/,
  36. // TODO(owensd): Support the @availability macro properly.
  37. attributes: [
  38. '@autoclosure',
  39. '@noescape',
  40. '@noreturn',
  41. '@NSApplicationMain',
  42. '@NSCopying',
  43. '@NSManaged',
  44. '@objc',
  45. '@UIApplicationMain',
  46. '@noreturn',
  47. '@availability',
  48. '@IBAction',
  49. '@IBDesignable',
  50. '@IBInspectable',
  51. '@IBOutlet'
  52. ],
  53. accessmodifiers: ['public', 'private', 'fileprivate', 'internal'],
  54. keywords: [
  55. '__COLUMN__',
  56. '__FILE__',
  57. '__FUNCTION__',
  58. '__LINE__',
  59. 'as',
  60. 'as!',
  61. 'as?',
  62. 'associativity',
  63. 'break',
  64. 'case',
  65. 'catch',
  66. 'class',
  67. 'continue',
  68. 'convenience',
  69. 'default',
  70. 'deinit',
  71. 'didSet',
  72. 'do',
  73. 'dynamic',
  74. 'dynamicType',
  75. 'else',
  76. 'enum',
  77. 'extension',
  78. 'fallthrough',
  79. 'fileprivate',
  80. 'final',
  81. 'for',
  82. 'func',
  83. 'get',
  84. 'guard',
  85. 'if',
  86. 'import',
  87. 'in',
  88. 'infix',
  89. 'init',
  90. 'inout',
  91. 'internal',
  92. 'is',
  93. 'lazy',
  94. 'left',
  95. 'let',
  96. 'mutating',
  97. 'nil',
  98. 'none',
  99. 'nonmutating',
  100. 'operator',
  101. 'optional',
  102. 'override',
  103. 'postfix',
  104. 'precedence',
  105. 'prefix',
  106. 'private',
  107. 'protocol',
  108. 'Protocol',
  109. 'public',
  110. 'repeat',
  111. 'required',
  112. 'return',
  113. 'right',
  114. 'self',
  115. 'Self',
  116. 'set',
  117. 'static',
  118. 'struct',
  119. 'subscript',
  120. 'super',
  121. 'switch',
  122. 'throw',
  123. 'throws',
  124. 'try',
  125. 'try!',
  126. 'Type',
  127. 'typealias',
  128. 'unowned',
  129. 'var',
  130. 'weak',
  131. 'where',
  132. 'while',
  133. 'willSet',
  134. 'FALSE',
  135. 'TRUE'
  136. ],
  137. symbols: /[=(){}\[\].,:;@#\_&\-<>`?!+*\\\/]/,
  138. // Moved . to operatorstart so it can be a delimiter
  139. 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]/,
  140. operatorend: /[\u0300-\u036F\u1DC0-\u1DFF\u20D0-\u20FF\uFE00-\uFE0F\uFE20-\uFE2F\uE0100-\uE01EF]/,
  141. operators: /(@operatorstart)((@operatorstart)|(@operatorend))*/,
  142. // TODO(owensd): These are borrowed from C#; need to validate correctness for Swift.
  143. escapes: /\\(?:[abfnrtv\\"']|x[0-9A-Fa-f]{1,4}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8})/,
  144. tokenizer: {
  145. root: [
  146. { include: '@whitespace' },
  147. { include: '@comment' },
  148. { include: '@attribute' },
  149. { include: '@literal' },
  150. { include: '@keyword' },
  151. { include: '@invokedmethod' },
  152. { include: '@symbol' }
  153. ],
  154. whitespace: [
  155. [/\s+/, 'white'],
  156. [/"""/, 'string.quote', '@endDblDocString']
  157. ],
  158. endDblDocString: [
  159. [/[^"]+/, 'string'],
  160. [/\\"/, 'string'],
  161. [/"""/, 'string.quote', '@popall'],
  162. [/"/, 'string']
  163. ],
  164. symbol: [
  165. [/[{}()\[\]]/, '@brackets'],
  166. [/[<>](?!@symbols)/, '@brackets'],
  167. [/[.]/, 'delimiter'],
  168. [/@operators/, 'operator'],
  169. [/@symbols/, 'operator']
  170. ],
  171. comment: [
  172. [/\/\/\/.*$/, 'comment.doc'],
  173. [/\/\*\*/, 'comment.doc', '@commentdocbody'],
  174. [/\/\/.*$/, 'comment'],
  175. [/\/\*/, 'comment', '@commentbody']
  176. ],
  177. commentdocbody: [
  178. [/\/\*/, 'comment', '@commentbody'],
  179. [/\*\//, 'comment.doc', '@pop'],
  180. [/\:[a-zA-Z]+\:/, 'comment.doc.param'],
  181. [/./, 'comment.doc']
  182. ],
  183. commentbody: [
  184. [/\/\*/, 'comment', '@commentbody'],
  185. [/\*\//, 'comment', '@pop'],
  186. [/./, 'comment']
  187. ],
  188. attribute: [
  189. [
  190. /@@@identifier/,
  191. {
  192. cases: {
  193. '@attributes': 'keyword.control',
  194. '@default': ''
  195. }
  196. }
  197. ]
  198. ],
  199. literal: [
  200. [/"/, { token: 'string.quote', next: '@stringlit' }],
  201. [/0[b]([01]_?)+/, 'number.binary'],
  202. [/0[o]([0-7]_?)+/, 'number.octal'],
  203. [/0[x]([0-9a-fA-F]_?)+([pP][\-+](\d_?)+)?/, 'number.hex'],
  204. [/(\d_?)*\.(\d_?)+([eE][\-+]?(\d_?)+)?/, 'number.float'],
  205. [/(\d_?)+/, 'number']
  206. ],
  207. stringlit: [
  208. [/\\\(/, { token: 'operator', next: '@interpolatedexpression' }],
  209. [/@escapes/, 'string'],
  210. [/\\./, 'string.escape.invalid'],
  211. [/"/, { token: 'string.quote', next: '@pop' }],
  212. [/./, 'string']
  213. ],
  214. interpolatedexpression: [
  215. [/\(/, { token: 'operator', next: '@interpolatedexpression' }],
  216. [/\)/, { token: 'operator', next: '@pop' }],
  217. { include: '@literal' },
  218. { include: '@keyword' },
  219. { include: '@symbol' }
  220. ],
  221. keyword: [
  222. [/`/, { token: 'operator', next: '@escapedkeyword' }],
  223. [
  224. /@identifier/,
  225. {
  226. cases: {
  227. '@keywords': 'keyword',
  228. '[A-Z][a-zA-Z0-9$]*': 'type.identifier',
  229. '@default': 'identifier'
  230. }
  231. }
  232. ]
  233. ],
  234. escapedkeyword: [
  235. [/`/, { token: 'operator', next: '@pop' }],
  236. [/./, 'identifier']
  237. ],
  238. // symbol: [
  239. // [ /@symbols/, 'operator' ],
  240. // [ /@operators/, 'operator' ]
  241. // ],
  242. invokedmethod: [
  243. [
  244. /([.])(@identifier)/,
  245. {
  246. cases: {
  247. $2: ['delimeter', 'type.identifier'],
  248. '@default': ''
  249. }
  250. }
  251. ]
  252. ]
  253. }
  254. };