/
regexp_nfa.c
3958 lines (3574 loc) · 92.6 KB
/
regexp_nfa.c
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
/* vi:set ts=8 sts=4 sw=4:
*
* NFA regular expression implementation.
*
* This file is included in "regexp.c".
*/
/*
* Logging of NFA engine.
*
* The NFA engine can write four log files:
* - Error log: Contains NFA engine's fatal errors.
* - Dump log: Contains compiled NFA state machine's information.
* - Run log: Contains information of matching procedure.
* - Debug log: Contains detailed information of matching procedure. Can be
* disabled by undefining NFA_REGEXP_DEBUG_LOG.
* The first one can also be used without debug mode.
* The last three are enabled when compiled as debug mode and individually
* disabled by commenting them out.
* The log files can get quite big!
* Do disable all of this when compiling Vim for debugging, undefine DEBUG in
* regexp.c
*/
#ifdef DEBUG
# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
# define ENABLE_LOG
# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
#endif
/* Upper limit allowed for {m,n} repetitions handled by NFA */
#define NFA_BRACES_MAXLIMIT 10
/* For allocating space for the postfix representation */
#define NFA_POSTFIX_MULTIPLIER (NFA_BRACES_MAXLIMIT + 2)*2
enum
{
NFA_SPLIT = -1024,
NFA_MATCH,
NFA_SKIP_CHAR, /* matches a 0-length char */
NFA_END_NEG_RANGE, /* Used when expanding [^ab] */
NFA_CONCAT,
NFA_OR,
NFA_STAR,
NFA_PLUS,
NFA_QUEST,
NFA_QUEST_NONGREEDY, /* Non-greedy version of \? */
NFA_NOT, /* used for [^ab] negated char ranges */
NFA_BOL, /* ^ Begin line */
NFA_EOL, /* $ End line */
NFA_BOW, /* \< Begin word */
NFA_EOW, /* \> End word */
NFA_BOF, /* \%^ Begin file */
NFA_EOF, /* \%$ End file */
NFA_NEWL,
NFA_ZSTART, /* Used for \zs */
NFA_ZEND, /* Used for \ze */
NFA_NOPEN, /* Start of subexpression marked with \%( */
NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
NFA_START_INVISIBLE,
NFA_END_INVISIBLE,
NFA_COMPOSING, /* Next nodes in NFA are part of the
composing multibyte char */
NFA_END_COMPOSING, /* End of a composing char in the NFA */
/* The following are used only in the postfix form, not in the NFA */
NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
NFA_MOPEN,
NFA_MCLOSE = NFA_MOPEN + NSUBEXP,
/* NFA_FIRST_NL */
NFA_ANY = NFA_MCLOSE + NSUBEXP, /* Match any one character. */
NFA_ANYOF, /* Match any character in this string. */
NFA_ANYBUT, /* Match any character not in this string. */
NFA_IDENT, /* Match identifier char */
NFA_SIDENT, /* Match identifier char but no digit */
NFA_KWORD, /* Match keyword char */
NFA_SKWORD, /* Match word char but no digit */
NFA_FNAME, /* Match file name char */
NFA_SFNAME, /* Match file name char but no digit */
NFA_PRINT, /* Match printable char */
NFA_SPRINT, /* Match printable char but no digit */
NFA_WHITE, /* Match whitespace char */
NFA_NWHITE, /* Match non-whitespace char */
NFA_DIGIT, /* Match digit char */
NFA_NDIGIT, /* Match non-digit char */
NFA_HEX, /* Match hex char */
NFA_NHEX, /* Match non-hex char */
NFA_OCTAL, /* Match octal char */
NFA_NOCTAL, /* Match non-octal char */
NFA_WORD, /* Match word char */
NFA_NWORD, /* Match non-word char */
NFA_HEAD, /* Match head char */
NFA_NHEAD, /* Match non-head char */
NFA_ALPHA, /* Match alpha char */
NFA_NALPHA, /* Match non-alpha char */
NFA_LOWER, /* Match lowercase char */
NFA_NLOWER, /* Match non-lowercase char */
NFA_UPPER, /* Match uppercase char */
NFA_NUPPER, /* Match non-uppercase char */
NFA_FIRST_NL = NFA_ANY + ADD_NL,
NFA_LAST_NL = NFA_NUPPER + ADD_NL,
/* Character classes [:alnum:] etc */
NFA_CLASS_ALNUM,
NFA_CLASS_ALPHA,
NFA_CLASS_BLANK,
NFA_CLASS_CNTRL,
NFA_CLASS_DIGIT,
NFA_CLASS_GRAPH,
NFA_CLASS_LOWER,
NFA_CLASS_PRINT,
NFA_CLASS_PUNCT,
NFA_CLASS_SPACE,
NFA_CLASS_UPPER,
NFA_CLASS_XDIGIT,
NFA_CLASS_TAB,
NFA_CLASS_RETURN,
NFA_CLASS_BACKSPACE,
NFA_CLASS_ESCAPE
};
/* Keep in sync with classchars. */
static int nfa_classcodes[] = {
NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
NFA_UPPER, NFA_NUPPER
};
static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
/*
* NFA errors can be of 3 types:
* *** NFA runtime errors, when something unknown goes wrong. The NFA fails
* silently and revert the to backtracking engine.
* syntax_error = FALSE;
* *** Regexp syntax errors, when the input regexp is not syntactically correct.
* The NFA engine displays an error message, and nothing else happens.
* syntax_error = TRUE
* *** Unsupported features, when the input regexp uses an operator that is not
* implemented in the NFA. The NFA engine fails silently, and reverts to the
* old backtracking engine.
* syntax_error = FALSE
* "The NFA fails" means that "compiling the regexp with the NFA fails":
* nfa_regcomp() returns FAIL.
*/
static int syntax_error = FALSE;
/* NFA regexp \ze operator encountered. */
static int nfa_has_zend = FALSE;
static int *post_start; /* holds the postfix form of r.e. */
static int *post_end;
static int *post_ptr;
static int nstate; /* Number of states in the NFA. */
static int istate; /* Index in the state vector, used in new_state() */
static int nstate_max; /* Upper bound of estimated number of states. */
static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags));
static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
static int nfa_emit_equi_class __ARGS((int c, int neg));
static void nfa_inc __ARGS((char_u **p));
static void nfa_dec __ARGS((char_u **p));
static int nfa_regatom __ARGS((void));
static int nfa_regpiece __ARGS((void));
static int nfa_regconcat __ARGS((void));
static int nfa_regbranch __ARGS((void));
static int nfa_reg __ARGS((int paren));
#ifdef DEBUG
static void nfa_set_code __ARGS((int c));
static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
static void nfa_dump __ARGS((nfa_regprog_T *prog));
#endif
static int *re2post __ARGS((void));
static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
static int check_char_class __ARGS((int class, int c));
static void st_error __ARGS((int *postfix, int *end, int *p));
static void nfa_save_listids __ARGS((nfa_state_T *start, int *list));
static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list));
static void nfa_set_null_listids __ARGS((nfa_state_T *start));
static void nfa_set_neg_listids __ARGS((nfa_state_T *start));
static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col));
static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
/* helper functions used when doing re2post() ... regatom() parsing */
#define EMIT(c) do { \
if (post_ptr >= post_end) \
return FAIL; \
*post_ptr++ = c; \
} while (0)
/*
* Initialize internal variables before NFA compilation.
* Return OK on success, FAIL otherwise.
*/
static int
nfa_regcomp_start(expr, re_flags)
char_u *expr;
int re_flags; /* see vim_regcomp() */
{
size_t postfix_size;
nstate = 0;
istate = 0;
/* A reasonable estimation for size */
nstate_max = (int)(STRLEN(expr) + 1) * NFA_POSTFIX_MULTIPLIER;
/* Some items blow up in size, such as [A-z]. Add more space for that.
* TODO: some patterns may still fail. */
nstate_max += 1000;
/* Size for postfix representation of expr. */
postfix_size = sizeof(*post_start) * nstate_max;
post_start = (int *)lalloc(postfix_size, TRUE);
if (post_start == NULL)
return FAIL;
vim_memset(post_start, 0, postfix_size);
post_ptr = post_start;
post_end = post_start + nstate_max;
nfa_has_zend = FALSE;
regcomp_start(expr, re_flags);
return OK;
}
/*
* Search between "start" and "end" and try to recognize a
* character class in expanded form. For example [0-9].
* On success, return the id the character class to be emitted.
* On failure, return 0 (=FAIL)
* Start points to the first char of the range, while end should point
* to the closing brace.
*/
static int
nfa_recognize_char_class(start, end, extra_newl)
char_u *start;
char_u *end;
int extra_newl;
{
int i;
/* Each of these variables takes up a char in "config[]",
* in the order they are here. */
int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE,
o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE;
char_u *p;
#define NCONFIGS 16
int classid[NCONFIGS] = {
NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX,
NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD,
NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA,
NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER
};
char_u myconfig[10];
char_u config[NCONFIGS][9] = {
"000000100", /* digit */
"100000100", /* non digit */
"011000100", /* hex-digit */
"111000100", /* non hex-digit */
"000001000", /* octal-digit */
"100001000", /* [^0-7] */
"000110110", /* [0-9A-Za-z_] */
"100110110", /* [^0-9A-Za-z_] */
"000110010", /* head of word */
"100110010", /* not head of word */
"000110000", /* alphabetic char a-z */
"100110000", /* non alphabetic char */
"000100000", /* lowercase letter */
"100100000", /* non lowercase */
"000010000", /* uppercase */
"100010000" /* non uppercase */
};
if (extra_newl == TRUE)
newl = TRUE;
if (*end != ']')
return FAIL;
p = start;
if (*p == '^')
{
not = TRUE;
p ++;
}
while (p < end)
{
if (p + 2 < end && *(p + 1) == '-')
{
switch (*p)
{
case '0':
if (*(p + 2) == '9')
{
o9 = TRUE;
break;
}
else
if (*(p + 2) == '7')
{
o7 = TRUE;
break;
}
case 'a':
if (*(p + 2) == 'z')
{
az = TRUE;
break;
}
else
if (*(p + 2) == 'f')
{
af = TRUE;
break;
}
case 'A':
if (*(p + 2) == 'Z')
{
AZ = TRUE;
break;
}
else
if (*(p + 2) == 'F')
{
AF = TRUE;
break;
}
/* FALLTHROUGH */
default:
return FAIL;
}
p += 3;
}
else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
{
newl = TRUE;
p += 2;
}
else if (*p == '_')
{
underscore = TRUE;
p ++;
}
else if (*p == '\n')
{
newl = TRUE;
p ++;
}
else
return FAIL;
} /* while (p < end) */
if (p != end)
return FAIL;
/* build the config that represents the ranges we gathered */
STRCPY(myconfig, "000000000");
if (not == TRUE)
myconfig[0] = '1';
if (af == TRUE)
myconfig[1] = '1';
if (AF == TRUE)
myconfig[2] = '1';
if (az == TRUE)
myconfig[3] = '1';
if (AZ == TRUE)
myconfig[4] = '1';
if (o7 == TRUE)
myconfig[5] = '1';
if (o9 == TRUE)
myconfig[6] = '1';
if (underscore == TRUE)
myconfig[7] = '1';
if (newl == TRUE)
{
myconfig[8] = '1';
extra_newl = ADD_NL;
}
/* try to recognize character classes */
for (i = 0; i < NCONFIGS; i++)
if (STRNCMP(myconfig, config[i], 8) == 0)
return classid[i] + extra_newl;
/* fallthrough => no success so far */
return FAIL;
#undef NCONFIGS
}
/*
* Produce the bytes for equivalence class "c".
* Currently only handles latin1, latin9 and utf-8.
* Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
* equivalent to 'a OR b OR c'
*
* NOTE! When changing this function, also update reg_equi_class()
*/
static int
nfa_emit_equi_class(c, neg)
int c;
int neg;
{
int first = TRUE;
int glue = neg == TRUE ? NFA_CONCAT : NFA_OR;
#define EMIT2(c) \
EMIT(c); \
if (neg == TRUE) { \
EMIT(NFA_NOT); \
} \
if (first == FALSE) \
EMIT(glue); \
else \
first = FALSE; \
#ifdef FEAT_MBYTE
if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
|| STRCMP(p_enc, "iso-8859-15") == 0)
#endif
{
switch (c)
{
case 'A': case '\300': case '\301': case '\302':
case '\303': case '\304': case '\305':
EMIT2('A'); EMIT2('\300'); EMIT2('\301');
EMIT2('\302'); EMIT2('\303'); EMIT2('\304');
EMIT2('\305');
return OK;
case 'C': case '\307':
EMIT2('C'); EMIT2('\307');
return OK;
case 'E': case '\310': case '\311': case '\312': case '\313':
EMIT2('E'); EMIT2('\310'); EMIT2('\311');
EMIT2('\312'); EMIT2('\313');
return OK;
case 'I': case '\314': case '\315': case '\316': case '\317':
EMIT2('I'); EMIT2('\314'); EMIT2('\315');
EMIT2('\316'); EMIT2('\317');
return OK;
case 'N': case '\321':
EMIT2('N'); EMIT2('\321');
return OK;
case 'O': case '\322': case '\323': case '\324': case '\325':
case '\326':
EMIT2('O'); EMIT2('\322'); EMIT2('\323');
EMIT2('\324'); EMIT2('\325'); EMIT2('\326');
return OK;
case 'U': case '\331': case '\332': case '\333': case '\334':
EMIT2('U'); EMIT2('\331'); EMIT2('\332');
EMIT2('\333'); EMIT2('\334');
return OK;
case 'Y': case '\335':
EMIT2('Y'); EMIT2('\335');
return OK;
case 'a': case '\340': case '\341': case '\342':
case '\343': case '\344': case '\345':
EMIT2('a'); EMIT2('\340'); EMIT2('\341');
EMIT2('\342'); EMIT2('\343'); EMIT2('\344');
EMIT2('\345');
return OK;
case 'c': case '\347':
EMIT2('c'); EMIT2('\347');
return OK;
case 'e': case '\350': case '\351': case '\352': case '\353':
EMIT2('e'); EMIT2('\350'); EMIT2('\351');
EMIT2('\352'); EMIT2('\353');
return OK;
case 'i': case '\354': case '\355': case '\356': case '\357':
EMIT2('i'); EMIT2('\354'); EMIT2('\355');
EMIT2('\356'); EMIT2('\357');
return OK;
case 'n': case '\361':
EMIT2('n'); EMIT2('\361');
return OK;
case 'o': case '\362': case '\363': case '\364': case '\365':
case '\366':
EMIT2('o'); EMIT2('\362'); EMIT2('\363');
EMIT2('\364'); EMIT2('\365'); EMIT2('\366');
return OK;
case 'u': case '\371': case '\372': case '\373': case '\374':
EMIT2('u'); EMIT2('\371'); EMIT2('\372');
EMIT2('\373'); EMIT2('\374');
return OK;
case 'y': case '\375': case '\377':
EMIT2('y'); EMIT2('\375'); EMIT2('\377');
return OK;
default:
return FAIL;
}
}
EMIT(c);
return OK;
#undef EMIT2
}
/*
* Code to parse regular expression.
*
* We try to reuse parsing functions in regexp.c to
* minimize surprise and keep the syntax consistent.
*/
/*
* Increments the pointer "p" by one (multi-byte) character.
*/
static void
nfa_inc(p)
char_u **p;
{
#ifdef FEAT_MBYTE
if (has_mbyte)
mb_ptr2char_adv(p);
else
#endif
*p = *p + 1;
}
/*
* Decrements the pointer "p" by one (multi-byte) character.
*/
static void
nfa_dec(p)
char_u **p;
{
#ifdef FEAT_MBYTE
char_u *p2, *oldp;
if (has_mbyte)
{
oldp = *p;
/* Try to find the multibyte char that advances to the current
* position. */
do
{
*p = *p - 1;
p2 = *p;
mb_ptr2char_adv(&p2);
} while (p2 != oldp);
}
#else
*p = *p - 1;
#endif
}
/*
* Parse the lowest level.
*
* An atom can be one of a long list of items. Many atoms match one character
* in the text. It is often an ordinary character or a character class.
* Braces can be used to make a pattern into an atom. The "\z(\)" construct
* is only for syntax highlighting.
*
* atom ::= ordinary-atom
* or \( pattern \)
* or \%( pattern \)
* or \z( pattern \)
*/
static int
nfa_regatom()
{
int c;
int charclass;
int equiclass;
int collclass;
int got_coll_char;
char_u *p;
char_u *endp;
#ifdef FEAT_MBYTE
char_u *old_regparse = regparse;
int clen;
int i;
#endif
int extra = 0;
int first;
int emit_range;
int negated;
int result;
int startc = -1;
int endc = -1;
int oldstartc = -1;
int cpo_lit; /* 'cpoptions' contains 'l' flag */
int cpo_bsl; /* 'cpoptions' contains '\' flag */
int glue; /* ID that will "glue" nodes together */
cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
c = getchr();
#ifdef FEAT_MBYTE
/* clen has the length of the current char, without composing chars */
clen = (*mb_char2len)(c);
if (has_mbyte && clen > 1)
goto nfa_do_multibyte;
#endif
switch (c)
{
case Magic('^'):
EMIT(NFA_BOL);
break;
case Magic('$'):
EMIT(NFA_EOL);
#if defined(FEAT_SYN_HL) || defined(PROTO)
had_eol = TRUE;
#endif
break;
case Magic('<'):
EMIT(NFA_BOW);
break;
case Magic('>'):
EMIT(NFA_EOW);
break;
case Magic('_'):
c = no_Magic(getchr());
if (c == '^') /* "\_^" is start-of-line */
{
EMIT(NFA_BOL);
break;
}
if (c == '$') /* "\_$" is end-of-line */
{
EMIT(NFA_EOL);
#if defined(FEAT_SYN_HL) || defined(PROTO)
had_eol = TRUE;
#endif
break;
}
extra = ADD_NL;
/* "\_[" is collection plus newline */
if (c == '[')
goto collection;
/* "\_x" is character class plus newline */
/*FALLTHROUGH*/
/*
* Character classes.
*/
case Magic('.'):
case Magic('i'):
case Magic('I'):
case Magic('k'):
case Magic('K'):
case Magic('f'):
case Magic('F'):
case Magic('p'):
case Magic('P'):
case Magic('s'):
case Magic('S'):
case Magic('d'):
case Magic('D'):
case Magic('x'):
case Magic('X'):
case Magic('o'):
case Magic('O'):
case Magic('w'):
case Magic('W'):
case Magic('h'):
case Magic('H'):
case Magic('a'):
case Magic('A'):
case Magic('l'):
case Magic('L'):
case Magic('u'):
case Magic('U'):
p = vim_strchr(classchars, no_Magic(c));
if (p == NULL)
{
return FAIL; /* runtime error */
}
#ifdef FEAT_MBYTE
/* When '.' is followed by a composing char ignore the dot, so that
* the composing char is matched here. */
if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
{
old_regparse = regparse;
c = getchr();
goto nfa_do_multibyte;
}
#endif
EMIT(nfa_classcodes[p - classchars]);
if (extra == ADD_NL)
{
EMIT(NFA_NEWL);
EMIT(NFA_OR);
regflags |= RF_HASNL;
}
break;
case Magic('n'):
if (reg_string)
/* In a string "\n" matches a newline character. */
EMIT(NL);
else
{
/* In buffer text "\n" matches the end of a line. */
EMIT(NFA_NEWL);
regflags |= RF_HASNL;
}
break;
case Magic('('):
if (nfa_reg(REG_PAREN) == FAIL)
return FAIL; /* cascaded error */
break;
case NUL:
syntax_error = TRUE;
EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
case Magic('|'):
case Magic('&'):
case Magic(')'):
syntax_error = TRUE;
EMSGN(_(e_misplaced), no_Magic(c));
return FAIL;
case Magic('='):
case Magic('?'):
case Magic('+'):
case Magic('@'):
case Magic('*'):
case Magic('{'):
/* these should follow an atom, not form an atom */
syntax_error = TRUE;
EMSGN(_(e_misplaced), no_Magic(c));
return FAIL;
case Magic('~'): /* previous substitute pattern */
/* Not supported yet */
return FAIL;
case Magic('1'):
case Magic('2'):
case Magic('3'):
case Magic('4'):
case Magic('5'):
case Magic('6'):
case Magic('7'):
case Magic('8'):
case Magic('9'):
/* not supported yet */
return FAIL;
case Magic('z'):
c = no_Magic(getchr());
switch (c)
{
case 's':
EMIT(NFA_ZSTART);
break;
case 'e':
EMIT(NFA_ZEND);
nfa_has_zend = TRUE;
/* TODO: Currently \ze does not work properly. */
return FAIL;
/* break; */
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '(':
/* \z1...\z9 and \z( not yet supported */
return FAIL;
default:
syntax_error = TRUE;
EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
no_Magic(c));
return FAIL;
}
break;
case Magic('%'):
c = no_Magic(getchr());
switch (c)
{
/* () without a back reference */
case '(':
if (nfa_reg(REG_NPAREN) == FAIL)
return FAIL;
EMIT(NFA_NOPEN);
break;
case 'd': /* %d123 decimal */
case 'o': /* %o123 octal */
case 'x': /* %xab hex 2 */
case 'u': /* %uabcd hex 4 */
case 'U': /* %U1234abcd hex 8 */
/* Not yet supported */
return FAIL;
c = coll_get_char();
EMIT(c);
break;
/* Catch \%^ and \%$ regardless of where they appear in the
* pattern -- regardless of whether or not it makes sense. */
case '^':
EMIT(NFA_BOF);
/* Not yet supported */
return FAIL;
break;
case '$':
EMIT(NFA_EOF);
/* Not yet supported */
return FAIL;
break;
case '#':
/* not supported yet */
return FAIL;
break;
case 'V':
/* not supported yet */
return FAIL;
break;
case '[':
/* \%[abc] not supported yet */
return FAIL;
default:
/* not supported yet */
return FAIL;
}
break;
case Magic('['):
collection:
/*
* Glue is emitted between several atoms from the [].
* It is either NFA_OR, or NFA_CONCAT.
*
* [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation)
* [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT
* NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix
* notation)
*
*/
/* Emit negation atoms, if needed.
* The CONCAT below merges the NOT with the previous node. */
#define TRY_NEG() \
if (negated == TRUE) \
{ \
EMIT(NFA_NOT); \
}
/* Emit glue between important nodes : CONCAT or OR. */
#define EMIT_GLUE() \
if (first == FALSE) \
EMIT(glue); \
else \
first = FALSE;
p = regparse;
endp = skip_anyof(p);
if (*endp == ']')
{
/*
* Try to reverse engineer character classes. For example,
* recognize that [0-9] stands for \d and [A-Za-z_] with \h,
* and perform the necessary substitutions in the NFA.
*/
result = nfa_recognize_char_class(regparse, endp,
extra == ADD_NL);
if (result != FAIL)
{
if (result >= NFA_DIGIT && result <= NFA_NUPPER)
EMIT(result);
else /* must be char class + newline */
{
EMIT(result - ADD_NL);
EMIT(NFA_NEWL);
EMIT(NFA_OR);
}
regparse = endp;
nfa_inc(®parse);
return OK;
}
/*
* Failed to recognize a character class. Use the simple
* version that turns [abc] into 'a' OR 'b' OR 'c'
*/
startc = endc = oldstartc = -1;
first = TRUE; /* Emitting first atom in this sequence? */
negated = FALSE;
glue = NFA_OR;
if (*regparse == '^') /* negated range */
{
negated = TRUE;
glue = NFA_CONCAT;
nfa_inc(®parse);
}
if (*regparse == '-')
{
startc = '-';
EMIT(startc);
TRY_NEG();
EMIT_GLUE();
nfa_inc(®parse);
}
/* Emit the OR branches for each character in the [] */
emit_range = FALSE;
while (regparse < endp)
{
oldstartc = startc;
startc = -1;
got_coll_char = FALSE;
if (*regparse == '[')
{
/* Check for [: :], [= =], [. .] */
equiclass = collclass = 0;
charclass = get_char_class(®parse);
if (charclass == CLASS_NONE)
{
equiclass = get_equi_class(®parse);
if (equiclass == 0)
collclass = get_coll_element(®parse);
}
/* Character class like [:alpha:] */
if (charclass != CLASS_NONE)
{
switch (charclass)
{
case CLASS_ALNUM:
EMIT(NFA_CLASS_ALNUM);
break;
case CLASS_ALPHA:
EMIT(NFA_CLASS_ALPHA);
break;
case CLASS_BLANK:
EMIT(NFA_CLASS_BLANK);
break;
case CLASS_CNTRL:
EMIT(NFA_CLASS_CNTRL);
break;
case CLASS_DIGIT:
EMIT(NFA_CLASS_DIGIT);
break;
case CLASS_GRAPH:
EMIT(NFA_CLASS_GRAPH);
break;
case CLASS_LOWER:
EMIT(NFA_CLASS_LOWER);
break;
case CLASS_PRINT:
EMIT(NFA_CLASS_PRINT);