all repos — NoPaste @ 29b774f090102303e43cf939b38ac2083e62d9f1

Resurrected - The PussTheCat.org fork of NoPaste

scripts/CodeMirror/mode/commonlisp/commonlisp.js (view raw)

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