commonlisp.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. CodeMirror.defineMode("commonlisp", function (config) {
  2. var assumeBody = /^with|^def|^do|^prog|case$|^cond$|bind$|when$|unless$/;
  3. var numLiteral = /^(?:[+\-]?(?:\d+|\d*\.\d+)(?:[efd][+\-]?\d+)?|[+\-]?\d+(?:\/[+\-]?\d+)?|#b[+\-]?[01]+|#o[+\-]?[0-7]+|#x[+\-]?[\da-f]+)/;
  4. var symbol = /[^\s'`,@()\[\]";]/;
  5. var type;
  6. function readSym(stream) {
  7. var ch;
  8. while (ch = stream.next()) {
  9. if (ch == "\\") stream.next();
  10. else if (!symbol.test(ch)) { stream.backUp(1); break; }
  11. }
  12. return stream.current();
  13. }
  14. function base(stream, state) {
  15. if (stream.eatSpace()) {type = "ws"; return null;}
  16. if (stream.match(numLiteral)) return "number";
  17. var ch = stream.next();
  18. if (ch == "\\") ch = stream.next();
  19. if (ch == '"') return (state.tokenize = inString)(stream, state);
  20. else if (ch == "(") { type = "open"; return "bracket"; }
  21. else if (ch == ")" || ch == "]") { type = "close"; return "bracket"; }
  22. else if (ch == ";") { stream.skipToEnd(); type = "ws"; return "comment"; }
  23. else if (/['`,@]/.test(ch)) return null;
  24. else if (ch == "|") {
  25. if (stream.skipTo("|")) { stream.next(); return "symbol"; }
  26. else { stream.skipToEnd(); return "error"; }
  27. } else if (ch == "#") {
  28. var ch = stream.next();
  29. if (ch == "[") { type = "open"; return "bracket"; }
  30. else if (/[+\-=\.']/.test(ch)) return null;
  31. else if (/\d/.test(ch) && stream.match(/^\d*#/)) return null;
  32. else if (ch == "|") return (state.tokenize = inComment)(stream, state);
  33. else if (ch == ":") { readSym(stream); return "meta"; }
  34. else return "error";
  35. } else {
  36. var name = readSym(stream);
  37. if (name == ".") return null;
  38. type = "symbol";
  39. if (name == "nil" || name == "t") return "atom";
  40. if (name.charAt(0) == ":") return "keyword";
  41. if (name.charAt(0) == "&") return "variable-2";
  42. return "variable";
  43. }
  44. }
  45. function inString(stream, state) {
  46. var escaped = false, next;
  47. while (next = stream.next()) {
  48. if (next == '"' && !escaped) { state.tokenize = base; break; }
  49. escaped = !escaped && next == "\\";
  50. }
  51. return "string";
  52. }
  53. function inComment(stream, state) {
  54. var next, last;
  55. while (next = stream.next()) {
  56. if (next == "#" && last == "|") { state.tokenize = base; break; }
  57. last = next;
  58. }
  59. type = "ws";
  60. return "comment";
  61. }
  62. return {
  63. startState: function () {
  64. return {ctx: {prev: null, start: 0, indentTo: 0}, tokenize: base};
  65. },
  66. token: function (stream, state) {
  67. if (stream.sol() && typeof state.ctx.indentTo != "number")
  68. state.ctx.indentTo = state.ctx.start + 1;
  69. type = null;
  70. var style = state.tokenize(stream, state);
  71. if (type != "ws") {
  72. if (state.ctx.indentTo == null) {
  73. if (type == "symbol" && assumeBody.test(stream.current()))
  74. state.ctx.indentTo = state.ctx.start + config.indentUnit;
  75. else
  76. state.ctx.indentTo = "next";
  77. } else if (state.ctx.indentTo == "next") {
  78. state.ctx.indentTo = stream.column();
  79. }
  80. }
  81. if (type == "open") state.ctx = {prev: state.ctx, start: stream.column(), indentTo: null};
  82. else if (type == "close") state.ctx = state.ctx.prev || state.ctx;
  83. return style;
  84. },
  85. indent: function (state, _textAfter) {
  86. var i = state.ctx.indentTo;
  87. return typeof i == "number" ? i : state.ctx.start + 1;
  88. },
  89. lineComment: ";;",
  90. blockCommentStart: "#|",
  91. blockCommentEnd: "|#"
  92. };
  93. });
  94. CodeMirror.defineMIME("text/x-common-lisp", "commonlisp");