Mercurial
comparison .cms/lib/codemirror/mode/scheme/scheme.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 /** | |
5 * Author: Koh Zi Han, based on implementation by Koh Zi Chun | |
6 * Improved by: Jakub T. Jankiewicz | |
7 */ | |
8 | |
9 (function(mod) { | |
10 if (typeof exports == "object" && typeof module == "object") // CommonJS | |
11 mod(require("../../lib/codemirror")); | |
12 else if (typeof define == "function" && define.amd) // AMD | |
13 define(["../../lib/codemirror"], mod); | |
14 else // Plain browser env | |
15 mod(CodeMirror); | |
16 })(function(CodeMirror) { | |
17 "use strict"; | |
18 | |
19 CodeMirror.defineMode("scheme", function () { | |
20 var BUILTIN = "builtin", COMMENT = "comment", STRING = "string", | |
21 SYMBOL = "symbol", ATOM = "atom", NUMBER = "number", BRACKET = "bracket"; | |
22 var INDENT_WORD_SKIP = 2; | |
23 | |
24 function makeKeywords(str) { | |
25 var obj = {}, words = str.split(" "); | |
26 for (var i = 0; i < words.length; ++i) obj[words[i]] = true; | |
27 return obj; | |
28 } | |
29 | |
30 var keywords = makeKeywords("λ case-lambda call/cc class cond-expand define-class define-values exit-handler field import inherit init-field interface let*-values let-values let/ec mixin opt-lambda override protect provide public rename require require-for-syntax syntax syntax-case syntax-error unit/sig unless when with-syntax and begin call-with-current-continuation call-with-input-file call-with-output-file case cond define define-syntax define-macro defmacro delay do dynamic-wind else for-each if lambda let let* let-syntax letrec letrec-syntax map or syntax-rules abs acos angle append apply asin assoc assq assv atan boolean? caar cadr call-with-input-file call-with-output-file call-with-values car cdddar cddddr cdr ceiling char->integer char-alphabetic? char-ci<=? char-ci<? char-ci=? char-ci>=? char-ci>? char-downcase char-lower-case? char-numeric? char-ready? char-upcase char-upper-case? char-whitespace? char<=? char<? char=? char>=? char>? char? close-input-port close-output-port complex? cons cos current-input-port current-output-port denominator display eof-object? eq? equal? eqv? eval even? exact->inexact exact? exp expt #f floor force gcd imag-part inexact->exact inexact? input-port? integer->char integer? interaction-environment lcm length list list->string list->vector list-ref list-tail list? load log magnitude make-polar make-rectangular make-string make-vector max member memq memv min modulo negative? newline not null-environment null? number->string number? numerator odd? open-input-file open-output-file output-port? pair? peek-char port? positive? procedure? quasiquote quote quotient rational? rationalize read read-char real-part real? remainder reverse round scheme-report-environment set! set-car! set-cdr! sin sqrt string string->list string->number string->symbol string-append string-ci<=? string-ci<? string-ci=? string-ci>=? string-ci>? string-copy string-fill! string-length string-ref string-set! string<=? string<? string=? string>=? string>? string? substring symbol->string symbol? #t tan transcript-off transcript-on truncate values vector vector->list vector-fill! vector-length vector-ref vector-set! with-input-from-file with-output-to-file write write-char zero?"); | |
31 var indentKeys = makeKeywords("define let letrec let* lambda define-macro defmacro let-syntax letrec-syntax let-values let*-values define-syntax syntax-rules define-values when unless"); | |
32 | |
33 function stateStack(indent, type, prev) { // represents a state stack object | |
34 this.indent = indent; | |
35 this.type = type; | |
36 this.prev = prev; | |
37 } | |
38 | |
39 function pushStack(state, indent, type) { | |
40 state.indentStack = new stateStack(indent, type, state.indentStack); | |
41 } | |
42 | |
43 function popStack(state) { | |
44 state.indentStack = state.indentStack.prev; | |
45 } | |
46 | |
47 var binaryMatcher = new RegExp(/^(?:[-+]i|[-+][01]+#*(?:\/[01]+#*)?i|[-+]?[01]+#*(?:\/[01]+#*)?@[-+]?[01]+#*(?:\/[01]+#*)?|[-+]?[01]+#*(?:\/[01]+#*)?[-+](?:[01]+#*(?:\/[01]+#*)?)?i|[-+]?[01]+#*(?:\/[01]+#*)?)(?=[()\s;"]|$)/i); | |
48 var octalMatcher = new RegExp(/^(?:[-+]i|[-+][0-7]+#*(?:\/[0-7]+#*)?i|[-+]?[0-7]+#*(?:\/[0-7]+#*)?@[-+]?[0-7]+#*(?:\/[0-7]+#*)?|[-+]?[0-7]+#*(?:\/[0-7]+#*)?[-+](?:[0-7]+#*(?:\/[0-7]+#*)?)?i|[-+]?[0-7]+#*(?:\/[0-7]+#*)?)(?=[()\s;"]|$)/i); | |
49 var hexMatcher = new RegExp(/^(?:[-+]i|[-+][\da-f]+#*(?:\/[\da-f]+#*)?i|[-+]?[\da-f]+#*(?:\/[\da-f]+#*)?@[-+]?[\da-f]+#*(?:\/[\da-f]+#*)?|[-+]?[\da-f]+#*(?:\/[\da-f]+#*)?[-+](?:[\da-f]+#*(?:\/[\da-f]+#*)?)?i|[-+]?[\da-f]+#*(?:\/[\da-f]+#*)?)(?=[()\s;"]|$)/i); | |
50 var decimalMatcher = new RegExp(/^(?:[-+]i|[-+](?:(?:(?:\d+#+\.?#*|\d+\.\d*#*|\.\d+#*|\d+)(?:[esfdl][-+]?\d+)?)|\d+#*\/\d+#*)i|[-+]?(?:(?:(?:\d+#+\.?#*|\d+\.\d*#*|\.\d+#*|\d+)(?:[esfdl][-+]?\d+)?)|\d+#*\/\d+#*)@[-+]?(?:(?:(?:\d+#+\.?#*|\d+\.\d*#*|\.\d+#*|\d+)(?:[esfdl][-+]?\d+)?)|\d+#*\/\d+#*)|[-+]?(?:(?:(?:\d+#+\.?#*|\d+\.\d*#*|\.\d+#*|\d+)(?:[esfdl][-+]?\d+)?)|\d+#*\/\d+#*)[-+](?:(?:(?:\d+#+\.?#*|\d+\.\d*#*|\.\d+#*|\d+)(?:[esfdl][-+]?\d+)?)|\d+#*\/\d+#*)?i|(?:(?:(?:\d+#+\.?#*|\d+\.\d*#*|\.\d+#*|\d+)(?:[esfdl][-+]?\d+)?)|\d+#*\/\d+#*))(?=[()\s;"]|$)/i); | |
51 | |
52 function isBinaryNumber (stream) { | |
53 return stream.match(binaryMatcher); | |
54 } | |
55 | |
56 function isOctalNumber (stream) { | |
57 return stream.match(octalMatcher); | |
58 } | |
59 | |
60 function isDecimalNumber (stream, backup) { | |
61 if (backup === true) { | |
62 stream.backUp(1); | |
63 } | |
64 return stream.match(decimalMatcher); | |
65 } | |
66 | |
67 function isHexNumber (stream) { | |
68 return stream.match(hexMatcher); | |
69 } | |
70 | |
71 function processEscapedSequence(stream, options) { | |
72 var next, escaped = false; | |
73 while ((next = stream.next()) != null) { | |
74 if (next == options.token && !escaped) { | |
75 | |
76 options.state.mode = false; | |
77 break; | |
78 } | |
79 escaped = !escaped && next == "\\"; | |
80 } | |
81 } | |
82 | |
83 return { | |
84 startState: function () { | |
85 return { | |
86 indentStack: null, | |
87 indentation: 0, | |
88 mode: false, | |
89 sExprComment: false, | |
90 sExprQuote: false | |
91 }; | |
92 }, | |
93 | |
94 token: function (stream, state) { | |
95 if (state.indentStack == null && stream.sol()) { | |
96 // update indentation, but only if indentStack is empty | |
97 state.indentation = stream.indentation(); | |
98 } | |
99 | |
100 // skip spaces | |
101 if (stream.eatSpace()) { | |
102 return null; | |
103 } | |
104 var returnType = null; | |
105 | |
106 switch(state.mode){ | |
107 case "string": // multi-line string parsing mode | |
108 processEscapedSequence(stream, { | |
109 token: "\"", | |
110 state: state | |
111 }); | |
112 returnType = STRING; // continue on in scheme-string mode | |
113 break; | |
114 case "symbol": // escape symbol | |
115 processEscapedSequence(stream, { | |
116 token: "|", | |
117 state: state | |
118 }); | |
119 returnType = SYMBOL; // continue on in scheme-symbol mode | |
120 break; | |
121 case "comment": // comment parsing mode | |
122 var next, maybeEnd = false; | |
123 while ((next = stream.next()) != null) { | |
124 if (next == "#" && maybeEnd) { | |
125 | |
126 state.mode = false; | |
127 break; | |
128 } | |
129 maybeEnd = (next == "|"); | |
130 } | |
131 returnType = COMMENT; | |
132 break; | |
133 case "s-expr-comment": // s-expr commenting mode | |
134 state.mode = false; | |
135 if(stream.peek() == "(" || stream.peek() == "["){ | |
136 // actually start scheme s-expr commenting mode | |
137 state.sExprComment = 0; | |
138 }else{ | |
139 // if not we just comment the entire of the next token | |
140 stream.eatWhile(/[^\s\(\)\[\]]/); // eat symbol atom | |
141 returnType = COMMENT; | |
142 break; | |
143 } | |
144 default: // default parsing mode | |
145 var ch = stream.next(); | |
146 | |
147 if (ch == "\"") { | |
148 state.mode = "string"; | |
149 returnType = STRING; | |
150 | |
151 } else if (ch == "'") { | |
152 if (stream.peek() == "(" || stream.peek() == "["){ | |
153 if (typeof state.sExprQuote != "number") { | |
154 state.sExprQuote = 0; | |
155 } // else already in a quoted expression | |
156 returnType = ATOM; | |
157 } else { | |
158 stream.eatWhile(/[\w_\-!$%&*+\.\/:<=>?@\^~]/); | |
159 returnType = ATOM; | |
160 } | |
161 } else if (ch == '|') { | |
162 state.mode = "symbol"; | |
163 returnType = SYMBOL; | |
164 } else if (ch == '#') { | |
165 if (stream.eat("|")) { // Multi-line comment | |
166 state.mode = "comment"; // toggle to comment mode | |
167 returnType = COMMENT; | |
168 } else if (stream.eat(/[tf]/i)) { // #t/#f (atom) | |
169 returnType = ATOM; | |
170 } else if (stream.eat(';')) { // S-Expr comment | |
171 state.mode = "s-expr-comment"; | |
172 returnType = COMMENT; | |
173 } else { | |
174 var numTest = null, hasExactness = false, hasRadix = true; | |
175 if (stream.eat(/[ei]/i)) { | |
176 hasExactness = true; | |
177 } else { | |
178 stream.backUp(1); // must be radix specifier | |
179 } | |
180 if (stream.match(/^#b/i)) { | |
181 numTest = isBinaryNumber; | |
182 } else if (stream.match(/^#o/i)) { | |
183 numTest = isOctalNumber; | |
184 } else if (stream.match(/^#x/i)) { | |
185 numTest = isHexNumber; | |
186 } else if (stream.match(/^#d/i)) { | |
187 numTest = isDecimalNumber; | |
188 } else if (stream.match(/^[-+0-9.]/, false)) { | |
189 hasRadix = false; | |
190 numTest = isDecimalNumber; | |
191 // re-consume the initial # if all matches failed | |
192 } else if (!hasExactness) { | |
193 stream.eat('#'); | |
194 } | |
195 if (numTest != null) { | |
196 if (hasRadix && !hasExactness) { | |
197 // consume optional exactness after radix | |
198 stream.match(/^#[ei]/i); | |
199 } | |
200 if (numTest(stream)) | |
201 returnType = NUMBER; | |
202 } | |
203 } | |
204 } else if (/^[-+0-9.]/.test(ch) && isDecimalNumber(stream, true)) { // match non-prefixed number, must be decimal | |
205 returnType = NUMBER; | |
206 } else if (ch == ";") { // comment | |
207 stream.skipToEnd(); // rest of the line is a comment | |
208 returnType = COMMENT; | |
209 } else if (ch == "(" || ch == "[") { | |
210 var keyWord = ''; var indentTemp = stream.column(), letter; | |
211 /** | |
212 Either | |
213 (indent-word .. | |
214 (non-indent-word .. | |
215 (;something else, bracket, etc. | |
216 */ | |
217 | |
218 while ((letter = stream.eat(/[^\s\(\[\;\)\]]/)) != null) { | |
219 keyWord += letter; | |
220 } | |
221 | |
222 if (keyWord.length > 0 && indentKeys.propertyIsEnumerable(keyWord)) { // indent-word | |
223 | |
224 pushStack(state, indentTemp + INDENT_WORD_SKIP, ch); | |
225 } else { // non-indent word | |
226 // we continue eating the spaces | |
227 stream.eatSpace(); | |
228 if (stream.eol() || stream.peek() == ";") { | |
229 // nothing significant after | |
230 // we restart indentation 1 space after | |
231 pushStack(state, indentTemp + 1, ch); | |
232 } else { | |
233 pushStack(state, indentTemp + stream.current().length, ch); // else we match | |
234 } | |
235 } | |
236 stream.backUp(stream.current().length - 1); // undo all the eating | |
237 | |
238 if(typeof state.sExprComment == "number") state.sExprComment++; | |
239 if(typeof state.sExprQuote == "number") state.sExprQuote++; | |
240 | |
241 returnType = BRACKET; | |
242 } else if (ch == ")" || ch == "]") { | |
243 returnType = BRACKET; | |
244 if (state.indentStack != null && state.indentStack.type == (ch == ")" ? "(" : "[")) { | |
245 popStack(state); | |
246 | |
247 if(typeof state.sExprComment == "number"){ | |
248 if(--state.sExprComment == 0){ | |
249 returnType = COMMENT; // final closing bracket | |
250 state.sExprComment = false; // turn off s-expr commenting mode | |
251 } | |
252 } | |
253 if(typeof state.sExprQuote == "number"){ | |
254 if(--state.sExprQuote == 0){ | |
255 returnType = ATOM; // final closing bracket | |
256 state.sExprQuote = false; // turn off s-expr quote mode | |
257 } | |
258 } | |
259 } | |
260 } else { | |
261 stream.eatWhile(/[\w_\-!$%&*+\.\/:<=>?@\^~]/); | |
262 | |
263 if (keywords && keywords.propertyIsEnumerable(stream.current())) { | |
264 returnType = BUILTIN; | |
265 } else returnType = "variable"; | |
266 } | |
267 } | |
268 return (typeof state.sExprComment == "number") ? COMMENT : ((typeof state.sExprQuote == "number") ? ATOM : returnType); | |
269 }, | |
270 | |
271 indent: function (state) { | |
272 if (state.indentStack == null) return state.indentation; | |
273 return state.indentStack.indent; | |
274 }, | |
275 | |
276 fold: "brace-paren", | |
277 closeBrackets: {pairs: "()[]{}\"\""}, | |
278 lineComment: ";;" | |
279 }; | |
280 }); | |
281 | |
282 CodeMirror.defineMIME("text/x-scheme", "scheme"); | |
283 | |
284 }); |