commonlisp.js 4.0 KB

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