diff options
Diffstat (limited to 'imports/codemirror/mode/scheme')
-rwxr-xr-x | imports/codemirror/mode/scheme/index.html | 64 | ||||
-rwxr-xr-x | imports/codemirror/mode/scheme/scheme.js | 202 |
2 files changed, 266 insertions, 0 deletions
diff --git a/imports/codemirror/mode/scheme/index.html b/imports/codemirror/mode/scheme/index.html new file mode 100755 index 00000000..2a6105fa --- /dev/null +++ b/imports/codemirror/mode/scheme/index.html | |||
@@ -0,0 +1,64 @@ | |||
1 | <!doctype html> | ||
2 | <html> | ||
3 | <head> | ||
4 | <title>CodeMirror: Scheme mode</title> | ||
5 | <link rel="stylesheet" href="../../lib/codemirror.css"> | ||
6 | <script src="../../lib/codemirror.js"></script> | ||
7 | <script src="scheme.js"></script> | ||
8 | <style>.CodeMirror {background: #f8f8f8;}</style> | ||
9 | <link rel="stylesheet" href="../../doc/docs.css"> | ||
10 | </head> | ||
11 | <body> | ||
12 | <h1>CodeMirror: Scheme mode</h1> | ||
13 | <form><textarea id="code" name="code"> | ||
14 | ; See if the input starts with a given symbol. | ||
15 | (define (match-symbol input pattern) | ||
16 | (cond ((null? (remain input)) #f) | ||
17 | ((eqv? (car (remain input)) pattern) (r-cdr input)) | ||
18 | (else #f))) | ||
19 | |||
20 | ; Allow the input to start with one of a list of patterns. | ||
21 | (define (match-or input pattern) | ||
22 | (cond ((null? pattern) #f) | ||
23 | ((match-pattern input (car pattern))) | ||
24 | (else (match-or input (cdr pattern))))) | ||
25 | |||
26 | ; Allow a sequence of patterns. | ||
27 | (define (match-seq input pattern) | ||
28 | (if (null? pattern) | ||
29 | input | ||
30 | (let ((match (match-pattern input (car pattern)))) | ||
31 | (if match (match-seq match (cdr pattern)) #f)))) | ||
32 | |||
33 | ; Match with the pattern but no problem if it does not match. | ||
34 | (define (match-opt input pattern) | ||
35 | (let ((match (match-pattern input (car pattern)))) | ||
36 | (if match match input))) | ||
37 | |||
38 | ; Match anything (other than '()), until pattern is found. The rather | ||
39 | ; clumsy form of requiring an ending pattern is needed to decide where | ||
40 | ; the end of the match is. If none is given, this will match the rest | ||
41 | ; of the sentence. | ||
42 | (define (match-any input pattern) | ||
43 | (cond ((null? (remain input)) #f) | ||
44 | ((null? pattern) (f-cons (remain input) (clear-remain input))) | ||
45 | (else | ||
46 | (let ((accum-any (collector))) | ||
47 | (define (match-pattern-any input pattern) | ||
48 | (cond ((null? (remain input)) #f) | ||
49 | (else (accum-any (car (remain input))) | ||
50 | (cond ((match-pattern (r-cdr input) pattern)) | ||
51 | (else (match-pattern-any (r-cdr input) pattern)))))) | ||
52 | (let ((retval (match-pattern-any input (car pattern)))) | ||
53 | (if retval | ||
54 | (f-cons (accum-any) retval) | ||
55 | #f)))))) | ||
56 | </textarea></form> | ||
57 | <script> | ||
58 | var editor = CodeMirror.fromTextArea(document.getElementById("code"), {}); | ||
59 | </script> | ||
60 | |||
61 | <p><strong>MIME types defined:</strong> <code>text/x-scheme</code>.</p> | ||
62 | |||
63 | </body> | ||
64 | </html> | ||
diff --git a/imports/codemirror/mode/scheme/scheme.js b/imports/codemirror/mode/scheme/scheme.js new file mode 100755 index 00000000..caf78db0 --- /dev/null +++ b/imports/codemirror/mode/scheme/scheme.js | |||
@@ -0,0 +1,202 @@ | |||
1 | /** | ||
2 | * Author: Koh Zi Han, based on implementation by Koh Zi Chun | ||
3 | */ | ||
4 | CodeMirror.defineMode("scheme", function (config, mode) { | ||
5 | var BUILTIN = "builtin", COMMENT = "comment", STRING = "string", | ||
6 | ATOM = "atom", NUMBER = "number", BRACKET = "bracket", KEYWORD="keyword"; | ||
7 | var INDENT_WORD_SKIP = 2, KEYWORDS_SKIP = 1; | ||
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 | |||
19 | function stateStack(indent, type, prev) { // represents a state stack object | ||
20 | this.indent = indent; | ||
21 | this.type = type; | ||
22 | this.prev = prev; | ||
23 | } | ||
24 | |||
25 | function pushStack(state, indent, type) { | ||
26 | state.indentStack = new stateStack(indent, type, state.indentStack); | ||
27 | } | ||
28 | |||
29 | function popStack(state) { | ||
30 | state.indentStack = state.indentStack.prev; | ||
31 | } | ||
32 | |||
33 | /** | ||
34 | * Scheme numbers are complicated unfortunately. | ||
35 | * Checks if we're looking at a number, which might be possibly a fraction. | ||
36 | * Also checks that it is not part of a longer identifier. Returns true/false accordingly. | ||
37 | */ | ||
38 | function isNumber(ch, stream){ | ||
39 | if(/[0-9]/.exec(ch) != null){ | ||
40 | stream.eatWhile(/[0-9]/); | ||
41 | stream.eat(/\//); | ||
42 | stream.eatWhile(/[0-9]/); | ||
43 | if (stream.eol() || !(/[a-zA-Z\-\_\/]/.exec(stream.peek()))) return true; | ||
44 | stream.backUp(stream.current().length - 1); // undo all the eating | ||
45 | } | ||
46 | return false; | ||
47 | } | ||
48 | |||
49 | return { | ||
50 | startState: function () { | ||
51 | return { | ||
52 | indentStack: null, | ||
53 | indentation: 0, | ||
54 | mode: false, | ||
55 | sExprComment: false | ||
56 | }; | ||
57 | }, | ||
58 | |||
59 | token: function (stream, state) { | ||
60 | if (state.indentStack == null && stream.sol()) { | ||
61 | // update indentation, but only if indentStack is empty | ||
62 | state.indentation = stream.indentation(); | ||
63 | } | ||
64 | |||
65 | // skip spaces | ||
66 | if (stream.eatSpace()) { | ||
67 | return null; | ||
68 | } | ||
69 | var returnType = null; | ||
70 | |||
71 | switch(state.mode){ | ||
72 | case "string": // multi-line string parsing mode | ||
73 | var next, escaped = false; | ||
74 | while ((next = stream.next()) != null) { | ||
75 | if (next == "\"" && !escaped) { | ||
76 | |||
77 | state.mode = false; | ||
78 | break; | ||
79 | } | ||
80 | escaped = !escaped && next == "\\"; | ||
81 | } | ||
82 | returnType = STRING; // continue on in scheme-string mode | ||
83 | break; | ||
84 | case "comment": // comment parsing mode | ||
85 | var next, maybeEnd = false; | ||
86 | while ((next = stream.next()) != null) { | ||
87 | if (next == "#" && maybeEnd) { | ||
88 | |||
89 | state.mode = false; | ||
90 | break; | ||
91 | } | ||
92 | maybeEnd = (next == "|"); | ||
93 | } | ||
94 | returnType = COMMENT; | ||
95 | break; | ||
96 | case "s-expr-comment": // s-expr commenting mode | ||
97 | state.mode = false; | ||
98 | if(stream.peek() == "(" || stream.peek() == "["){ | ||
99 | // actually start scheme s-expr commenting mode | ||
100 | state.sExprComment = 0; | ||
101 | }else{ | ||
102 | // if not we just comment the entire of the next token | ||
103 | stream.eatWhile(/[^/s]/); // eat non spaces | ||
104 | returnType = COMMENT; | ||
105 | break; | ||
106 | } | ||
107 | default: // default parsing mode | ||
108 | var ch = stream.next(); | ||
109 | |||
110 | if (ch == "\"") { | ||
111 | state.mode = "string"; | ||
112 | returnType = STRING; | ||
113 | |||
114 | } else if (ch == "'") { | ||
115 | returnType = ATOM; | ||
116 | } else if (ch == '#') { | ||
117 | if (stream.eat("|")) { // Multi-line comment | ||
118 | state.mode = "comment"; // toggle to comment mode | ||
119 | returnType = COMMENT; | ||
120 | } else if (stream.eat(/[tf]/)) { // #t/#f (atom) | ||
121 | returnType = ATOM; | ||
122 | } else if (stream.eat(';')) { // S-Expr comment | ||
123 | state.mode = "s-expr-comment"; | ||
124 | returnType = COMMENT; | ||
125 | } | ||
126 | |||
127 | } else if (ch == ";") { // comment | ||
128 | stream.skipToEnd(); // rest of the line is a comment | ||
129 | 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 == "[") { | ||
141 | var keyWord = ''; var indentTemp = stream.column(); | ||
142 | /** | ||
143 | Either | ||
144 | (indent-word .. | ||
145 | (non-indent-word .. | ||
146 | (;something else, bracket, etc. | ||
147 | */ | ||
148 | |||
149 | while ((letter = stream.eat(/[^\s\(\[\;\)\]]/)) != null) { |