/
RegExp.java
4858 lines (4460 loc) · 144 KB
/
RegExp.java
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
/*
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
* Copyright (C) 2003-2014 The IdeaVim authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.maddyhome.idea.vim.regexp;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.LogicalPosition;
import com.maddyhome.idea.vim.VimPlugin;
import com.maddyhome.idea.vim.helper.EditorHelper;
import com.maddyhome.idea.vim.helper.MessageHelper;
import com.maddyhome.idea.vim.helper.Msg;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class RegExp {
@Nullable public static reg_extmatch_T re_extmatch_out = null;
@Nullable public static reg_extmatch_T re_extmatch_in = null;
/*
* The opcodes are:
*/
/* definition number opnd? meaning */
private static final int END = 0; /* End of program or NOMATCH operand. */
private static final int BOL = 1; /* Match "" at beginning of line. */
private static final int EOL = 2; /* Match "" at end of line. */
private static final int BRANCH = 3; /* node Match this alternative, or the next... */
private static final int BACK = 4; /* Match "", "next" ptr points backward. */
private static final int EXACTLY = 5; /* str Match this string. */
private static final int NOTHING = 6; /* Match empty string. */
private static final int STAR = 7; /* node Match this (simple) thing 0 or more times. */
private static final int PLUS = 8; /* node Match this (simple) thing 1 or more times. */
private static final int MATCH = 9; /* node match the operand zero-width */
private static final int NOMATCH = 10; /* node check for no match with operand */
private static final int BEHIND = 11; /* node look behind for a match with operand */
private static final int NOBEHIND = 12; /* node look behind for no match with operand */
private static final int SUBPAT = 13; /* node match the operand here */
private static final int BRACE_SIMPLE = 14; /* node Match this (simple) thing between m and
* n times (\{m,n\}). */
private static final int BOW = 15; /* Match "" after [^a-zA-Z0-9_] */
private static final int EOW = 16; /* Match "" at [^a-zA-Z0-9_] */
private static final int BRACE_LIMITS = 17; /* nr nr define the min & max for BRACE_SIMPLE
* and BRACE_COMPLEX. */
private static final int NEWL = 18; /* Match line-break */
private static final int BHPOS = 19; /* End position for BEHIND or NOBEHIND */
/* character classes: 20-48 normal, 50-78 include a line-break */
private static final int ADD_NL = 30;
private static final int ANY = 20; /* Match any one character. */
private static final int FIRST_NL = ANY + ADD_NL;
private static final int ANYOF = 21; /* str Match any character in this string. */
private static final int ANYBUT = 22; /* str Match any character not in this
* string. */
private static final int IDENT = 23; /* Match identifier char */
private static final int SIDENT = 24; /* Match identifier char but no digit */
private static final int KWORD = 25; /* Match keyword char */
private static final int SKWORD = 26; /* Match word char but no digit */
private static final int FNAME = 27; /* Match file name char */
private static final int SFNAME = 28; /* Match file name char but no digit */
private static final int PRINT = 29; /* Match printable char */
private static final int SPRINT = 30; /* Match printable char but no digit */
private static final int WHITE = 31; /* Match whitespace char */
private static final int NWHITE = 32; /* Match non-whitespace char */
private static final int DIGIT = 33; /* Match digit char */
private static final int NDIGIT = 34; /* Match non-digit char */
private static final int HEX = 35; /* Match hex char */
private static final int NHEX = 36; /* Match non-hex char */
private static final int OCTAL = 37; /* Match octal char */
private static final int NOCTAL = 38; /* Match non-octal char */
private static final int WORD = 39; /* Match word char */
private static final int NWORD = 40; /* Match non-word char */
private static final int HEAD = 41; /* Match head char */
private static final int NHEAD = 42; /* Match non-head char */
private static final int ALPHA = 43; /* Match alpha char */
private static final int NALPHA = 44; /* Match non-alpha char */
private static final int LOWER = 45; /* Match lowercase char */
private static final int NLOWER = 46; /* Match non-lowercase char */
private static final int UPPER = 47; /* Match uppercase char */
private static final int NUPPER = 48; /* Match non-uppercase char */
private static final int LAST_NL = NUPPER + ADD_NL;
private static final int MOPEN = 80; /* -89 Mark this point in input as start of
* \( subexpr. MOPEN + 0 marks start of
* match. */
private static final int MCLOSE = 90; /* -99 Analogous to MOPEN. MCLOSE + 0 marks
* end of match. */
private static final int BACKREF = 100; /* -109 node Match same string again \1-\9 */
private static final int ZOPEN = 110; /* -119 Mark this point in input as start of
* \z( subexpr. */
private static final int ZCLOSE = 120; /* -129 Analogous to ZOPEN. */
private static final int ZREF = 130; /* -139 node Match external submatch \z1-\z9 */
private static final int BRACE_COMPLEX = 140; /* -149 node Match nodes between m & n times */
private static final int NOPEN = 150; /* Mark this point in input as start of
\%( subexpr. */
private static final int NCLOSE = 151; /* Analogous to NOPEN. */
private static final int RE_BOF = 201; /* Match "" at beginning of file. */
private static final int RE_EOF = 202; /* Match "" at end of file. */
private static final int CURSOR = 203; /* Match location of cursor. */
private static final int RE_LNUM = 204; /* nr cmp Match line number */
private static final int RE_COL = 205; /* nr cmp Match column number */
private static final int RE_VCOL = 206; /* nr cmp Match virtual column number */
private static final int REGMAGIC = 0234;
private static final int REX_SET = 1;
private static final int REX_USE = 2;
private static final int MAX_LIMIT = Integer.MAX_VALUE;
private static final int NOT_MULTI = 0;
private static final int MULTI_ONE = 1;
private static final int MULTI_MULT = 2;
/*
* Flags to be passed up and down.
*/
private static final int HASWIDTH = 0x1; /* Known never to match null string. */
private static final int SIMPLE = 0x2; /* Simple enough to be STAR/PLUS operand. */
private static final int SPSTART = 0x4; /* Starts with * or +. */
private static final int HASNL = 0x8; /* Contains some \n. */
private static final int WORST = 0; /* Worst case. */
/*
* REGEXP_INRANGE contains all characters which are always special in a []
* range after '\'.
* REGEXP_ABBR contains all characters which act as abbreviations after '\'.
* These are:
* \n - New line (NL).
* \r - Carriage Return (CR).
* \t - Tab (TAB).
* \e - Escape (ESC).
* \b - Backspace (Ctrl_H).
*/
private static final String REGEXP_INRANGE = "]^-n\\";
private static final String REGEXP_ABBR = "nrteb";
/* flags for regflags */
private static final int RF_ICASE = 1; /* ignore case */
private static final int RF_NOICASE = 2; /* don't ignore case */
private static final int RF_HASNL = 4; /* can match a NL */
/*
* Global work variables for vim_regcomp().
*/
private static final int NSUBEXP = 10;
private static final int MAGIC_NONE = 1; /* "\V" very unmagic */
private static final int MAGIC_OFF = 2; /* "\M" or 'magic' off */
private static final int MAGIC_ON = 3; /* "\m" or 'magic' */
private static final int MAGIC_ALL = 4; /* "\v" very magic */
/*
* META contains all characters that may be magic, except '^' and '$'.
*/
/* META[] is used often enough to justify turning it into a table. */
private static final int META_flags[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
/* % & ( ) * + . */
0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0,
/* 1 2 3 4 5 6 7 8 9 < = > ? */
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1,
/* @ A C D F H I K L M O */
1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1,
/* P S U V W X [ _ */
1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1,
/* a c d f h i k l m n o */
0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
/* p s u v w x z { | ~ */
1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1
};
/* arguments for reg() */
private static final int REG_NOPAREN = 0; /* toplevel reg() */
private static final int REG_PAREN = 1; /* \(\) */
private static final int REG_ZPAREN = 2; /* \z(\) */
private static final int REG_NPAREN = 3; /* \%(\) */
private static boolean WITH_NL(int op) {
return op >= FIRST_NL && op <= LAST_NL;
}
/*
* The first byte of the regexp internal "program" is actually this magic
* number; the start node begins in the second byte. It's used to catch the
* most severe mutilation of the program by the caller.
*/
/*
* Opcode notes:
*
* BRANCH The set of branches constituting a single choice are hooked
* together with their "next" pointers, since precedence prevents
* anything being concatenated to any individual branch. The
* "next" pointer of the last BRANCH in a choice points to the
* thing following the whole choice. This is also where the
* final "next" pointer of each individual branch points; each
* branch starts with the operand node of a BRANCH node.
*
* BACK Normal "next" pointers all implicitly point forward; BACK
* exists to make loop structures possible.
*
* STAR,PLUS '=', and complex '*' and '+', are implemented as circular
* BRANCH structures using BACK. Simple cases (one character
* per match) are implemented with STAR and PLUS for speed
* and to minimize recursive plunges.
*
* BRACE_LIMITS This is always followed by a BRACE_SIMPLE or BRACE_COMPLEX
* node, and defines the min and max limits to be used for that
* node.
*
* MOPEN,MCLOSE ...are numbered at compile time.
* ZOPEN,ZCLOSE ...ditto
*/
/*
* A node is one char of opcode followed by two chars of "next" pointer.
* "Next" pointers are stored as two 8-bit bytes, high order first. The
* value is a positive offset from the opcode of the node containing it.
* An operand, if any, simply follows the node. (Note that much of the
* code generation knows about this implicit relationship.)
*
* Using two bytes for the "next" pointer is vast overkill for most things,
* but allows patterns to get big without disasters.
*/
// #define OP(p) ((int)*(p))
// #define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377))
// #define OPERAND(p) ((p) + 3)
/* Obtain an operand that was stored as four bytes, MSB first. */
// #define OPERAND_MIN(p) (((long)(p)[3] << 24) + ((long)(p)[4] << 16) \
// + ((long)(p)[5] << 8) + (long)(p)[6])
/* Obtain a second operand stored as four bytes. */
// #define OPERAND_MAX(p) OPERAND_MIN((p) + 4)
/* Obtain a second single-byte operand stored after a four bytes operand. */
// #define OPERAND_CMP(p) (p)[7]
/*
* Utility definitions.
*/
// #define UCHARAT(p) ((int)*(char_u *)(p))
/* Used for an error (down from) vim_regcomp(): give the error message, set rc_did_emsg and return null */
/*
#define EMSG_RET_null(m)
{
EMSG(_(m));
rc_did_emsg = true;
return null;
}
#define EMSG_M_RET_null(m, c)
{
EMSG2(_(m), c ? "" : "\\");
rc_did_emsg = true;
return null;
}
#define EMSG_RET_FAIL(m)
{
EMSG(_(m));
rc_did_emsg = true;
return FAIL;
}
#define EMSG_ONE_RET_null
*/
//EMSG_M_RET_null("E369: invalid item in %s%%[]", reg_magic == MAGIC_ALL)
private void EMSG_RET_null(@NotNull String key) {
VimPlugin.showMessage(MessageHelper.message(key));
}
private void EMSG_M_RET_null(@NotNull String key, boolean isMagic) {
String val = isMagic ? "" : "\\";
VimPlugin.showMessage(MessageHelper.message(key, val));
}
private void EMSG_ONE_RET_null() {
EMSG_M_RET_null(Msg.E369, reg_magic == MAGIC_ALL);
}
/*
* Return NOT_MULTI if c is not a "multi" operator.
* Return MULTI_ONE if c is a single "multi" operator.
* Return MULTI_MULT if c is a multi "multi" operator.
*/
private int re_multi_type(int c) {
if (c == Magic.AT || c == Magic.EQUAL || c == Magic.QUESTION) {
return MULTI_ONE;
}
if (c == Magic.STAR || c == Magic.PLUS || c == Magic.LCURLY) {
return MULTI_MULT;
}
return NOT_MULTI;
}
/*
* Translate '\x' to its control character, except "\n", which is Magic.
*/
private int backslash_trans(int c) {
switch (c) {
case 'r':
return '\r';
case 't':
return '\t';
case 'e':
return 0x1b;
case 'b':
return '\b';
}
return c;
}
/*
* Check for a character class name. "pp" points to the '['.
* Returns one of the CLASS_ items. CLASS_NONE means that no item was
* recognized. Otherwise "pp" is advanced to after the item.
*/
private static int skip_class_name(@NotNull CharPointer pp) {
int i;
if (pp.charAt(1) == ':') {
for (i = 0; i < CharacterClasses.CLASS_NAMES.length; ++i) {
if (pp.ref(2).strncmp(CharacterClasses.CLASS_NAMES[i], CharacterClasses.CLASS_NAMES[i].length()) == 0) {
pp.inc(CharacterClasses.CLASS_NAMES[i].length() + 2);
return i;
}
}
}
return CharacterClasses.CLASS_NONE;
}
/*
* Skip over a "[]" range.
* "p" must point to the character after the '['.
* The returned pointer is on the matching ']', or the terminating NUL.
*/
@NotNull
private static CharPointer skip_anyof(@NotNull CharPointer p) {
if (p.charAt() == '^') /* Complement of range. */ {
p.inc();
}
if (p.charAt() == ']' || p.charAt() == '-') {
p.inc();
}
while (!p.end() && p.charAt() != ']') {
if (p.charAt() == '-') {
p.inc();
if (!p.end() && p.charAt() != ']') {
p.inc();
}
}
else if (p.charAt() == '\\' &&
(REGEXP_INRANGE.indexOf(p.charAt(1)) != -1 || REGEXP_ABBR.indexOf(p.charAt(1)) != -1)) {
p.inc(2);
}
else if (p.charAt() == '[') {
if (skip_class_name(p) == CharacterClasses.CLASS_NONE) {
p.inc(); /* It was not a class name */
}
}
else {
p.inc();
}
}
return p;
}
/*
* Return true if compiled regular expression "prog" can match a line break.
*/
public int re_multiline(@NotNull regprog_T prog) {
return (prog.regflags & RF_HASNL);
}
/*
* Skip past regular expression.
* Stop at end of 'p' of where 'dirc' is found ('/', '?', etc).
* Take care of characters with a backslash in front of it.
* Skip strings inside [ and ].
*/
@NotNull
public static CharPointer skip_regexp(@NotNull CharPointer p, char dirc, boolean magic) {
int mymagic;
if (magic) {
mymagic = MAGIC_ON;
}
else {
mymagic = MAGIC_OFF;
}
for (; !p.end(); p.inc()) {
if (p.charAt() == dirc) /* found end of regexp */ {
break;
}
if ((p.charAt() == '[' && mymagic >= MAGIC_ON) ||
(p.charAt() == '\\' && p.charAt(1) == '[' && mymagic <= MAGIC_OFF)) {
p = skip_anyof(p.ref(1));
if (p.end()) {
break;
}
}
else if (p.charAt() == '\\' && p.charAt(1) != '\u0000') {
p.inc(); /* skip next character */
if (p.charAt() == 'v') {
mymagic = MAGIC_ALL;
}
else if (p.charAt() == 'V') {
mymagic = MAGIC_NONE;
}
}
}
return p;
}
/*
* vim_regcomp - compile a regular expression into internal code
*
* We can't allocate space until we know how big the compiled form will be,
* but we can't compile it (and thus know how big it is) until we've got a
* place to put the code. So we cheat: we compile it twice, once with code
* generation turned off and size counting turned on, and once "for real".
* This also means that we don't allocate space until we are sure that the
* thing really will compile successfully, and we never have to move the
* code and thus invalidate pointers into it. (Note that it has to be in
* one piece because vim_free() must be able to free it all.)
*
* Whether upper/lower case is to be ignored is decided when executing the
* program, it does not matter here.
*
* Beware that the optimization-preparation code in here knows about some
* of the structure of the compiled regexp.
*/
@Nullable
public regprog_T vim_regcomp(@Nullable String expr, int magic) {
regprog_T r;
CharPointer scan;
CharPointer longest;
int len;
Flags flags = new Flags();
if (expr == null) {
VimPlugin.showMessage(MessageHelper.message(Msg.e_null));
return null;
}
r = new regprog_T();
r.program = new StringBuffer();
/*
* Second pass: emit code.
*/
regcomp_start(expr, magic);
regcode = new CharPointer(r.program);
regc(REGMAGIC);
if (reg(REG_NOPAREN, flags) == null) {
return null;
}
/* Dig out information for optimizations. */
r.regstart = 0; /* Worst-case defaults. */
r.reganch = 0;
r.regmust = null;
r.regmlen = 0;
r.regflags = regflags;
if (flags.isSet(HASNL)) {
r.regflags |= RF_HASNL;
}
/* Remember whether this pattern has any \z specials in it. */
r.reghasz = re_has_z;
scan = (new CharPointer(r.program)).ref(1); /* First BRANCH. */
if (regnext(scan).OP() == END) /* Only one top-level choice. */ {
scan = scan.OPERAND();
/* Starting-point info. */
if (scan.OP() == BOL || scan.OP() == RE_BOF) {
r.reganch++;
scan = regnext(scan);
}
if (scan.OP() == EXACTLY) {
r.regstart = scan.OPERAND().charAt();
}
else if ((scan.OP() == BOW
|| scan.OP() == EOW
|| scan.OP() == NOTHING
|| scan.OP() == MOPEN || scan.OP() == NOPEN
|| scan.OP() == MCLOSE || scan.OP() == NCLOSE)
&& regnext(scan).OP() == EXACTLY) {
r.regstart = regnext(scan).OPERAND().charAt();
}
/*
* If there's something expensive in the r.e., find the longest
* literal string that must appear and make it the regmust. Resolve
* ties in favor of later strings, since the regstart check works
* with the beginning of the r.e. and avoiding duplication
* strengthens checking. Not a strong reason, but sufficient in the
* absence of others.
*/
/*
* When the r.e. starts with BOW, it is faster to look for a regmust
* first. Used a lot for "#" and "*" commands. (Added by mool).
*/
if ((flags.isSet(SPSTART) || scan.OP() == BOW || scan.OP() == EOW)
&& !(flags.isSet(HASNL))) {
longest = null;
len = 0;
for (; scan != null; scan = regnext(scan)) {
CharPointer so = scan.OPERAND();
if (scan.OP() == EXACTLY && so.strlen() >= len) {
longest = so.ref(0);
len = so.strlen();
}
}
if (longest != null) {
r.regmust = longest.ref(0);
}
r.regmlen = len;
}
}
if (logger.isDebugEnabled()) logger.debug(regdump(expr, r));
return r;
}
/*
* Setup to parse the regexp. Used once to get the length and once to do it.
*/
private void regcomp_start(String expr, int magic) {
initchr(expr);
if (magic != 0) {
reg_magic = MAGIC_ON;
}
else {
reg_magic = MAGIC_OFF;
}
num_complex_braces = 0;
regnpar = 1;
for (int i = 0; i < had_endbrace.length; i++) {
had_endbrace[i] = false;
}
regnzpar = 1;
re_has_z = 0;
regflags = 0;
had_eol = false;
}
/*
* Check if during the previous call to vim_regcomp the EOL item "$" has been
* found. This is messy, but it works fine.
*/
public boolean vim_regcomp_had_eol() {
return had_eol;
}
/*
* reg - regular expression, i.e. main body or parenthesized thing
*
* Caller must absorb opening parenthesis.
*
* Combining parenthesis handling with the base level of regular expression
* is a trifle forced, but the need to tie the tails of the branches to what
* follows makes it hard to avoid.
*/
@Nullable
private CharPointer reg(int paren, @NotNull Flags flagp) {
CharPointer ret;
CharPointer br;
CharPointer ender;
int parno = 0;
Flags flags = new Flags();
flagp.init(HASWIDTH); /* Tentatively. */
if (paren == REG_ZPAREN) {
/* Make a ZOPEN node. */
if (regnzpar >= NSUBEXP) {
VimPlugin.showMessage(MessageHelper.message(Msg.E50));
return null;
}
parno = regnzpar;
regnzpar++;
ret = regnode(ZOPEN + parno);
}
else if (paren == REG_PAREN) {
/* Make a MOPEN node. */
if (regnpar >= NSUBEXP) {
EMSG_M_RET_null(Msg.E51, reg_magic == MAGIC_ALL);
return null;
}
parno = regnpar;
++regnpar;
ret = regnode(MOPEN + parno);
}
else if (paren == REG_NPAREN) {
/* Make a NOPEN node. */
ret = regnode(NOPEN);
}
else {
ret = null;
}
/* Pick up the branches, linking them together. */
br = regbranch(flags);
if (br == null) {
return null;
}
if (ret != null) {
regtail(ret, br); /* [MZ]OPEN -> first. */
}
else {
ret = br.ref(0);
}
/* If one of the branches can be zero-width, the whole thing can.
* If one of the branches has * at start or matches a line-break, the
* whole thing can. */
if (!flags.isSet(HASWIDTH)) {
flagp.unset(HASWIDTH);
}
flagp.set(flags.get() & (SPSTART | HASNL));
while (peekchr() == Magic.PIPE) {
skipchr();
br = regbranch(flags);
if (br == null) {
return null;
}
regtail(ret, br); /* BRANCH -> BRANCH. */
if (!flags.isSet(HASWIDTH)) {
flagp.unset(HASWIDTH);
}
flagp.set(flags.get() & (SPSTART | HASNL));
}
/* Make a closing node, and hook it on the end. */
ender = regnode(paren == REG_ZPAREN ? ZCLOSE + parno : paren == REG_PAREN ? MCLOSE + parno :
paren == REG_NPAREN ? NCLOSE : END);
regtail(ret, ender);
/* Hook the tails of the branches to the closing node. */
for (br = ret.ref(0); br != null; br = regnext(br)) {
regoptail(br, ender);
}
/* Check for proper termination. */
if (paren != REG_NOPAREN && getchr() != Magic.RPAREN) {
if (paren == REG_ZPAREN) {
VimPlugin.showMessage(MessageHelper.message(Msg.E52));
return null;
}
else if (paren == REG_NPAREN) {
EMSG_M_RET_null(Msg.E53, reg_magic == MAGIC_ALL);
return null;
}
else {
EMSG_M_RET_null(Msg.E54, reg_magic == MAGIC_ALL);
return null;
}
}
else if (paren == REG_NOPAREN && peekchr() != '\u0000') {
if (curchr == Magic.LPAREN) {
EMSG_M_RET_null(Msg.E55, reg_magic == MAGIC_ALL);
return null;
}
else {
VimPlugin.showMessage(MessageHelper.message(Msg.e_trailing));
return null;
}
/* NOTREACHED */
}
/*
* Here we set the flag allowing back references to this set of
* parentheses.
*/
if (paren == REG_PAREN) {
had_endbrace[parno] = true; /* have seen the close paren */
}
return ret;
}
/*
* regbranch - one alternative of an | operator
*
* Implements the & operator.
*/
@Nullable
private CharPointer regbranch(@NotNull Flags flagp) {
CharPointer ret;
CharPointer chain = null;
CharPointer latest;
Flags flags = new Flags();
flagp.init(WORST | HASNL); /* Tentatively. */
ret = regnode(BRANCH);
for (; ; ) {
latest = regconcat(flags);
if (latest == null) {
return null;
}
/* If one of the branches has width, the whole thing has. If one of
* the branches anchors at start-of-line, the whole thing does. */
flagp.set(flags.get() & (HASWIDTH | SPSTART));
/* If one of the branches doesn't match a line-break, the whole thing
* doesn't. */
flagp.set(flagp.get() & (~HASNL | (flags.get() & HASNL)));
if (chain != null) {
regtail(chain, latest);
}
if (peekchr() != Magic.AMP) {
break;
}
skipchr();
regtail(latest, regnode(END)); /* operand ends */
reginsert(MATCH, latest.ref(0));
chain = latest.ref(0);
}
return ret;
}
/*
* regbranch - one alternative of an | or & operator
*
* Implements the concatenation operator.
*/
@Nullable
private CharPointer regconcat(@NotNull Flags flagp) {
CharPointer first = null;
CharPointer chain = null;
CharPointer latest;
Flags flags = new Flags();
boolean cont = true;
flagp.init(WORST); /* Tentatively. */
while (cont) {
switch (peekchr()) {
case '\u0000':
case Magic.PIPE:
case Magic.AMP:
case Magic.RPAREN:
cont = false;
break;
case Magic.c:
regflags |= RF_ICASE;
skipchr_keepstart();
break;
case Magic.C:
regflags |= RF_NOICASE;
skipchr_keepstart();
break;
case Magic.v:
reg_magic = MAGIC_ALL;
skipchr_keepstart();
curchr = -1;
break;
case Magic.m:
reg_magic = MAGIC_ON;
skipchr_keepstart();
curchr = -1;
break;
case Magic.M:
reg_magic = MAGIC_OFF;
skipchr_keepstart();
curchr = -1;
break;
case Magic.V:
reg_magic = MAGIC_NONE;
skipchr_keepstart();
curchr = -1;
break;
default:
latest = regpiece(flags);
if (latest == null) {
return null;
}
flagp.set(flags.get() & (HASWIDTH | HASNL));
if (chain == null) /* First piece. */ {
flagp.set(flags.get() & SPSTART);
}
else {
regtail(chain, latest);
}
chain = latest.ref(0);
if (first == null) {
first = latest.ref(0);
}
break;
}
}
if (first == null) /* Loop ran zero times. */ {
first = regnode(NOTHING);
}
return first;
}
/*
* regpiece - something followed by possible [*+=]
*
* Note that the branching code sequences used for = and the general cases
* of * and + are somewhat optimized: they use the same NOTHING node as
* both the endmarker for their branch list and the body of the last branch.
* It might seem that this node could be dispensed with entirely, but the
* endmarker role is not redundant.
*/
@Nullable
private CharPointer regpiece(@NotNull Flags flagp) {
CharPointer ret;
int op;
CharPointer next;
Flags flags = new Flags();
ret = regatom(flags);
if (ret == null) {
return null;
}
op = peekchr();
if (re_multi_type(op) == NOT_MULTI) {
flagp.init(flags.get());
return ret;
}
if (!(flags.isSet(HASWIDTH)) && re_multi_type(op) == MULTI_MULT) {
if (op == Magic.STAR) {
EMSG_M_RET_null(Msg.E56, reg_magic >= MAGIC_ON);
return null;
}
if (op == Magic.PLUS) {
EMSG_M_RET_null(Msg.E57, reg_magic == MAGIC_ALL);
return null;
}
/* "\{}" is checked below, it's allowed when there is an upper limit */
}
flagp.init((WORST | SPSTART | (flags.get() & HASNL))); /* default flags */
skipchr();
switch (op) {
case Magic.STAR:
if (flags.isSet(SIMPLE)) {
reginsert(STAR, ret.ref(0));
}
else {
/* Emit x* as (x&|), where & means "self". */
reginsert(BRANCH, ret.ref(0)); /* Either x */
regoptail(ret, regnode(BACK)); /* and loop */
regoptail(ret, ret); /* back */
regtail(ret, regnode(BRANCH)); /* or */
regtail(ret, regnode(NOTHING)); /* null. */
}
break;
case Magic.PLUS:
if (flags.isSet(SIMPLE)) {
reginsert(PLUS, ret.ref(0));
}
else {
/* Emit x+ as x(&|), where & means "self". */
next = regnode(BRANCH); /* Either */
regtail(ret, next);
regtail(regnode(BACK), ret); /* loop back */
regtail(next, regnode(BRANCH)); /* or */
regtail(ret, regnode(NOTHING)); /* null. */
}
flagp.init((WORST | HASWIDTH | (flags.get() & HASNL)));
break;
case Magic.AT: {
int lop = END;
switch (Magic.no_Magic(getchr())) {
case '=':
lop = MATCH;
break; /* \@= */
case '!':
lop = NOMATCH;
break; /* \@! */
case '>':
lop = SUBPAT;
break; /* \@> */
case '<':
switch (Magic.no_Magic(getchr())) {
case '=':
lop = BEHIND;
break; /* \@<= */
case '!':
lop = NOBEHIND;
break; /* \@<! */
}
}
if (lop == END) {
EMSG_M_RET_null(Msg.E59, reg_magic == MAGIC_ALL);
return null;
}
/* Look behind must match with behind_pos. */
if (lop == BEHIND || lop == NOBEHIND) {
regtail(ret, regnode(BHPOS));
}
regtail(ret, regnode(END)); /* operand ends */
reginsert(lop, ret.ref(0));
break;
}
case Magic.QUESTION:
case Magic.EQUAL:
/* Emit x= as (x|) */
reginsert(BRANCH, ret.ref(0)); /* Either x */
regtail(ret, regnode(BRANCH)); /* or */
next = regnode(NOTHING); /* null. */
regtail(ret, next);
regoptail(ret, next);
break;
case Magic.LCURLY:
MinMax limits = read_limits();
if (limits == null) {
return null;
}
int maxval = limits.maxvalue;
int minval = limits.minvalue;
if (!(flags.isSet(HASWIDTH)) && (maxval > minval ? maxval >= MAX_LIMIT : minval >= MAX_LIMIT)) {
EMSG_M_RET_null(Msg.E58, reg_magic == MAGIC_ALL);
return null;
}
if (flags.isSet(SIMPLE)) {
reginsert(BRACE_SIMPLE, ret.ref(0));
reginsert_limits(BRACE_LIMITS, minval, maxval, ret.ref(0));
}
else {
if (num_complex_braces >= 10) {
EMSG_M_RET_null(Msg.E60, reg_magic == MAGIC_ALL);
return null;
}
reginsert(BRACE_COMPLEX + num_complex_braces, ret.ref(0));
regoptail(ret, regnode(BACK));
regoptail(ret, ret);
reginsert_limits(BRACE_LIMITS, minval, maxval, ret.ref(0));
++num_complex_braces;
}
if (minval > 0 && maxval > 0) {
flagp.init((HASWIDTH | (flags.get() & HASNL)));
}
break;
}
if (re_multi_type(peekchr()) != NOT_MULTI) {
/* Can't have a multi follow a multi. */
if (peekchr() == Magic.STAR) {
String val = reg_magic >= MAGIC_ON ? "" : "\\";
VimPlugin.showMessage(MessageHelper.message(Msg.E61, val));
}
else {
String val = reg_magic >= MAGIC_ON ? "" : "\\";
VimPlugin.showMessage(MessageHelper.message(Msg.E62, val, Character.toString((char)Magic.no_Magic(peekchr()))));
}
return null;
}
return ret;
}
/*
* regatom - the lowest level
*
* Optimization: gobbles an entire sequence of ordinary characters so that
* it can turn them into a single node, which is smaller to store and
* faster to run. Don't do this when one_exactly is set.
*/
@Nullable
private CharPointer regatom(@NotNull Flags flagp) {
CharPointer ret = null;
Flags flags = new Flags();
boolean cpo_lit = false; /* 'cpoptions' contains 'l' flag */