10
if (typeof exports == "object" && typeof module == "object")
11
mod(require("../../lib/codemirror"));
12
else if (typeof define == "function" && define.amd)
13
define(["../../lib/codemirror"], mod);
16
})(function(CodeMirror) {
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;
24
function makeKeywords(str) {
25
var obj = {}, words = str.split(" ");
26
for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
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");
33
function stateStack(indent, type, prev) {
39
function pushStack(state, indent, type) {
40
state.indentStack = new stateStack(indent, type, state.indentStack);
43
function popStack(state) {
44
state.indentStack = state.indentStack.prev;
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);
52
function isBinaryNumber (stream) {
53
return stream.match(binaryMatcher);
56
function isOctalNumber (stream) {
57
return stream.match(octalMatcher);
60
function isDecimalNumber (stream, backup) {
61
if (backup === true) {
64
return stream.match(decimalMatcher);
67
function isHexNumber (stream) {
68
return stream.match(hexMatcher);
71
function processEscapedSequence(stream, options) {
72
var next, escaped = false;
73
while ((next = stream.next()) != null) {
74
if (next == options.token && !escaped) {
76
options.state.mode = false;
79
escaped = !escaped && next == "\\";
84
startState: function () {
94
token: function (stream, state) {
95
if (state.indentStack == null && stream.sol()) {
97
state.indentation = stream.indentation();
101
if (stream.eatSpace()) {
104
var returnType = null;
108
processEscapedSequence(stream, {
115
processEscapedSequence(stream, {
122
var next, maybeEnd = false;
123
while ((next = stream.next()) != null) {
124
if (next == "#" && maybeEnd) {
129
maybeEnd = (next == "|");
131
returnType = COMMENT;
133
case "s-expr-comment":
135
if(stream.peek() == "(" || stream.peek() == "["){
137
state.sExprComment = 0;
140
stream.eatWhile(/[^\s\(\)\[\]]/);
141
returnType = COMMENT;
145
var ch = stream.next();
148
state.mode = "string";
151
} else if (ch == "'") {
152
if (stream.peek() == "(" || stream.peek() == "["){
153
if (typeof state.sExprQuote != "number") {
154
state.sExprQuote = 0;
158
stream.eatWhile(/[\w_\-!$%&*+\.\/:<=>?@\^~]/);
161
} else if (ch == '|') {
162
state.mode = "symbol";
164
} else if (ch == '#') {
165
if (stream.eat("|")) {
166
state.mode = "comment";
167
returnType = COMMENT;
168
} else if (stream.eat(/[tf]/i)) {
170
} else if (stream.eat(';')) {
171
state.mode = "s-expr-comment";
172
returnType = COMMENT;
174
var numTest = null, hasExactness = false, hasRadix = true;
175
if (stream.eat(/[ei]/i)) {
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)) {
190
numTest = isDecimalNumber;
192
} else if (!hasExactness) {
195
if (numTest != null) {
196
if (hasRadix && !hasExactness) {
198
stream.match(/^#[ei]/i);
204
} else if (/^[-+0-9.]/.test(ch) && isDecimalNumber(stream, true)) {
206
} else if (ch == ";") {
208
returnType = COMMENT;
209
} else if (ch == "(" || ch == "[") {
210
var keyWord = ''; var indentTemp = stream.column(), letter;
218
while ((letter = stream.eat(/[^\s\(\[\;\)\]]/)) != null) {
222
if (keyWord.length > 0 && indentKeys.propertyIsEnumerable(keyWord)) {
224
pushStack(state, indentTemp + INDENT_WORD_SKIP, ch);
228
if (stream.eol() || stream.peek() == ";") {
231
pushStack(state, indentTemp + 1, ch);
233
pushStack(state, indentTemp + stream.current().length, ch);
236
stream.backUp(stream.current().length - 1);
238
if(typeof state.sExprComment == "number") state.sExprComment++;
239
if(typeof state.sExprQuote == "number") state.sExprQuote++;
241
returnType = BRACKET;
242
} else if (ch == ")" || ch == "]") {
243
returnType = BRACKET;
244
if (state.indentStack != null && state.indentStack.type == (ch == ")" ? "(" : "[")) {
247
if(typeof state.sExprComment == "number"){
248
if(--state.sExprComment == 0){
249
returnType = COMMENT;
250
state.sExprComment = false;
253
if(typeof state.sExprQuote == "number"){
254
if(--state.sExprQuote == 0){
256
state.sExprQuote = false;
261
stream.eatWhile(/[\w_\-!$%&*+\.\/:<=>?@\^~]/);
263
if (keywords && keywords.propertyIsEnumerable(stream.current())) {
264
returnType = BUILTIN;
265
} else returnType = "variable";
268
return (typeof state.sExprComment == "number") ? COMMENT : ((typeof state.sExprQuote == "number") ? ATOM : returnType);
271
indent: function (state) {
272
if (state.indentStack == null) return state.indentation;
273
return state.indentStack.indent;
277
closeBrackets: {pairs: "()[]{}\"\""},
282
CodeMirror.defineMIME("text/x-scheme", "scheme");