-
Notifications
You must be signed in to change notification settings - Fork 4
/
YamlLexer.g4
514 lines (418 loc) · 12.5 KB
/
YamlLexer.g4
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
lexer grammar YamlLexer;
// All comments that start with "///" are copy-pasted from
// The Python Language Reference: https://docs.python.org/3.3/reference/grammar.html
tokens { INDENT, DEDENT }
@lexer::members {
/**
* A queue where extra tokens are pushed on (see the NEWLINE lexer rule for instance).
* Tokens are returned from the queue first. Once the queue is empty, lexer goes back to input stream for producing tokens.
*/
private java.util.LinkedList<Token> tokens = new java.util.LinkedList<>();
// The stack that keeps track of the indentation level.
private java.util.Stack<Integer> indents = new java.util.Stack<>();
// The amount of opened braces, brackets and parenthesis.
private int opened = 0;
// The most recently produced token.
private Token lastToken = null;
/**
* Add a token to the queue. Tokens are returned from the queue first, before new tokens are extracted from input stream.
*/
public void schedule(Token t) {
tokens.offer(t);
}
@Override
public Token nextToken() {
if (tokens.isEmpty()) {
Token next = super.nextToken();
if (next.getType() == EOF) {
processEOF_NextToken();
next = tokens.poll();
} else if(next.getType() == NEWLINE) {
processNEWLINE_NextToken();
next = tokens.poll();
}
if (lastToken != null && lastToken.getType() == MINUS) {
switch(next.getType()) {
case MINUS:
next = commonToken(YamlParser.NEWLINE, "\n");
createAndScheduleIndent(this._tokenStartCharPositionInLine);
schedule(commonToken(YamlParser.MINUS, "-"));
break;
case STRING_MY:
int indent = this._tokenStartCharPositionInLine;
Token afterNext = super.nextToken();
if(afterNext.getType() == COLON) {
createAndScheduleIndent(indent); //2 - after next (INDENT)
schedule(next); //3 - last (string)
next = commonToken(YamlParser.NEWLINE, "\n"); //1 - next (NEWLINE)
}
if (afterNext.getType() == EOF) {
processEOF_NextToken();
} else if(afterNext.getType() == NEWLINE) {
processNEWLINE_NextToken();
}
else {
schedule(afterNext);
}
break;
}
}
this.lastToken = next;
} else {
this.lastToken = tokens.poll();
}
return this.lastToken;
}
private void processEOF_NextToken() {
schedule(commonToken(YamlParser.NEWLINE, "\n"));
if (!this.indents.isEmpty()) {
// Now emit as much DEDENT tokens as needed.
while (!indents.isEmpty()) {
this.schedule(createDedent());
indents.pop();
}
}
// Put the EOF back on the token stream.
this.schedule(commonToken(YamlParser.EOF, "<EOF>"));
}
private void processNEWLINE_NextToken() {
//String newLine = getText().replaceAll("[^\r\n]+", "");
String spaces = getText().replaceAll("[\r\n]+", "");
schedule(commonToken(NEWLINE, "\n"));
int indent = getIndentationCount(spaces);
int previous = indents.isEmpty() ? 0 : indents.peek();
if (indent == previous) {
// skip indents of the same size as the present indent-size
//skip();
}
else if (indent > previous) {
indents.push(indent);
schedule(commonToken(YamlParser.INDENT, spaces));
}
else {
// Possibly emit more than 1 DEDENT token.
while(!indents.isEmpty() && indents.peek() > indent) {
this.schedule(createDedent());
indents.pop();
}
}
}
private Token createDedent() {
CommonToken dedent = commonToken(YamlParser.DEDENT, "");
dedent.setLine(this.lastToken.getLine());
return dedent;
}
private CommonToken commonToken(int type, String text) {
int stop = this.getCharIndex() - 1;
int start = text.isEmpty() ? stop : stop - text.length() + 1;
return new CommonToken(this._tokenFactorySourcePair, type, DEFAULT_TOKEN_CHANNEL, start, stop);
}
/**
* Create a Indent token if given indentation level is greater and schedule it in 'tokens' queue.
* @param indent indentation level
*/
private void createAndScheduleIndent(int indent) {
int previous = indents.isEmpty() ? 0 : indents.peek();
if (indent > previous) {
indents.push(indent);
schedule(commonToken(YamlParser.INDENT, "-"));
}
}
// Calculates the indentation of the provided spaces, taking the
// following rules into account:
//
// "Tabs are replaced (from left to right) by one to eight spaces
// such that the total number of characters up to and including
// the replacement is a multiple of eight [...]"
//
// -- https://docs.python.org/3.1/reference/lexical_analysis.html#indentation
static int getIndentationCount(String spaces) {
int count = 0;
for (char ch : spaces.toCharArray()) {
switch (ch) {
case '\t':
count += 8 - (count % 8);
break;
default:
// A normal space char.
count++;
}
}
return count;
}
boolean atStartOfInput() {
return super.getCharPositionInLine() == 0 && super.getLine() == 1;
}
/**
* Indentation of a string literal. '-1' means the value is not set.
*/
private int string_literal_start = -1;
}
/*
* lexer rules
*/
NEWLINE
: ( {atStartOfInput()}? SPACES
| ( '\r'? '\n' | '\r' ) SPACES?
)
{
int next = _input.LA(1);
if (opened > 0 || next == '\r' || next == '\n' || next == '#') {
// If we're inside a list or on a blank line, ignore all indents,
// dedents and line breaks.
skip();
}
}
;
/// bytesliteral ::= bytesprefix(shortbytes | longbytes)
/// bytesprefix ::= "b" | "B" | "br" | "Br" | "bR" | "BR"
BYTES_LITERAL
: [bB] [rR]? ( SHORT_BYTES | LONG_BYTES )
;
/// decimalinteger ::= nonzerodigit digit* | "0"+
DECIMAL_INTEGER
: NON_ZERO_DIGIT DIGIT*
| '0'+
;
/// octinteger ::= "0" ("o" | "O") octdigit+
OCT_INTEGER
: '0' [oO] OCT_DIGIT+
;
/// hexinteger ::= "0" ("x" | "X") hexdigit+
HEX_INTEGER
: '0' [xX] HEX_DIGIT+
;
/// bininteger ::= "0" ("b" | "B") bindigit+
BIN_INTEGER
: '0' [bB] BIN_DIGIT+
;
/// floatnumber ::= pointfloat | exponentfloat
FLOAT_NUMBER
: POINT_FLOAT
| EXPONENT_FLOAT
;
/// imagnumber ::= (floatnumber | intpart) ("j" | "J")
IMAG_NUMBER
: ( FLOAT_NUMBER | INT_PART ) [jJ]
;
MINUS: '-';
DOCUMENTSTART: { super.getCharPositionInLine() == 0 }? '---';
DOCUMENTEND: { super.getCharPositionInLine() == 0 }? '...';
AMPERSAND : '&';
STAR : '*';
OPEN_PAREN : '(' {opened++;};
CLOSE_PAREN : ')' {opened--;};
COMMA : ',';
COLON : ':';
OPEN_BRACK : '[' {opened++;} -> pushMode(FLOW);
CLOSE_BRACK : ']' {opened--;};
OPEN_BRACE : '{' {opened++;} -> pushMode(FLOW);
CLOSE_BRACE : '}' {opened--;};
LITERIAL_STR_IND: '|' {string_literal_start=-1;} -> pushMode(LITERAL_STRING);
FOLD_STR_IND: '>' {string_literal_start=-1;} -> pushMode(LITERAL_STRING);
DOUBLE_QUOTE: '"' -> pushMode(DOUBLE_QUOTE_STR);
ANCHOR
: AMPERSAND NAME
;
ALIAS
: STAR NAME
;
fragment NAME
: [A-Za-z0-9]+
;
STRING_MY
: STRING_MY_START (~(' '|'\r'|'\n'|'"'|':') | (':' ~[ \r\n]) | (' '+ ~[ :#\r\n]) )*
;
fragment STRING_MY_START
: ~('-'|' '|'\r'|'\n'|'"'|':'|'#'|'['|'{'|'&'|'*'|'|') | (':' ~[ \r\n]) | ('-' ~[ \-\r\n]) | {_input.LA(3) != 45}? '--' | {super.getCharPositionInLine() != 0}? '---'
;
SKIP1
: ( SPACES | COMMENT | LINE_JOINING ) -> skip
;
UNKNOWN_CHAR
: .
;
/*
* fragments
*/
/// nonzerodigit ::= "1"..."9"
fragment NON_ZERO_DIGIT
: [1-9]
;
/// digit ::= "0"..."9"
fragment DIGIT
: [0-9]
;
/// octdigit ::= "0"..."7"
fragment OCT_DIGIT
: [0-7]
;
/// hexdigit ::= digit | "a"..."f" | "A"..."F"
fragment HEX_DIGIT
: [0-9a-fA-F]
;
/// bindigit ::= "0" | "1"
fragment BIN_DIGIT
: [01]
;
/// pointfloat ::= [intpart] fraction | intpart "."
fragment POINT_FLOAT
: INT_PART? FRACTION
| INT_PART '.'
;
/// exponentfloat ::= (intpart | pointfloat) exponent
fragment EXPONENT_FLOAT
: ( INT_PART | POINT_FLOAT ) EXPONENT
;
/// intpart ::= digit+
fragment INT_PART
: DIGIT+
;
/// fraction ::= "." digit+
fragment FRACTION
: '.' DIGIT+
;
/// exponent ::= ("e" | "E") ["+" | "-"] digit+
fragment EXPONENT
: [eE] [+-]? DIGIT+
;
/// shortbytes ::= "'" shortbytesitem* "'" | '"' shortbytesitem* '"'
/// shortbytesitem ::= shortbyteschar | bytesescapeseq
fragment SHORT_BYTES
: '\'' ( SHORT_BYTES_CHAR_NO_SINGLE_QUOTE | BYTES_ESCAPE_SEQ )* '\''
| '"' ( SHORT_BYTES_CHAR_NO_DOUBLE_QUOTE | BYTES_ESCAPE_SEQ )* '"'
;
/// longbytes ::= "'''" longbytesitem* "'''" | '"""' longbytesitem* '"""'
fragment LONG_BYTES
: '\'\'\'' LONG_BYTES_ITEM*? '\'\'\''
| '"""' LONG_BYTES_ITEM*? '"""'
;
/// longbytesitem ::= longbyteschar | bytesescapeseq
fragment LONG_BYTES_ITEM
: LONG_BYTES_CHAR
| BYTES_ESCAPE_SEQ
;
/// shortbyteschar ::= <any ASCII character except "\" or newline or the quote>
fragment SHORT_BYTES_CHAR_NO_SINGLE_QUOTE
: [\u0000-\u0009]
| [\u000B-\u000C]
| [\u000E-\u0026]
| [\u0028-\u005B]
| [\u005D-\u007F]
;
fragment SHORT_BYTES_CHAR_NO_DOUBLE_QUOTE
: [\u0000-\u0009]
| [\u000B-\u000C]
| [\u000E-\u0021]
| [\u0023-\u005B]
| [\u005D-\u007F]
;
/// longbyteschar ::= <any ASCII character except "\">
fragment LONG_BYTES_CHAR
: [\u0000-\u005B]
| [\u005D-\u007F]
;
/// bytesescapeseq ::= "\" <any ASCII character>
fragment BYTES_ESCAPE_SEQ
: '\\' [\u0000-\u007F]
;
fragment SPACES
: [ \t]+
;
fragment COMMENT
: '#' ~[\r\n]*
;
fragment LINE_JOINING
: '\\' SPACES? ( '\r'? '\n' | '\r' )
;
// MODE CHANGE
mode FLOW;
// decimalinteger ::= nonzerodigit digit* | "0"+
DECIMAL_INTEGER2
: (NON_ZERO_DIGIT DIGIT*
| '0'+) -> type(DECIMAL_INTEGER)
;
/// octinteger ::= "0" ("o" | "O") octdigit+
OCT_INTEGER2
: '0' [oO] OCT_DIGIT+ -> type(OCT_INTEGER)
;
/// hexinteger ::= "0" ("x" | "X") hexdigit+
HEX_INTEGER2
: '0' [xX] HEX_DIGIT+ -> type(HEX_INTEGER)
;
/// bininteger ::= "0" ("b" | "B") bindigit+
BIN_INTEGER2
: '0' [bB] BIN_DIGIT+ -> type(BIN_INTEGER)
;
/// floatnumber ::= pointfloat | exponentfloat
FLOAT_NUMBER2
: (POINT_FLOAT
| EXPONENT_FLOAT) -> type(FLOAT_NUMBER)
;
/// imagnumber ::= (floatnumber | intpart) ("j" | "J")
IMAG_NUMBER2
: ( FLOAT_NUMBER | INT_PART ) [jJ] -> type(IMAG_NUMBER)
;
STRING_MY_2
: STRING_MY_START_2 (~(' '|'\r'|'\n'|'"'|':'|'['|']'|','|'{'|'}') | (':' ~[ \r\n]) | (' '+ ~(' '|':'|'#'|'\r'|'\n'|'['|']'|','|'{'|'}')) )* -> type(STRING_MY)
;
fragment STRING_MY_START_2
: ~('-'|' '|'\r'|'\n'|'"'|':'|'#'|'['|']'|','|'{'|'}') | (':' ~[ \r\n]) | ('-' ~[ \r\n])
;
COMMA2
: ',' -> type(COMMA)
;
COLON2
: ':' -> type(COLON)
;
SKIP2
: ( SPACES | COMMENT | LINE_JOINING | ( '\r'? '\n' | '\r' )) -> skip
;
CLOSE_BRACK2
: ']' -> type(CLOSE_BRACK), popMode
;
CLOSE_BRACE2
: '}' -> type(CLOSE_BRACE), popMode
;
// MODE CHANGE
mode LITERAL_STRING;
NEWLINE_STR_LITERAL: ( '\r'? '\n' | '\r' ) [ \t]*
{
int next = _input.LA(1);
if(!(opened > 0 || next == '\r' || next == '\n' || next == '#')) {
int indent = indents.isEmpty() ? 0 : indents.peek();
int space_count = getIndentationCount(getText().replaceAll("[\r\n]+", ""));
if(space_count <= indent) {
popMode();
setType(NEWLINE);
}
else if (string_literal_start == -1) {
string_literal_start = space_count;
}
}
}
;
//shouldn't do ->type(NEWLINE) to avoid NEWLINE post processing
STRING_MY_3
: ~('\r'|'\n')*
{
int extraSpace = _tokenStartCharPositionInLine - string_literal_start;
if(extraSpace > 0) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < extraSpace; i++) {
builder.append(' ');
}
builder.append(getText());
setText(builder.toString());
}
}
-> type(STRING_MY)
;
// MODE CHANGE
mode DOUBLE_QUOTE_STR;
STRING_MY_4
: (~["\r\n\\] | '\\\\')*
-> type(STRING_MY)
;
NEWLINE_STR_QUOTE: ( '\r'? '\n' | '\r' ) [ \t]* ;
DOUBLE_QUOTE2: '"' -> type(DOUBLE_QUOTE), popMode;