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