forked from JetBrains/ideavim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegExp.kt
4100 lines (3943 loc) · 135 KB
/
RegExp.kt
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
package com.maddyhome.idea.vim.regexp
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.getLineBuffer
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.diagnostic.VimLogger
import com.maddyhome.idea.vim.helper.Msg
import org.jetbrains.annotations.NonNls
import java.util.*
@Deprecated("Please use VimRegex class instead")
internal class RegExp {
/*
* 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 fun EMSG_RET_null(key: String) {
injector.messages.showStatusBarMessage(null, injector.messages.message(key))
}
private fun EMSG_M_RET_null(key: String, isMagic: Boolean) {
val `val` = if (isMagic) "" else "\\"
injector.messages.showStatusBarMessage(null, injector.messages.message(key, `val`))
}
private fun 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 fun re_multi_type(c: Int): Int {
if (c == Magic.AT || c == Magic.EQUAL || c == Magic.QUESTION) {
return MULTI_ONE
}
return if (c == Magic.STAR || c == Magic.PLUS || c == Magic.LCURLY) {
MULTI_MULT
} else {
NOT_MULTI
}
}
/*
* Translate '\x' to its control character, except "\n", which is Magic.
*/
private fun backslash_trans(c: Int): Int {
when (c) {
'r'.code -> return '\r'.code
't'.code -> return '\t'.code
'e'.code -> return 0x1b
'b'.code -> return '\b'.code
}
return c
}
/*
* Return true if compiled regular expression "prog" can match a line break.
*/
fun re_multiline(prog: regprog_T): Int {
return prog.regflags and RF_HASNL
}
/*
* 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.
*/
fun vim_regcomp(expr: String?, magic: Int): regprog_T? {
val r: regprog_T
var scan: CharPointer
var longest: CharPointer?
var len: Int
val flags = Flags()
if (expr == null) {
injector.messages.showStatusBarMessage(null, injector.messages.message(Msg.e_null))
return null
}
r = regprog_T()
/*
* Second pass: emit code.
*/regcomp_start(expr, magic)
regcode = CharPointer(r.program)
regc(REGMAGIC)
if (reg(REG_NOPAREN, flags) == null) {
return null
}
/* Dig out information for optimizations. */r.regstart = 0.toChar() /* Worst-case defaults. */
r.reganch = 0.toChar()
r.regmust = null
r.regmlen = 0
r.regflags = regflags
if (flags.isSet(HASNL)) {
r.regflags = r.regflags or RF_HASNL
}
/* Remember whether this pattern has any \z specials in it. */r.reghasz = re_has_z
scan = 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
while (scan != null) {
val so = scan.OPERAND()
if (scan.OP() == EXACTLY && so.strlen() >= len) {
longest = so.ref(0)
len = so.strlen()
}
scan = regnext(scan)!!
}
if (longest != null) {
r.regmust = longest.ref(0)
}
r.regmlen = len
}
}
if (logger.isDebug()) logger.debug(regdump(expr, r))
return r
}
/*
* Setup to parse the regexp. Used once to get the length and once to do it.
*/
private fun regcomp_start(expr: String, magic: Int) {
initchr(expr)
reg_magic = if (magic != 0) {
MAGIC_ON
} else {
MAGIC_OFF
}
num_complex_braces = 0
regnpar = 1
Arrays.fill(had_endbrace, false)
regnzpar = 1
re_has_z = 0.toChar()
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.
*/
fun vim_regcomp_had_eol(): Boolean {
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.
*/
private fun reg(paren: Int, flagp: Flags): CharPointer? {
var ret: CharPointer?
var br: CharPointer?
val ender: CharPointer
var parno = 0
val flags = Flags()
flagp.init(HASWIDTH) /* Tentatively. */
if (paren == REG_ZPAREN) {
/* Make a ZOPEN node. */
if (regnzpar >= NSUBEXP) {
injector.messages.showStatusBarMessage(null, injector.messages.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() and (SPSTART or 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() and (SPSTART or HASNL))
}
/* Make a closing node, and hook it on the end. */ender =
regnode(if (paren == REG_ZPAREN) ZCLOSE + parno else if (paren == REG_PAREN) MCLOSE + parno else if (paren == REG_NPAREN) NCLOSE else END)
regtail(ret, ender)
/* Hook the tails of the branches to the closing node. */br = ret.ref(0)
while (br != null) {
regoptail(br, ender)
br = regnext(br)
}
/* Check for proper termination. */if (paren != REG_NOPAREN && getchr() != Magic.RPAREN) {
return if (paren == REG_ZPAREN) {
injector.messages.showStatusBarMessage(null, injector.messages.message(Msg.E52))
null
} else if (paren == REG_NPAREN) {
EMSG_M_RET_null(Msg.E53, reg_magic == MAGIC_ALL)
null
} else {
EMSG_M_RET_null(Msg.E54, reg_magic == MAGIC_ALL)
null
}
} else if (paren == REG_NOPAREN && peekchr() != '\u0000'.code) {
return if (curchr == Magic.LPAREN) {
EMSG_M_RET_null(Msg.E55, reg_magic == MAGIC_ALL)
null
} else {
injector.messages.showStatusBarMessage(null, injector.messages.message(Msg.e_trailing))
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.
*/
private fun regbranch(flagp: Flags): CharPointer? {
val ret: CharPointer
var chain: CharPointer? = null
var latest: CharPointer?
val flags = Flags()
flagp.init(WORST or HASNL) /* Tentatively. */
ret = regnode(BRANCH)
while (true) {
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() and (HASWIDTH or SPSTART))
/* If one of the branches doesn't match a line-break, the whole thing
* doesn't. */flagp.set(flagp.get() and (HASNL.inv() or (flags.get() and HASNL)))
chain?.let { regtail(it, 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.
*/
private fun regconcat(flagp: Flags): CharPointer? {
var first: CharPointer? = null
var chain: CharPointer? = null
var latest: CharPointer?
val flags = Flags()
var cont = true
flagp.init(WORST) /* Tentatively. */
while (cont) {
when (peekchr()) {
'\u0000'.code, Magic.PIPE, Magic.AMP, Magic.RPAREN -> cont = false
Magic.c -> {
regflags = regflags or RF_ICASE
skipchr_keepstart()
}
Magic.C -> {
regflags = regflags or RF_NOICASE
skipchr_keepstart()
}
Magic.v -> {
reg_magic = MAGIC_ALL
skipchr_keepstart()
curchr = -1
}
Magic.m -> {
reg_magic = MAGIC_ON
skipchr_keepstart()
curchr = -1
}
Magic.M -> {
reg_magic = MAGIC_OFF
skipchr_keepstart()
curchr = -1
}
Magic.V -> {
reg_magic = MAGIC_NONE
skipchr_keepstart()
curchr = -1
}
else -> {
latest = regpiece(flags)
if (latest == null) {
return null
}
flagp.set(flags.get() and (HASWIDTH or HASNL))
chain /* First piece. */?.let { regtail(it, latest) }
?: flagp.set(flags.get() and SPSTART)
chain = latest.ref(0)
if (first == null) {
first = latest.ref(0)
}
}
}
}
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.
*/
private fun regpiece(flagp: Flags): CharPointer? {
val ret: CharPointer?
val op: Int
val next: CharPointer
val flags = 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 or SPSTART or (flags.get() and HASNL)) /* default flags */
skipchr()
when (op) {
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. */
}
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 or HASWIDTH or (flags.get() and HASNL))
}
Magic.AT -> {
var lop = END
when (Magic.no_Magic(getchr())) {
'='.code -> lop = MATCH
'!'.code -> lop = NOMATCH
'>'.code -> lop = SUBPAT
'<'.code -> when (Magic.no_Magic(getchr())) {
'='.code -> lop = BEHIND
'!'.code -> lop = NOBEHIND
}
}
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))
}
Magic.QUESTION, 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)
}
Magic.LCURLY -> {
val limits = read_limits() ?: return null
val maxval = limits.maxvalue
val minval = limits.minvalue
if (!flags.isSet(HASWIDTH) && (if (maxval > minval) maxval >= MAX_LIMIT else 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 or (flags.get() and HASNL))
}
}
}
if (re_multi_type(peekchr()) != NOT_MULTI) {
/* Can't have a multi follow a multi. */
if (peekchr() == Magic.STAR) {
val `val` = if (reg_magic >= MAGIC_ON) "" else "\\"
injector.messages.showStatusBarMessage(null, injector.messages.message(Msg.E61, `val`))
} else {
val `val` = if (reg_magic >= MAGIC_ON) "" else "\\"
injector.messages.showStatusBarMessage(
null,
injector.messages.message(
Msg.E62,
`val`,
Character.toString(Magic.no_Magic(peekchr()).toChar()),
),
)
}
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.
*/
private fun regatom(flagp: Flags): CharPointer? {
var ret: CharPointer? = null
val flags = Flags()
val cpo_lit = false /* 'cpoptions' contains 'l' flag */
var c: Int
val classchars = ".iIkKfFpPsSdDxXoOwWhHaAlLuU"
val classcodes = intArrayOf(
ANY, IDENT, SIDENT, KWORD, SKWORD,
FNAME, SFNAME, PRINT, SPRINT,
WHITE, NWHITE, DIGIT, NDIGIT,
HEX, NHEX, OCTAL, NOCTAL,
WORD, NWORD, HEAD, NHEAD,
ALPHA, NALPHA, LOWER, NLOWER,
UPPER, NUPPER,
)
val p: CharPointer
var extra = 0
flagp.init(WORST) /* Tentatively. */
c = getchr()
var doCollection = false
var doDefault = false
when (c) {
Magic.HAT -> ret = regnode(BOL)
Magic.DOLLAR -> {
ret = regnode(EOL)
had_eol = true
}
Magic.LESS -> ret = regnode(BOW)
Magic.GREATER -> ret = regnode(EOW)
Magic.UNDER -> {
c = Magic.no_Magic(getchr())
if (c == '^'.code) /* "\_^" is start-of-line */ {
ret = regnode(BOL)
}
if (c == '$'.code) /* "\_$" is end-of-line */ {
ret = regnode(EOL)
had_eol = true
}
extra = ADD_NL
flagp.set(HASNL)
/* "\_[" is character range plus newline */if (c == '['.code) {
// goto collection;
doCollection = true
}
val i = classchars.indexOf(Magic.no_Magic(c).toChar())
if (i == -1) {
injector.messages.showStatusBarMessage(null, injector.messages.message(Msg.E63))
return null
}
ret = regnode(classcodes[i] + extra)
flagp.set(HASWIDTH or SIMPLE)
}
Magic.DOT, Magic.i, Magic.I, Magic.k, Magic.K, Magic.f, Magic.F, Magic.p, Magic.P, Magic.s, Magic.S, Magic.d, Magic.D, Magic.x, Magic.X, Magic.o, Magic.O, Magic.w, Magic.W, Magic.h, Magic.H, Magic.a, Magic.A, Magic.l, Magic.L, Magic.u, Magic.U -> {
val i = classchars.indexOf(Magic.no_Magic(c).toChar())
if (i == -1) {
injector.messages.showStatusBarMessage(null, injector.messages.message(Msg.E63))
return null
}
ret = regnode(classcodes[i] + extra)
flagp.set(HASWIDTH or SIMPLE)
}
Magic.n -> {
ret = regnode(NEWL)
flagp.set(HASWIDTH or HASNL)
}
Magic.LPAREN -> {
if (one_exactly) {
EMSG_ONE_RET_null()
return null
}
ret = reg(REG_PAREN, flags)
if (ret == null) {
return null
}
flagp.set(flags.get() and (HASWIDTH or SPSTART or HASNL))
}
'\u0000'.code, Magic.PIPE, Magic.AMP, Magic.RPAREN -> {
EMSG_RET_null(Msg.e_internal) /* Supposed to be caught earlier. */
return null
}
Magic.EQUAL, Magic.QUESTION, Magic.PLUS, Magic.AT, Magic.LCURLY, Magic.STAR -> {
c = Magic.no_Magic(c)
val `val` = if (if (c == '*'.code) reg_magic >= MAGIC_ON else reg_magic == MAGIC_ALL) "" else "\\"
injector.messages.showStatusBarMessage(
null,
injector.messages.message(
Msg.E64,
`val`,
Character.toString(c.toChar()),
),
)
return null
}
Magic.TILDE -> if (reg_prev_sub != null) {
val lp: CharPointer
ret = regnode(EXACTLY)
lp = reg_prev_sub.ref(0)
while (!lp.isNul) {
regc(lp.charAt().code)
}
lp.inc()
regc('\u0000'.code)
if (!reg_prev_sub.isNul) {
flagp.set(HASWIDTH)
if (lp.pointer() - reg_prev_sub.pointer() == 1) {
flagp.set(SIMPLE)
}
}
} else {
injector.messages.showStatusBarMessage(null, injector.messages.message(Msg.e_nopresub))
return null
}
Magic.N1, Magic.N2, Magic.N3, Magic.N4, Magic.N5, Magic.N6, Magic.N7, Magic.N8, Magic.N9 -> {
val refnum: Int
refnum = c - Magic.N0
/*
* Check if the back reference is legal. We must have seen the
* close brace.
* TODO: Should also check that we don't refer to something
* that is repeated (+*=): what instance of the repetition
* should we match?
*/if (!had_endbrace[refnum]) {
/* Trick: check if "@<=" or "@<!" follows, in which case
* the \1 can appear before the referenced match. */
p = regparse!!.ref(0)
while (!p.isNul) {
if (p.charAt(0) == '@' && p.charAt(1) == '<' && (p.charAt(2) == '!' || p.charAt(2) == '=')) {
break
}
p.inc()
}
if (p.isNul) {
EMSG_RET_null(Msg.E65)
return null
}
}
ret = regnode(BACKREF + refnum)
}
Magic.z -> {
c = Magic.no_Magic(getchr())
when (c) {
'('.code -> {
if (reg_do_extmatch != REX_SET) {
injector.messages.showStatusBarMessage(null, injector.messages.message(Msg.E66))
return null
}
if (one_exactly) {
EMSG_ONE_RET_null()
return null
}
ret = reg(REG_ZPAREN, flags)
if (ret == null) {
return null
}
flagp.set(flags.get() and (HASWIDTH or SPSTART or HASNL))
re_has_z = REX_SET.toChar()
}
'1'.code, '2'.code, '3'.code, '4'.code, '5'.code, '6'.code, '7'.code, '8'.code, '9'.code -> {
if (reg_do_extmatch != REX_USE) {
injector.messages.showStatusBarMessage(null, injector.messages.message(Msg.E67))
return null
}
ret = regnode(ZREF + c - '0'.code)
re_has_z = REX_USE.toChar()
}
's'.code -> ret = regnode(MOPEN)
'e'.code -> ret = regnode(MCLOSE)
else -> {
injector.messages.showStatusBarMessage(null, injector.messages.message(Msg.E68))
return null
}
}
}
Magic.PERCENT -> {
c = Magic.no_Magic(getchr())
when (c) {
'('.code -> {
if (one_exactly) {
EMSG_ONE_RET_null()
return null
}
ret = reg(REG_NPAREN, flags)
if (ret == null) {
return null
}
flagp.set(flags.get() and (HASWIDTH or SPSTART or HASNL))
}
'^'.code -> ret = regnode(RE_BOF)
'$'.code -> ret = regnode(RE_EOF)
'#'.code -> ret = regnode(CURSOR)
'['.code -> if (one_exactly) /* doesn't nest */ {
EMSG_ONE_RET_null()
return null
} else {
val lastbranch: CharPointer
var lastnode: CharPointer? = null
var br: CharPointer?
ret = null
while (getchr().also { c = it } != ']'.code) {
if (c == '\u0000'.code) {
EMSG_M_RET_null(Msg.E69, reg_magic == MAGIC_ALL)
return null
}
br = regnode(BRANCH)
if (ret == null) {
ret = br.ref(0)
} else {
regtail(lastnode!!, br)
}
ungetchr()
one_exactly = true
lastnode = regatom(flagp)
one_exactly = false
if (lastnode == null) {
return null
}
}
if (ret == null) {
EMSG_M_RET_null(Msg.E70, reg_magic == MAGIC_ALL)
return null
}
lastbranch = regnode(BRANCH)
br = regnode(NOTHING)
regtail(lastnode!!, br)
regtail(lastbranch, br)
/* connect all branches to the NOTHING
* branch at the end */br = ret.ref(0)
while (br !== lastnode) {
br = if (br!!.OP() == BRANCH) {
regtail(br, lastbranch)
br.OPERAND()
} else {
regnext(br)
}
}
flagp.unset(HASWIDTH)
}
else -> {
if (Character.isDigit(c.toChar()) || c == '<'.code || c == '>'.code) {
var n = 0
val cmp: Int
cmp = c
if (cmp == '<'.code || cmp == '>'.code) {
c = getchr()
}
while (Character.isDigit(c.toChar())) {
n = n * 10 + (c - '0'.code)
c = getchr()
}
if (c == 'l'.code || c == 'c'.code || c == 'v'.code) {
ret = if (c == 'l'.code) {
regnode(RE_LNUM)
} else if (c == 'c'.code) {
regnode(RE_COL)
} else {
regnode(RE_VCOL)
}
/* put the number and the optional
* comparator after the opcode */regcode = re_put_long(regcode!!.ref(0), n)
regcode!!.set(cmp.toChar()).inc()
}
}
EMSG_M_RET_null(Msg.E71, reg_magic == MAGIC_ALL)
return null
}
}
}
Magic.LBRACE -> doCollection = true
else -> doDefault = true
}
if (doCollection) {
val lp: CharPointer
/*
* If there is no matching ']', we assume the '[' is a normal
* character. This makes 'incsearch' and ":help [" work.
*/lp = skip_anyof(regparse!!.ref(0))
if (lp.charAt() == ']') /* there is a matching ']' */ {
var startc = -1 /* > 0 when next '-' is a range */
var endc: Int
/*
* In a character class, different parsing rules apply.
* Not even \ is special anymore, nothing is.
*/if (regparse!!.charAt() == '^') /* Complement of range. */ {
ret = regnode(ANYBUT + extra)
regparse!!.inc()
} else {
ret = regnode(ANYOF + extra)
}
/* At the start ']' and '-' mean the literal character. */if (regparse!!.charAt() == ']' || regparse!!.charAt() == '-') {
regc(regparse!!.charAt().code)
regparse!!.inc()
}
while (!regparse!!.isNul && regparse!!.charAt() != ']') {
if (regparse!!.charAt() == '-') {
regparse!!.inc()
/* The '-' is not used for a range at the end and
* after or before a '\n'. */if (regparse!!.isNul || regparse!!.charAt() == ']' || startc == -1 ||
regparse!!.charAt(0) == '\\' && regparse!!.charAt(1) == 'n'
) {
regc('-'.code)
startc = '-'.code /* [--x] is a range */
} else {
// endc = *regparse++;
endc = regparse!!.charAt().code
regparse!!.inc()
if (startc > endc) {
injector.messages.showStatusBarMessage(null, injector.messages.message(Msg.e_invrange))
return null
}
while (++startc <= endc) {
regc(startc)
}
startc = -1
}
} else if (regparse!!.charAt() == '\\' &&
(
REGEXP_INRANGE.indexOf(regparse!!.charAt(1)) != -1 || !cpo_lit &&
REGEXP_ABBR.indexOf(regparse!!.charAt(1)) != -1
)
) {
regparse!!.inc()
if (regparse!!.charAt() == 'n') {
/* '\n' in range: also match NL */
if (ret.charAt().code == ANYBUT) {
ret.set((ANYBUT + ADD_NL).toChar())
} else if (ret.charAt().code == ANYOF) {
ret.set((ANYOF + ADD_NL).toChar())
}
/* else: must have had a \n already */flagp.set(HASNL)
regparse!!.inc()
startc = -1
} else {
startc = backslash_trans(regparse!!.charAt().code)
regparse!!.inc()
regc(startc)
}
} else if (regparse!!.charAt() == '[') {
var c_class: Int
var cu: Int
c_class = skip_class_name(regparse!!)
startc = -1
when (c_class) {
CharacterClasses.CLASS_NONE -> {
/* literal '[', allow [[-x] as a range */startc = regparse!!.charAt().code
regparse!!.inc()
regc(startc)
}
CharacterClasses.CLASS_ALNUM -> {
cu = 1
while (cu <= 255) {
if (Character.isLetterOrDigit(cu.toChar())) {
regc(cu)
}
cu++
}
}
CharacterClasses.CLASS_ALPHA -> {
cu = 1
while (cu <= 255) {
if (Character.isLetter(cu.toChar())) {
regc(cu)
}
cu++
}
}
CharacterClasses.CLASS_BLANK -> {
regc(' '.code)
regc('\t'.code)
}
CharacterClasses.CLASS_CNTRL -> {
cu = 1
while (cu <= 255) {
if (Character.isISOControl(cu.toChar())) {
regc(cu)
}
cu++
}
}
CharacterClasses.CLASS_DIGIT -> {
cu = 1
while (cu <= 255) {
if (Character.isDigit(cu.toChar())) {
regc(cu)
}
cu++