Mercurial
comparison .cms/lib/codemirror/mode/swift/swift.js @ 0:78edf6b517a0 draft
24.10
author | Coffee CMS <info@coffee-cms.ru> |
---|---|
date | Fri, 11 Oct 2024 22:40:23 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:78edf6b517a0 |
---|---|
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others | |
2 // Distributed under an MIT license: https://codemirror.net/5/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","actor","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","isolated","nonisolated","catch","do","rethrows","throw","throws","async","await","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","actor","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 var stringMatch | |
77 if (stringMatch = stream.match(/("""|"|')/)) { | |
78 var tokenize = tokenString.bind(null, stringMatch[0]) | |
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(openQuote, stream, state) { | |
120 var singleLine = openQuote.length == 1 | |
121 var ch, escaped = false | |
122 while (ch = stream.peek()) { | |
123 if (escaped) { | |
124 stream.next() | |
125 if (ch == "(") { | |
126 state.tokenize.push(tokenUntilClosingParen()) | |
127 return "string" | |
128 } | |
129 escaped = false | |
130 } else if (stream.match(openQuote)) { | |
131 state.tokenize.pop() | |
132 return "string" | |
133 } else { | |
134 stream.next() | |
135 escaped = ch == "\\" | |
136 } | |
137 } | |
138 if (singleLine) { | |
139 state.tokenize.pop() | |
140 } | |
141 return "string" | |
142 } | |
143 | |
144 function tokenComment(stream, state) { | |
145 var ch | |
146 while (ch = stream.next()) { | |
147 if (ch === "/" && stream.eat("*")) { | |
148 state.tokenize.push(tokenComment) | |
149 } else if (ch === "*" && stream.eat("/")) { | |
150 state.tokenize.pop() | |
151 break | |
152 } | |
153 } | |
154 return "comment" | |
155 } | |
156 | |
157 function Context(prev, align, indented) { | |
158 this.prev = prev | |
159 this.align = align | |
160 this.indented = indented | |
161 } | |
162 | |
163 function pushContext(state, stream) { | |
164 var align = stream.match(/^\s*($|\/[\/\*])/, false) ? null : stream.column() + 1 | |
165 state.context = new Context(state.context, align, state.indented) | |
166 } | |
167 | |
168 function popContext(state) { | |
169 if (state.context) { | |
170 state.indented = state.context.indented | |
171 state.context = state.context.prev | |
172 } | |
173 } | |
174 | |
175 CodeMirror.defineMode("swift", function(config) { | |
176 return { | |
177 startState: function() { | |
178 return { | |
179 prev: null, | |
180 context: null, | |
181 indented: 0, | |
182 tokenize: [] | |
183 } | |
184 }, | |
185 | |
186 token: function(stream, state) { | |
187 var prev = state.prev | |
188 state.prev = null | |
189 var tokenize = state.tokenize[state.tokenize.length - 1] || tokenBase | |
190 var style = tokenize(stream, state, prev) | |
191 if (!style || style == "comment") state.prev = prev | |
192 else if (!state.prev) state.prev = style | |
193 | |
194 if (style == "punctuation") { | |
195 var bracket = /[\(\[\{]|([\]\)\}])/.exec(stream.current()) | |
196 if (bracket) (bracket[1] ? popContext : pushContext)(state, stream) | |
197 } | |
198 | |
199 return style | |
200 }, | |
201 | |
202 indent: function(state, textAfter) { | |
203 var cx = state.context | |
204 if (!cx) return 0 | |
205 var closing = /^[\]\}\)]/.test(textAfter) | |
206 if (cx.align != null) return cx.align - (closing ? 1 : 0) | |
207 return cx.indented + (closing ? 0 : config.indentUnit) | |
208 }, | |
209 | |
210 electricInput: /^\s*[\)\}\]]$/, | |
211 | |
212 lineComment: "//", | |
213 blockCommentStart: "/*", | |
214 blockCommentEnd: "*/", | |
215 fold: "brace", | |
216 closeBrackets: "()[]{}''\"\"``" | |
217 } | |
218 }) | |
219 | |
220 CodeMirror.defineMIME("text/x-swift","swift") | |
221 }); |