commonlisp.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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 return "error";
  47. } else {
  48. var name = readSym(stream);
  49. if (name == ".") return null;
  50. type = "symbol";
  51. if (name == "nil" || name == "t" || name.charAt(0) == ":") return "atom";
  52. if (state.lastType == "open" && (specialForm.test(name) || assumeBody.test(name))) return "keyword";
  53. if (name.charAt(0) == "&") return "variable-2";
  54. return "variable";
  55. }
  56. }
  57. function inString(stream, state) {
  58. var escaped = false, next;
  59. while (next = stream.next()) {
  60. if (next == '"' && !escaped) { state.tokenize = base; break; }
  61. escaped = !escaped && next == "\\";
  62. }
  63. return "string";
  64. }
  65. function inComment(stream, state) {
  66. var next, last;
  67. while (next = stream.next()) {
  68. if (next == "#" && last == "|") { state.tokenize = base; break; }
  69. last = next;
  70. }
  71. type = "ws";
  72. return "comment";
  73. }
  74. return {
  75. startState: function () {
  76. return {ctx: {prev: null, start: 0, indentTo: 0}, lastType: null, tokenize: base};
  77. },
  78. token: function (stream, state) {
  79. if (stream.sol() && typeof state.ctx.indentTo != "number")
  80. state.ctx.indentTo = state.ctx.start + 1;
  81. type = null;
  82. var style = state.tokenize(stream, state);
  83. if (type != "ws") {
  84. if (state.ctx.indentTo == null) {
  85. if (type == "symbol" && assumeBody.test(stream.current()))
  86. state.ctx.indentTo = state.ctx.start + config.indentUnit;
  87. else
  88. state.ctx.indentTo = "next";
  89. } else if (state.ctx.indentTo == "next") {
  90. state.ctx.indentTo = stream.column();
  91. }
  92. state.lastType = type;
  93. }
  94. if (type == "open") state.ctx = {prev: state.ctx, start: stream.column(), indentTo: null};
  95. else if (type == "close") state.ctx = state.ctx.prev || state.ctx;
  96. return style;
  97. },
  98. indent: function (state, _textAfter) {
  99. var i = state.ctx.indentTo;
  100. return typeof i == "number" ? i : state.ctx.start + 1;
  101. },
  102. lineComment: ";;",
  103. blockCommentStart: "#|",
  104. blockCommentEnd: "|#"
  105. };
  106. });
  107. CodeMirror.defineMIME("text/x-common-lisp", "commonlisp");
  108. });