7
if (typeof exports == "object" && typeof module == "object")
8
mod(require("../../lib/codemirror"))
9
else if (typeof define == "function" && define.amd)
10
define(["../../lib/codemirror"], mod)
13
})(function(CodeMirror) {
16
function wordSet(words) {
18
for (var i = 0; i < words.length; i++) set[words[i]] = true
22
var keywords = wordSet(["_","var","let","class","enum","extension","import","protocol","struct","func","typealias","associatedtype",
23
"open","public","internal","fileprivate","private","deinit","init","new","override","self","subscript","super",
24
"convenience","dynamic","final","indirect","lazy","required","static","unowned","unowned(safe)","unowned(unsafe)","weak","as","is",
25
"break","case","continue","default","else","fallthrough","for","guard","if","in","repeat","switch","where","while",
26
"defer","return","inout","mutating","nonmutating","catch","do","rethrows","throw","throws","try","didSet","get","set","willSet",
27
"assignment","associativity","infix","left","none","operator","postfix","precedence","precedencegroup","prefix","right",
28
"Any","AnyObject","Type","dynamicType","Self","Protocol","__COLUMN__","__FILE__","__FUNCTION__","__LINE__"])
29
var definingKeywords = wordSet(["var","let","class","enum","extension","import","protocol","struct","func","typealias","associatedtype","for"])
30
var atoms = wordSet(["true","false","nil","self","super","_"])
31
var types = wordSet(["Array","Bool","Character","Dictionary","Double","Float","Int","Int8","Int16","Int32","Int64","Never","Optional","Set","String",
32
"UInt8","UInt16","UInt32","UInt64","Void"])
33
var operators = "+-/*%=|&<>~^?!"
34
var punc = ":;,.(){}[]"
35
var binary = /^\-?0b[01][01_]*/
36
var octal = /^\-?0o[0-7][0-7_]*/
37
var hexadecimal = /^\-?0x[\dA-Fa-f][\dA-Fa-f_]*(?:(?:\.[\dA-Fa-f][\dA-Fa-f_]*)?[Pp]\-?\d[\d_]*)?/
38
var decimal = /^\-?\d[\d_]*(?:\.\d[\d_]*)?(?:[Ee]\-?\d[\d_]*)?/
39
var identifier = /^\$\d+|(`?)[_A-Za-z][_A-Za-z$0-9]*\1/
40
var property = /^\.(?:\$\d+|(`?)[_A-Za-z][_A-Za-z$0-9]*\1)/
41
var instruction = /^\#[A-Za-z]+/
42
var attribute = /^@(?:\$\d+|(`?)[_A-Za-z][_A-Za-z$0-9]*\1)/
45
function tokenBase(stream, state, prev) {
46
if (stream.sol()) state.indented = stream.indentation()
47
if (stream.eatSpace()) return null
49
var ch = stream.peek()
51
if (stream.match("//")) {
55
if (stream.match("/*")) {
56
state.tokenize.push(tokenComment)
57
return tokenComment(stream, state)
60
if (stream.match(instruction)) return "builtin"
61
if (stream.match(attribute)) return "attribute"
62
if (stream.match(binary)) return "number"
63
if (stream.match(octal)) return "number"
64
if (stream.match(hexadecimal)) return "number"
65
if (stream.match(decimal)) return "number"
66
if (stream.match(property)) return "property"
67
if (operators.indexOf(ch) > -1) {
71
if (punc.indexOf(ch) > -1) {
77
if (stringMatch = stream.match(/("""|"|')/)) {
78
var tokenize = tokenString.bind(null, stringMatch[0])
79
state.tokenize.push(tokenize)
80
return tokenize(stream, state)
83
if (stream.match(identifier)) {
84
var ident = stream.current()
85
if (types.hasOwnProperty(ident)) return "variable-2"
86
if (atoms.hasOwnProperty(ident)) return "atom"
87
if (keywords.hasOwnProperty(ident)) {
88
if (definingKeywords.hasOwnProperty(ident))
92
if (prev == "define") return "def"
100
function tokenUntilClosingParen() {
102
return function(stream, state, prev) {
103
var inner = tokenBase(stream, state, prev)
104
if (inner == "punctuation") {
105
if (stream.current() == "(") ++depth
106
else if (stream.current() == ")") {
110
return state.tokenize[state.tokenize.length - 1](stream, state)
119
function tokenString(openQuote, stream, state) {
120
var singleLine = openQuote.length == 1
121
var ch, escaped = false
122
while (ch = stream.peek()) {
126
state.tokenize.push(tokenUntilClosingParen())
130
} else if (stream.match(openQuote)) {
144
function tokenComment(stream, state) {
147
stream.match(/^[^/*]+/, true)
150
if (ch === "/" && stream.eat("*")) {
151
state.tokenize.push(tokenComment)
152
} else if (ch === "*" && stream.eat("/")) {
159
function Context(prev, align, indented) {
162
this.indented = indented
165
function pushContext(state, stream) {
166
var align = stream.match(/^\s*($|\/[\/\*])/, false) ? null : stream.column() + 1
167
state.context = new Context(state.context, align, state.indented)
170
function popContext(state) {
172
state.indented = state.context.indented
173
state.context = state.context.prev
177
CodeMirror.defineMode("swift", function(config) {
179
startState: function() {
188
token: function(stream, state) {
189
var prev = state.prev
191
var tokenize = state.tokenize[state.tokenize.length - 1] || tokenBase
192
var style = tokenize(stream, state, prev)
193
if (!style || style == "comment") state.prev = prev
194
else if (!state.prev) state.prev = style
196
if (style == "punctuation") {
197
var bracket = /[\(\[\{]|([\]\)\}])/.exec(stream.current())
198
if (bracket) (bracket[1] ? popContext : pushContext)(state, stream)
204
indent: function(state, textAfter) {
205
var cx = state.context
207
var closing = /^[\]\}\)]/.test(textAfter)
208
if (cx.align != null) return cx.align - (closing ? 1 : 0)
209
return cx.indented + (closing ? 0 : config.indentUnit)
212
electricInput: /^\s*[\)\}\]]$/,
215
blockCommentStart: "/*",
216
blockCommentEnd: "*/",
218
closeBrackets: "()[]{}''\"\"``"
222
CodeMirror.defineMIME("text/x-swift","swift")