本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
14KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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. sExprQuote: false
  64. };
  65. },
  66. token: function (stream, state) {
  67. if (state.indentStack == null && stream.sol()) {
  68. // update indentation, but only if indentStack is empty
  69. state.indentation = stream.indentation();
  70. }
  71. // skip spaces
  72. if (stream.eatSpace()) {
  73. return null;
  74. }
  75. var returnType = null;
  76. switch(state.mode){
  77. case "string": // multi-line string parsing mode
  78. var next, escaped = false;
  79. while ((next = stream.next()) != null) {
  80. if (next == "\"" && !escaped) {
  81. state.mode = false;
  82. break;
  83. }
  84. escaped = !escaped && next == "\\";
  85. }
  86. returnType = STRING; // continue on in scheme-string mode
  87. break;
  88. case "comment": // comment parsing mode
  89. var next, maybeEnd = false;
  90. while ((next = stream.next()) != null) {
  91. if (next == "#" && maybeEnd) {
  92. state.mode = false;
  93. break;
  94. }
  95. maybeEnd = (next == "|");
  96. }
  97. returnType = COMMENT;
  98. break;
  99. case "s-expr-comment": // s-expr commenting mode
  100. state.mode = false;
  101. if(stream.peek() == "(" || stream.peek() == "["){
  102. // actually start scheme s-expr commenting mode
  103. state.sExprComment = 0;
  104. }else{
  105. // if not we just comment the entire of the next token
  106. stream.eatWhile(/[^\s\(\)\[\]]/); // eat symbol atom
  107. returnType = COMMENT;
  108. break;
  109. }
  110. default: // default parsing mode
  111. var ch = stream.next();
  112. if (ch == "\"") {
  113. state.mode = "string";
  114. returnType = STRING;
  115. } else if (ch == "'") {
  116. if (stream.peek() == "(" || stream.peek() == "["){
  117. if (typeof state.sExprQuote != "number") {
  118. state.sExprQuote = 0;
  119. } // else already in a quoted expression
  120. returnType = ATOM;
  121. } else {
  122. stream.eatWhile(/[\w_\-!$%&*+\.\/:<=>?@\^~]/);
  123. returnType = ATOM;
  124. }
  125. } else if (ch == '#') {
  126. if (stream.eat("|")) { // Multi-line comment
  127. state.mode = "comment"; // toggle to comment mode
  128. returnType = COMMENT;
  129. } else if (stream.eat(/[tf]/i)) { // #t/#f (atom)
  130. returnType = ATOM;
  131. } else if (stream.eat(';')) { // S-Expr comment
  132. state.mode = "s-expr-comment";
  133. returnType = COMMENT;
  134. } else {
  135. var numTest = null, hasExactness = false, hasRadix = true;
  136. if (stream.eat(/[ei]/i)) {
  137. hasExactness = true;
  138. } else {
  139. stream.backUp(1); // must be radix specifier
  140. }
  141. if (stream.match(/^#b/i)) {
  142. numTest = isBinaryNumber;
  143. } else if (stream.match(/^#o/i)) {
  144. numTest = isOctalNumber;
  145. } else if (stream.match(/^#x/i)) {
  146. numTest = isHexNumber;
  147. } else if (stream.match(/^#d/i)) {
  148. numTest = isDecimalNumber;
  149. } else if (stream.match(/^[-+0-9.]/, false)) {
  150. hasRadix = false;
  151. numTest = isDecimalNumber;
  152. // re-consume the intial # if all matches failed
  153. } else if (!hasExactness) {
  154. stream.eat('#');
  155. }
  156. if (numTest != null) {
  157. if (hasRadix && !hasExactness) {
  158. // consume optional exactness after radix
  159. stream.match(/^#[ei]/i);
  160. }
  161. if (numTest(stream))
  162. returnType = NUMBER;
  163. }
  164. }
  165. } else if (/^[-+0-9.]/.test(ch) && isDecimalNumber(stream, true)) { // match non-prefixed number, must be decimal
  166. returnType = NUMBER;
  167. } else if (ch == ";") { // comment
  168. stream.skipToEnd(); // rest of the line is a comment
  169. returnType = COMMENT;
  170. } else if (ch == "(" || ch == "[") {
  171. var keyWord = ''; var indentTemp = stream.column(), letter;
  172. /**
  173. Either
  174. (indent-word ..
  175. (non-indent-word ..
  176. (;something else, bracket, etc.
  177. */
  178. while ((letter = stream.eat(/[^\s\(\[\;\)\]]/)) != null) {
  179. keyWord += letter;
  180. }
  181. if (keyWord.length > 0 && indentKeys.propertyIsEnumerable(keyWord)) { // indent-word
  182. pushStack(state, indentTemp + INDENT_WORD_SKIP, ch);
  183. } else { // non-indent word
  184. // we continue eating the spaces
  185. stream.eatSpace();
  186. if (stream.eol() || stream.peek() == ";") {
  187. // nothing significant after
  188. // we restart indentation 1 space after
  189. pushStack(state, indentTemp + 1, ch);
  190. } else {
  191. pushStack(state, indentTemp + stream.current().length, ch); // else we match
  192. }
  193. }
  194. stream.backUp(stream.current().length - 1); // undo all the eating
  195. if(typeof state.sExprComment == "number") state.sExprComment++;
  196. if(typeof state.sExprQuote == "number") state.sExprQuote++;
  197. returnType = BRACKET;
  198. } else if (ch == ")" || ch == "]") {
  199. returnType = BRACKET;
  200. if (state.indentStack != null && state.indentStack.type == (ch == ")" ? "(" : "[")) {
  201. popStack(state);
  202. if(typeof state.sExprComment == "number"){
  203. if(--state.sExprComment == 0){
  204. returnType = COMMENT; // final closing bracket
  205. state.sExprComment = false; // turn off s-expr commenting mode
  206. }
  207. }
  208. if(typeof state.sExprQuote == "number"){
  209. if(--state.sExprQuote == 0){
  210. returnType = ATOM; // final closing bracket
  211. state.sExprQuote = false; // turn off s-expr quote mode
  212. }
  213. }
  214. }
  215. } else {
  216. stream.eatWhile(/[\w_\-!$%&*+\.\/:<=>?@\^~]/);
  217. if (keywords && keywords.propertyIsEnumerable(stream.current())) {
  218. returnType = BUILTIN;
  219. } else returnType = "variable";
  220. }
  221. }
  222. return (typeof state.sExprComment == "number") ? COMMENT : ((typeof state.sExprQuote == "number") ? ATOM : returnType);
  223. },
  224. indent: function (state) {
  225. if (state.indentStack == null) return state.indentation;
  226. return state.indentStack.indent;
  227. },
  228. closeBrackets: {pairs: "()[]{}\"\""},
  229. lineComment: ";;"
  230. };
  231. });
  232. CodeMirror.defineMIME("text/x-scheme", "scheme");
  233. });
上海开阖软件有限公司 沪ICP备12045867号-1