-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathvimlparser.js
6023 lines (5716 loc) · 273 KB
/
vimlparser.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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//!/usr/bin/env nodejs
// usage: nodejs vimlparser.js [--neovim] foo.vim
var fs = require('fs');
var util = require('util');
function main() {
var neovim = false;
var fpath = ''
var args = process.argv;
if (args.length == 4) {
if (args[2] == '--neovim') {
neovim = true;
}
fpath = args[3];
} else if (args.length == 3) {
neovim = false;
fpath = args[2]
}
var r = new StringReader(viml_readfile(fpath));
var p = new VimLParser(neovim);
var c = new Compiler();
try {
var lines = c.compile(p.parse(r));
for (var i in lines) {
process.stdout.write(lines[i] + "\n");
}
} catch (e) {
process.stdout.write(e + '\n');
}
}
var pat_vim2js = {
"[0-9a-zA-Z]" : "[0-9a-zA-Z]",
"[@*!=><&~#]" : "[@*!=><&~#]",
"\\<ARGOPT\\>" : "\\bARGOPT\\b",
"\\<BANG\\>" : "\\bBANG\\b",
"\\<EDITCMD\\>" : "\\bEDITCMD\\b",
"\\<NOTRLCOM\\>" : "\\bNOTRLCOM\\b",
"\\<TRLBAR\\>" : "\\bTRLBAR\\b",
"\\<USECTRLV\\>" : "\\bUSECTRLV\\b",
"\\<USERCMD\\>" : "\\bUSERCMD\\b",
"\\<\\(XFILE\\|FILES\\|FILE1\\)\\>" : "\\b(XFILE|FILES|FILE1)\\b",
"\\S" : "\\S",
"\\a" : "[A-Za-z]",
"\\d" : "\\d",
"\\h" : "[A-Za-z_]",
"\\s" : "\\s",
"\\v^d%[elete][lp]$" : "^d(elete|elet|ele|el|e)[lp]$",
"\\v^s%(c[^sr][^i][^p]|g|i[^mlg]|I|r[^e])" : "^s(c[^sr][^i][^p]|g|i[^mlg]|I|r[^e])",
"\\w" : "[0-9A-Za-z_]",
"\\w\\|[:#]" : "[0-9A-Za-z_]|[:#]",
"\\x" : "[0-9A-Fa-f]",
"^++" : "^\+\+",
"^++bad=\\(keep\\|drop\\|.\\)\\>" : "^\\+\\+bad=(keep|drop|.)\\b",
"^++bad=drop" : "^\\+\\+bad=drop",
"^++bad=keep" : "^\\+\\+bad=keep",
"^++bin\\>" : "^\\+\\+bin\\b",
"^++edit\\>" : "^\\+\\+edit\\b",
"^++enc=\\S" : "^\\+\\+enc=\\S",
"^++encoding=\\S" : "^\\+\\+encoding=\\S",
"^++ff=\\(dos\\|unix\\|mac\\)\\>" : "^\\+\\+ff=(dos|unix|mac)\\b",
"^++fileformat=\\(dos\\|unix\\|mac\\)\\>" : "^\\+\\+fileformat=(dos|unix|mac)\\b",
"^++nobin\\>" : "^\\+\\+nobin\\b",
"^[A-Z]" : "^[A-Z]",
"^\\$\\w\\+" : "^\\$[0-9A-Za-z_]+",
"^\\(!\\|global\\|vglobal\\)$" : "^(!|global|vglobal)$",
"^\\(WHILE\\|FOR\\)$" : "^(WHILE|FOR)$",
"^\\(vimgrep\\|vimgrepadd\\|lvimgrep\\|lvimgrepadd\\)$" : "^(vimgrep|vimgrepadd|lvimgrep|lvimgrepadd)$",
"^\\d" : "^\\d",
"^\\h" : "^[A-Za-z_]",
"^\\s" : "^\\s",
"^\\s*\\\\" : "^\\s*\\\\",
"^[ \\t]$" : "^[ \\t]$",
"^[A-Za-z]$" : "^[A-Za-z]$",
"^[0-9A-Za-z]$" : "^[0-9A-Za-z]$",
"^[0-9]$" : "^[0-9]$",
"^[0-9A-Fa-f]$" : "^[0-9A-Fa-f]$",
"^[0-9A-Za-z_]$" : "^[0-9A-Za-z_]$",
"^[A-Za-z_]$" : "^[A-Za-z_]$",
"^[0-9A-Za-z_:#]$" : "^[0-9A-Za-z_:#]$",
"^[A-Za-z_][0-9A-Za-z_]*$" : "^[A-Za-z_][0-9A-Za-z_]*$",
"^[A-Z]$" : "^[A-Z]$",
"^[a-z]$" : "^[a-z]$",
"^[vgslabwt]:$\\|^\\([vgslabwt]:\\)\\?[A-Za-z_][0-9A-Za-z_#]*$" : "^[vgslabwt]:$|^([vgslabwt]:)?[A-Za-z_][0-9A-Za-z_#]*$",
"^[0-7]$" : "^[0-7]$",
"^[0-9A-Fa-f][0-9A-Fa-f]$" : "^[0-9A-Fa-f][0-9A-Fa-f]$",
"^\\.[0-9A-Fa-f]$" : "^\\.[0-9A-Fa-f]$",
"^[0-9A-Fa-f][^0-9A-Fa-f]$" : "^[0-9A-Fa-f][^0-9A-Fa-f]$",
}
function viml_add(lst, item) {
lst.push(item);
}
function viml_call(func, args) {
return func.apply(null, args);
}
function viml_char2nr(c) {
return c.charCodeAt(0);
}
function viml_empty(obj) {
return obj.length == 0;
}
function viml_equalci(a, b) {
return a.toLowerCase() == b.toLowerCase();
}
function viml_eqreg(s, reg) {
var mx = new RegExp(pat_vim2js[reg]);
return mx.exec(s) != null;
}
function viml_eqregh(s, reg) {
var mx = new RegExp(pat_vim2js[reg]);
return mx.exec(s) != null;
}
function viml_eqregq(s, reg) {
var mx = new RegExp(pat_vim2js[reg], "i");
return mx.exec(s) != null;
}
function viml_escape(s, chars) {
var r = '';
for (var i = 0; i < s.length; ++i) {
if (chars.indexOf(s.charAt(i)) != -1) {
r = r + "\\" + s.charAt(i);
} else {
r = r + s.charAt(i);
}
}
return r;
}
function viml_extend(obj, item) {
obj.push.apply(obj, item);
}
function viml_insert(lst, item) {
var idx = arguments.length >= 3 ? arguments[2] : 0;
lst.splice(0, 0, item);
}
function viml_join(lst, sep) {
return lst.join(sep);
}
function viml_keys(obj) {
return Object.keys(obj);
}
function viml_len(obj) {
if (typeof obj === 'string') {
var len = 0;
for (var i = 0; i < obj.length; i++) {
var c = obj.charCodeAt(i);
len += c < 128 ? 1 : ((c > 127) && (c < 2048)) ? 2 : 3;
}
return len;
}
return obj.length;
}
function viml_printf() {
var a000 = Array.prototype.slice.call(arguments, 0);
if (a000.length == 1) {
return a000[0];
} else {
return util.format.apply(null, a000);
}
}
function viml_range(start) {
var end = arguments.length >= 2 ? arguments[1] : null;
if (end == null) {
var x = [];
for (var i = 0; i < start; ++i) {
x.push(i);
}
return x;
} else {
var x = []
for (var i = start; i <= end; ++i) {
x.push(i);
}
return x;
}
}
function viml_readfile(path) {
// FIXME: newline?
return fs.readFileSync(path, 'utf-8').split(/\r\n|\r|\n/);
}
function viml_remove(lst, idx) {
lst.splice(idx, 1);
}
function viml_split(s, sep) {
if (sep == "\\zs") {
return s.split("");
}
throw "NotImplemented";
}
function viml_str2nr(s) {
var base = arguments.length >= 2 ? arguments[1] : 10;
return parseInt(s, base);
}
function viml_string(obj) {
return obj.toString();
}
function viml_has_key(obj, key) {
return obj[key] !== undefined;
}
function viml_stridx(a, b) {
return a.indexOf(b);
}
var NIL = [];
var TRUE = 1;
var FALSE = 0;
var NODE_TOPLEVEL = 1;
var NODE_COMMENT = 2;
var NODE_EXCMD = 3;
var NODE_FUNCTION = 4;
var NODE_ENDFUNCTION = 5;
var NODE_DELFUNCTION = 6;
var NODE_RETURN = 7;
var NODE_EXCALL = 8;
var NODE_LET = 9;
var NODE_UNLET = 10;
var NODE_LOCKVAR = 11;
var NODE_UNLOCKVAR = 12;
var NODE_IF = 13;
var NODE_ELSEIF = 14;
var NODE_ELSE = 15;
var NODE_ENDIF = 16;
var NODE_WHILE = 17;
var NODE_ENDWHILE = 18;
var NODE_FOR = 19;
var NODE_ENDFOR = 20;
var NODE_CONTINUE = 21;
var NODE_BREAK = 22;
var NODE_TRY = 23;
var NODE_CATCH = 24;
var NODE_FINALLY = 25;
var NODE_ENDTRY = 26;
var NODE_THROW = 27;
var NODE_ECHO = 28;
var NODE_ECHON = 29;
var NODE_ECHOHL = 30;
var NODE_ECHOMSG = 31;
var NODE_ECHOERR = 32;
var NODE_EXECUTE = 33;
var NODE_TERNARY = 34;
var NODE_OR = 35;
var NODE_AND = 36;
var NODE_EQUAL = 37;
var NODE_EQUALCI = 38;
var NODE_EQUALCS = 39;
var NODE_NEQUAL = 40;
var NODE_NEQUALCI = 41;
var NODE_NEQUALCS = 42;
var NODE_GREATER = 43;
var NODE_GREATERCI = 44;
var NODE_GREATERCS = 45;
var NODE_GEQUAL = 46;
var NODE_GEQUALCI = 47;
var NODE_GEQUALCS = 48;
var NODE_SMALLER = 49;
var NODE_SMALLERCI = 50;
var NODE_SMALLERCS = 51;
var NODE_SEQUAL = 52;
var NODE_SEQUALCI = 53;
var NODE_SEQUALCS = 54;
var NODE_MATCH = 55;
var NODE_MATCHCI = 56;
var NODE_MATCHCS = 57;
var NODE_NOMATCH = 58;
var NODE_NOMATCHCI = 59;
var NODE_NOMATCHCS = 60;
var NODE_IS = 61;
var NODE_ISCI = 62;
var NODE_ISCS = 63;
var NODE_ISNOT = 64;
var NODE_ISNOTCI = 65;
var NODE_ISNOTCS = 66;
var NODE_ADD = 67;
var NODE_SUBTRACT = 68;
var NODE_CONCAT = 69;
var NODE_MULTIPLY = 70;
var NODE_DIVIDE = 71;
var NODE_REMAINDER = 72;
var NODE_NOT = 73;
var NODE_MINUS = 74;
var NODE_PLUS = 75;
var NODE_SUBSCRIPT = 76;
var NODE_SLICE = 77;
var NODE_CALL = 78;
var NODE_DOT = 79;
var NODE_NUMBER = 80;
var NODE_STRING = 81;
var NODE_LIST = 82;
var NODE_DICT = 83;
var NODE_OPTION = 85;
var NODE_IDENTIFIER = 86;
var NODE_CURLYNAME = 87;
var NODE_ENV = 88;
var NODE_REG = 89;
var NODE_CURLYNAMEPART = 90;
var NODE_CURLYNAMEEXPR = 91;
var NODE_LAMBDA = 92;
var NODE_BLOB = 93;
var NODE_CONST = 94;
var NODE_EVAL = 95;
var NODE_HEREDOC = 96;
var NODE_METHOD = 97;
var NODE_ECHOCONSOLE = 98;
var NODE_ECHOWINDOW = 99;
var TOKEN_EOF = 1;
var TOKEN_EOL = 2;
var TOKEN_SPACE = 3;
var TOKEN_OROR = 4;
var TOKEN_ANDAND = 5;
var TOKEN_EQEQ = 6;
var TOKEN_EQEQCI = 7;
var TOKEN_EQEQCS = 8;
var TOKEN_NEQ = 9;
var TOKEN_NEQCI = 10;
var TOKEN_NEQCS = 11;
var TOKEN_GT = 12;
var TOKEN_GTCI = 13;
var TOKEN_GTCS = 14;
var TOKEN_GTEQ = 15;
var TOKEN_GTEQCI = 16;
var TOKEN_GTEQCS = 17;
var TOKEN_LT = 18;
var TOKEN_LTCI = 19;
var TOKEN_LTCS = 20;
var TOKEN_LTEQ = 21;
var TOKEN_LTEQCI = 22;
var TOKEN_LTEQCS = 23;
var TOKEN_MATCH = 24;
var TOKEN_MATCHCI = 25;
var TOKEN_MATCHCS = 26;
var TOKEN_NOMATCH = 27;
var TOKEN_NOMATCHCI = 28;
var TOKEN_NOMATCHCS = 29;
var TOKEN_IS = 30;
var TOKEN_ISCI = 31;
var TOKEN_ISCS = 32;
var TOKEN_ISNOT = 33;
var TOKEN_ISNOTCI = 34;
var TOKEN_ISNOTCS = 35;
var TOKEN_PLUS = 36;
var TOKEN_MINUS = 37;
var TOKEN_DOT = 38;
var TOKEN_STAR = 39;
var TOKEN_SLASH = 40;
var TOKEN_PERCENT = 41;
var TOKEN_NOT = 42;
var TOKEN_QUESTION = 43;
var TOKEN_COLON = 44;
var TOKEN_POPEN = 45;
var TOKEN_PCLOSE = 46;
var TOKEN_SQOPEN = 47;
var TOKEN_SQCLOSE = 48;
var TOKEN_COPEN = 49;
var TOKEN_CCLOSE = 50;
var TOKEN_COMMA = 51;
var TOKEN_NUMBER = 52;
var TOKEN_SQUOTE = 53;
var TOKEN_DQUOTE = 54;
var TOKEN_OPTION = 55;
var TOKEN_IDENTIFIER = 56;
var TOKEN_ENV = 57;
var TOKEN_REG = 58;
var TOKEN_EQ = 59;
var TOKEN_OR = 60;
var TOKEN_SEMICOLON = 61;
var TOKEN_BACKTICK = 62;
var TOKEN_DOTDOTDOT = 63;
var TOKEN_SHARP = 64;
var TOKEN_ARROW = 65;
var TOKEN_BLOB = 66;
var TOKEN_LITCOPEN = 67;
var TOKEN_DOTDOT = 68;
var TOKEN_HEREDOC = 69;
var MAX_FUNC_ARGS = 20;
function isalpha(c) {
return viml_eqregh(c, "^[A-Za-z]$");
}
function isalnum(c) {
return viml_eqregh(c, "^[0-9A-Za-z]$");
}
function isdigit(c) {
return viml_eqregh(c, "^[0-9]$");
}
function isodigit(c) {
return viml_eqregh(c, "^[0-7]$");
}
function isxdigit(c) {
return viml_eqregh(c, "^[0-9A-Fa-f]$");
}
function iswordc(c) {
return viml_eqregh(c, "^[0-9A-Za-z_]$");
}
function iswordc1(c) {
return viml_eqregh(c, "^[A-Za-z_]$");
}
function iswhite(c) {
return viml_eqregh(c, "^[ \\t]$");
}
function isnamec(c) {
return viml_eqregh(c, "^[0-9A-Za-z_:#]$");
}
function isnamec1(c) {
return viml_eqregh(c, "^[A-Za-z_]$");
}
function isargname(s) {
return viml_eqregh(s, "^[A-Za-z_][0-9A-Za-z_]*$");
}
function isvarname(s) {
return viml_eqregh(s, "^[vgslabwt]:$\\|^\\([vgslabwt]:\\)\\?[A-Za-z_][0-9A-Za-z_#]*$");
}
// FIXME:
function isidc(c) {
return viml_eqregh(c, "^[0-9A-Za-z_]$");
}
function isupper(c) {
return viml_eqregh(c, "^[A-Z]$");
}
function islower(c) {
return viml_eqregh(c, "^[a-z]$");
}
function ExArg() {
var ea = {};
ea.forceit = FALSE;
ea.addr_count = 0;
ea.line1 = 0;
ea.line2 = 0;
ea.flags = 0;
ea.do_ecmd_cmd = "";
ea.do_ecmd_lnum = 0;
ea.append = 0;
ea.usefilter = FALSE;
ea.amount = 0;
ea.regname = 0;
ea.force_bin = 0;
ea.read_edit = 0;
ea.force_ff = 0;
ea.force_enc = 0;
ea.bad_char = 0;
ea.linepos = {};
ea.cmdpos = [];
ea.argpos = [];
ea.cmd = {};
ea.modifiers = [];
ea.range = [];
ea.argopt = {};
ea.argcmd = {};
return ea;
}
// struct node {
// int type
// pos pos
// node left
// node right
// node cond
// node rest
// node[] list
// node[] rlist
// node[] default_args
// node[] body
// string op
// string str
// int depth
// variant value
// }
// TOPLEVEL .body
// COMMENT .str
// EXCMD .ea .str
// FUNCTION .ea .body .left .rlist .default_args .attr .endfunction
// ENDFUNCTION .ea
// DELFUNCTION .ea .left
// RETURN .ea .left
// EXCALL .ea .left
// LET .ea .op .left .list .rest .right
// CONST .ea .op .left .list .rest .right
// UNLET .ea .list
// LOCKVAR .ea .depth .list
// UNLOCKVAR .ea .depth .list
// IF .ea .body .cond .elseif .else .endif
// ELSEIF .ea .body .cond
// ELSE .ea .body
// ENDIF .ea
// WHILE .ea .body .cond .endwhile
// ENDWHILE .ea
// FOR .ea .body .left .list .rest .right .endfor
// ENDFOR .ea
// CONTINUE .ea
// BREAK .ea
// TRY .ea .body .catch .finally .endtry
// CATCH .ea .body .pattern
// FINALLY .ea .body
// ENDTRY .ea
// THROW .ea .left
// EVAL .ea .left
// ECHO .ea .list
// ECHON .ea .list
// ECHOHL .ea .str
// ECHOMSG .ea .list
// ECHOERR .ea .list
// EXECUTE .ea .list
// TERNARY .cond .left .right
// OR .left .right
// AND .left .right
// EQUAL .left .right
// EQUALCI .left .right
// EQUALCS .left .right
// NEQUAL .left .right
// NEQUALCI .left .right
// NEQUALCS .left .right
// GREATER .left .right
// GREATERCI .left .right
// GREATERCS .left .right
// GEQUAL .left .right
// GEQUALCI .left .right
// GEQUALCS .left .right
// SMALLER .left .right
// SMALLERCI .left .right
// SMALLERCS .left .right
// SEQUAL .left .right
// SEQUALCI .left .right
// SEQUALCS .left .right
// MATCH .left .right
// MATCHCI .left .right
// MATCHCS .left .right
// NOMATCH .left .right
// NOMATCHCI .left .right
// NOMATCHCS .left .right
// IS .left .right
// ISCI .left .right
// ISCS .left .right
// ISNOT .left .right
// ISNOTCI .left .right
// ISNOTCS .left .right
// ADD .left .right
// SUBTRACT .left .right
// CONCAT .left .right
// MULTIPLY .left .right
// DIVIDE .left .right
// REMAINDER .left .right
// NOT .left
// MINUS .left
// PLUS .left
// SUBSCRIPT .left .right
// SLICE .left .rlist
// METHOD .left .right
// CALL .left .rlist
// DOT .left .right
// NUMBER .value
// STRING .value
// LIST .value
// DICT .value
// BLOB .value
// NESTING .left
// OPTION .value
// IDENTIFIER .value
// CURLYNAME .value
// ENV .value
// REG .value
// CURLYNAMEPART .value
// CURLYNAMEEXPR .value
// LAMBDA .rlist .left
// HEREDOC .rlist .op .body
function Node(type) {
return {"type":type};
}
function Err(msg, pos) {
return viml_printf("vimlparser: %s: line %d col %d", msg, pos.lnum, pos.col);
}
function VimLParser() { this.__init__.apply(this, arguments); }
VimLParser.prototype.__init__ = function() {
var a000 = Array.prototype.slice.call(arguments, 0);
if (viml_len(a000) > 0) {
this.neovim = a000[0];
}
else {
this.neovim = 0;
}
this.find_command_cache = {};
}
VimLParser.prototype.push_context = function(node) {
viml_insert(this.context, node);
}
VimLParser.prototype.pop_context = function() {
viml_remove(this.context, 0);
}
VimLParser.prototype.find_context = function(type) {
var i = 0;
var __c3 = this.context;
for (var __i3 = 0; __i3 < __c3.length; ++__i3) {
var node = __c3[__i3];
if (node.type == type) {
return i;
}
i += 1;
}
return -1;
}
VimLParser.prototype.add_node = function(node) {
viml_add(this.context[0].body, node);
}
VimLParser.prototype.check_missing_endfunction = function(ends, pos) {
if (this.context[0].type == NODE_FUNCTION) {
throw Err(viml_printf("E126: Missing :endfunction: %s", ends), pos);
}
}
VimLParser.prototype.check_missing_endif = function(ends, pos) {
if (this.context[0].type == NODE_IF || this.context[0].type == NODE_ELSEIF || this.context[0].type == NODE_ELSE) {
throw Err(viml_printf("E171: Missing :endif: %s", ends), pos);
}
}
VimLParser.prototype.check_missing_endtry = function(ends, pos) {
if (this.context[0].type == NODE_TRY || this.context[0].type == NODE_CATCH || this.context[0].type == NODE_FINALLY) {
throw Err(viml_printf("E600: Missing :endtry: %s", ends), pos);
}
}
VimLParser.prototype.check_missing_endwhile = function(ends, pos) {
if (this.context[0].type == NODE_WHILE) {
throw Err(viml_printf("E170: Missing :endwhile: %s", ends), pos);
}
}
VimLParser.prototype.check_missing_endfor = function(ends, pos) {
if (this.context[0].type == NODE_FOR) {
throw Err(viml_printf("E170: Missing :endfor: %s", ends), pos);
}
}
VimLParser.prototype.parse = function(reader) {
this.reader = reader;
this.context = [];
var toplevel = Node(NODE_TOPLEVEL);
toplevel.pos = this.reader.getpos();
toplevel.body = [];
this.push_context(toplevel);
while (this.reader.peek() != "<EOF>") {
this.parse_one_cmd();
}
this.check_missing_endfunction("TOPLEVEL", this.reader.getpos());
this.check_missing_endif("TOPLEVEL", this.reader.getpos());
this.check_missing_endtry("TOPLEVEL", this.reader.getpos());
this.check_missing_endwhile("TOPLEVEL", this.reader.getpos());
this.check_missing_endfor("TOPLEVEL", this.reader.getpos());
this.pop_context();
return toplevel;
}
VimLParser.prototype.parse_one_cmd = function() {
this.ea = ExArg();
if (this.reader.peekn(2) == "#!") {
this.parse_hashbang();
this.reader.get();
return;
}
this.reader.skip_white_and_colon();
if (this.reader.peekn(1) == "") {
this.reader.get();
return;
}
if (this.reader.peekn(1) == "\"") {
this.parse_comment();
this.reader.get();
return;
}
this.ea.linepos = this.reader.getpos();
this.parse_command_modifiers();
this.parse_range();
this.parse_command();
this.parse_trail();
}
// FIXME:
VimLParser.prototype.parse_command_modifiers = function() {
var modifiers = [];
while (TRUE) {
var pos = this.reader.tell();
var d = "";
if (isdigit(this.reader.peekn(1))) {
var d = this.reader.read_digit();
this.reader.skip_white();
}
var k = this.reader.read_alpha();
var c = this.reader.peekn(1);
this.reader.skip_white();
if (viml_stridx("aboveleft", k) == 0 && viml_len(k) >= 3) {
// abo\%[veleft]
viml_add(modifiers, {"name":"aboveleft"});
}
else if (viml_stridx("belowright", k) == 0 && viml_len(k) >= 3) {
// bel\%[owright]
viml_add(modifiers, {"name":"belowright"});
}
else if (viml_stridx("browse", k) == 0 && viml_len(k) >= 3) {
// bro\%[wse]
viml_add(modifiers, {"name":"browse"});
}
else if (viml_stridx("botright", k) == 0 && viml_len(k) >= 2) {
// bo\%[tright]
viml_add(modifiers, {"name":"botright"});
}
else if (viml_stridx("confirm", k) == 0 && viml_len(k) >= 4) {
// conf\%[irm]
viml_add(modifiers, {"name":"confirm"});
}
else if (viml_stridx("keepmarks", k) == 0 && viml_len(k) >= 3) {
// kee\%[pmarks]
viml_add(modifiers, {"name":"keepmarks"});
}
else if (viml_stridx("keepalt", k) == 0 && viml_len(k) >= 5) {
// keepa\%[lt]
viml_add(modifiers, {"name":"keepalt"});
}
else if (viml_stridx("keepjumps", k) == 0 && viml_len(k) >= 5) {
// keepj\%[umps]
viml_add(modifiers, {"name":"keepjumps"});
}
else if (viml_stridx("keeppatterns", k) == 0 && viml_len(k) >= 5) {
// keepp\%[atterns]
viml_add(modifiers, {"name":"keeppatterns"});
}
else if (viml_stridx("hide", k) == 0 && viml_len(k) >= 3) {
// hid\%[e]
if (this.ends_excmds(c)) {
break;
}
viml_add(modifiers, {"name":"hide"});
}
else if (viml_stridx("lockmarks", k) == 0 && viml_len(k) >= 3) {
// loc\%[kmarks]
viml_add(modifiers, {"name":"lockmarks"});
}
else if (viml_stridx("leftabove", k) == 0 && viml_len(k) >= 5) {
// lefta\%[bove]
viml_add(modifiers, {"name":"leftabove"});
}
else if (viml_stridx("noautocmd", k) == 0 && viml_len(k) >= 3) {
// noa\%[utocmd]
viml_add(modifiers, {"name":"noautocmd"});
}
else if (viml_stridx("noswapfile", k) == 0 && viml_len(k) >= 3) {
// :nos\%[wapfile]
viml_add(modifiers, {"name":"noswapfile"});
}
else if (viml_stridx("rightbelow", k) == 0 && viml_len(k) >= 6) {
// rightb\%[elow]
viml_add(modifiers, {"name":"rightbelow"});
}
else if (viml_stridx("sandbox", k) == 0 && viml_len(k) >= 3) {
// san\%[dbox]
viml_add(modifiers, {"name":"sandbox"});
}
else if (viml_stridx("silent", k) == 0 && viml_len(k) >= 3) {
// sil\%[ent]
if (c == "!") {
this.reader.get();
viml_add(modifiers, {"name":"silent", "bang":1});
}
else {
viml_add(modifiers, {"name":"silent", "bang":0});
}
}
else if (k == "tab") {
// tab
if (d != "") {
viml_add(modifiers, {"name":"tab", "count":viml_str2nr(d, 10)});
}
else {
viml_add(modifiers, {"name":"tab"});
}
}
else if (viml_stridx("topleft", k) == 0 && viml_len(k) >= 2) {
// to\%[pleft]
viml_add(modifiers, {"name":"topleft"});
}
else if (viml_stridx("unsilent", k) == 0 && viml_len(k) >= 3) {
// uns\%[ilent]
viml_add(modifiers, {"name":"unsilent"});
}
else if (viml_stridx("vertical", k) == 0 && viml_len(k) >= 4) {
// vert\%[ical]
viml_add(modifiers, {"name":"vertical"});
}
else if (viml_stridx("verbose", k) == 0 && viml_len(k) >= 4) {
// verb\%[ose]
if (d != "") {
viml_add(modifiers, {"name":"verbose", "count":viml_str2nr(d, 10)});
}
else {
viml_add(modifiers, {"name":"verbose", "count":1});
}
}
else {
this.reader.seek_set(pos);
break;
}
}
this.ea.modifiers = modifiers;
}
// FIXME:
VimLParser.prototype.parse_range = function() {
var tokens = [];
while (TRUE) {
while (TRUE) {
this.reader.skip_white();
var c = this.reader.peekn(1);
if (c == "") {
break;
}
if (c == ".") {
viml_add(tokens, this.reader.getn(1));
}
else if (c == "$") {
viml_add(tokens, this.reader.getn(1));
}
else if (c == "'") {
this.reader.getn(1);
var m = this.reader.getn(1);
if (m == "") {
break;
}
viml_add(tokens, "'" + m);
}
else if (c == "/") {
this.reader.getn(1);
var __tmp = this.parse_pattern(c);
var pattern = __tmp[0];
var _ = __tmp[1];
viml_add(tokens, pattern);
}
else if (c == "?") {
this.reader.getn(1);
var __tmp = this.parse_pattern(c);
var pattern = __tmp[0];
var _ = __tmp[1];
viml_add(tokens, pattern);
}
else if (c == "\\") {
var m = this.reader.p(1);
if (m == "&" || m == "?" || m == "/") {
this.reader.seek_cur(2);
viml_add(tokens, "\\" + m);
}
else {
throw Err("E10: \\\\ should be followed by /, ? or &", this.reader.getpos());
}
}
else if (isdigit(c)) {
viml_add(tokens, this.reader.read_digit());
}
while (TRUE) {
this.reader.skip_white();
if (this.reader.peekn(1) == "") {
break;
}
var n = this.reader.read_integer();
if (n == "") {
break;
}
viml_add(tokens, n);
}
if (this.reader.p(0) != "/" && this.reader.p(0) != "?") {
break;
}
}
if (this.reader.peekn(1) == "%") {
viml_add(tokens, this.reader.getn(1));
}
else if (this.reader.peekn(1) == "*") {
// && &cpoptions !~ '\*'
viml_add(tokens, this.reader.getn(1));
}
if (this.reader.peekn(1) == ";") {
viml_add(tokens, this.reader.getn(1));
continue;
}
else if (this.reader.peekn(1) == ",") {
viml_add(tokens, this.reader.getn(1));
continue;
}
break;
}
this.ea.range = tokens;
}
// FIXME:
VimLParser.prototype.parse_pattern = function(delimiter) {
var pattern = "";
var endc = "";
var inbracket = 0;
while (TRUE) {
var c = this.reader.getn(1);
if (c == "") {
break;
}
if (c == delimiter && inbracket == 0) {
var endc = c;
break;
}
pattern += c;
if (c == "\\") {
var c = this.reader.peekn(1);
if (c == "") {
throw Err("E682: Invalid search pattern or delimiter", this.reader.getpos());
}
this.reader.getn(1);
pattern += c;
}
else if (c == "[") {
inbracket += 1;
}
else if (c == "]") {
inbracket -= 1;
}
}
return [pattern, endc];
}
VimLParser.prototype.parse_command = function() {
this.reader.skip_white_and_colon();
this.ea.cmdpos = this.reader.getpos();
if (this.reader.peekn(1) == "" || this.reader.peekn(1) == "\"") {
if (!viml_empty(this.ea.modifiers) || !viml_empty(this.ea.range)) {
this.parse_cmd_modifier_range();
}
return;
}
this.ea.cmd = this.find_command();
if (this.ea.cmd === NIL) {
this.reader.setpos(this.ea.cmdpos);
throw Err(viml_printf("E492: Not an editor command: %s", this.reader.peekline()), this.ea.cmdpos);
}
if (this.reader.peekn(1) == "!" && this.ea.cmd.name != "substitute" && this.ea.cmd.name != "smagic" && this.ea.cmd.name != "snomagic") {
this.reader.getn(1);
this.ea.forceit = TRUE;
}
else {
this.ea.forceit = FALSE;
}
if (!viml_eqregh(this.ea.cmd.flags, "\\<BANG\\>") && this.ea.forceit && !viml_eqregh(this.ea.cmd.flags, "\\<USERCMD\\>")) {
throw Err("E477: No ! allowed", this.ea.cmdpos);
}
if (this.ea.cmd.name != "!") {
this.reader.skip_white();
}
this.ea.argpos = this.reader.getpos();
if (viml_eqregh(this.ea.cmd.flags, "\\<ARGOPT\\>")) {
this.parse_argopt();
}
if (this.ea.cmd.name == "write" || this.ea.cmd.name == "update") {
if (this.reader.p(0) == ">") {
if (this.reader.p(1) != ">") {
throw Err("E494: Use w or w>>", this.ea.cmdpos);