aboutsummaryrefslogtreecommitdiff
path: root/imports/codemirror/mode/scheme/scheme.js
diff options
context:
space:
mode:
Diffstat (limited to 'imports/codemirror/mode/scheme/scheme.js')
-rw-r--r--imports/codemirror/mode/scheme/scheme.js120
1 files changed, 74 insertions, 46 deletions
diff --git a/imports/codemirror/mode/scheme/scheme.js b/imports/codemirror/mode/scheme/scheme.js
index caf78db0..f981b235 100644
--- a/imports/codemirror/mode/scheme/scheme.js
+++ b/imports/codemirror/mode/scheme/scheme.js
@@ -5,7 +5,7 @@ CodeMirror.defineMode("scheme", function (config, mode) {
5 var BUILTIN = "builtin", COMMENT = "comment", STRING = "string", 5 var BUILTIN = "builtin", COMMENT = "comment", STRING = "string",
6 ATOM = "atom", NUMBER = "number", BRACKET = "bracket", KEYWORD="keyword"; 6 ATOM = "atom", NUMBER = "number", BRACKET = "bracket", KEYWORD="keyword";
7 var INDENT_WORD_SKIP = 2, KEYWORDS_SKIP = 1; 7 var INDENT_WORD_SKIP = 2, KEYWORDS_SKIP = 1;
8 8
9 function makeKeywords(str) { 9 function makeKeywords(str) {
10 var obj = {}, words = str.split(" "); 10 var obj = {}, words = str.split(" ");
11 for (var i = 0; i < words.length; ++i) obj[words[i]] = true; 11 for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
@@ -14,7 +14,6 @@ CodeMirror.defineMode("scheme", function (config, mode) {
14 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?"); 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"); 16 var indentKeys = makeKeywords("define let letrec let* lambda");
17
18 17
19 function stateStack(indent, type, prev) { // represents a state stack object 18 function stateStack(indent, type, prev) { // represents a state stack object
20 this.indent = indent; 19 this.indent = indent;
@@ -29,21 +28,29 @@ CodeMirror.defineMode("scheme", function (config, mode) {
29 function popStack(state) { 28 function popStack(state) {
30 state.indentStack = state.indentStack.prev; 29 state.indentStack = state.indentStack.prev;
31 } 30 }
32 31
33 /** 32 var binaryMatcher = new RegExp(/^(?:[-+]i|[-+][01]+#*(?:\/[01]+#*)?i|[-+]?[01]+#*(?:\/[01]+#*)?@[-+]?[01]+#*(?:\/[01]+#*)?|[-+]?[01]+#*(?:\/[01]+#*)?[-+](?:[01]+#*(?:\/[01]+#*)?)?i|[-+]?[01]+#*(?:\/[01]+#*)?)(?=[()\s;"]|$)/i);
34 * Scheme numbers are complicated unfortunately. 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);
35 * Checks if we're looking at a number, which might be possibly a fraction. 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);
36 * Also checks that it is not part of a longer identifier. Returns true/false accordingly. 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);
37 */ 36
38 function isNumber(ch, stream){ 37 function isBinaryNumber (stream) {
39 if(/[0-9]/.exec(ch) != null){ 38 return stream.match(binaryMatcher);
40 stream.eatWhile(/[0-9]/); 39 }
41 stream.eat(/\//); 40
42 stream.eatWhile(/[0-9]/); 41 function isOctalNumber (stream) {
43 if (stream.eol() || !(/[a-zA-Z\-\_\/]/.exec(stream.peek()))) return true; 42 return stream.match(octalMatcher);
44 stream.backUp(stream.current().length - 1); // undo all the eating 43 }
44
45 function isDecimalNumber (stream, backup) {
46 if (backup === true) {
47 stream.backUp(1);
45 } 48 }
46 return false; 49 return stream.match(decimalMatcher);
50 }
51
52 function isHexNumber (stream) {
53 return stream.match(hexMatcher);
47 } 54 }
48 55
49 return { 56 return {
@@ -67,13 +74,13 @@ CodeMirror.defineMode("scheme", function (config, mode) {
67 return null; 74 return null;
68 } 75 }
69 var returnType = null; 76 var returnType = null;
70 77
71 switch(state.mode){ 78 switch(state.mode){
72 case "string": // multi-line string parsing mode 79 case "string": // multi-line string parsing mode
73 var next, escaped = false; 80 var next, escaped = false;
74 while ((next = stream.next()) != null) { 81 while ((next = stream.next()) != null) {
75 if (next == "\"" && !escaped) { 82 if (next == "\"" && !escaped) {
76 83
77 state.mode = false; 84 state.mode = false;
78 break; 85 break;
79 } 86 }
@@ -85,7 +92,7 @@ CodeMirror.defineMode("scheme", function (config, mode) {
85 var next, maybeEnd = false; 92 var next, maybeEnd = false;
86 while ((next = stream.next()) != null) { 93 while ((next = stream.next()) != null) {
87 if (next == "#" && maybeEnd) { 94 if (next == "#" && maybeEnd) {
88 95
89 state.mode = false; 96 state.mode = false;
90 break; 97 break;
91 } 98 }
@@ -106,52 +113,73 @@ CodeMirror.defineMode("scheme", function (config, mode) {
106 } 113 }
107 default: // default parsing mode 114 default: // default parsing mode
108 var ch = stream.next(); 115 var ch = stream.next();
109 116
110 if (ch == "\"") { 117 if (ch == "\"") {
111 state.mode = "string"; 118 state.mode = "string";
112 returnType = STRING; 119 returnType = STRING;
113 120
114 } else if (ch == "'") { 121 } else if (ch == "'") {
115 returnType = ATOM; 122 returnType = ATOM;
116 } else if (ch == '#') { 123 } else if (ch == '#') {
117 if (stream.eat("|")) { // Multi-line comment 124 if (stream.eat("|")) { // Multi-line comment
118 state.mode = "comment"; // toggle to comment mode 125 state.mode = "comment"; // toggle to comment mode
119 returnType = COMMENT; 126 returnType = COMMENT;
120 } else if (stream.eat(/[tf]/)) { // #t/#f (atom) 127 } else if (stream.eat(/[tf]/i)) { // #t/#f (atom)
121 returnType = ATOM; 128 returnType = ATOM;
122 } else if (stream.eat(';')) { // S-Expr comment 129 } else if (stream.eat(';')) { // S-Expr comment
123 state.mode = "s-expr-comment"; 130 state.mode = "s-expr-comment";
124 returnType = 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 }
125 } 162 }
126 163 } else if (/^[-+0-9.]/.test(ch) && isDecimalNumber(stream, true)) { // match non-prefixed number, must be decimal
164 returnType = NUMBER;
127 } else if (ch == ";") { // comment 165 } else if (ch == ";") { // comment
128 stream.skipToEnd(); // rest of the line is a comment 166 stream.skipToEnd(); // rest of the line is a comment
129 returnType = COMMENT; 167 returnType = COMMENT;
130 } else if (ch == "-"){
131
132 if(!isNaN(parseInt(stream.peek()))){
133 stream.eatWhile(/[\/0-9]/);
134 returnType = NUMBER;
135 }else{
136 returnType = null;
137 }
138 } else if (isNumber(ch,stream)){
139 returnType = NUMBER;
140 } else if (ch == "(" || ch == "[") { 168 } else if (ch == "(" || ch == "[") {
141 var keyWord = ''; var indentTemp = stream.column(); 169 var keyWord = ''; var indentTemp = stream.column();
142 /** 170 /**
143 Either 171 Either
144 (indent-word .. 172 (indent-word ..
145 (non-indent-word .. 173 (non-indent-word ..
146 (;something else, bracket, etc. 174 (;something else, bracket, etc.
147 */ 175 */
148 176
149 while ((letter = stream.eat(/[^\s\(\[\;\)\]]/)) != null) { 177 while ((letter = stream.eat(/[^\s\(\[\;\)\]]/)) != null) {
150 keyWord += letter; 178 keyWord += letter;
151 } 179 }
152 180
153 if (keyWord.length > 0 && indentKeys.propertyIsEnumerable(keyWord)) { // indent-word 181 if (keyWord.length > 0 && indentKeys.propertyIsEnumerable(keyWord)) { // indent-word
154 182
155 pushStack(state, indentTemp + INDENT_WORD_SKIP, ch);