-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroovy.c
461 lines (435 loc) · 14.5 KB
/
groovy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
* Groovy mode for QEmacs.
*
* Copyright (c) 2015-2017 Charlie Gordon.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
static const char groovy_keywords[] = {
/* language specific keywords */
"as|def|in|trait|"
/* documented java keywords */
"assert|break|case|catch|class|const|continue|"
"default|do|else|enum|extends|final|finally|for|goto|"
"if|implements|import|instanceof|interface|new|"
"package|return|super|switch|"
"this|throw|throws|try|while|"
/* boolean and null literals */
"false|null|true|"
/* other java keywords */
"abstract|native|private|protected|public|static|strictfp|"
"synchronized|threadsafe|transient|volatile|"
};
static const char groovy_types[] = {
"void|boolean|byte|char|short|int|long|double|float|"
};
enum {
IN_GROOVY_COMMENT = 0x01,
IN_GROOVY_STRING = 0x02,
IN_GROOVY_STRING2 = 0x04,
IN_GROOVY_LONG_STRING = 0x08,
IN_GROOVY_LONG_STRING2 = 0x10,
IN_GROOVY_DOLLAR_STRING = 0x20,
};
enum {
GROOVY_STYLE_TEXT = QE_STYLE_DEFAULT,
GROOVY_STYLE_PREPROCESS = QE_STYLE_PREPROCESS,
GROOVY_STYLE_COMMENT = QE_STYLE_COMMENT,
GROOVY_STYLE_STRING = QE_STYLE_STRING,
GROOVY_STYLE_DOLLAR_STRING = QE_STYLE_STRING,
GROOVY_STYLE_REGEX = QE_STYLE_STRING_Q,
GROOVY_STYLE_NUMBER = QE_STYLE_NUMBER,
GROOVY_STYLE_KEYWORD = QE_STYLE_KEYWORD,
GROOVY_STYLE_TYPE = QE_STYLE_TYPE,
GROOVY_STYLE_FUNCTION = QE_STYLE_FUNCTION,
GROOVY_STYLE_ERROR = QE_STYLE_ERROR,
};
static int qe_is_groovy_letter(int c) {
return qe_isalpha_(c) ||
(qe_inrange(c, 0x00C0, 0xFFFE) && c != 0x00D7 && c != 0x00F7);
}
static int java_scan_number(unsigned int *str0, int flavor)
{
unsigned int *str = str0;
int c = *str++;
int octal = 0, nonoctal = 0, isfloat = 0;
/* Number types:
* binary integers: 0[bB][01]([01_]*[01])?[gliGLI]?
* octal integers: 0([0-7_]*[0-7])?[gliGLI]?
* hex integers: 0[xX][0-9a-zA-Z]([0-9a-zA-Z_]*[0-9a-zA-Z])?
* [gliGLI]?
* decimal integers: [1-9]([0-9_]*[0-9])?[gliGLI]?
* decimal floats: [0-9]([0-9_]*[0-9])?
* [.]([0-9]([0-9_]*[0-9])?)?
* ([eE][-+]?[0-9]([0-9_]*[0-9])?)?[dfDF]?
* [.][0-9]([0-9_]*[0-9])?
* ([eE][-+]?[0-9]([0-9_]*[0-9])?)?[dfDF]?
* [0-9]([0-9_]*[0-9])?
* [eE][-+]?[0-9]([0-9_]*[0-9])?[dfDF]?
* [0-9]([0-9_]*[0-9])?[dfDF]
* hex floats: 0[xX][0-9a-zA-Z]([0-9a-zA-Z_]*[0-9a-zA-Z])?
* ([.]([0-9a-zA-Z]([0-9a-zA-Z_]*[0-9a-zA-Z])?)?)?
* [pP][-+]?[0-9]([0-9_]*[0-9])?[dfDF]?
* 0[xX][.][0-9a-zA-Z]([0-9a-zA-Z_]*[0-9a-zA-Z])?
* [pP][-+]?[0-9]([0-9_]*[0-9])?[dfDF]?
*
* Groovy requires a digit after the decimal point.
*
* This scanner is relaxed to allow for partial numbers at end of
* line to avoid showing errors as numbers are typed.
*/
if (c == '0') {
if (qe_match2(*str, 'b', 'B')) {
/* binary numbers */
str++;
if (!*str) goto done;
if (!qe_isbindigit(*str)) goto error;
for (str += 1; qe_isbindigit_(*str); str++)
continue;
if (!*str) goto done;
if (str[-1] == '_') goto error;
if (qe_findchar("gliGLI", *str))
str++;
goto done;
}
if (qe_match2(*str, 'x', 'X')) {
/* hexadecimal numbers */
str++;
if (!*str) goto done;
if (*str != '.') {
if (!qe_isxdigit(*str)) goto error;
for (str += 1; qe_isxdigit_(*str); str++)
continue;
if (!*str) goto done;
if (str[-1] == '_') goto error;
if (qe_findchar("gliGLI", *str)) {
str++;
goto done;
}
}
if (qe_findchar(".pP", *str)) {
isfloat = 1;
if (*str == '.') {
if (str == str0 + 2 && !qe_isxdigit(str[1])) goto error;
if (flavor == CLANG_GROOVY && !qe_isxdigit(str[1])) goto done;
for (str += 1; qe_isxdigit_(*str); str++)
continue;
}
if (!*str) goto done;
if (!qe_match2(*str, 'p', 'P')) goto error;
str++;
if (qe_match2(*str, '+', '-'))
str++;
if (!*str) goto done;
if (!qe_isdigit(*str)) goto error;
for (str += 1; qe_isdigit_(*str); str++)
continue;
if (str[-1] == '_') goto error;
}
if (qe_findchar("dfDF", *str))
str++;
goto done;
}
octal = 1;
} else
if (c == '.')
str--;
/* decimal and octal numbers */
for (; qe_isdigit_(*str); str++) {
nonoctal |= qe_match2(*str, '8', '9');
}
if (!*str) goto done;
if (str[-1] == '_') goto error;
if (*str == '.') {
if (str == str0 && !qe_isdigit(str[1])) goto done;
if (flavor == CLANG_GROOVY && !qe_isdigit(str[1])) goto done;
str++;
isfloat = 1;
if (!*str) goto done;
if (qe_isdigit(*str)) {
for (str += 1; qe_isdigit_(*str); str++)
continue;
if (!*str) goto done;
if (str[-1] == '_') goto error;
}
}
if (qe_match2(*str, 'e', 'E')) {
str++;
isfloat = 1;
if (qe_match2(*str, '+', '-'))
str++;
if (!*str) goto done;
if (!qe_isdigit(*str)) goto error;
for (str += 1; qe_isdigit_(*str); str++)
continue;
if (!*str) goto done;
if (str[-1] == '_') goto error;
}
if (qe_findchar("dfDF", *str)) {
str++;
isfloat = 1;
goto done;
}
if (!*str) goto done;
if (!isfloat) {
if (octal && nonoctal) goto error;
if (qe_findchar("gliGLI", *str)) {
str++;
goto done;
}
}
done:
if (!qe_isalnum_(*str)) {
return str - str0;
}
error:
while (qe_isalnum_(*str))
str++;
return -(str - str0);
}
static void groovy_colorize_line(QEColorizeContext *cp,
unsigned int *str, int n, ModeDef *syn)
{
int i = 0, start = i, c, style, sep = 0, klen, haslower;
int state = cp->colorize_state;
char kbuf[64];
/* all these states are exclusive */
if (state & IN_GROOVY_COMMENT) {
goto parse_comment;
}
if (state & IN_GROOVY_STRING) {
sep = '\'';
goto parse_string;
}
if (state & IN_GROOVY_STRING2) {
sep = '\"';
goto parse_string;
}
if (state & IN_GROOVY_LONG_STRING) {
sep = '\'';
goto parse_long_string;
}
if (state & IN_GROOVY_LONG_STRING2) {
sep = '\"';
goto parse_long_string;
}
if (state & IN_GROOVY_DOLLAR_STRING) {
goto parse_dollar_string;
}
style = 0;
while (i < n) {
start = i;
c = str[i++];
switch (c) {
case '#':
if (start == 0 && str[i] == '!') {
/* shebang line (should do this generically) */
i = n;
style = GROOVY_STYLE_PREPROCESS;
break;
}
continue;
case '~':
while (qe_isblank(str[i]))
i++;
if (str[i] == '/') {
/* parse slashy string as regex */
sep = '/';
start = i++;
goto parse_string;
}
continue;
case '/':
if (str[i] == '*') {
/* normal comment */
i++;
parse_comment:
state |= IN_GROOVY_COMMENT;
for (; i < n; i++) {
if (str[i] == '*' && str[i + 1] == '/') {
i += 2;
state &= ~IN_GROOVY_COMMENT;
break;
}
}
style = GROOVY_STYLE_COMMENT;
break;
} else
if (str[i] == '/') {
/* line comment */
i = n;
style = GROOVY_STYLE_COMMENT;
break;
}
/* XXX: should handle slashy strings */
continue;
case '\'':
case '\"':
/* parse string const */
i--;
sep = str[i++];
/* XXX: should colorize interpolated strings */
if (str[i] == (unsigned int)sep && str[i + 1] == (unsigned int)sep) {
/* long string */
state |= (sep == '\"') ? IN_GROOVY_LONG_STRING2 :
IN_GROOVY_LONG_STRING;
i += 2;
parse_long_string:
while (i < n) {
c = str[i++];
if (c == '\\') {
if (i < n) {
i += 1;
}
} else
if (c == sep && str[i] == (unsigned int)sep && str[i + 1] == (unsigned int)sep) {
i += 2;
state &= (sep == '\"') ? ~IN_GROOVY_LONG_STRING2 :
~IN_GROOVY_LONG_STRING;
break;
}
}
} else {
state |= (sep == '\"') ? IN_GROOVY_STRING2 : IN_GROOVY_STRING;
parse_string:
while (i < n) {
c = str[i++];
if (c == '\\') {
if (i < n) {
i += 1;
}
} else
if (c == sep) {
state &= (sep == '\"') ? ~IN_GROOVY_STRING2 : ~IN_GROOVY_STRING;
break;
}
}
}
style = GROOVY_STYLE_STRING;
break;
case '$':
if (str[i] == '/') {
/* $ slashy string */
i++;
state |= IN_GROOVY_DOLLAR_STRING;
parse_dollar_string:
while (i < n) {
c = str[i++];
if (c == '$') {
if (i < n) {
i += 1;
}
} else
if (c == '/' && str[i] == '$') {
state &= ~IN_GROOVY_DOLLAR_STRING;
i += 2;
break;
}
}
style = GROOVY_STYLE_DOLLAR_STRING;
break;
}
goto hasname;
case '@':
if (qe_isalpha(str[i])) {
while (qe_isalnum_(str[i]) || qe_is_groovy_letter(str[i]) || str[i] == '.')
i++;
if (start == 0 || str[start - 1] != '.')
style = GROOVY_STYLE_PREPROCESS;
break;
}
continue;
case '.':
if (!qe_isdigit(str[i]))
continue;
/* fallthru */
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
i--;
klen = java_scan_number(str + i, CLANG_GROOVY);
if (klen > 0) {
i += klen;
style = GROOVY_STYLE_NUMBER;
break;
} else
if (klen < 0) {
/* Malformed number constants */
i -= klen;
style = GROOVY_STYLE_ERROR;
break;
}
i++;
continue;
default:
if (qe_is_groovy_letter(c)) {
hasname:
haslower = 0;
klen = 0;
kbuf[klen++] = c;
for (; qe_isalnum_(str[i]) || qe_is_groovy_letter(str[i]); i++) {
haslower |= qe_islower(str[i]);
if (klen < countof(kbuf) - 1)
kbuf[klen++] = str[i];
}
kbuf[klen] = '\0';
/* keywords are not recognised after '.',
* nor before a single '.' nor a map key indicator ':' */
if ((start == 0 || str[start - 1] != '.')
&& (str[i] != '.' || str[i + 1] == '.')
&& str[i] != ':') {
if ((qe_isupper(c) && haslower && !check_fcall(str, i))
|| strfind(syn->types, kbuf)) {
style = GROOVY_STYLE_TYPE;
break;
}
if (strfind(syn->keywords, kbuf)) {
style = GROOVY_STYLE_KEYWORD;
break;
}
}
if (check_fcall(str, i)) {
style = GROOVY_STYLE_FUNCTION;
break;
}
continue;
}
continue;
}
if (style) {
SET_COLOR(str, start, i, style);
style = 0;
}
}
/* set style on eol char */
SET_COLOR1(str, n, style);
cp->colorize_state = state;
}
static ModeDef groovy_mode = {
.name = "Groovy",
.extensions = "groovy|gradle",
.shell_handlers = "groovy",
.colorize_func = groovy_colorize_line,
.colorize_flags = CLANG_GROOVY,
.keywords = groovy_keywords,
.types = groovy_types,
.indent_func = c_indent_line,
.auto_indent = 1,
.fallback = &c_mode,
};
static int groovy_init(void)
{
qe_register_mode(&groovy_mode, MODEF_SYNTAX);
return 0;
}