-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.js
768 lines (713 loc) · 19.7 KB
/
input.js
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/*eslint-disable*/
// ../core/src/files/_abstract_file.ts
var AbstractFile = class {
constructor(filename) {
this.filename = filename;
}
getFilename() {
return this.filename;
}
baseName() {
let name = this.getFilename();
let index = name.lastIndexOf("\\");
if (index) {
index = index + 1;
}
name = name.substring(index);
index = name.lastIndexOf("/");
if (index) {
index = index + 1;
}
return name.substring(index);
}
getObjectType() {
const split = this.baseName().split(".");
return split[1]?.toUpperCase();
}
getObjectName() {
const split = this.baseName().split(".");
split[0] = split[0].replace(/%23/g, "#");
split[0] = split[0].replace(/%3e/g, ">");
split[0] = split[0].replace(/%3c/g, "<");
split[0] = split[0].toUpperCase().replace(/#/g, "/");
split[0] = split[0].replace("(", "/");
split[0] = split[0].replace(")", "/");
return split[0];
}
};
// ../core/src/files/memory_file.ts
var MemoryFile = class extends AbstractFile {
constructor(filename, raw) {
super(filename);
this.raw = raw;
}
getRaw() {
return this.raw;
}
getRawRows() {
return this.raw.split("\n");
}
};
// ../core/src/position.ts
var Position = class {
constructor(row, col) {
this.row = row;
this.col = col;
}
getCol() {
return this.col;
}
getRow() {
return this.row;
}
isAfter(p) {
return this.row > p.row || this.row === p.row && this.col >= p.col;
}
equals(p) {
return this.row === p.getRow() && this.col === p.getCol();
}
isBefore(p) {
return this.row < p.row || this.row === p.row && this.col < p.col;
}
isBetween(p1, p2) {
return this.isAfter(p1) && this.isBefore(p2);
}
};
// ../core/src/virtual_position.ts
var VirtualPosition = class _VirtualPosition extends Position {
constructor(virtual, row, col) {
super(virtual.getRow(), virtual.getCol());
this.vrow = row;
this.vcol = col;
}
equals(p) {
if (!(p instanceof _VirtualPosition)) {
return false;
}
const casted = p;
return super.equals(this) && this.vrow === casted.vrow && this.vcol === casted.vcol;
}
};
// ../core/src/abap/1_lexer/tokens/abstract_token.ts
var AbstractToken = class {
constructor(start, str) {
this.start = start;
this.str = str;
}
// special function, for debugging purposes, see https://github.com/abaplint/abaplint/pull/3137
[Symbol.for("debug.description")]() {
return `${this.constructor.name} ${this.str}`;
}
getStr() {
return this.str;
}
getRow() {
return this.start.getRow();
}
getCol() {
return this.start.getCol();
}
getStart() {
return this.start;
}
getEnd() {
return new Position(this.start.getRow(), this.start.getCol() + this.str.length);
}
};
// ../core/src/abap/1_lexer/tokens/at.ts
var At = class extends AbstractToken {
static railroad() {
return "@";
}
};
// ../core/src/abap/1_lexer/tokens/atw.ts
var AtW = class extends AbstractToken {
static railroad() {
return "@ ";
}
};
// ../core/src/abap/1_lexer/tokens/wat.ts
var WAt = class extends AbstractToken {
static railroad() {
return " @";
}
};
// ../core/src/abap/1_lexer/tokens/watw.ts
var WAtW = class extends AbstractToken {
static railroad() {
return " @ ";
}
};
// ../core/src/abap/1_lexer/tokens/bracket_left.ts
var BracketLeft = class extends AbstractToken {
static railroad() {
return "[";
}
};
// ../core/src/abap/1_lexer/tokens/wbracket_left.ts
var WBracketLeft = class extends AbstractToken {
static railroad() {
return " [";
}
};
// ../core/src/abap/1_lexer/tokens/bracket_leftw.ts
var BracketLeftW = class extends AbstractToken {
static railroad() {
return "[ ";
}
};
// ../core/src/abap/1_lexer/tokens/wbracket_leftw.ts
var WBracketLeftW = class extends AbstractToken {
static railroad() {
return " [ ";
}
};
// ../core/src/abap/1_lexer/tokens/bracket_right.ts
var BracketRight = class extends AbstractToken {
static railroad() {
return "]";
}
};
// ../core/src/abap/1_lexer/tokens/wbracket_right.ts
var WBracketRight = class extends AbstractToken {
static railroad() {
return " ]";
}
};
// ../core/src/abap/1_lexer/tokens/bracket_rightw.ts
var BracketRightW = class extends AbstractToken {
static railroad() {
return "] ";
}
};
// ../core/src/abap/1_lexer/tokens/wbracket_rightw.ts
var WBracketRightW = class extends AbstractToken {
static railroad() {
return " ] ";
}
};
// ../core/src/abap/1_lexer/tokens/instance_arrow.ts
var InstanceArrow = class extends AbstractToken {
static railroad() {
return "->";
}
};
// ../core/src/abap/1_lexer/tokens/winstance_arrow.ts
var WInstanceArrow = class extends AbstractToken {
static railroad() {
return " ->";
}
};
// ../core/src/abap/1_lexer/tokens/instance_arroww.ts
var InstanceArrowW = class extends AbstractToken {
static railroad() {
return "-> ";
}
};
// ../core/src/abap/1_lexer/tokens/winstance_arroww.ts
var WInstanceArrowW = class extends AbstractToken {
static railroad() {
return " -> ";
}
};
// ../core/src/abap/1_lexer/tokens/paren_left.ts
var ParenLeft = class extends AbstractToken {
static railroad() {
return "(";
}
};
// ../core/src/abap/1_lexer/tokens/wparen_left.ts
var WParenLeft = class extends AbstractToken {
static railroad() {
return " (";
}
};
// ../core/src/abap/1_lexer/tokens/paren_leftw.ts
var ParenLeftW = class extends AbstractToken {
static railroad() {
return "( ";
}
};
// ../core/src/abap/1_lexer/tokens/wparen_leftw.ts
var WParenLeftW = class extends AbstractToken {
static railroad() {
return " ( ";
}
};
// ../core/src/abap/1_lexer/tokens/paren_right.ts
var ParenRight = class extends AbstractToken {
static railroad() {
return ")";
}
};
// ../core/src/abap/1_lexer/tokens/wparen_right.ts
var WParenRight = class extends AbstractToken {
static railroad() {
return " )";
}
};
// ../core/src/abap/1_lexer/tokens/paren_rightw.ts
var ParenRightW = class extends AbstractToken {
static railroad() {
return ") ";
}
};
// ../core/src/abap/1_lexer/tokens/wparen_rightw.ts
var WParenRightW = class extends AbstractToken {
static railroad() {
return " ) ";
}
};
// ../core/src/abap/1_lexer/tokens/dash.ts
var Dash = class extends AbstractToken {
static railroad() {
return "-";
}
};
// ../core/src/abap/1_lexer/tokens/wdash.ts
var WDash = class extends AbstractToken {
static railroad() {
return " -";
}
};
// ../core/src/abap/1_lexer/tokens/dashw.ts
var DashW = class extends AbstractToken {
static railroad() {
return "- ";
}
};
// ../core/src/abap/1_lexer/tokens/wdashw.ts
var WDashW = class extends AbstractToken {
static railroad() {
return " - ";
}
};
// ../core/src/abap/1_lexer/tokens/plus.ts
var Plus = class extends AbstractToken {
static railroad() {
return "+";
}
};
// ../core/src/abap/1_lexer/tokens/wplus.ts
var WPlus = class extends AbstractToken {
static railroad() {
return " +";
}
};
// ../core/src/abap/1_lexer/tokens/plusw.ts
var PlusW = class extends AbstractToken {
static railroad() {
return "+ ";
}
};
// ../core/src/abap/1_lexer/tokens/wplusw.ts
var WPlusW = class extends AbstractToken {
static railroad() {
return " + ";
}
};
// ../core/src/abap/1_lexer/tokens/static_arrow.ts
var StaticArrow = class extends AbstractToken {
static railroad() {
return "=>";
}
};
// ../core/src/abap/1_lexer/tokens/wstatic_arrow.ts
var WStaticArrow = class extends AbstractToken {
static railroad() {
return " =>";
}
};
// ../core/src/abap/1_lexer/tokens/static_arroww.ts
var StaticArrowW = class extends AbstractToken {
static railroad() {
return "=> ";
}
};
// ../core/src/abap/1_lexer/tokens/wstatic_arroww.ts
var WStaticArrowW = class extends AbstractToken {
static railroad() {
return " => ";
}
};
// ../core/src/abap/1_lexer/tokens/string.ts
var StringToken = class extends AbstractToken {
};
// ../core/src/abap/1_lexer/tokens/string_template.ts
var StringTemplate = class extends AbstractToken {
};
// ../core/src/abap/1_lexer/tokens/string_template_begin.ts
var StringTemplateBegin = class extends AbstractToken {
};
// ../core/src/abap/1_lexer/tokens/string_template_end.ts
var StringTemplateEnd = class extends AbstractToken {
};
// ../core/src/abap/1_lexer/tokens/string_template_middle.ts
var StringTemplateMiddle = class extends AbstractToken {
};
// ../core/src/abap/1_lexer/tokens/comment.ts
var Comment = class extends AbstractToken {
};
// ../core/src/abap/1_lexer/tokens/identifier.ts
var Identifier = class extends AbstractToken {
};
// ../core/src/abap/1_lexer/tokens/pragma.ts
var Pragma = class extends AbstractToken {
};
// ../core/src/abap/1_lexer/tokens/punctuation.ts
var Punctuation = class extends AbstractToken {
};
// ../core/src/abap/1_lexer/lexer_buffer.ts
var LexerBuffer = class {
constructor() {
this.buf = "";
}
add(s) {
this.buf = this.buf + s;
return this.buf;
}
get() {
return this.buf;
}
clear() {
this.buf = "";
}
countIsEven(char) {
let count = 0;
for (let i = 0; i < this.buf.length; i += 1) {
if (this.buf.charAt(i) === char) {
count += 1;
}
}
return count % 2 === 0;
}
};
// ../core/src/abap/1_lexer/lexer_stream.ts
var LexerStream = class {
constructor(raw) {
this.offset = -1;
this.raw = raw;
this.row = 0;
this.col = 0;
}
advance() {
if (this.currentChar() === "\n") {
this.col = 1;
this.row = this.row + 1;
}
if (this.offset === this.raw.length) {
return false;
}
this.col = this.col + 1;
this.offset = this.offset + 1;
return true;
}
getCol() {
return this.col;
}
getRow() {
return this.row;
}
prevChar() {
if (this.offset - 1 < 0) {
return "";
}
return this.raw.substr(this.offset - 1, 1);
}
prevPrevChar() {
if (this.offset - 2 < 0) {
return "";
}
return this.raw.substr(this.offset - 2, 2);
}
currentChar() {
if (this.offset < 0) {
return "\n";
} else if (this.offset >= this.raw.length) {
return "";
}
return this.raw.substr(this.offset, 1);
}
nextChar() {
if (this.offset + 2 > this.raw.length) {
return "";
}
return this.raw.substr(this.offset + 1, 1);
}
nextNextChar() {
if (this.offset + 3 > this.raw.length) {
return this.nextChar();
}
return this.raw.substr(this.offset + 1, 2);
}
getRaw() {
return this.raw;
}
getOffset() {
return this.offset;
}
};
// ../core/src/abap/1_lexer/lexer.ts
var Lexer = class {
constructor() {
this.ModeNormal = 1;
this.ModePing = 2;
this.ModeStr = 3;
this.ModeTemplate = 4;
this.ModeComment = 5;
this.ModePragma = 6;
}
run(file, virtual) {
this.virtual = virtual;
this.tokens = [];
this.m = this.ModeNormal;
this.process(file.getRaw());
return { file, tokens: this.tokens };
}
add() {
const s = this.buffer.get().trim();
if (s.length > 0) {
const col = this.stream.getCol();
const row = this.stream.getRow();
let whiteBefore = false;
if (this.stream.getOffset() - s.length >= 0) {
const prev = this.stream.getRaw().substr(this.stream.getOffset() - s.length, 1);
if (prev === " " || prev === "\n" || prev === " " || prev === ":") {
whiteBefore = true;
}
}
let whiteAfter = false;
const next = this.stream.nextChar();
if (next === " " || next === "\n" || next === " " || next === ":" || next === "," || next === "." || next === "" || next === '"') {
whiteAfter = true;
}
let pos = new Position(row, col - s.length);
if (this.virtual) {
pos = new VirtualPosition(this.virtual, pos.getRow(), pos.getCol());
}
let tok = void 0;
if (this.m === this.ModeComment) {
tok = new Comment(pos, s);
} else if (this.m === this.ModePing || this.m === this.ModeStr) {
tok = new StringToken(pos, s);
} else if (this.m === this.ModeTemplate) {
const first = s.charAt(0);
const last = s.charAt(s.length - 1);
if (first === "|" && last === "|") {
tok = new StringTemplate(pos, s);
} else if (first === "|" && last === "{" && whiteAfter === true) {
tok = new StringTemplateBegin(pos, s);
} else if (first === "}" && last === "|" && whiteBefore === true) {
tok = new StringTemplateEnd(pos, s);
} else if (first === "}" && last === "{" && whiteAfter === true && whiteBefore === true) {
tok = new StringTemplateMiddle(pos, s);
} else {
tok = new Identifier(pos, s);
}
} else if (s.length > 2 && s.substr(0, 2) === "##") {
tok = new Pragma(pos, s);
} else if (s.length === 1) {
if (s === "." || s === ",") {
tok = new Punctuation(pos, s);
} else if (s === "[") {
if (whiteBefore === true && whiteAfter === true) {
tok = new WBracketLeftW(pos, s);
} else if (whiteBefore === true) {
tok = new WBracketLeft(pos, s);
} else if (whiteAfter === true) {
tok = new BracketLeftW(pos, s);
} else {
tok = new BracketLeft(pos, s);
}
} else if (s === "(") {
if (whiteBefore === true && whiteAfter === true) {
tok = new WParenLeftW(pos, s);
} else if (whiteBefore === true) {
tok = new WParenLeft(pos, s);
} else if (whiteAfter === true) {
tok = new ParenLeftW(pos, s);
} else {
tok = new ParenLeft(pos, s);
}
} else if (s === "]") {
if (whiteBefore === true && whiteAfter === true) {
tok = new WBracketRightW(pos, s);
} else if (whiteBefore === true) {
tok = new WBracketRight(pos, s);
} else if (whiteAfter === true) {
tok = new BracketRightW(pos, s);
} else {
tok = new BracketRight(pos, s);
}
} else if (s === ")") {
if (whiteBefore === true && whiteAfter === true) {
tok = new WParenRightW(pos, s);
} else if (whiteBefore === true) {
tok = new WParenRight(pos, s);
} else if (whiteAfter === true) {
tok = new ParenRightW(pos, s);
} else {
tok = new ParenRight(pos, s);
}
} else if (s === "-") {
if (whiteBefore === true && whiteAfter === true) {
tok = new WDashW(pos, s);
} else if (whiteBefore === true) {
tok = new WDash(pos, s);
} else if (whiteAfter === true) {
tok = new DashW(pos, s);
} else {
tok = new Dash(pos, s);
}
} else if (s === "+") {
if (whiteBefore === true && whiteAfter === true) {
tok = new WPlusW(pos, s);
} else if (whiteBefore === true) {
tok = new WPlus(pos, s);
} else if (whiteAfter === true) {
tok = new PlusW(pos, s);
} else {
tok = new Plus(pos, s);
}
} else if (s === "@") {
if (whiteBefore === true && whiteAfter === true) {
tok = new WAtW(pos, s);
} else if (whiteBefore === true) {
tok = new WAt(pos, s);
} else if (whiteAfter === true) {
tok = new AtW(pos, s);
} else {
tok = new At(pos, s);
}
}
} else if (s.length === 2) {
if (s === "->") {
if (whiteBefore === true && whiteAfter === true) {
tok = new WInstanceArrowW(pos, s);
} else if (whiteBefore === true) {
tok = new WInstanceArrow(pos, s);
} else if (whiteAfter === true) {
tok = new InstanceArrowW(pos, s);
} else {
tok = new InstanceArrow(pos, s);
}
} else if (s === "=>") {
if (whiteBefore === true && whiteAfter === true) {
tok = new WStaticArrowW(pos, s);
} else if (whiteBefore === true) {
tok = new WStaticArrow(pos, s);
} else if (whiteAfter === true) {
tok = new StaticArrowW(pos, s);
} else {
tok = new StaticArrow(pos, s);
}
}
}
if (tok === void 0) {
tok = new Identifier(pos, s);
}
this.tokens.push(tok);
}
this.buffer.clear();
}
process(raw) {
this.stream = new LexerStream(raw.replace(/\r/g, ""));
this.buffer = new LexerBuffer();
const splits = {};
splits[" "] = true;
splits[":"] = true;
splits["."] = true;
splits[","] = true;
splits["-"] = true;
splits["+"] = true;
splits["("] = true;
splits[")"] = true;
splits["["] = true;
splits["]"] = true;
splits[" "] = true;
splits["\n"] = true;
const bufs = {};
bufs["."] = true;
bufs[","] = true;
bufs[":"] = true;
bufs["("] = true;
bufs[")"] = true;
bufs["["] = true;
bufs["]"] = true;
bufs["+"] = true;
bufs["@"] = true;
for (; ; ) {
const current = this.stream.currentChar();
const buf = this.buffer.add(current);
const ahead = this.stream.nextChar();
const aahead = this.stream.nextNextChar();
if (this.m === this.ModeNormal) {
if (splits[ahead]) {
this.add();
} else if (ahead === "'") {
this.add();
this.m = this.ModeStr;
} else if (ahead === "|" || ahead === "}") {
this.add();
this.m = this.ModeTemplate;
} else if (ahead === "`") {
this.add();
this.m = this.ModePing;
} else if (aahead === "##") {
this.add();
this.m = this.ModePragma;
} else if (ahead === '"' || ahead === "*" && current === "\n") {
this.add();
this.m = this.ModeComment;
} else if (ahead === "@" && buf.trim().length === 0) {
this.add();
} else if (aahead === "->" || aahead === "=>") {
this.add();
} else if (current === ">" && ahead !== " " && (this.stream.prevChar() === "-" || this.stream.prevChar() === "=")) {
this.add();
} else if (buf.length === 1 && (bufs[buf] || buf === "-" && ahead !== ">")) {
this.add();
}
} else if (this.m === this.ModePragma && (ahead === "," || ahead === ":" || ahead === "." || ahead === " " || ahead === "\n")) {
this.add();
this.m = this.ModeNormal;
} else if (this.m === this.ModePing && buf.length > 1 && current === "`" && aahead !== "``" && ahead !== "`" && this.buffer.countIsEven("`")) {
this.add();
if (ahead === `"`) {
this.m = this.ModeComment;
} else {
this.m = this.ModeNormal;
}
} else if (this.m === this.ModeTemplate && buf.length > 1 && (current === "|" || current === "{") && (this.stream.prevChar() !== "\\" || this.stream.prevPrevChar() === "\\\\")) {
this.add();
this.m = this.ModeNormal;
} else if (this.m === this.ModeTemplate && ahead === "}" && current !== "\\") {
this.add();
} else if (this.m === this.ModeStr && current === "'" && buf.length > 1 && aahead !== "''" && ahead !== "'" && this.buffer.countIsEven("'")) {
this.add();
if (ahead === '"') {
this.m = this.ModeComment;
} else {
this.m = this.ModeNormal;
}
} else if (ahead === "\n" && this.m !== this.ModeTemplate) {
this.add();
this.m = this.ModeNormal;
} else if (this.m === this.ModeTemplate && current === "\n") {
this.add();
}
if (!this.stream.advance()) {
break;
}
}
this.add();
}
};
// src/index.ts
function main(filename, code) {
const file = new MemoryFile(filename, code);
const lexer = new Lexer();
const result = lexer.run(file);
return JSON.stringify(result);
}
main("foo", "bar");