1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 // Copyright (C) 2002-2015 International Business Machines Corporation
5 // and others. All rights reserved.
9 // ICU Regular Expressions,
10 // Definitions of constant values used in the compiled form of
11 // a regular expression pattern.
17 #include "unicode/utypes.h"
18 #include "unicode/uobject.h"
19 #include "unicode/uniset.h"
20 #include "unicode/utext.h"
27 // For debugging, define REGEX_DEBUG
28 // To define with configure,
29 // CPPFLAGS="-DREGEX_DEBUG" ./runConfigureICU --enable-debug --disable-release Linux
33 // debugging options. Enable one or more of the three #defines immediately following
36 //#define REGEX_SCAN_DEBUG
37 #define REGEX_DUMP_DEBUG
38 #define REGEX_RUN_DEBUG
40 // End of #defines inteded to be directly set.
45 #ifdef REGEX_SCAN_DEBUG
46 #define REGEX_SCAN_DEBUG_PRINTF(a) printf a
48 #define REGEX_SCAN_DEBUG_PRINTF(a)
53 // Opcode types In the compiled form of the regexp, these are the type, or opcodes,
57 URX_RESERVED_OP = 0, // For multi-operand ops, most non-first words.
58 URX_RESERVED_OP_N = 255, // For multi-operand ops, negative operand values.
59 URX_BACKTRACK = 1, // Force a backtrack, as if a match test had failed.
61 URX_ONECHAR = 3, // Value field is the 21 bit unicode char to match
62 URX_STRING = 4, // Value field is index of string start
63 URX_STRING_LEN = 5, // Value field is string length (code units)
64 URX_STATE_SAVE = 6, // Value field is pattern position to push
66 URX_START_CAPTURE = 8, // Value field is capture group number.
67 URX_END_CAPTURE = 9, // Value field is capture group number
68 URX_STATIC_SETREF = 10, // Value field is index of set in array of sets.
69 URX_SETREF = 11, // Value field is index of set in array of sets.
71 URX_JMP = 13, // Value field is destination position in
73 URX_FAIL = 14, // Stop match operation, No match.
75 URX_JMP_SAV = 15, // Operand: JMP destination location
76 URX_BACKSLASH_B = 16, // Value field: 0: \b 1: \B
78 URX_JMP_SAV_X = 18, // Conditional JMP_SAV,
79 // Used in (x)+, breaks loop on zero length match.
80 // Operand: Jmp destination.
82 URX_BACKSLASH_Z = 20, // \z Unconditional end of line.
84 URX_DOTANY_ALL = 21, // ., in the . matches any mode.
85 URX_BACKSLASH_D = 22, // Value field: 0: \d 1: \D
86 URX_CARET = 23, // Value field: 1: multi-line mode.
87 URX_DOLLAR = 24, // Also for \Z
89 URX_CTR_INIT = 25, // Counter Inits for {Interval} loops.
90 URX_CTR_INIT_NG = 26, // 2 kinds, normal and non-greedy.
91 // These are 4 word opcodes. See description.
92 // First Operand: Data loc of counter variable
93 // 2nd Operand: Pat loc of the URX_CTR_LOOPx
94 // at the end of the loop.
95 // 3rd Operand: Minimum count.
96 // 4th Operand: Max count, -1 for unbounded.
98 URX_DOTANY_UNIX = 27, // '.' operator in UNIX_LINES mode, only \n marks end of line.
100 URX_CTR_LOOP = 28, // Loop Ops for {interval} loops.
101 URX_CTR_LOOP_NG = 29, // Also in three flavors.
102 // Operand is loc of corresponding CTR_INIT.
104 URX_CARET_M_UNIX = 30, // '^' operator, test for start of line in multi-line
105 // plus UNIX_LINES mode.
107 URX_RELOC_OPRND = 31, // Operand value in multi-operand ops that refers
108 // back into compiled pattern code, and thus must
109 // be relocated when inserting/deleting ops in code.
111 URX_STO_SP = 32, // Store the stack ptr. Operand is location within
112 // matcher data (not stack data) to store it.
113 URX_LD_SP = 33, // Load the stack pointer. Operand is location
115 URX_BACKREF = 34, // Back Reference. Parameter is the index of the
116 // capture group variables in the state stack frame.
117 URX_STO_INP_LOC = 35, // Store the input location. Operand is location
118 // within the matcher stack frame.
119 URX_JMPX = 36, // Conditional JMP.
120 // First Operand: JMP target location.
121 // Second Operand: Data location containing an
122 // input position. If current input position ==
123 // saved input position, FAIL rather than taking
125 URX_LA_START = 37, // Starting a LookAround expression.
126 // Save InputPos and SP in static data.
127 // Operand: Static data offset for the save
128 URX_LA_END = 38, // Ending a Lookaround expression.
129 // Restore InputPos and Stack to saved values.
130 // Operand: Static data offset for saved data.
131 URX_ONECHAR_I = 39, // Test for case-insensitive match of a literal character.
132 // Operand: the literal char.
133 URX_STRING_I = 40, // Case insensitive string compare.
134 // First Operand: Index of start of string in string literals
135 // Second Operand (next word in compiled code):
136 // the length of the string.
137 URX_BACKREF_I = 41, // Case insensitive back reference.
138 // Parameter is the index of the
139 // capture group variables in the state stack frame.
140 URX_DOLLAR_M = 42, // $ in multi-line mode.
141 URX_CARET_M = 43, // ^ in multi-line mode.
142 URX_LB_START = 44, // LookBehind Start.
143 // Paramater is data location
144 URX_LB_CONT = 45, // LookBehind Continue.
145 // Param 0: the data location
146 // Param 1: The minimum length of the look-behind match
147 // Param 2: The max length of the look-behind match
148 URX_LB_END = 46, // LookBehind End.
149 // Parameter is the data location.
150 // Check that match ended at the right spot,
151 // Restore original input string len.
152 URX_LBN_CONT = 47, // Negative LookBehind Continue
153 // Param 0: the data location
154 // Param 1: The minimum length of the look-behind match
155 // Param 2: The max length of the look-behind match
156 // Param 3: The pattern loc following the look-behind block.
157 URX_LBN_END = 48, // Negative LookBehind end
158 // Parameter is the data location.
159 // Check that the match ended at the right spot.
160 URX_STAT_SETREF_N = 49, // Reference to a prebuilt set (e.g. \w), negated
161 // Operand is index of set in array of sets.
162 URX_LOOP_SR_I = 50, // Init a [set]* loop.
163 // Operand is the sets index in array of user sets.
164 URX_LOOP_C = 51, // Continue a [set]* or OneChar* loop.
165 // Operand is a matcher static data location.
166 // Must always immediately follow LOOP_x_I instruction.
167 URX_LOOP_DOT_I = 52, // .*, initialization of the optimized loop.
170 // 0: Normal (. doesn't match new-line) mode.
171 // 1: . matches new-line mode.
172 // bit 1: controls what new-lines are recognized by this operation.
173 // 0: All Unicode New-lines
174 // 1: UNIX_LINES, \u000a only.
175 URX_BACKSLASH_BU = 53, // \b or \B in UREGEX_UWORD mode, using Unicode style
177 URX_DOLLAR_D = 54, // $ end of input test, in UNIX_LINES mode.
178 URX_DOLLAR_MD = 55, // $ end of input test, in MULTI_LINE and UNIX_LINES mode.
179 URX_BACKSLASH_H = 56, // Value field: 0: \h 1: \H
180 URX_BACKSLASH_R = 57, // Any line break sequence.
181 URX_BACKSLASH_V = 58 // Value field: 0: \v 1: \V
185 // Keep this list of opcode names in sync with the above enum
186 // Used for debug printing only.
187 #define URX_OPCODE_NAMES \
198 "URX_STATIC_SETREF", \
218 "URX_CARET_M_UNIX", \
250 // Convenience macros for assembling and disassembling a compiled operation.
252 #define URX_TYPE(x) ((uint32_t)(x) >> 24)
253 #define URX_VAL(x) ((x) & 0xffffff)
257 // Access to Unicode Sets composite character properties
258 // The sets are accessed by the match engine for things like \w (word boundary)
266 URX_GC_NORMAL, // Sets for finding grapheme cluster boundaries.
277 URX_NEG_SET = 0x800000 // Flag bit to reverse sense of set
283 // Match Engine State Stack Frame Layout.
285 struct REStackFrame {
287 int64_t fInputIdx; // Position of next character in the input string
288 int64_t fPatIdx; // Position of next Op in the compiled pattern
289 // (int64_t for UVector64, values fit in an int32_t)
291 int64_t fExtra[1]; // Extra state, for capture group start/ends
292 // atomic parentheses, repeat counts, etc.
293 // Locations assigned at pattern compile time.
294 // Variable-length array.
296 // number of UVector elements in the header
297 #define RESTACKFRAME_HDRCOUNT 2
300 // Start-Of-Match type. Used by find() to quickly scan to positions where a
301 // match might start before firing up the full match engine.
304 START_NO_INFO, // No hint available.
305 START_CHAR, // Match starts with a literal code point.
306 START_SET, // Match starts with something matching a set.
307 START_START, // Match starts at start of buffer only (^ or \A)
308 START_LINE, // Match starts with ^ in multi-line mode.
309 START_STRING // Match starts with a literal string.
312 #define START_OF_MATCH_STR(v) ((v)==START_NO_INFO? "START_NO_INFO" : \
313 (v)==START_CHAR? "START_CHAR" : \
314 (v)==START_SET? "START_SET" : \
315 (v)==START_START? "START_START" : \
316 (v)==START_LINE? "START_LINE" : \
317 (v)==START_STRING? "START_STRING" : \
321 // 8 bit set, to fast-path latin-1 set membership tests.
323 struct Regex8BitSet : public UMemory {
324 inline Regex8BitSet();
325 inline void operator = (const Regex8BitSet &s);
326 inline void init(const UnicodeSet *src);
327 inline UBool contains(UChar32 c);
328 inline void add(UChar32 c);
332 inline Regex8BitSet::Regex8BitSet() {
333 uprv_memset(d, 0, sizeof(d));
336 inline UBool Regex8BitSet::contains(UChar32 c) {
337 // No bounds checking! This is deliberate.
338 return ((d[c>>3] & 1 <<(c&7)) != 0);
341 inline void Regex8BitSet::add(UChar32 c) {
342 d[c>>3] |= 1 << (c&7);
345 inline void Regex8BitSet::init(const UnicodeSet *s) {
347 for (int32_t i=0; i<=255; i++) {
348 if (s->contains(i)) {
355 inline void Regex8BitSet::operator = (const Regex8BitSet &s) {
356 uprv_memcpy(d, s.d, sizeof(d));
360 // Case folded UText Iterator helper class.
361 // Wraps a UText, provides a case-folded enumeration over its contents.
362 // Used in implementing case insensitive matching constructs.
363 // Implementation in rematch.cpp
365 class CaseFoldingUTextIterator: public UMemory {
367 CaseFoldingUTextIterator(UText &text);
368 ~CaseFoldingUTextIterator();
370 UChar32 next(); // Next case folded character
372 UBool inExpansion(); // True if last char returned from next() and the
373 // next to be returned both originated from a string
374 // folding of the same code point from the orignal UText.
377 const UCaseProps *fcsp;
378 const UChar *fFoldChars;
385 // Case folded UChar * string iterator.
386 // Wraps a UChar *, provides a case-folded enumeration over its contents.
387 // Used in implementing case insensitive matching constructs.
388 // Implementation in rematch.cpp
390 class CaseFoldingUCharIterator: public UMemory {
392 CaseFoldingUCharIterator(const UChar *chars, int64_t start, int64_t limit);
393 ~CaseFoldingUCharIterator();
395 UChar32 next(); // Next case folded character
397 UBool inExpansion(); // True if last char returned from next() and the
398 // next to be returned both originated from a string
399 // folding of the same code point from the orignal UText.
401 int64_t getIndex(); // Return the current input buffer index.
407 const UCaseProps *fcsp;
408 const UChar *fFoldChars;