scheme.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. 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. 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 indentKeys = makeKeywords("define let letrec let* lambda begin");
  16. function stateStack(indent, type, prev) { // represents a state stack object
  17. this.indent = indent;
  18. this.type = type;
  19. this.prev = prev;
  20. }
  21. function pushStack(state, indent, type) {
  22. state.indentStack = new stateStack(indent, type, state.indentStack);
  23. }
  24. function popStack(state) {
  25. state.indentStack = state.indentStack.prev;
  26. }
  27. return {
  28. startState: function () {
  29. return {
  30. indentStack: null,
  31. indentation: 0,
  32. mode: false,
  33. sExprComment: false
  34. };
  35. },
  36. token: function (stream, state) {
  37. if (state.indentStack == null && stream.sol()) {
  38. // update indentation, but only if indentStack is empty
  39. state.indentation = stream.indentation();
  40. }
  41. // skip spaces
  42. if (stream.eatSpace()) {
  43. return null;
  44. }
  45. var returnType = null;
  46. switch(state.mode){
  47. case "string": // multi-line string parsing mode
  48. var next, escaped = false;
  49. while ((next = stream.next()) != null) {
  50. if (next == "\"" && !escaped) {
  51. state.mode = false;
  52. break;
  53. }
  54. escaped = !escaped && next == "\\";
  55. }
  56. returnType = STRING; // continue on in scheme-string mode
  57. break;
  58. case "comment": // comment parsing mode
  59. var next, maybeEnd = false;
  60. while ((next = stream.next()) != null) {
  61. if (next == "#" && maybeEnd) {
  62. state.mode = false;
  63. break;
  64. }
  65. maybeEnd = (next == "|");
  66. }
  67. returnType = COMMENT;
  68. break;
  69. case "s-expr-comment": // s-expr commenting mode
  70. state.mode = false;
  71. if(stream.peek() == "(" || stream.peek() == "["){
  72. // actually start scheme s-expr commenting mode
  73. state.sExprComment = 0;
  74. }else{
  75. // if not we just comment the entire of the next token
  76. stream.eatWhile(/[^/s]/); // eat non spaces
  77. returnType = COMMENT;
  78. break;
  79. }
  80. default: // default parsing mode
  81. var ch = stream.next();
  82. if (ch == "\"") {
  83. state.mode = "string";
  84. returnType = STRING;
  85. } else if (ch == "'") {
  86. returnType = ATOM;
  87. } else if (ch == '#') {
  88. if (stream.eat("|")) { // Multi-line comment
  89. state.mode = "comment"; // toggle to comment mode
  90. returnType = COMMENT;
  91. } else if (stream.eat(/[tf]/)) { // #t/#f (atom)
  92. returnType = ATOM;
  93. } else if (stream.eat(';')) { // S-Expr comment
  94. state.mode = "s-expr-comment";
  95. returnType = COMMENT;
  96. }
  97. } else if (ch == ";") { // comment
  98. stream.skipToEnd(); // rest of the line is a comment
  99. returnType = COMMENT;
  100. } else if (numRegex.exec(ch) != null) { // numbers
  101. returnType = NUMBER;
  102. } else if (ch == "(" || ch == "[") {
  103. var keyWord = ''; var indentTemp = stream.column();
  104. /**
  105. Either
  106. (indent-word ..
  107. (non-indent-word ..
  108. (;something else, bracket, etc.
  109. */
  110. while ((letter = stream.eat(/[^\s\(\[\;\)\]]/)) != null) {
  111. keyWord += letter;
  112. }
  113. if (keyWord.length > 0 && indentKeys.propertyIsEnumerable(keyWord)) { // indent-word
  114. pushStack(state, indentTemp + INDENT_WORD_SKIP, ch);
  115. } else { // non-indent word
  116. // we continue eating the spaces
  117. stream.eatSpace();
  118. if (stream.eol() || stream.peek() == ";") {
  119. // nothing significant after
  120. // we restart indentation 1 space after
  121. pushStack(state, indentTemp + 1, ch);
  122. } else {
  123. pushStack(state, indentTemp + stream.current().length, ch); // else we match
  124. }
  125. }
  126. stream.backUp(stream.current().length - 1); // undo all the eating
  127. if(typeof state.sExprComment == "number") state.sExprComment++;
  128. returnType = BRACKET;
  129. } else if (ch == ")" || ch == "]") {
  130. returnType = BRACKET;
  131. if (state.indentStack != null && state.indentStack.type == (ch == ")" ? "(" : "[")) {
  132. popStack(state);
  133. if(typeof state.sExprComment == "number"){
  134. if(--state.sExprComment == 0){
  135. returnType = COMMENT; // final closing bracket
  136. state.sExprComment = false; // turn off s-expr commenting mode
  137. }
  138. }
  139. }
  140. } else {
  141. stream.eatWhile(/[\w\$_]/);
  142. if (keywords && keywords.propertyIsEnumerable(stream.current())) {
  143. returnType = BUILTIN;
  144. } else returnType = null;
  145. }
  146. }
  147. return (typeof state.sExprComment == "number") ? COMMENT : returnType;
  148. },
  149. indent: function (state, textAfter) {
  150. if (state.indentStack == null) return state.indentation;
  151. return state.indentStack.indent;
  152. }
  153. };
  154. });
  155. CodeMirror.defineMIME("text/x-scheme", "css");