File modules/editor/codemirror/mode/swift/swift.js

Last commit: Sun Dec 17 01:14:09 2017 +0100	Jan Dankert	Integration eines weiteren Code-Editors: Codemirror. Demnächst müssen wir hier mal aufräumen und andere Editoren rauswerfen.
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others 2 // Distributed under an MIT license: http://codemirror.net/LICENSE 3 4 // Swift mode created by Michael Kaminsky https://github.com/mkaminsky11 5 6 (function(mod) { 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) 11 else 12 mod(CodeMirror) 13 })(function(CodeMirror) { 14 "use strict" 15 16 function wordSet(words) { 17 var set = {} 18 for (var i = 0; i < words.length; i++) set[words[i]] = true 19 return set 20 } 21 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)/ 43 //var regexp = /^\/(?!\s)(?:\/\/)?(?:\\.|[^\/])+\// 44 45 function tokenBase(stream, state, prev) { 46 if (stream.sol()) state.indented = stream.indentation() 47 if (stream.eatSpace()) return null 48 49 var ch = stream.peek() 50 if (ch == "/") { 51 if (stream.match("//")) { 52 stream.skipToEnd() 53 return "comment" 54 } 55 if (stream.match("/*")) { 56 state.tokenize.push(tokenComment) 57 return tokenComment(stream, state) 58 } 59 } 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) { 68 stream.next() 69 return "operator" 70 } 71 if (punc.indexOf(ch) > -1) { 72 stream.next() 73 stream.match("..") 74 return "punctuation" 75 } 76 if (ch == '"' || ch == "'") { 77 stream.next() 78 var tokenize = tokenString(ch) 79 state.tokenize.push(tokenize) 80 return tokenize(stream, state) 81 } 82 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)) 89 state.prev = "define" 90 return "keyword" 91 } 92 if (prev == "define") return "def" 93 return "variable" 94 } 95 96 stream.next() 97 return null 98 } 99 100 function tokenUntilClosingParen() { 101 var depth = 0 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() == ")") { 107 if (depth == 0) { 108 stream.backUp(1) 109 state.tokenize.pop() 110 return state.tokenize[state.tokenize.length - 1](stream, state) 111 } 112 else --depth 113 } 114 } 115 return inner 116 } 117 } 118 119 function tokenString(quote) { 120 return function(stream, state) { 121 var ch, escaped = false 122 while (ch = stream.next()) { 123 if (escaped) { 124 if (ch == "(") { 125 state.tokenize.push(tokenUntilClosingParen()) 126 return "string" 127 } 128 escaped = false 129 } else if (ch == quote) { 130 break 131 } else { 132 escaped = ch == "\\" 133 } 134 } 135 state.tokenize.pop() 136 return "string" 137 } 138 } 139 140 function tokenComment(stream, state) { 141 stream.match(/^(?:[^*]|\*(?!\/))*/) 142 if (stream.match("*/")) state.tokenize.pop() 143 return "comment" 144 } 145 146 function Context(prev, align, indented) { 147 this.prev = prev 148 this.align = align 149 this.indented = indented 150 } 151 152 function pushContext(state, stream) { 153 var align = stream.match(/^\s*($|\/[\/\*])/, false) ? null : stream.column() + 1 154 state.context = new Context(state.context, align, state.indented) 155 } 156 157 function popContext(state) { 158 if (state.context) { 159 state.indented = state.context.indented 160 state.context = state.context.prev 161 } 162 } 163 164 CodeMirror.defineMode("swift", function(config) { 165 return { 166 startState: function() { 167 return { 168 prev: null, 169 context: null, 170 indented: 0, 171 tokenize: [] 172 } 173 }, 174 175 token: function(stream, state) { 176 var prev = state.prev 177 state.prev = null 178 var tokenize = state.tokenize[state.tokenize.length - 1] || tokenBase 179 var style = tokenize(stream, state, prev) 180 if (!style || style == "comment") state.prev = prev 181 else if (!state.prev) state.prev = style 182 183 if (style == "punctuation") { 184 var bracket = /[\(\[\{]|([\]\)\}])/.exec(stream.current()) 185 if (bracket) (bracket[1] ? popContext : pushContext)(state, stream) 186 } 187 188 return style 189 }, 190 191 indent: function(state, textAfter) { 192 var cx = state.context 193 if (!cx) return 0 194 var closing = /^[\]\}\)]/.test(textAfter) 195 if (cx.align != null) return cx.align - (closing ? 1 : 0) 196 return cx.indented + (closing ? 0 : config.indentUnit) 197 }, 198 199 electricInput: /^\s*[\)\}\]]$/, 200 201 lineComment: "//", 202 blockCommentStart: "/*", 203 blockCommentEnd: "*/", 204 fold: "brace", 205 closeBrackets: "()[]{}''\"\"``" 206 } 207 }) 208 209 CodeMirror.defineMIME("text/x-swift","swift") 210 });
Download modules/editor/codemirror/mode/swift/swift.js
History Sun, 17 Dec 2017 01:14:09 +0100 Jan Dankert Integration eines weiteren Code-Editors: Codemirror. Demnächst müssen wir hier mal aufräumen und andere Editoren rauswerfen.