github.com/elliott5/community@v0.14.1-0.20160709191136-823126fb026a/app/public/codemirror/mode/commonlisp/commonlisp.js (about)

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