blob: d07d536d951c16ad2dbf093a15f8623bf0a35f30 [file] [log] [blame]
Guido van Rossum51b3aa31997-10-06 14:43:11 +00001/*************************************************
2* Perl-Compatible Regular Expressions *
3*************************************************/
4
5
Guido van Rossumdda66961998-05-07 15:32:44 +00006#define PCRE_VERSION "1.09 28-Apr-1998"
Guido van Rossum51b3aa31997-10-06 14:43:11 +00007
8
9/* This is a library of functions to support regular expressions whose syntax
10and semantics are as close as possible to those of the Perl 5 language. See
11the file Tech.Notes for some information on the internals.
12
13Written by: Philip Hazel <ph10@cam.ac.uk>
14
Guido van Rossum042ff9e1998-04-03 21:13:31 +000015 Copyright (c) 1998 University of Cambridge
Guido van Rossum51b3aa31997-10-06 14:43:11 +000016
17-----------------------------------------------------------------------------
18Permission is granted to anyone to use this software for any purpose on any
19computer system, and to redistribute it freely, subject to the following
20restrictions:
21
221. This software is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
25
262. The origin of this software must not be misrepresented, either by
27 explicit claim or by omission.
28
293. Altered versions must be plainly marked as such, and must not be
30 misrepresented as being the original software.
31-----------------------------------------------------------------------------
32*/
33
34/* This header contains definitions that are shared between the different
35modules, but which are not relevant to the outside. */
36
Guido van Rossum50700601997-12-08 17:15:20 +000037
Guido van Rossum51b3aa31997-10-06 14:43:11 +000038/* Standard C headers plus the external interface definition */
39
40#include <ctype.h>
41#include <limits.h>
Guido van Rossum50700601997-12-08 17:15:20 +000042#include <setjmp.h>
43#include <stddef.h>
Guido van Rossum51b3aa31997-10-06 14:43:11 +000044#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include "pcre.h"
48
Guido van Rossum557dea11997-12-22 22:46:52 +000049/* In case there is no definition of offsetof() provided - though any proper
50Standard C system should have one. */
51
52#ifndef offsetof
53#define offsetof(p_type,field) ((size_t)&(((p_type *)0)->field))
54#endif
55
Guido van Rossum50700601997-12-08 17:15:20 +000056/* Private options flags start at the most significant end of the two bytes.
57The public options defined in pcre.h start at the least significant end. Make
58sure they don't overlap! */
Guido van Rossum51b3aa31997-10-06 14:43:11 +000059
Guido van Rossum50700601997-12-08 17:15:20 +000060#define PCRE_FIRSTSET 0x8000 /* first_char is set */
61#define PCRE_STARTLINE 0x4000 /* start after \n for multiline */
62#define PCRE_COMPILED_CASELESS 0x2000 /* like it says */
Guido van Rossum51b3aa31997-10-06 14:43:11 +000063
64/* Options for the "extra" block produced by pcre_study(). */
65
66#define PCRE_STUDY_CASELESS 0x01 /* study was caseless */
Guido van Rossum50700601997-12-08 17:15:20 +000067#define PCRE_STUDY_MAPPED 0x02 /* a map of starting chars exists */
Guido van Rossum51b3aa31997-10-06 14:43:11 +000068
69/* Masks for identifying the public options: all permitted at compile time,
70only some permitted at run or study time. */
71
72#ifdef FOR_PYTHON
73#define PUBLIC_OPTIONS \
Guido van Rossum50700601997-12-08 17:15:20 +000074 (PCRE_CASELESS|PCRE_EXTENDED|PCRE_ANCHORED|PCRE_MULTILINE| \
Guido van Rossumdda66961998-05-07 15:32:44 +000075 PCRE_DOTALL|PCRE_DOLLAR_ENDONLY|PCRE_EXTRA|PCRE_UNGREEDY| \
76 PCRE_LOCALE)
Guido van Rossum51b3aa31997-10-06 14:43:11 +000077#else
78#define PUBLIC_OPTIONS \
Guido van Rossum50700601997-12-08 17:15:20 +000079 (PCRE_CASELESS|PCRE_EXTENDED|PCRE_ANCHORED|PCRE_MULTILINE| \
Guido van Rossumdda66961998-05-07 15:32:44 +000080 PCRE_DOTALL|PCRE_DOLLAR_ENDONLY|PCRE_EXTRA|PCRE_UNGREEDY)
Guido van Rossum51b3aa31997-10-06 14:43:11 +000081#endif
Guido van Rossum50700601997-12-08 17:15:20 +000082#define PUBLIC_EXEC_OPTIONS \
83 (PCRE_CASELESS|PCRE_ANCHORED|PCRE_MULTILINE|PCRE_NOTBOL|PCRE_NOTEOL| \
84 PCRE_DOTALL|PCRE_DOLLAR_ENDONLY)
85
Guido van Rossum51b3aa31997-10-06 14:43:11 +000086#define PUBLIC_STUDY_OPTIONS (PCRE_CASELESS)
87
88/* Magic number to provide a small check against being handed junk. */
89
90#define MAGIC_NUMBER 0x50435245 /* 'PCRE' */
91
92/* Miscellaneous definitions */
93
94typedef int BOOL;
95
96#define FALSE 0
97#define TRUE 1
98
Guido van Rossum51b3aa31997-10-06 14:43:11 +000099/* These are escaped items that aren't just an encoding of a particular data
100value such as \n. They must have non-zero values, as check_escape() returns
101their negation. Also, they must appear in the same order as in the opcode
102definitions below, up to ESC_Z. The final one must be ESC_REF as subsequent
103values are used for \1, \2, \3, etc. There is a test in the code for an escape
Guido van Rossum50700601997-12-08 17:15:20 +0000104greater than ESC_b and less than ESC_X to detect the types that may be
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000105repeated. If any new escapes are put in-between that don't consume a character,
106that code will have to change. */
107
108enum { ESC_A = 1, ESC_B, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, ESC_W, ESC_w,
Guido van Rossum50700601997-12-08 17:15:20 +0000109
110 /* These are not Perl escapes, so can't appear in the */
111 ESC_X, /* simple table-lookup because they must be conditional */
112 /* on PCRE_EXTRA. */
113 ESC_Z,
114 ESC_REF };
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000115
116/* Opcode table: OP_BRA must be last, as all values >= it are used for brackets
117that extract substrings. Starting from 1 (i.e. after OP_END), the values up to
Guido van Rossum58132c61997-12-17 00:24:13 +0000118OP_EOD must correspond in order to the list of escapes immediately above. */
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000119
120enum {
121 OP_END, /* End of pattern */
122
123 /* Values corresponding to backslashed metacharacters */
124
125 OP_SOD, /* Start of data: \A */
Guido van Rossum50700601997-12-08 17:15:20 +0000126 OP_NOT_WORD_BOUNDARY, /* \B */
127 OP_WORD_BOUNDARY, /* \b */
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000128 OP_NOT_DIGIT, /* \D */
129 OP_DIGIT, /* \d */
130 OP_NOT_WHITESPACE, /* \S */
131 OP_WHITESPACE, /* \s */
132 OP_NOT_WORDCHAR, /* \W */
133 OP_WORDCHAR, /* \w */
Guido van Rossum50700601997-12-08 17:15:20 +0000134 OP_CUT, /* The analogue of Prolog's "cut" operation (extension) */
Guido van Rossum58132c61997-12-17 00:24:13 +0000135 OP_EOD, /* End of data: \Z. */
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000136
Guido van Rossum50700601997-12-08 17:15:20 +0000137 OP_NOT_WORD_BOUNDARY_L, /* localized \B */
138 OP_WORD_BOUNDARY_L, /* localized \b */
139 OP_NOT_WORDCHAR_L, /* localized \W */
140 OP_WORDCHAR_L, /* localized \w */
141
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000142 OP_CIRC, /* Start of line - varies with multiline switch */
143 OP_DOLL, /* End of line - varies with multiline switch */
144 OP_ANY, /* Match any character */
145 OP_CHARS, /* Match string of characters */
Guido van Rossum50700601997-12-08 17:15:20 +0000146 OP_NOT, /* Match anything but the following char */
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000147
148 OP_STAR, /* The maximizing and minimizing versions of */
149 OP_MINSTAR, /* all these opcodes must come in pairs, with */
150 OP_PLUS, /* the minimizing one second. */
151 OP_MINPLUS, /* This first set applies to single characters */
152 OP_QUERY,
153 OP_MINQUERY,
Guido van Rossum50700601997-12-08 17:15:20 +0000154 OP_UPTO, /* From 0 to n matches */
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000155 OP_MINUPTO,
Guido van Rossum50700601997-12-08 17:15:20 +0000156 OP_EXACT, /* Exactly n matches */
157
158 OP_NOTSTAR, /* The maximizing and minimizing versions of */
159 OP_NOTMINSTAR, /* all these opcodes must come in pairs, with */
160 OP_NOTPLUS, /* the minimizing one second. */
161 OP_NOTMINPLUS, /* This first set applies to "not" single characters */
162 OP_NOTQUERY,
163 OP_NOTMINQUERY,
164 OP_NOTUPTO, /* From 0 to n matches */
165 OP_NOTMINUPTO,
166 OP_NOTEXACT, /* Exactly n matches */
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000167
168 OP_TYPESTAR, /* The maximizing and minimizing versions of */
169 OP_TYPEMINSTAR, /* all these opcodes must come in pairs, with */
170 OP_TYPEPLUS, /* the minimizing one second. These codes must */
171 OP_TYPEMINPLUS, /* be in exactly the same order as those above. */
172 OP_TYPEQUERY, /* This set applies to character types such as \d */
173 OP_TYPEMINQUERY,
Guido van Rossum50700601997-12-08 17:15:20 +0000174 OP_TYPEUPTO, /* From 0 to n matches */
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000175 OP_TYPEMINUPTO,
Guido van Rossum50700601997-12-08 17:15:20 +0000176 OP_TYPEEXACT, /* Exactly n matches */
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000177
178 OP_CRSTAR, /* The maximizing and minimizing versions of */
179 OP_CRMINSTAR, /* all these opcodes must come in pairs, with */
180 OP_CRPLUS, /* the minimizing one second. These codes must */
181 OP_CRMINPLUS, /* be in exactly the same order as those above. */
182 OP_CRQUERY, /* These are for character classes and back refs */
183 OP_CRMINQUERY,
Guido van Rossum50700601997-12-08 17:15:20 +0000184 OP_CRRANGE, /* These are different to the three seta above. */
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000185 OP_CRMINRANGE,
186
187 OP_CLASS, /* Match a character class */
Guido van Rossum042ff9e1998-04-03 21:13:31 +0000188 OP_NEGCLASS, /* Match a character class, specified negatively */
Guido van Rossum50700601997-12-08 17:15:20 +0000189 OP_CLASS_L, /* Match a character class */
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000190 OP_REF, /* Match a back reference */
191
192 OP_ALT, /* Start of alternation */
193 OP_KET, /* End of group that doesn't have an unbounded repeat */
194 OP_KETRMAX, /* These two must remain together and in this */
195 OP_KETRMIN, /* order. They are for groups the repeat for ever. */
196
197 OP_ASSERT,
198 OP_ASSERT_NOT,
Guido van Rossum50700601997-12-08 17:15:20 +0000199 OP_ONCE, /* Once matched, don't back up into the subpattern */
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000200
201 OP_BRAZERO, /* These two must remain together and in this */
202 OP_BRAMINZERO, /* order. */
203
204 OP_BRA /* This and greater values are used for brackets that
205 extract substrings. */
206};
207
208/* The highest extraction number. This is limited by the number of opcodes
209left after OP_BRA, i.e. 255 - OP_BRA. We actually set it somewhat lower. */
210
211#define EXTRACT_MAX 99
212
Guido van Rossum50700601997-12-08 17:15:20 +0000213/* The texts of compile-time error messages are defined as macros here so that
214they can be accessed by the POSIX wrapper and converted into error codes. Yes,
215I could have used error codes in the first place, but didn't feel like changing
216just to accommodate the POSIX wrapper. */
217
218#define ERR1 "\\ at end of pattern"
219#define ERR2 "\\c at end of pattern"
220#define ERR3 "unrecognized character follows \\"
221#define ERR4 "numbers out of order in {} quantifier"
222#define ERR5 "number too big in {} quantifier"
223#define ERR6 "missing terminating ] for character class"
224#define ERR7 "invalid escape sequence in character class"
225#define ERR8 "range out of order in character class"
226#define ERR9 "nothing to repeat"
227#define ERR10 "operand of unlimited repeat could match the empty string"
228#define ERR11 "internal error: unexpected repeat"
229#define ERR12 "unrecognized character after (?"
230#define ERR13 "too many capturing parenthesized sub-patterns"
231#define ERR14 "missing )"
232#define ERR15 "back reference to non-existent subpattern"
233#define ERR16 "erroffset passed as NULL"
234#define ERR17 "unknown option bit(s) set"
235#define ERR18 "missing ) after comment"
236#define ERR19 "too many sets of parentheses"
237#define ERR20 "regular expression too large"
238#define ERR21 "failed to get memory"
239#define ERR22 "unmatched brackets"
240#define ERR23 "internal error: code overflow"
241
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000242/* All character handling must be done as unsigned characters. Otherwise there
243are problems with top-bit-set characters and functions such as isspace().
244However, we leave the interface to the outside world as char *, because that
245should make things easier for callers. We define a short type for unsigned char
246to save lots of typing. I tried "uchar", but it causes problems on Digital
247Unix, where it is defined in sys/types, so use "uschar" instead. */
248
249typedef unsigned char uschar;
250
251/* The real format of the start of the pcre block; the actual code vector
252runs on as long as necessary after the end. */
253
254typedef struct real_pcre {
255 unsigned int magic_number;
Guido van Rossum50700601997-12-08 17:15:20 +0000256 unsigned short int options;
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000257 unsigned char top_bracket;
Guido van Rossum50700601997-12-08 17:15:20 +0000258 unsigned char top_backref;
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000259 unsigned char first_char;
260 unsigned char code[1];
261} real_pcre;
262
263/* The real format of the extra block returned by pcre_study(). */
264
265typedef struct real_pcre_extra {
266 unsigned char options;
267 unsigned char start_bits[32];
268} real_pcre_extra;
269
Guido van Rossum50700601997-12-08 17:15:20 +0000270/* Global tables from chartables.c */
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000271
272extern uschar pcre_lcc[];
Guido van Rossum50700601997-12-08 17:15:20 +0000273extern uschar pcre_fcc[];
274extern uschar pcre_cbits[];
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000275extern uschar pcre_ctypes[];
276
277/* Bit definitions for entries in pcre_ctypes[]. */
278
279#define ctype_space 0x01
Guido van Rossum50700601997-12-08 17:15:20 +0000280#define ctype_letter 0x02
281#define ctype_digit 0x04
282#define ctype_xdigit 0x08
283#define ctype_word 0x10 /* alphameric or '_' */
284#define ctype_odigit 0x20 /* octal digit */
Guido van Rossum51b3aa31997-10-06 14:43:11 +0000285#define ctype_meta 0x80 /* regexp meta char or zero (end pattern) */
286
Guido van Rossum50700601997-12-08 17:15:20 +0000287/* Offsets for the bitmap tables */
288
289#define cbit_digit 0
290#define cbit_letter 32
291#define cbit_word 64
292#define cbit_space 96
293#define cbit_length 128 /* Length of the cbits table */
294
295/* End of internal.h */