-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregexp.c
3084 lines (2804 loc) · 75.3 KB
/
regexp.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 noet:
*
* Handling of regular expressions: vim_regcomp(), vim_regexec(), vim_regsub()
*/
// By default: do not create debugging logs or files related to regular
// expressions, even when compiling with -DDEBUG.
// Uncomment the second line to get the regexp debugging.
#undef DEBUG
// #define DEBUG
#include "vim.h"
#ifdef DEBUG
// show/save debugging data when BT engine is used
# define BT_REGEXP_DUMP
// save the debugging data to a file instead of displaying it
# define BT_REGEXP_LOG
# define BT_REGEXP_DEBUG_LOG
# define BT_REGEXP_DEBUG_LOG_NAME "bt_regexp_debug.log"
#endif
#ifdef FEAT_RELTIME
static sig_atomic_t dummy_timeout_flag = 0;
static volatile sig_atomic_t *timeout_flag = &dummy_timeout_flag;
#endif
/*
* Magic characters have a special meaning, they don't match literally.
* Magic characters are negative. This separates them from literal characters
* (possibly multi-byte). Only ASCII characters can be Magic.
*/
#define Magic(x) ((int)(x) - 256)
#define un_Magic(x) ((x) + 256)
#define is_Magic(x) ((x) < 0)
static int
no_Magic(int x)
{
if (is_Magic(x))
return un_Magic(x);
return x;
}
static int
toggle_Magic(int x)
{
if (is_Magic(x))
return un_Magic(x);
return Magic(x);
}
#ifdef FEAT_RELTIME
static int timeout_nesting = 0;
/*
* Start a timer that will cause the regexp to abort after "msec".
* This doesn't work well recursively. In case it happens anyway, the first
* set timeout will prevail, nested ones are ignored.
* The caller must make sure there is a matching disable_regexp_timeout() call!
*/
void
init_regexp_timeout(long msec)
{
if (timeout_nesting == 0)
timeout_flag = start_timeout(msec);
++timeout_nesting;
}
void
disable_regexp_timeout(void)
{
if (timeout_nesting == 0)
iemsg("disable_regexp_timeout() called without active timer");
else if (--timeout_nesting == 0)
{
stop_timeout();
timeout_flag = &dummy_timeout_flag;
}
}
#endif
#if defined(FEAT_EVAL) || defined(PROTO)
# ifdef FEAT_RELTIME
static sig_atomic_t *saved_timeout_flag;
# endif
/*
* Used at the debug prompt: disable the timeout so that expression evaluation
* can used patterns.
* Must be followed by calling restore_timeout_for_debugging().
*/
void
save_timeout_for_debugging(void)
{
# ifdef FEAT_RELTIME
saved_timeout_flag = (sig_atomic_t *)timeout_flag;
timeout_flag = &dummy_timeout_flag;
# endif
}
void
restore_timeout_for_debugging(void)
{
# ifdef FEAT_RELTIME
timeout_flag = saved_timeout_flag;
# endif
}
#endif
/*
* The first byte of the BT 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.
*/
#define REGMAGIC 0234
/*
* 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) return (emsg((m)), rc_did_emsg = TRUE, (void *)NULL)
#define IEMSG_RET_NULL(m) return (iemsg((m)), rc_did_emsg = TRUE, (void *)NULL)
#define EMSG_RET_FAIL(m) return (emsg((m)), rc_did_emsg = TRUE, FAIL)
#define EMSG2_RET_NULL(m, c) return (semsg((const char *)(m), (c) ? "" : "\\"), rc_did_emsg = TRUE, (void *)NULL)
#define EMSG3_RET_NULL(m, c, a) return (semsg((const char *)(m), (c) ? "" : "\\", (a)), rc_did_emsg = TRUE, (void *)NULL)
#define EMSG2_RET_FAIL(m, c) return (semsg((const char *)(m), (c) ? "" : "\\"), rc_did_emsg = TRUE, FAIL)
#define EMSG_ONE_RET_NULL EMSG2_RET_NULL(_(e_invalid_item_in_str_brackets), reg_magic == MAGIC_ALL)
#define MAX_LIMIT (32767L << 16L)
#define NOT_MULTI 0
#define MULTI_ONE 1
#define MULTI_MULT 2
// return values for regmatch()
#define RA_FAIL 1 // something failed, abort
#define RA_CONT 2 // continue in inner loop
#define RA_BREAK 3 // break inner loop
#define RA_MATCH 4 // successful match
#define RA_NOMATCH 5 // didn't match
/*
* 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.
*/
static int
re_multi_type(int c)
{
if (c == Magic('@') || c == Magic('=') || c == Magic('?'))
return MULTI_ONE;
if (c == Magic('*') || c == Magic('+') || c == Magic('{'))
return MULTI_MULT;
return NOT_MULTI;
}
static char_u *reg_prev_sub = NULL;
/*
* 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).
* \d - Character code in decimal, eg \d123
* \o - Character code in octal, eg \o80
* \x - Character code in hex, eg \x4a
* \u - Multibyte character code, eg \u20ac
* \U - Long multibyte character code, eg \U12345678
*/
static char_u REGEXP_INRANGE[] = "]^-n\\";
static char_u REGEXP_ABBR[] = "nrtebdoxuU";
/*
* Translate '\x' to its control character, except "\n", which is Magic.
*/
static int
backslash_trans(int c)
{
switch (c)
{
case 'r': return CAR;
case 't': return TAB;
case 'e': return ESC;
case 'b': return BS;
}
return c;
}
/*
* Check for a character class name "[: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.
*/
static int
get_char_class(char_u **pp)
{
static const char *(class_names[]) =
{
"alnum:]",
#define CLASS_ALNUM 0
"alpha:]",
#define CLASS_ALPHA 1
"blank:]",
#define CLASS_BLANK 2
"cntrl:]",
#define CLASS_CNTRL 3
"digit:]",
#define CLASS_DIGIT 4
"graph:]",
#define CLASS_GRAPH 5
"lower:]",
#define CLASS_LOWER 6
"print:]",
#define CLASS_PRINT 7
"punct:]",
#define CLASS_PUNCT 8
"space:]",
#define CLASS_SPACE 9
"upper:]",
#define CLASS_UPPER 10
"xdigit:]",
#define CLASS_XDIGIT 11
"tab:]",
#define CLASS_TAB 12
"return:]",
#define CLASS_RETURN 13
"backspace:]",
#define CLASS_BACKSPACE 14
"escape:]",
#define CLASS_ESCAPE 15
"ident:]",
#define CLASS_IDENT 16
"keyword:]",
#define CLASS_KEYWORD 17
"fname:]",
#define CLASS_FNAME 18
};
#define CLASS_NONE 99
int i;
if ((*pp)[1] == ':')
{
for (i = 0; i < (int)ARRAY_LENGTH(class_names); ++i)
if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0)
{
*pp += STRLEN(class_names[i]) + 2;
return i;
}
}
return CLASS_NONE;
}
/*
* Specific version of character class functions.
* Using a table to keep this fast.
*/
static short class_tab[256];
#define RI_DIGIT 0x01
#define RI_HEX 0x02
#define RI_OCTAL 0x04
#define RI_WORD 0x08
#define RI_HEAD 0x10
#define RI_ALPHA 0x20
#define RI_LOWER 0x40
#define RI_UPPER 0x80
#define RI_WHITE 0x100
static void
init_class_tab(void)
{
int i;
static int done = FALSE;
if (done)
return;
for (i = 0; i < 256; ++i)
{
if (i >= '0' && i <= '7')
class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD;
else if (i >= '8' && i <= '9')
class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD;
else if (i >= 'a' && i <= 'f')
class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
else if (i >= 'g' && i <= 'z')
class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
else if (i >= 'A' && i <= 'F')
class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
else if (i >= 'G' && i <= 'Z')
class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
else if (i == '_')
class_tab[i] = RI_WORD + RI_HEAD;
else
class_tab[i] = 0;
}
class_tab[' '] |= RI_WHITE;
class_tab['\t'] |= RI_WHITE;
done = TRUE;
}
#define ri_digit(c) ((c) < 0x100 && (class_tab[c] & RI_DIGIT))
#define ri_hex(c) ((c) < 0x100 && (class_tab[c] & RI_HEX))
#define ri_octal(c) ((c) < 0x100 && (class_tab[c] & RI_OCTAL))
#define ri_word(c) ((c) < 0x100 && (class_tab[c] & RI_WORD))
#define ri_head(c) ((c) < 0x100 && (class_tab[c] & RI_HEAD))
#define ri_alpha(c) ((c) < 0x100 && (class_tab[c] & RI_ALPHA))
#define ri_lower(c) ((c) < 0x100 && (class_tab[c] & RI_LOWER))
#define ri_upper(c) ((c) < 0x100 && (class_tab[c] & RI_UPPER))
#define ri_white(c) ((c) < 0x100 && (class_tab[c] & RI_WHITE))
// flags for regflags
#define RF_ICASE 1 // ignore case
#define RF_NOICASE 2 // don't ignore case
#define RF_HASNL 4 // can match a NL
#define RF_ICOMBINE 8 // ignore combining characters
#define RF_LOOKBH 16 // uses "\@<=" or "\@<!"
/*
* Global work variables for vim_regcomp().
*/
static char_u *regparse; // Input-scan pointer.
static int regnpar; // () count.
static int wants_nfa; // regex should use NFA engine
#ifdef FEAT_SYN_HL
static int regnzpar; // \z() count.
static int re_has_z; // \z item detected
#endif
static unsigned regflags; // RF_ flags for prog
#if defined(FEAT_SYN_HL) || defined(PROTO)
static int had_eol; // TRUE when EOL found by vim_regcomp()
#endif
static magic_T reg_magic; // magicness of the pattern
static int reg_string; // matching with a string instead of a buffer
// line
static int reg_strict; // "[abc" is illegal
/*
* META contains all characters that may be magic, except '^' and '$'.
*/
// META[] is used often enough to justify turning it into a table.
static char_u 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 Z [ _
1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 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
};
static int curchr; // currently parsed character
// Previous character. Note: prevchr is sometimes -1 when we are not at the
// start, eg in /[ ^I]^ the pattern was never found even if it existed,
// because ^ was taken to be magic -- webb
static int prevchr;
static int prevprevchr; // previous-previous character
static int nextchr; // used for ungetchr()
// arguments for reg()
#define REG_NOPAREN 0 // toplevel reg()
#define REG_PAREN 1 // \(\)
#define REG_ZPAREN 2 // \z(\)
#define REG_NPAREN 3 // \%(\)
typedef struct
{
char_u *regparse;
int prevchr_len;
int curchr;
int prevchr;
int prevprevchr;
int nextchr;
int at_start;
int prev_at_start;
int regnpar;
} parse_state_T;
static void initchr(char_u *);
static int getchr(void);
static void skipchr_keepstart(void);
static int peekchr(void);
static void skipchr(void);
static void ungetchr(void);
static long gethexchrs(int maxinputlen);
static long getoctchrs(void);
static long getdecchrs(void);
static int coll_get_char(void);
static int prog_magic_wrong(void);
static int cstrncmp(char_u *s1, char_u *s2, int *n);
static char_u *cstrchr(char_u *, int);
static int re_mult_next(char *what);
static int reg_iswordc(int);
#ifdef FEAT_EVAL
static void report_re_switch(char_u *pat);
#endif
static regengine_T bt_regengine;
static regengine_T nfa_regengine;
/*
* Return TRUE if compiled regular expression "prog" can match a line break.
*/
int
re_multiline(regprog_T *prog)
{
return (prog->regflags & RF_HASNL);
}
/*
* Check for an equivalence class name "[=a=]". "pp" points to the '['.
* Returns a character representing the class. Zero means that no item was
* recognized. Otherwise "pp" is advanced to after the item.
*/
static int
get_equi_class(char_u **pp)
{
int c;
int l = 1;
char_u *p = *pp;
if (p[1] == '=' && p[2] != NUL)
{
if (has_mbyte)
l = (*mb_ptr2len)(p + 2);
if (p[l + 2] == '=' && p[l + 3] == ']')
{
if (has_mbyte)
c = mb_ptr2char(p + 2);
else
c = p[2];
*pp += l + 4;
return c;
}
}
return 0;
}
/*
* Check for a collating element "[.a.]". "pp" points to the '['.
* Returns a character. Zero means that no item was recognized. Otherwise
* "pp" is advanced to after the item.
* Currently only single characters are recognized!
*/
static int
get_coll_element(char_u **pp)
{
int c;
int l = 1;
char_u *p = *pp;
if (p[0] != NUL && p[1] == '.' && p[2] != NUL)
{
if (has_mbyte)
l = (*mb_ptr2len)(p + 2);
if (p[l + 2] == '.' && p[l + 3] == ']')
{
if (has_mbyte)
c = mb_ptr2char(p + 2);
else
c = p[2];
*pp += l + 4;
return c;
}
}
return 0;
}
static int reg_cpo_lit; // 'cpoptions' contains 'l' flag
static int reg_cpo_bsl; // 'cpoptions' contains '\' flag
static void
get_cpo_flags(void)
{
reg_cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
reg_cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
}
/*
* Skip over a "[]" range.
* "p" must point to the character after the '['.
* The returned pointer is on the matching ']', or the terminating NUL.
*/
static char_u *
skip_anyof(char_u *p)
{
int l;
if (*p == '^') // Complement of range.
++p;
if (*p == ']' || *p == '-')
++p;
while (*p != NUL && *p != ']')
{
if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
p += l;
else
if (*p == '-')
{
++p;
if (*p != ']' && *p != NUL)
MB_PTR_ADV(p);
}
else if (*p == '\\'
&& !reg_cpo_bsl
&& (vim_strchr(REGEXP_INRANGE, p[1]) != NULL
|| (!reg_cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL)))
p += 2;
else if (*p == '[')
{
if (get_char_class(&p) == CLASS_NONE
&& get_equi_class(&p) == 0
&& get_coll_element(&p) == 0
&& *p != NUL)
++p; // it is not a class name and not NUL
}
else
++p;
}
return p;
}
/*
* Skip past regular expression.
* Stop at end of "startp" or where "delim" is found ('/', '?', etc).
* Take care of characters with a backslash in front of it.
* Skip strings inside [ and ].
*/
char_u *
skip_regexp(
char_u *startp,
int delim,
int magic)
{
return skip_regexp_ex(startp, delim, magic, NULL, NULL, NULL);
}
/*
* Call skip_regexp() and when the delimiter does not match give an error and
* return NULL.
*/
char_u *
skip_regexp_err(
char_u *startp,
int delim,
int magic)
{
char_u *p = skip_regexp(startp, delim, magic);
if (*p != delim)
{
semsg(_(e_missing_delimiter_after_search_pattern_str), startp);
return NULL;
}
return p;
}
/*
* skip_regexp() with extra arguments:
* When "newp" is not NULL and "dirc" is '?', make an allocated copy of the
* expression and change "\?" to "?". If "*newp" is not NULL the expression
* is changed in-place.
* If a "\?" is changed to "?" then "dropped" is incremented, unless NULL.
* If "magic_val" is not NULL, returns the effective magicness of the pattern
*/
char_u *
skip_regexp_ex(
char_u *startp,
int dirc,
int magic,
char_u **newp,
int *dropped,
magic_T *magic_val)
{
magic_T mymagic;
char_u *p = startp;
if (magic)
mymagic = MAGIC_ON;
else
mymagic = MAGIC_OFF;
get_cpo_flags();
for (; p[0] != NUL; MB_PTR_ADV(p))
{
if (p[0] == dirc) // found end of regexp
break;
if ((p[0] == '[' && mymagic >= MAGIC_ON)
|| (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF))
{
p = skip_anyof(p + 1);
if (p[0] == NUL)
break;
}
else if (p[0] == '\\' && p[1] != NUL)
{
if (dirc == '?' && newp != NULL && p[1] == '?')
{
// change "\?" to "?", make a copy first.
if (*newp == NULL)
{
*newp = vim_strsave(startp);
if (*newp != NULL)
p = *newp + (p - startp);
}
if (dropped != NULL)
++*dropped;
if (*newp != NULL)
STRMOVE(p, p + 1);
else
++p;
}
else
++p; // skip next character
if (*p == 'v')
mymagic = MAGIC_ALL;
else if (*p == 'V')
mymagic = MAGIC_NONE;
}
}
if (magic_val != NULL)
*magic_val = mymagic;
return p;
}
/*
* Functions for getting characters from the regexp input.
*/
static int prevchr_len; // byte length of previous char
static int at_start; // True when on the first character
static int prev_at_start; // True when on the second character
/*
* Start parsing at "str".
*/
static void
initchr(char_u *str)
{
regparse = str;
prevchr_len = 0;
curchr = prevprevchr = prevchr = nextchr = -1;
at_start = TRUE;
prev_at_start = FALSE;
}
/*
* Save the current parse state, so that it can be restored and parsing
* starts in the same state again.
*/
static void
save_parse_state(parse_state_T *ps)
{
ps->regparse = regparse;
ps->prevchr_len = prevchr_len;
ps->curchr = curchr;
ps->prevchr = prevchr;
ps->prevprevchr = prevprevchr;
ps->nextchr = nextchr;
ps->at_start = at_start;
ps->prev_at_start = prev_at_start;
ps->regnpar = regnpar;
}
/*
* Restore a previously saved parse state.
*/
static void
restore_parse_state(parse_state_T *ps)
{
regparse = ps->regparse;
prevchr_len = ps->prevchr_len;
curchr = ps->curchr;
prevchr = ps->prevchr;
prevprevchr = ps->prevprevchr;
nextchr = ps->nextchr;
at_start = ps->at_start;
prev_at_start = ps->prev_at_start;
regnpar = ps->regnpar;
}
/*
* Get the next character without advancing.
*/
static int
peekchr(void)
{
static int after_slash = FALSE;
if (curchr != -1)
return curchr;
switch (curchr = regparse[0])
{
case '.':
case '[':
case '~':
// magic when 'magic' is on
if (reg_magic >= MAGIC_ON)
curchr = Magic(curchr);
break;
case '(':
case ')':
case '{':
case '%':
case '+':
case '=':
case '?':
case '@':
case '!':
case '&':
case '|':
case '<':
case '>':
case '#': // future ext.
case '"': // future ext.
case '\'': // future ext.
case ',': // future ext.
case '-': // future ext.
case ':': // future ext.
case ';': // future ext.
case '`': // future ext.
case '/': // Can't be used in / command
// magic only after "\v"
if (reg_magic == MAGIC_ALL)
curchr = Magic(curchr);
break;
case '*':
// * is not magic as the very first character, eg "?*ptr", when
// after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But
// "\(\*" is not magic, thus must be magic if "after_slash"
if (reg_magic >= MAGIC_ON
&& !at_start
&& !(prev_at_start && prevchr == Magic('^'))
&& (after_slash
|| (prevchr != Magic('(')
&& prevchr != Magic('&')
&& prevchr != Magic('|'))))
curchr = Magic('*');
break;
case '^':
// '^' is only magic as the very first character and if it's after
// "\(", "\|", "\&' or "\n"
if (reg_magic >= MAGIC_OFF
&& (at_start
|| reg_magic == MAGIC_ALL
|| prevchr == Magic('(')
|| prevchr == Magic('|')
|| prevchr == Magic('&')
|| prevchr == Magic('n')
|| (no_Magic(prevchr) == '('
&& prevprevchr == Magic('%'))))
{
curchr = Magic('^');
at_start = TRUE;
prev_at_start = FALSE;
}
break;
case '$':
// '$' is only magic as the very last char and if it's in front of
// either "\|", "\)", "\&", or "\n"
if (reg_magic >= MAGIC_OFF)
{
char_u *p = regparse + 1;
int is_magic_all = (reg_magic == MAGIC_ALL);
// ignore \c \C \m \M \v \V and \Z after '$'
while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C'
|| p[1] == 'm' || p[1] == 'M'
|| p[1] == 'v' || p[1] == 'V' || p[1] == 'Z'))
{
if (p[1] == 'v')
is_magic_all = TRUE;
else if (p[1] == 'm' || p[1] == 'M' || p[1] == 'V')
is_magic_all = FALSE;
p += 2;
}
if (p[0] == NUL
|| (p[0] == '\\'
&& (p[1] == '|' || p[1] == '&' || p[1] == ')'
|| p[1] == 'n'))
|| (is_magic_all
&& (p[0] == '|' || p[0] == '&' || p[0] == ')'))
|| reg_magic == MAGIC_ALL)
curchr = Magic('$');
}
break;
case '\\':
{
int c = regparse[1];
if (c == NUL)
curchr = '\\'; // trailing '\'
else if (c <= '~' && META_flags[c])
{
/*
* META contains everything that may be magic sometimes,
* except ^ and $ ("\^" and "\$" are only magic after
* "\V"). We now fetch the next character and toggle its
* magicness. Therefore, \ is so meta-magic that it is
* not in META.
*/
curchr = -1;
prev_at_start = at_start;
at_start = FALSE; // be able to say "/\*ptr"
++regparse;
++after_slash;
peekchr();
--regparse;
--after_slash;
curchr = toggle_Magic(curchr);
}
else if (vim_strchr(REGEXP_ABBR, c))
{
/*
* Handle abbreviations, like "\t" for TAB -- webb
*/
curchr = backslash_trans(c);
}
else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^'))
curchr = toggle_Magic(c);
else
{
/*
* Next character can never be (made) magic?
* Then backslashing it won't do anything.
*/
if (has_mbyte)
curchr = (*mb_ptr2char)(regparse + 1);
else
curchr = c;
}
break;
}
default:
if (has_mbyte)
curchr = (*mb_ptr2char)(regparse);
}
return curchr;
}
/*
* Eat one lexed character. Do this in a way that we can undo it.
*/
static void
skipchr(void)
{
// peekchr() eats a backslash, do the same here
if (*regparse == '\\')
prevchr_len = 1;
else
prevchr_len = 0;
if (regparse[prevchr_len] != NUL)
{
if (enc_utf8)
// exclude composing chars that mb_ptr2len does include
prevchr_len += utf_ptr2len(regparse + prevchr_len);
else if (has_mbyte)
prevchr_len += (*mb_ptr2len)(regparse + prevchr_len);
else
++prevchr_len;
}
regparse += prevchr_len;
prev_at_start = at_start;
at_start = FALSE;
prevprevchr = prevchr;
prevchr = curchr;
curchr = nextchr; // use previously unget char, or -1
nextchr = -1;
}
/*
* Skip a character while keeping the value of prev_at_start for at_start.
* prevchr and prevprevchr are also kept.
*/
static void
skipchr_keepstart(void)
{
int as = prev_at_start;
int pr = prevchr;
int prpr = prevprevchr;
skipchr();
at_start = as;
prevchr = pr;
prevprevchr = prpr;
}
/*
* Get the next character from the pattern. We know about magic and such, so
* therefore we need a lexical analyzer.
*/
static int
getchr(void)
{
int chr = peekchr();
skipchr();
return chr;
}
/*
* put character back. Works only once!
*/
static void
ungetchr(void)
{
nextchr = curchr;
curchr = prevchr;
prevchr = prevprevchr;
at_start = prev_at_start;
prev_at_start = FALSE;
// Backup regparse, so that it's at the same position as before the
// getchr().
regparse -= prevchr_len;
}
/*
* Get and return the value of the hex string at the current position.
* Return -1 if there is no valid hex number.
* The position is updated:
* blahblah\%x20asdf
* before-^ ^-after
* The parameter controls the maximum number of input characters. This will be
* 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence.
*/
static long
gethexchrs(int maxinputlen)
{
long_u nr = 0;
int c;
int i;
for (i = 0; i < maxinputlen; ++i)
{
c = regparse[0];
if (!vim_isxdigit(c))
break;
nr <<= 4;
nr |= hex2nr(c);
++regparse;
}
if (i == 0)
return -1;
return (long)nr;
}
/*
* Get and return the value of the decimal string immediately after the
* current position. Return -1 for invalid. Consumes all digits.
*/
static long
getdecchrs(void)
{
long_u nr = 0;
int c;
int i;
for (i = 0; ; ++i)
{
c = regparse[0];
if (c < '0' || c > '9')
break;
nr *= 10;
nr += c - '0';
++regparse;
curchr = -1; // no longer valid
}
if (i == 0)
return -1;
return (long)nr;
}