| 1 |
function wordSet(words) { |
| 2 |
var set = {} |
| 3 |
for (var i = 0; i < words.length; i++) set[words[i]] = true |
| 4 |
return set |
| 5 |
} |
| 6 |
|
| 7 |
var keywords = wordSet(["_","var","let","actor","class","enum","extension","import","protocol","struct","func","typealias","associatedtype", |
| 8 |
"open","public","internal","fileprivate","private","deinit","init","new","override","self","subscript","super", |
| 9 |
"convenience","dynamic","final","indirect","lazy","required","static","unowned","unowned(safe)","unowned(unsafe)","weak","as","is", |
| 10 |
"break","case","continue","default","else","fallthrough","for","guard","if","in","repeat","switch","where","while", |
| 11 |
"defer","return","inout","mutating","nonmutating","isolated","nonisolated","catch","do","rethrows","throw","throws","async","await","try","didSet","get","set","willSet", |
| 12 |
"assignment","associativity","infix","left","none","operator","postfix","precedence","precedencegroup","prefix","right", |
| 13 |
"Any","AnyObject","Type","dynamicType","Self","Protocol","__COLUMN__","__FILE__","__FUNCTION__","__LINE__"]) |
| 14 |
var definingKeywords = wordSet(["var","let","actor","class","enum","extension","import","protocol","struct","func","typealias","associatedtype","for"]) |
| 15 |
var atoms = wordSet(["true","false","nil","self","super","_"]) |
| 16 |
var types = wordSet(["Array","Bool","Character","Dictionary","Double","Float","Int","Int8","Int16","Int32","Int64","Never","Optional","Set","String", |
| 17 |
"UInt8","UInt16","UInt32","UInt64","Void"]) |
| 18 |
var operators = "+-/*%=|&<>~^?!" |
| 19 |
var punc = ":;,.(){}[]" |
| 20 |
var binary = /^\-?0b[01][01_]*/ |
| 21 |
var octal = /^\-?0o[0-7][0-7_]*/ |
| 22 |
var hexadecimal = /^\-?0x[\dA-Fa-f][\dA-Fa-f_]*(?:(?:\.[\dA-Fa-f][\dA-Fa-f_]*)?[Pp]\-?\d[\d_]*)?/ |
| 23 |
var decimal = /^\-?\d[\d_]*(?:\.\d[\d_]*)?(?:[Ee]\-?\d[\d_]*)?/ |
| 24 |
var identifier = /^\$\d+|(`?)[_A-Za-z][_A-Za-z$0-9]*\1/ |
| 25 |
var property = /^\.(?:\$\d+|(`?)[_A-Za-z][_A-Za-z$0-9]*\1)/ |
| 26 |
var instruction = /^\#[A-Za-z]+/ |
| 27 |
var attribute = /^@(?:\$\d+|(`?)[_A-Za-z][_A-Za-z$0-9]*\1)/ |
| 28 |
//var regexp = /^\/(?!\s)(?:\/\/)?(?:\\.|[^\/])+\// |
| 29 |
|
| 30 |
function tokenBase(stream, state, prev) { |
| 31 |
if (stream.sol()) state.indented = stream.indentation() |
| 32 |
if (stream.eatSpace()) return null |
| 33 |
|
| 34 |
var ch = stream.peek() |
| 35 |
if (ch == "/") { |
| 36 |
if (stream.match("//")) { |
| 37 |
stream.skipToEnd() |
| 38 |
return "comment" |
| 39 |
} |
| 40 |
if (stream.match("/*")) { |
| 41 |
state.tokenize.push(tokenComment) |
| 42 |
return tokenComment(stream, state) |
| 43 |
} |
| 44 |
} |
| 45 |
if (stream.match(instruction)) return "builtin" |
| 46 |
if (stream.match(attribute)) return "attribute" |
| 47 |
if (stream.match(binary)) return "number" |
| 48 |
if (stream.match(octal)) return "number" |
| 49 |
if (stream.match(hexadecimal)) return "number" |
| 50 |
if (stream.match(decimal)) return "number" |
| 51 |
if (stream.match(property)) return "property" |
| 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 |
var stringMatch |
| 62 |
if (stringMatch = stream.match(/("""|"|')/)) { |
| 63 |
var tokenize = tokenString.bind(null, stringMatch[0]) |
| 64 |
state.tokenize.push(tokenize) |
| 65 |
return tokenize(stream, state) |
| 66 |
} |
| 67 |
|
| 68 |
if (stream.match(identifier)) { |
| 69 |
var ident = stream.current() |
| 70 |
if (types.hasOwnProperty(ident)) return "type" |
| 71 |
if (atoms.hasOwnProperty(ident)) return "atom" |
| 72 |
if (keywords.hasOwnProperty(ident)) { |
| 73 |
if (definingKeywords.hasOwnProperty(ident)) |
| 74 |
state.prev = "define" |
| 75 |
return "keyword" |
| 76 |
} |
| 77 |
if (prev == "define") return "def" |
| 78 |
return "variable" |
| 79 |
} |
| 80 |
|
| 81 |
stream.next() |
| 82 |
return null |
| 83 |
} |
| 84 |
|
| 85 |
function tokenUntilClosingParen() { |
| 86 |
var depth = 0 |
| 87 |
return function(stream, state, prev) { |
| 88 |
var inner = tokenBase(stream, state, prev) |
| 89 |
if (inner == "punctuation") { |
| 90 |
if (stream.current() == "(") ++depth |
| 91 |
else if (stream.current() == ")") { |
| 92 |
if (depth == 0) { |
| 93 |
stream.backUp(1) |
| 94 |
state.tokenize.pop() |
| 95 |
return state.tokenize[state.tokenize.length - 1](stream, state) |
| 96 |
} |
| 97 |
else --depth |
| 98 |
} |
| 99 |
} |
| 100 |
return inner |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
function tokenString(openQuote, stream, state) { |
| 105 |
var singleLine = openQuote.length == 1 |
| 106 |
var ch, escaped = false |
| 107 |
while (ch = stream.peek()) { |
| 108 |
if (escaped) { |
| 109 |
stream.next() |
| 110 |
if (ch == "(") { |
| 111 |
state.tokenize.push(tokenUntilClosingParen()) |
| 112 |
return "string" |
| 113 |
} |
| 114 |
escaped = false |
| 115 |
} else if (stream.match(openQuote)) { |
| 116 |
state.tokenize.pop() |
| 117 |
return "string" |
| 118 |
} else { |
| 119 |
stream.next() |
| 120 |
escaped = ch == "\\" |
| 121 |
} |
| 122 |
} |
| 123 |
if (singleLine) { |
| 124 |
state.tokenize.pop() |
| 125 |
} |
| 126 |
return "string" |
| 127 |
} |
| 128 |
|
| 129 |
function tokenComment(stream, state) { |
| 130 |
var ch |
| 131 |
while (ch = stream.next()) { |
| 132 |
if (ch === "/" && stream.eat("*")) { |
| 133 |
state.tokenize.push(tokenComment) |
| 134 |
} else if (ch === "*" && stream.eat("/")) { |
| 135 |
state.tokenize.pop() |
| 136 |
break |
| 137 |
} |
| 138 |
} |
| 139 |
return "comment" |
| 140 |
} |
| 141 |
|
| 142 |
function Context(prev, align, indented) { |
| 143 |
this.prev = prev |
| 144 |
this.align = align |
| 145 |
this.indented = indented |
| 146 |
} |
| 147 |
|
| 148 |
function pushContext(state, stream) { |
| 149 |
var align = stream.match(/^\s*($|\/[\/\*]|[)}\]])/, false) ? null : stream.column() + 1 |
| 150 |
state.context = new Context(state.context, align, state.indented) |
| 151 |
} |
| 152 |
|
| 153 |
function popContext(state) { |
| 154 |
if (state.context) { |
| 155 |
state.indented = state.context.indented |
| 156 |
state.context = state.context.prev |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
export const swift = { |
| 161 |
name: "swift", |
| 162 |
startState: function() { |
| 163 |
return { |
| 164 |
prev: null, |
| 165 |
context: null, |
| 166 |
indented: 0, |
| 167 |
tokenize: [] |
| 168 |
} |
| 169 |
}, |
| 170 |
|
| 171 |
token: function(stream, state) { |
| 172 |
var prev = state.prev |
| 173 |
state.prev = null |
| 174 |
var tokenize = state.tokenize[state.tokenize.length - 1] || tokenBase |
| 175 |
var style = tokenize(stream, state, prev) |
| 176 |
if (!style || style == "comment") state.prev = prev |
| 177 |
else if (!state.prev) state.prev = style |
| 178 |
|
| 179 |
if (style == "punctuation") { |
| 180 |
var bracket = /[\(\[\{]|([\]\)\}])/.exec(stream.current()) |
| 181 |
if (bracket) (bracket[1] ? popContext : pushContext)(state, stream) |
| 182 |
} |
| 183 |
|
| 184 |
return style |
| 185 |
}, |
| 186 |
|
| 187 |
indent: function(state, textAfter, iCx) { |
| 188 |
var cx = state.context |
| 189 |
if (!cx) return 0 |
| 190 |
var closing = /^[\]\}\)]/.test(textAfter) |
| 191 |
if (cx.align != null) return cx.align - (closing ? 1 : 0) |
| 192 |
return cx.indented + (closing ? 0 : iCx.unit) |
| 193 |
}, |
| 194 |
|
| 195 |
languageData: { |
| 196 |
indentOnInput: /^\s*[\)\}\]]$/, |
| 197 |
commentTokens: {line: "//", block: {open: "/*", close: "*/"}}, |
| 198 |
closeBrackets: {brackets: ["(", "[", "{", "'", '"', "`"]} |
| 199 |
} |
| 200 |
} |