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