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