swift.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. // Swift mode created by Michael Kaminsky https://github.com/mkaminsky11
  4. (function(mod) {
  5. if (typeof exports == "object" && typeof module == "object")
  6. mod(require("../../lib/codemirror"))
  7. else if (typeof define == "function" && define.amd)
  8. define(["../../lib/codemirror"], mod)
  9. else
  10. mod(CodeMirror)
  11. })(function(CodeMirror) {
  12. "use strict"
  13. function wordSet(words) {
  14. var set = {}
  15. for (var i = 0; i < words.length; i++) set[words[i]] = true
  16. return set
  17. }
  18. var keywords = wordSet(["var","let","class","deinit","enum","extension","func","import","init","protocol",
  19. "static","struct","subscript","typealias","as","dynamicType","is","new","super",
  20. "self","Self","Type","__COLUMN__","__FILE__","__FUNCTION__","__LINE__","break","case",
  21. "continue","default","do","else","fallthrough","if","in","for","return","switch",
  22. "where","while","associativity","didSet","get","infix","inout","left","mutating",
  23. "none","nonmutating","operator","override","postfix","precedence","prefix","right",
  24. "set","unowned","weak","willSet"])
  25. var definingKeywords = wordSet(["var","let","class","enum","extension","func","import","protocol","struct",
  26. "typealias","dynamicType","for"])
  27. var atoms = wordSet(["Infinity","NaN","undefined","null","true","false","on","off","yes","no","nil","null",
  28. "this","super"])
  29. var types = wordSet(["String","bool","int","string","double","Double","Int","Float","float","public",
  30. "private","extension"])
  31. var operators = "+-/*%=|&<>#"
  32. var punc = ";,.(){}[]"
  33. var number = /^-?(?:(?:[\d_]+\.[_\d]*|\.[_\d]+|0o[0-7_\.]+|0b[01_\.]+)(?:e-?[\d_]+)?|0x[\d_a-f\.]+(?:p-?[\d_]+)?)/i
  34. var identifier = /^[_A-Za-z$][_A-Za-z$0-9]*/
  35. var property = /^[@\.][_A-Za-z$][_A-Za-z$0-9]*/
  36. var regexp = /^\/(?!\s)(?:\/\/)?(?:\\.|[^\/])+\//
  37. function tokenBase(stream, state, prev) {
  38. if (stream.sol()) state.indented = stream.indentation()
  39. if (stream.eatSpace()) return null
  40. var ch = stream.peek()
  41. if (ch == "/") {
  42. if (stream.match("//")) {
  43. stream.skipToEnd()
  44. return "comment"
  45. }
  46. if (stream.match("/*")) {
  47. state.tokenize.push(tokenComment)
  48. return tokenComment(stream, state)
  49. }
  50. if (stream.match(regexp)) return "string-2"
  51. }
  52. if (operators.indexOf(ch) > -1) {
  53. stream.next()
  54. return "operator"
  55. }
  56. if (punc.indexOf(ch) > -1) {
  57. stream.next()
  58. stream.match("..")
  59. return "punctuation"
  60. }
  61. if (ch == '"' || ch == "'") {
  62. stream.next()
  63. var tokenize = tokenString(ch)
  64. state.tokenize.push(tokenize)
  65. return tokenize(stream, state)
  66. }
  67. if (stream.match(number)) return "number"
  68. if (stream.match(property)) return "property"
  69. if (stream.match(identifier)) {
  70. var ident = stream.current()
  71. if (keywords.hasOwnProperty(ident)) {
  72. if (definingKeywords.hasOwnProperty(ident))
  73. state.prev = "define"
  74. return "keyword"
  75. }
  76. if (types.hasOwnProperty(ident)) return "variable-2"
  77. if (atoms.hasOwnProperty(ident)) return "atom"
  78. if (prev == "define") return "def"
  79. return "variable"
  80. }
  81. stream.next()
  82. return null
  83. }
  84. function tokenUntilClosingParen() {
  85. var depth = 0
  86. return function(stream, state, prev) {
  87. var inner = tokenBase(stream, state, prev)
  88. if (inner == "punctuation") {
  89. if (stream.current() == "(") ++depth
  90. else if (stream.current() == ")") {
  91. if (depth == 0) {
  92. stream.backUp(1)
  93. state.tokenize.pop()
  94. return state.tokenize[state.tokenize.length - 1](stream, state)
  95. }
  96. else --depth
  97. }
  98. }
  99. return inner
  100. }
  101. }
  102. function tokenString(quote) {
  103. return function(stream, state) {
  104. var ch, escaped = false
  105. while (ch = stream.next()) {
  106. if (escaped) {
  107. if (ch == "(") {
  108. state.tokenize.push(tokenUntilClosingParen())
  109. return "string"
  110. }
  111. escaped = false
  112. } else if (ch == quote) {
  113. break
  114. } else {
  115. escaped = ch == "\\"
  116. }
  117. }
  118. state.tokenize.pop()
  119. return "string"
  120. }
  121. }
  122. function tokenComment(stream, state) {
  123. stream.match(/^(?:[^*]|\*(?!\/))*/)
  124. if (stream.match("*/")) state.tokenize.pop()
  125. return "comment"
  126. }
  127. function Context(prev, align, indented) {
  128. this.prev = prev
  129. this.align = align
  130. this.indented = indented
  131. }
  132. function pushContext(state, stream) {
  133. var align = stream.match(/^\s*($|\/[\/\*])/, false) ? null : stream.column() + 1
  134. state.context = new Context(state.context, align, state.indented)
  135. }
  136. function popContext(state) {
  137. if (state.context) {
  138. state.indented = state.context.indented
  139. state.context = state.context.prev
  140. }
  141. }
  142. CodeMirror.defineMode("swift", function(config) {
  143. return {
  144. startState: function() {
  145. return {
  146. prev: null,
  147. context: null,
  148. indented: 0,
  149. tokenize: []
  150. }
  151. },
  152. token: function(stream, state) {
  153. var prev = state.prev
  154. state.prev = null
  155. var tokenize = state.tokenize[state.tokenize.length - 1] || tokenBase
  156. var style = tokenize(stream, state, prev)
  157. if (!style || style == "comment") state.prev = prev
  158. else if (!state.prev) state.prev = style
  159. if (style == "punctuation") {
  160. var bracket = /[\(\[\{]|([\]\)\}])/.exec(stream.current())
  161. if (bracket) (bracket[1] ? popContext : pushContext)(state, stream)
  162. }
  163. return style
  164. },
  165. indent: function(state, textAfter) {
  166. var cx = state.context
  167. if (!cx) return 0
  168. var closing = /^[\]\}\)]/.test(textAfter)
  169. if (cx.align != null) return cx.align - (closing ? 1 : 0)
  170. return cx.indented + (closing ? 0 : config.indentUnit)
  171. },
  172. electricInput: /^\s*[\)\}\]]$/,
  173. lineComment: "//",
  174. blockCommentStart: "/*",
  175. blockCommentEnd: "*/"
  176. }
  177. })
  178. CodeMirror.defineMIME("text/x-swift","swift")
  179. });