blob: f8ff0962aa4f101c1ca42f470f75b0fa52ea5116 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE
4 *
5 * This is NOT the original regular expression code as written by Henry
6 * Spencer. This code has been modified specifically for use with Vim, and
7 * should not be used apart from compiling Vim. If you want a good regular
8 * expression library, get the original code.
9 *
10 * NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE
11 */
12
13#ifndef _REGEXP_H
14#define _REGEXP_H
15
16/*
17 * The number of sub-matches is limited to 10.
18 * The first one (index 0) is the whole match, referenced with "\0".
19 * The second one (index 1) is the first sub-match, referenced with "\1".
20 * This goes up to the tenth (index 9), referenced with "\9".
21 */
22#define NSUBEXP 10
23
24/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020025 * In the NFA engine: how many braces are allowed.
26 * TODO(RE): Use dynamic memory allocation instead of static, like here
27 */
28#define NFA_MAX_BRACES 20
29
30typedef struct regengine regengine_T;
31
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 * Structure returned by vim_regcomp() to pass on to vim_regexec().
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020034 * This is the general structure. For the actual matcher, two specific
35 * structures are used. See code below.
36 */
37typedef struct regprog
38{
39 regengine_T *engine;
40 unsigned regflags;
41} regprog_T;
42
43/*
44 * Structure used by the back track matcher.
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 * These fields are only to be used in regexp.c!
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020046 * See regexp.c for an explanation.
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 */
48typedef struct
49{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020050 /* These two members implement regprog_T */
51 regengine_T *engine;
52 unsigned regflags;
53
Bram Moolenaar071d4272004-06-13 20:20:40 +000054 int regstart;
55 char_u reganch;
56 char_u *regmust;
57 int regmlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 char_u reghasz;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020059 char_u program[1]; /* actually longer.. */
60} bt_regprog_T;
61
62/*
63 * Structure representing a NFA state.
64 * A NFA state may have no outgoing edge, when it is a NFA_MATCH state.
65 */
66typedef struct nfa_state nfa_state_T;
67struct nfa_state
68{
69 int c;
70 nfa_state_T *out;
71 nfa_state_T *out1;
72 int id;
73 int lastlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020074 int negated;
75};
76
77/*
78 * Structure used by the NFA matcher.
79 */
80typedef struct
81{
82 /* These two members implement regprog_T */
83 regengine_T *engine;
84 unsigned regflags;
85
86 regprog_T regprog;
87 nfa_state_T *start;
Bram Moolenaar57a285b2013-05-26 16:57:28 +020088 int has_zend; /* pattern contains \ze */
Bram Moolenaar963fee22013-05-26 21:47:28 +020089 int nsubexp; /* number of () */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020090 int nstate;
91 nfa_state_T state[0]; /* actually longer.. */
92} nfa_regprog_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +000093
94/*
95 * Structure to be used for single-line matching.
96 * Sub-match "no" starts at "startp[no]" and ends just before "endp[no]".
97 * When there is no match, the pointer is NULL.
98 */
99typedef struct
100{
101 regprog_T *regprog;
102 char_u *startp[NSUBEXP];
103 char_u *endp[NSUBEXP];
104 int rm_ic;
105} regmatch_T;
106
107/*
108 * Structure to be used for multi-line matching.
109 * Sub-match "no" starts in line "startpos[no].lnum" column "startpos[no].col"
110 * and ends in line "endpos[no].lnum" just before column "endpos[no].col".
111 * The line numbers are relative to the first line, thus startpos[0].lnum is
112 * always 0.
113 * When there is no match, the line number is -1.
114 */
115typedef struct
116{
117 regprog_T *regprog;
118 lpos_T startpos[NSUBEXP];
119 lpos_T endpos[NSUBEXP];
120 int rmm_ic;
Bram Moolenaarbbebc852005-07-18 21:47:53 +0000121 colnr_T rmm_maxcol; /* when not zero: maximum column */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122} regmmatch_T;
123
124/*
125 * Structure used to store external references: "\z\(\)" to "\z\1".
126 * Use a reference count to avoid the need to copy this around. When it goes
127 * from 1 to zero the matches need to be freed.
128 */
129typedef struct
130{
131 short refcnt;
132 char_u *matches[NSUBEXP];
133} reg_extmatch_T;
134
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200135struct regengine
136{
137 regprog_T *(*regcomp)(char_u*, int);
138 int (*regexec)(regmatch_T*, char_u*, colnr_T);
139#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
140 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
141 int (*regexec_nl)(regmatch_T*, char_u*, colnr_T);
142#endif
143 long (*regexec_multi)(regmmatch_T*, win_T*, buf_T*, linenr_T, colnr_T, proftime_T*);
144#ifdef DEBUG
145 char_u *expr;
146#endif
147};
148
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149#endif /* _REGEXP_H */