github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/app/public/codemirror/mode/swift/swift.js (about) 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","deinit","enum","extension","func","import","init","protocol", 23 "static","struct","subscript","typealias","as","dynamicType","is","new","super", 24 "self","Self","Type","__COLUMN__","__FILE__","__FUNCTION__","__LINE__","break","case", 25 "continue","default","do","else","fallthrough","if","in","for","return","switch", 26 "where","while","associativity","didSet","get","infix","inout","left","mutating", 27 "none","nonmutating","operator","override","postfix","precedence","prefix","right", 28 "set","unowned","weak","willSet"]) 29 var definingKeywords = wordSet(["var","let","class","enum","extension","func","import","protocol","struct", 30 "typealias","dynamicType","for"]) 31 var atoms = wordSet(["Infinity","NaN","undefined","null","true","false","on","off","yes","no","nil","null", 32 "this","super"]) 33 var types = wordSet(["String","bool","int","string","double","Double","Int","Float","float","public", 34 "private","extension"]) 35 var operators = "+-/*%=|&<>#" 36 var punc = ";,.(){}[]" 37 var delimiters = /^(?:[()\[\]{},:`=;]|\.\.?\.?)/ 38 var number = /^-?(?:(?:[\d_]+\.[_\d]*|\.[_\d]+|0o[0-7_\.]+|0b[01_\.]+)(?:e-?[\d_]+)?|0x[\d_a-f\.]+(?:p-?[\d_]+)?)/i 39 var identifier = /^[_A-Za-z$][_A-Za-z$0-9]*/ 40 var property = /^[@\.][_A-Za-z$][_A-Za-z$0-9]*/ 41 var regexp = /^\/(?!\s)(?:\/\/)?(?:\\.|[^\/])+\// 42 43 function tokenBase(stream, state, prev) { 44 if (stream.eatSpace()) return null 45 46 var ch = stream.peek() 47 if (ch == "/") { 48 if (stream.match("//")) { 49 stream.skipToEnd() 50 return "comment" 51 } 52 if (stream.match("/*")) { 53 state.tokenize.push(tokenComment) 54 return tokenComment(stream, state) 55 } 56 if (stream.match(regexp)) return "string-2" 57 } 58 if (operators.indexOf(ch) > -1) { 59 stream.next() 60 return "operator" 61 } 62 if (punc.indexOf(ch) > -1) { 63 stream.match(delimiters) 64 return "punctuation" 65 } 66 if (ch == '"' || ch == "'") { 67 stream.next() 68 var tokenize = tokenString(ch) 69 state.tokenize.push(tokenize) 70 return tokenize(stream, state) 71 } 72 73 if (stream.match(number)) return "number" 74 if (stream.match(property)) return "property" 75 76 if (stream.match(identifier)) { 77 var ident = stream.current() 78 if (keywords.hasOwnProperty(ident)) { 79 if (definingKeywords.hasOwnProperty(ident)) 80 state.prev = "define" 81 return "keyword" 82 } 83 if (types.hasOwnProperty(ident)) return "variable-2" 84 if (atoms.hasOwnProperty(ident)) return "atom" 85 if (prev == "define") return "def" 86 return "variable" 87 } 88 89 stream.next() 90 return null 91 } 92 93 function tokenUntilClosingParen() { 94 var depth = 0 95 return function(stream, state, prev) { 96 var inner = tokenBase(stream, state, prev) 97 if (inner == "punctuation") { 98 if (stream.current() == "(") ++depth 99 else if (stream.current() == ")") { 100 if (depth == 0) { 101 stream.backUp(1) 102 state.tokenize.pop() 103 return state.tokenize[state.tokenize.length - 1](stream, state) 104 } 105 else --depth 106 } 107 } 108 return inner 109 } 110 } 111 112 function tokenString(quote) { 113 return function(stream, state) { 114 var ch, escaped = false 115 while (ch = stream.next()) { 116 if (escaped) { 117 if (ch == "(") { 118 state.tokenize.push(tokenUntilClosingParen()) 119 return "string" 120 } 121 escaped = false 122 } else if (ch == quote) { 123 break 124 } else { 125 escaped = ch == "\\" 126 } 127 } 128 state.tokenize.pop() 129 return "string" 130 } 131 } 132 133 function tokenComment(stream, state) { 134 stream.match(/^(?:[^*]|\*(?!\/))*/) 135 if (stream.match("*/")) state.tokenize.pop() 136 return "comment" 137 } 138 139 CodeMirror.defineMode("swift", function() { 140 return { 141 startState: function() { 142 return { 143 prev: null, 144 tokenize: [] 145 } 146 }, 147 token: function(stream, state) { 148 var prev = state.prev 149 state.prev = null 150 var tokenize = state.tokenize[state.tokenize.length - 1] || tokenBase 151 var style = tokenize(stream, state, prev) 152 if (!style || style == "comment") state.prev = prev 153 else if (!state.prev) state.prev = style 154 return style 155 }, 156 lineComment: "//", 157 blockCommentStart: "/*", 158 blockCommentEnd: "*/" 159 } 160 }) 161 162 CodeMirror.defineMIME("text/x-swift","swift") 163 })