move a few things away in to-be-integrated/
[myslice.git] / to-be-integrated / third-party / codemirror-3.15 / mode / scheme / scheme.js
1 /**
2  * Author: Koh Zi Han, based on implementation by Koh Zi Chun
3  */
4 CodeMirror.defineMode("scheme", function () {
5     var BUILTIN = "builtin", COMMENT = "comment", STRING = "string",
6         ATOM = "atom", NUMBER = "number", BRACKET = "bracket";
7     var INDENT_WORD_SKIP = 2;
8
9     function makeKeywords(str) {
10         var obj = {}, words = str.split(" ");
11         for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
12         return obj;
13     }
14
15     var keywords = makeKeywords("λ case-lambda call/cc class define-class 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 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?");
16     var indentKeys = makeKeywords("define let letrec let* lambda");
17
18     function stateStack(indent, type, prev) { // represents a state stack object
19         this.indent = indent;
20         this.type = type;
21         this.prev = prev;
22     }
23
24     function pushStack(state, indent, type) {
25         state.indentStack = new stateStack(indent, type, state.indentStack);
26     }
27
28     function popStack(state) {
29         state.indentStack = state.indentStack.prev;
30     }
31
32     var binaryMatcher = new RegExp(/^(?:[-+]i|[-+][01]+#*(?:\/[01]+#*)?i|[-+]?[01]+#*(?:\/[01]+#*)?@[-+]?[01]+#*(?:\/[01]+#*)?|[-+]?[01]+#*(?:\/[01]+#*)?[-+](?:[01]+#*(?:\/[01]+#*)?)?i|[-+]?[01]+#*(?:\/[01]+#*)?)(?=[()\s;"]|$)/i);
33     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);
34     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);
35     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);
36
37     function isBinaryNumber (stream) {
38         return stream.match(binaryMatcher);
39     }
40
41     function isOctalNumber (stream) {
42         return stream.match(octalMatcher);
43     }
44
45     function isDecimalNumber (stream, backup) {
46         if (backup === true) {
47             stream.backUp(1);
48         }
49         return stream.match(decimalMatcher);
50     }
51
52     function isHexNumber (stream) {
53         return stream.match(hexMatcher);
54     }
55
56     return {
57         startState: function () {
58             return {
59                 indentStack: null,
60                 indentation: 0,
61                 mode: false,
62                 sExprComment: false
63             };
64         },
65
66         token: function (stream, state) {
67             if (state.indentStack == null && stream.sol()) {
68                 // update indentation, but only if indentStack is empty
69                 state.indentation = stream.indentation();
70             }
71
72             // skip spaces
73             if (stream.eatSpace()) {
74                 return null;
75             }
76             var returnType = null;
77
78             switch(state.mode){
79                 case "string": // multi-line string parsing mode
80                     var next, escaped = false;
81                     while ((next = stream.next()) != null) {
82                         if (next == "\"" && !escaped) {
83
84                             state.mode = false;
85                             break;
86                         }
87                         escaped = !escaped && next == "\\";
88                     }
89                     returnType = STRING; // continue on in scheme-string mode
90                     break;
91                 case "comment": // comment parsing mode
92                     var next, maybeEnd = false;
93                     while ((next = stream.next()) != null) {
94                         if (next == "#" && maybeEnd) {
95
96                             state.mode = false;
97                             break;
98                         }
99                         maybeEnd = (next == "|");
100                     }
101                     returnType = COMMENT;
102                     break;
103                 case "s-expr-comment": // s-expr commenting mode
104                     state.mode = false;
105                     if(stream.peek() == "(" || stream.peek() == "["){
106                         // actually start scheme s-expr commenting mode
107                         state.sExprComment = 0;
108                     }else{
109                         // if not we just comment the entire of the next token
110                         stream.eatWhile(/[^/s]/); // eat non spaces
111                         returnType = COMMENT;
112                         break;
113                     }
114                 default: // default parsing mode
115                     var ch = stream.next();
116
117                     if (ch == "\"") {
118                         state.mode = "string";
119                         returnType = STRING;
120
121                     } else if (ch == "'") {
122                         returnType = ATOM;
123                     } else if (ch == '#') {
124                         if (stream.eat("|")) {                    // Multi-line comment
125                             state.mode = "comment"; // toggle to comment mode
126                             returnType = COMMENT;
127                         } else if (stream.eat(/[tf]/i)) {            // #t/#f (atom)
128                             returnType = ATOM;
129                         } else if (stream.eat(';')) {                // S-Expr comment
130                             state.mode = "s-expr-comment";
131                             returnType = COMMENT;
132                         } else {
133                             var numTest = null, hasExactness = false, hasRadix = true;
134                             if (stream.eat(/[ei]/i)) {
135                                 hasExactness = true;
136                             } else {
137                                 stream.backUp(1);       // must be radix specifier
138                             }
139                             if (stream.match(/^#b/i)) {
140                                 numTest = isBinaryNumber;
141                             } else if (stream.match(/^#o/i)) {
142                                 numTest = isOctalNumber;
143                             } else if (stream.match(/^#x/i)) {
144                                 numTest = isHexNumber;
145                             } else if (stream.match(/^#d/i)) {
146                                 numTest = isDecimalNumber;
147                             } else if (stream.match(/^[-+0-9.]/, false)) {
148                                 hasRadix = false;
149                                 numTest = isDecimalNumber;
150                             // re-consume the intial # if all matches failed
151                             } else if (!hasExactness) {
152                                 stream.eat('#');
153                             }
154                             if (numTest != null) {
155                                 if (hasRadix && !hasExactness) {
156                                     // consume optional exactness after radix
157                                     stream.match(/^#[ei]/i);
158                                 }
159                                 if (numTest(stream))
160                                     returnType = NUMBER;
161                             }
162                         }
163                     } else if (/^[-+0-9.]/.test(ch) && isDecimalNumber(stream, true)) { // match non-prefixed number, must be decimal
164                         returnType = NUMBER;
165                     } else if (ch == ";") { // comment
166                         stream.skipToEnd(); // rest of the line is a comment
167                         returnType = COMMENT;
168                     } else if (ch == "(" || ch == "[") {
169                       var keyWord = ''; var indentTemp = stream.column(), letter;
170                         /**
171                         Either
172                         (indent-word ..
173                         (non-indent-word ..
174                         (;something else, bracket, etc.
175                         */
176
177                         while ((letter = stream.eat(/[^\s\(\[\;\)\]]/)) != null) {
178                             keyWord += letter;
179                         }
180
181                         if (keyWord.length > 0 && indentKeys.propertyIsEnumerable(keyWord)) { // indent-word
182
183                             pushStack(state, indentTemp + INDENT_WORD_SKIP, ch);
184                         } else { // non-indent word
185                             // we continue eating the spaces
186                             stream.eatSpace();
187                             if (stream.eol() || stream.peek() == ";") {
188                                 // nothing significant after
189                                 // we restart indentation 1 space after
190                                 pushStack(state, indentTemp + 1, ch);
191                             } else {
192                                 pushStack(state, indentTemp + stream.current().length, ch); // else we match
193                             }
194                         }
195                         stream.backUp(stream.current().length - 1); // undo all the eating
196
197                         if(typeof state.sExprComment == "number") state.sExprComment++;
198
199                         returnType = BRACKET;
200                     } else if (ch == ")" || ch == "]") {
201                         returnType = BRACKET;
202                         if (state.indentStack != null && state.indentStack.type == (ch == ")" ? "(" : "[")) {
203                             popStack(state);
204
205                             if(typeof state.sExprComment == "number"){
206                                 if(--state.sExprComment == 0){
207                                     returnType = COMMENT; // final closing bracket
208                                     state.sExprComment = false; // turn off s-expr commenting mode
209                                 }
210                             }
211                         }
212                     } else {
213                         stream.eatWhile(/[\w\$_\-!$%&*+\.\/:<=>?@\^~]/);
214
215                         if (keywords && keywords.propertyIsEnumerable(stream.current())) {
216                             returnType = BUILTIN;
217                         } else returnType = "variable";
218                     }
219             }
220             return (typeof state.sExprComment == "number") ? COMMENT : returnType;
221         },
222
223         indent: function (state) {
224             if (state.indentStack == null) return state.indentation;
225             return state.indentStack.indent;
226         },
227
228         lineComment: ";;"
229     };
230 });
231
232 CodeMirror.defineMIME("text/x-scheme", "scheme");