Debian Codegraph @

codemirror-legacy-modes/mode/commonlisp.js

raw
1 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)$/;
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 == ")") { 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 "variableName"; }
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 if (ch == "\\") { stream.next(); readSym(stream); return "string.special" }
38 else return "error";
39 } else {
40 var name = readSym(stream);
41 if (name == ".") return null;
42 type = "symbol";
43 if (name == "nil" || name == "t" || name.charAt(0) == ":") return "atom";
44 if (state.lastType == "open" && (specialForm.test(name) || assumeBody.test(name))) return "keyword";
45 if (name.charAt(0) == "&") return "variableName.special";
46 return "variableName";
47 }
48 }
49
50 function inString(stream, state) {
51 var escaped = false, next;
52 while (next = stream.next()) {
53 if (next == '"' && !escaped) { state.tokenize = base; break; }
54 escaped = !escaped && next == "\\";
55 }
56 return "string";
57 }
58
59 function inComment(stream, state) {
60 var next, last;
61 while (next = stream.next()) {
62 if (next == "#" && last == "|") { state.tokenize = base; break; }
63 last = next;
64 }
65 type = "ws";
66 return "comment";
67 }
68
69 export const commonLisp = {
70 name: "commonlisp",
71 startState: function () {
72 return {ctx: {prev: null, start: 0, indentTo: 0}, lastType: null, tokenize: base};
73 },
74
75 token: function (stream, state) {
76 if (stream.sol() && typeof state.ctx.indentTo != "number")
77 state.ctx.indentTo = state.ctx.start + 1;
78
79 type = null;
80 var style = state.tokenize(stream, state);
81 if (type != "ws") {
82 if (state.ctx.indentTo == null) {
83 if (type == "symbol" && assumeBody.test(stream.current()))
84 state.ctx.indentTo = state.ctx.start + stream.indentUnit;
85 else
86 state.ctx.indentTo = "next";
87 } else if (state.ctx.indentTo == "next") {
88 state.ctx.indentTo = stream.column();
89 }
90 state.lastType = type;
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
97 indent: function (state) {
98 var i = state.ctx.indentTo;
99 return typeof i == "number" ? i : state.ctx.start + 1;
100 },
101
102 languageData: {
103 commentTokens: {line: ";;", block: {open: "#|", close: "|#"}},
104 closeBrackets: {brackets: ["(", "[", "{", '"']}
105 }
106 };
107