Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50665 views
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("groovy", function(config) {
15
function words(str) {
16
var obj = {}, words = str.split(" ");
17
for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
18
return obj;
19
}
20
var keywords = words(
21
"abstract as assert boolean break byte case catch char class const continue def default " +
22
"do double else enum extends final finally float for goto if implements import in " +
23
"instanceof int interface long native new package private protected public return " +
24
"short static strictfp super switch synchronized threadsafe throw throws transient " +
25
"try void volatile while");
26
var blockKeywords = words("catch class do else finally for if switch try while enum interface def");
27
var atoms = words("null true false this");
28
29
var curPunc;
30
function tokenBase(stream, state) {
31
var ch = stream.next();
32
if (ch == '"' || ch == "'") {
33
return startString(ch, stream, state);
34
}
35
if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
36
curPunc = ch;
37
return null;
38
}
39
if (/\d/.test(ch)) {
40
stream.eatWhile(/[\w\.]/);
41
if (stream.eat(/eE/)) { stream.eat(/\+\-/); stream.eatWhile(/\d/); }
42
return "number";
43
}
44
if (ch == "/") {
45
if (stream.eat("*")) {
46
state.tokenize.push(tokenComment);
47
return tokenComment(stream, state);
48
}
49
if (stream.eat("/")) {
50
stream.skipToEnd();
51
return "comment";
52
}
53
if (expectExpression(state.lastToken)) {
54
return startString(ch, stream, state);
55
}
56
}
57
if (ch == "-" && stream.eat(">")) {
58
curPunc = "->";
59
return null;
60
}
61
if (/[+\-*&%=<>!?|\/~]/.test(ch)) {
62
stream.eatWhile(/[+\-*&%=<>|~]/);
63
return "operator";
64
}
65
stream.eatWhile(/[\w\$_]/);
66
if (ch == "@") { stream.eatWhile(/[\w\$_\.]/); return "meta"; }
67
if (state.lastToken == ".") return "property";
68
if (stream.eat(":")) { curPunc = "proplabel"; return "property"; }
69
var cur = stream.current();
70
if (atoms.propertyIsEnumerable(cur)) { return "atom"; }
71
if (keywords.propertyIsEnumerable(cur)) {
72
if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
73
return "keyword";
74
}
75
return "variable";
76
}
77
tokenBase.isBase = true;
78
79
function startString(quote, stream, state) {
80
var tripleQuoted = false;
81
if (quote != "/" && stream.eat(quote)) {
82
if (stream.eat(quote)) tripleQuoted = true;
83
else return "string";
84
}
85
function t(stream, state) {
86
var escaped = false, next, end = !tripleQuoted;
87
while ((next = stream.next()) != null) {
88
if (next == quote && !escaped) {
89
if (!tripleQuoted) { break; }
90
if (stream.match(quote + quote)) { end = true; break; }
91
}
92
if (quote == '"' && next == "$" && !escaped && stream.eat("{")) {
93
state.tokenize.push(tokenBaseUntilBrace());
94
return "string";
95
}
96
escaped = !escaped && next == "\\";
97
}
98
if (end) state.tokenize.pop();
99
return "string";
100
}
101
state.tokenize.push(t);
102
return t(stream, state);
103
}
104
105
function tokenBaseUntilBrace() {
106
var depth = 1;
107
function t(stream, state) {
108
if (stream.peek() == "}") {
109
depth--;
110
if (depth == 0) {
111
state.tokenize.pop();
112
return state.tokenize[state.tokenize.length-1](stream, state);
113
}
114
} else if (stream.peek() == "{") {
115
depth++;
116
}
117
return tokenBase(stream, state);
118
}
119
t.isBase = true;
120
return t;
121
}
122
123
function tokenComment(stream, state) {
124
var maybeEnd = false, ch;
125
while (ch = stream.next()) {
126
if (ch == "/" && maybeEnd) {
127
state.tokenize.pop();
128
break;
129
}
130
maybeEnd = (ch == "*");
131
}
132
return "comment";
133
}
134
135
function expectExpression(last) {
136
return !last || last == "operator" || last == "->" || /[\.\[\{\(,;:]/.test(last) ||
137
last == "newstatement" || last == "keyword" || last == "proplabel";
138
}
139
140
function Context(indented, column, type, align, prev) {
141
this.indented = indented;
142
this.column = column;
143
this.type = type;
144
this.align = align;
145
this.prev = prev;
146
}
147
function pushContext(state, col, type) {
148
return state.context = new Context(state.indented, col, type, null, state.context);
149
}
150
function popContext(state) {
151
var t = state.context.type;
152
if (t == ")" || t == "]" || t == "}")
153
state.indented = state.context.indented;
154
return state.context = state.context.prev;
155
}
156
157
// Interface
158
159
return {
160
startState: function(basecolumn) {
161
return {
162
tokenize: [tokenBase],
163
context: new Context((basecolumn || 0) - config.indentUnit, 0, "top", false),
164
indented: 0,
165
startOfLine: true,
166
lastToken: null
167
};
168
},
169
170
token: function(stream, state) {
171
var ctx = state.context;
172
if (stream.sol()) {
173
if (ctx.align == null) ctx.align = false;
174
state.indented = stream.indentation();
175
state.startOfLine = true;
176
// Automatic semicolon insertion
177
if (ctx.type == "statement" && !expectExpression(state.lastToken)) {
178
popContext(state); ctx = state.context;
179
}
180
}
181
if (stream.eatSpace()) return null;
182
curPunc = null;
183
var style = state.tokenize[state.tokenize.length-1](stream, state);
184
if (style == "comment") return style;
185
if (ctx.align == null) ctx.align = true;
186
187
if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state);
188
// Handle indentation for {x -> \n ... }
189
else if (curPunc == "->" && ctx.type == "statement" && ctx.prev.type == "}") {
190
popContext(state);
191
state.context.align = false;
192
}
193
else if (curPunc == "{") pushContext(state, stream.column(), "}");
194
else if (curPunc == "[") pushContext(state, stream.column(), "]");
195
else if (curPunc == "(") pushContext(state, stream.column(), ")");
196
else if (curPunc == "}") {
197
while (ctx.type == "statement") ctx = popContext(state);
198
if (ctx.type == "}") ctx = popContext(state);
199
while (ctx.type == "statement") ctx = popContext(state);
200
}
201
else if (curPunc == ctx.type) popContext(state);
202
else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" && curPunc == "newstatement"))
203
pushContext(state, stream.column(), "statement");
204
state.startOfLine = false;
205
state.lastToken = curPunc || style;
206
return style;
207
},
208
209
indent: function(state, textAfter) {
210
if (!state.tokenize[state.tokenize.length-1].isBase) return 0;
211
var firstChar = textAfter && textAfter.charAt(0), ctx = state.context;
212
if (ctx.type == "statement" && !expectExpression(state.lastToken)) ctx = ctx.prev;
213
var closing = firstChar == ctx.type;
214
if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : config.indentUnit);
215
else if (ctx.align) return ctx.column + (closing ? 0 : 1);
216
else return ctx.indented + (closing ? 0 : config.indentUnit);
217
},
218
219
electricChars: "{}",
220
fold: "brace"
221
};
222
});
223
224
CodeMirror.defineMIME("text/x-groovy", "groovy");
225
226
});
227
228