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