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.

commonlisp.js 4.5KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("commonlisp", function (config) {
  13. var specialForm = /^(block|let*|return-from|catch|load-time-value|setq|eval-when|locally|symbol-macrolet|flet|macrolet|tagbody|function|multiple-value-call|the|go|multiple-value-prog1|throw|if|progn|unwind-protect|labels|progv|let|quote)$/;
  14. var assumeBody = /^with|^def|^do|^prog|case$|^cond$|bind$|when$|unless$/;
  15. var numLiteral = /^(?:[+\-]?(?:\d+|\d*\.\d+)(?:[efd][+\-]?\d+)?|[+\-]?\d+(?:\/[+\-]?\d+)?|#b[+\-]?[01]+|#o[+\-]?[0-7]+|#x[+\-]?[\da-f]+)/;
  16. var symbol = /[^\s'`,@()\[\]";]/;
  17. var type;
  18. function readSym(stream) {
  19. var ch;
  20. while (ch = stream.next()) {
  21. if (ch == "\\") stream.next();
  22. else if (!symbol.test(ch)) { stream.backUp(1); break; }
  23. }
  24. return stream.current();
  25. }
  26. function base(stream, state) {
  27. if (stream.eatSpace()) {type = "ws"; return null;}
  28. if (stream.match(numLiteral)) return "number";
  29. var ch = stream.next();
  30. if (ch == "\\") ch = stream.next();
  31. if (ch == '"') return (state.tokenize = inString)(stream, state);
  32. else if (ch == "(") { type = "open"; return "bracket"; }
  33. else if (ch == ")" || ch == "]") { type = "close"; return "bracket"; }
  34. else if (ch == ";") { stream.skipToEnd(); type = "ws"; return "comment"; }
  35. else if (/['`,@]/.test(ch)) return null;
  36. else if (ch == "|") {
  37. if (stream.skipTo("|")) { stream.next(); return "symbol"; }
  38. else { stream.skipToEnd(); return "error"; }
  39. } else if (ch == "#") {
  40. var ch = stream.next();
  41. if (ch == "(") { type = "open"; return "bracket"; }
  42. else if (/[+\-=\.']/.test(ch)) return null;
  43. else if (/\d/.test(ch) && stream.match(/^\d*#/)) return null;
  44. else if (ch == "|") return (state.tokenize = inComment)(stream, state);
  45. else if (ch == ":") { readSym(stream); return "meta"; }
  46. else if (ch == "\\") { stream.next(); readSym(stream); return "string-2" }
  47. else return "error";
  48. } else {
  49. var name = readSym(stream);
  50. if (name == ".") return null;
  51. type = "symbol";
  52. if (name == "nil" || name == "t" || name.charAt(0) == ":") return "atom";
  53. if (state.lastType == "open" && (specialForm.test(name) || assumeBody.test(name))) return "keyword";
  54. if (name.charAt(0) == "&") return "variable-2";
  55. return "variable";
  56. }
  57. }
  58. function inString(stream, state) {
  59. var escaped = false, next;
  60. while (next = stream.next()) {
  61. if (next == '"' && !escaped) { state.tokenize = base; break; }
  62. escaped = !escaped && next == "\\";
  63. }
  64. return "string";
  65. }
  66. function inComment(stream, state) {
  67. var next, last;
  68. while (next = stream.next()) {
  69. if (next == "#" && last == "|") { state.tokenize = base; break; }
  70. last = next;
  71. }
  72. type = "ws";
  73. return "comment";
  74. }
  75. return {
  76. startState: function () {
  77. return {ctx: {prev: null, start: 0, indentTo: 0}, lastType: null, tokenize: base};
  78. },
  79. token: function (stream, state) {
  80. if (stream.sol() && typeof state.ctx.indentTo != "number")
  81. state.ctx.indentTo = state.ctx.start + 1;
  82. type = null;
  83. var style = state.tokenize(stream, state);
  84. if (type != "ws") {
  85. if (state.ctx.indentTo == null) {
  86. if (type == "symbol" && assumeBody.test(stream.current()))
  87. state.ctx.indentTo = state.ctx.start + config.indentUnit;
  88. else
  89. state.ctx.indentTo = "next";
  90. } else if (state.ctx.indentTo == "next") {
  91. state.ctx.indentTo = stream.column();
  92. }
  93. state.lastType = type;
  94. }
  95. if (type == "open") state.ctx = {prev: state.ctx, start: stream.column(), indentTo: null};
  96. else if (type == "close") state.ctx = state.ctx.prev || state.ctx;
  97. return style;
  98. },
  99. indent: function (state, _textAfter) {
  100. var i = state.ctx.indentTo;
  101. return typeof i == "number" ? i : state.ctx.start + 1;
  102. },
  103. closeBrackets: {pairs: "()[]{}\"\""},
  104. lineComment: ";;",
  105. fold: "brace-paren",
  106. blockCommentStart: "#|",
  107. blockCommentEnd: "|#"
  108. };
  109. });
  110. CodeMirror.defineMIME("text/x-common-lisp", "commonlisp");
  111. });