scheme.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. /**
  4. * Author: Koh Zi Han, based on implementation by Koh Zi Chun
  5. */
  6. (function(mod) {
  7. if (typeof exports == "object" && typeof module == "object") // CommonJS
  8. mod(require("../../lib/codemirror"));
  9. else if (typeof define == "function" && define.amd) // AMD
  10. define(["../../lib/codemirror"], mod);
  11. else // Plain browser env
  12. mod(CodeMirror);
  13. })(function(CodeMirror) {
  14. "use strict";
  15. CodeMirror.defineMode("scheme", function () {
  16. var BUILTIN = "builtin", COMMENT = "comment", STRING = "string",
  17. ATOM = "atom", NUMBER = "number", BRACKET = "bracket";
  18. var INDENT_WORD_SKIP = 2;
  19. function makeKeywords(str) {
  20. var obj = {}, words = str.split(" ");
  21. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  22. return obj;
  23. }
  24. 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?");
  25. var indentKeys = makeKeywords("define let letrec let* lambda");
  26. function stateStack(indent, type, prev) { // represents a state stack object
  27. this.indent = indent;
  28. this.type = type;
  29. this.prev = prev;
  30. }
  31. function pushStack(state, indent, type) {
  32. state.indentStack = new stateStack(indent, type, state.indentStack);
  33. }
  34. function popStack(state) {
  35. state.indentStack = state.indentStack.prev;
  36. }
  37. var binaryMatcher = new RegExp(/^(?:[-+]i|[-+][01]+#*(?:\/[01]+#*)?i|[-+]?[01]+#*(?:\/[01]+#*)?@[-+]?[01]+#*(?:\/[01]+#*)?|[-+]?[01]+#*(?:\/[01]+#*)?[-+](?:[01]+#*(?:\/[01]+#*)?)?i|[-+]?[01]+#*(?:\/[01]+#*)?)(?=[()\s;"]|$)/i);
  38. 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);
  39. 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);
  40. 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);
  41. function isBinaryNumber (stream) {
  42. return stream.match(binaryMatcher);
  43. }
  44. function isOctalNumber (stream) {
  45. return stream.match(octalMatcher);
  46. }
  47. function isDecimalNumber (stream, backup) {
  48. if (backup === true) {
  49. stream.backUp(1);
  50. }
  51. return stream.match(decimalMatcher);
  52. }
  53. function isHexNumber (stream) {
  54. return stream.match(hexMatcher);
  55. }
  56. return {
  57. startState: function () {
  58. return {
  59. indentStack: null,
  60. indentation: 0,
  61. mode: false,
  62. sExprComment: false
  63. };
  64. },
  65. token: function (stream, state) {
  66. if (state.indentStack == null && stream.sol()) {
  67. // update indentation, but only if indentStack is empty
  68. state.indentation = stream.indentation();
  69. }
  70. // skip spaces
  71. if (stream.eatSpace()) {
  72. return null;
  73. }
  74. var returnType = null;
  75. switch(state.mode){
  76. case "string": // multi-line string parsing mode
  77. var next, escaped = false;
  78. while ((next = stream.next()) != null) {
  79. if (next == "\"" && !escaped) {
  80. state.mode = false;
  81. break;
  82. }
  83. escaped = !escaped && next == "\\";
  84. }
  85. returnType = STRING; // continue on in scheme-string mode
  86. break;
  87. case "comment": // comment parsing mode
  88. var next, maybeEnd = false;
  89. while ((next = stream.next()) != null) {
  90. if (next == "#" && maybeEnd) {
  91. state.mode = false;
  92. break;
  93. }
  94. maybeEnd = (next == "|");
  95. }
  96. returnType = COMMENT;
  97. break;
  98. case "s-expr-comment": // s-expr commenting mode
  99. state.mode = false;
  100. if(stream.peek() == "(" || stream.peek() == "["){
  101. // actually start scheme s-expr commenting mode
  102. state.sExprComment = 0;
  103. }else{
  104. // if not we just comment the entire of the next token
  105. stream.eatWhile(/[^/s]/); // eat non spaces
  106. returnType = COMMENT;
  107. break;
  108. }
  109. default: // default parsing mode
  110. var ch = stream.next();
  111. if (ch == "\"") {
  112. state.mode = "string";
  113. returnType = STRING;
  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]/i)) { // #t/#f (atom)
  121. returnType = ATOM;
  122. } else if (stream.eat(';')) { // S-Expr comment
  123. state.mode = "s-expr-comment";
  124. returnType = COMMENT;
  125. } else {
  126. var numTest = null, hasExactness = false, hasRadix = true;
  127. if (stream.eat(/[ei]/i)) {
  128. hasExactness = true;
  129. } else {
  130. stream.backUp(1); // must be radix specifier
  131. }
  132. if (stream.match(/^#b/i)) {
  133. numTest = isBinaryNumber;
  134. } else if (stream.match(/^#o/i)) {
  135. numTest = isOctalNumber;
  136. } else if (stream.match(/^#x/i)) {
  137. numTest = isHexNumber;
  138. } else if (stream.match(/^#d/i)) {
  139. numTest = isDecimalNumber;
  140. } else if (stream.match(/^[-+0-9.]/, false)) {
  141. hasRadix = false;
  142. numTest = isDecimalNumber;
  143. // re-consume the intial # if all matches failed
  144. } else if (!hasExactness) {
  145. stream.eat('#');
  146. }
  147. if (numTest != null) {
  148. if (hasRadix && !hasExactness) {
  149. // consume optional exactness after radix
  150. stream.match(/^#[ei]/i);
  151. }
  152. if (numTest(stream))
  153. returnType = NUMBER;
  154. }
  155. }
  156. } else if (/^[-+0-9.]/.test(ch) && isDecimalNumber(stream, true)) { // match non-prefixed number, must be decimal
  157. returnType = NUMBER;
  158. } else if (ch == ";") { // comment
  159. stream.skipToEnd(); // rest of the line is a comment
  160. returnType = COMMENT;
  161. } else if (ch == "(" || ch == "[") {
  162. var keyWord = ''; var indentTemp = stream.column(), letter;
  163. /**
  164. Either
  165. (indent-word ..
  166. (non-indent-word ..
  167. (;something else, bracket, etc.
  168. */
  169. while ((letter = stream.eat(/[^\s\(\[\;\)\]]/)) != null) {
  170. keyWord += letter;
  171. }
  172. if (keyWord.length > 0 && indentKeys.propertyIsEnumerable(keyWord)) { // indent-word
  173. pushStack(state, indentTemp + INDENT_WORD_SKIP, ch);
  174. } else { // non-indent word
  175. // we continue eating the spaces
  176. stream.eatSpace();
  177. if (stream.eol() || stream.peek() == ";") {
  178. // nothing significant after
  179. // we restart indentation 1 space after
  180. pushStack(state, indentTemp + 1, ch);
  181. } else {
  182. pushStack(state, indentTemp + stream.current().length, ch); // else we match
  183. }
  184. }
  185. stream.backUp(stream.current().length - 1); // undo all the eating
  186. if(typeof state.sExprComment == "number") state.sExprComment++;
  187. returnType = BRACKET;
  188. } else if (ch == ")" || ch == "]") {
  189. returnType = BRACKET;
  190. if (state.indentStack != null && state.indentStack.type == (ch == ")" ? "(" : "[")) {
  191. popStack(state);
  192. if(typeof state.sExprComment == "number"){
  193. if(--state.sExprComment == 0){
  194. returnType = COMMENT; // final closing bracket
  195. state.sExprComment = false; // turn off s-expr commenting mode
  196. }
  197. }
  198. }
  199. } else {
  200. stream.eatWhile(/[\w\$_\-!$%&*+\.\/:<=>?@\^~]/);
  201. if (keywords && keywords.propertyIsEnumerable(stream.current())) {
  202. returnType = BUILTIN;
  203. } else returnType = "variable";
  204. }
  205. }
  206. return (typeof state.sExprComment == "number") ? COMMENT : returnType;
  207. },
  208. indent: function (state) {
  209. if (state.indentStack == null) return state.indentation;
  210. return state.indentStack.indent;
  211. },
  212. lineComment: ";;"
  213. };
  214. });
  215. CodeMirror.defineMIME("text/x-scheme", "scheme");
  216. });