2
0

scheme.js 13 KB

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