1// 2// Copyright (C) 2002-2013 International Business Machines Corporation 3// and others. All rights reserved. 4// 5// file: regeximp.h 6// 7// ICU Regular Expressions, 8// Definitions of constant values used in the compiled form of 9// a regular expression pattern. 10// 11 12#ifndef _REGEXIMP_H 13#define _REGEXIMP_H 14 15#include "unicode/utypes.h" 16#include "unicode/uobject.h" 17#include "unicode/uniset.h" 18#include "unicode/utext.h" 19 20#include "cmemory.h" 21#include "ucase.h" 22 23U_NAMESPACE_BEGIN 24 25// For debugging, define REGEX_DEBUG 26// To define with configure, 27// CPPFLAGS="-DREGEX_DEBUG" ./runConfigureICU --enable-debug --disable-release Linux 28 29#ifdef REGEX_DEBUG 30// 31// debugging options. Enable one or more of the three #defines immediately following 32// 33 34//#define REGEX_SCAN_DEBUG 35#define REGEX_DUMP_DEBUG 36#define REGEX_RUN_DEBUG 37 38// End of #defines inteded to be directly set. 39 40#include <stdio.h> 41#endif 42 43#ifdef REGEX_SCAN_DEBUG 44#define REGEX_SCAN_DEBUG_PRINTF(a) printf a 45#else 46#define REGEX_SCAN_DEBUG_PRINTF(a) 47#endif 48 49 50// 51// Opcode types In the compiled form of the regexp, these are the type, or opcodes, 52// of the entries. 53// 54enum { 55 URX_RESERVED_OP = 0, // For multi-operand ops, most non-first words. 56 URX_RESERVED_OP_N = 255, // For multi-operand ops, negative operand values. 57 URX_BACKTRACK = 1, // Force a backtrack, as if a match test had failed. 58 URX_END = 2, 59 URX_ONECHAR = 3, // Value field is the 21 bit unicode char to match 60 URX_STRING = 4, // Value field is index of string start 61 URX_STRING_LEN = 5, // Value field is string length (code units) 62 URX_STATE_SAVE = 6, // Value field is pattern position to push 63 URX_NOP = 7, 64 URX_START_CAPTURE = 8, // Value field is capture group number. 65 URX_END_CAPTURE = 9, // Value field is capture group number 66 URX_STATIC_SETREF = 10, // Value field is index of set in array of sets. 67 URX_SETREF = 11, // Value field is index of set in array of sets. 68 URX_DOTANY = 12, 69 URX_JMP = 13, // Value field is destination position in 70 // the pattern. 71 URX_FAIL = 14, // Stop match operation, No match. 72 73 URX_JMP_SAV = 15, // Operand: JMP destination location 74 URX_BACKSLASH_B = 16, // Value field: 0: \b 1: \B 75 URX_BACKSLASH_G = 17, 76 URX_JMP_SAV_X = 18, // Conditional JMP_SAV, 77 // Used in (x)+, breaks loop on zero length match. 78 // Operand: Jmp destination. 79 URX_BACKSLASH_X = 19, 80 URX_BACKSLASH_Z = 20, // \z Unconditional end of line. 81 82 URX_DOTANY_ALL = 21, // ., in the . matches any mode. 83 URX_BACKSLASH_D = 22, // Value field: 0: \d 1: \D 84 URX_CARET = 23, // Value field: 1: multi-line mode. 85 URX_DOLLAR = 24, // Also for \Z 86 87 URX_CTR_INIT = 25, // Counter Inits for {Interval} loops. 88 URX_CTR_INIT_NG = 26, // 2 kinds, normal and non-greedy. 89 // These are 4 word opcodes. See description. 90 // First Operand: Data loc of counter variable 91 // 2nd Operand: Pat loc of the URX_CTR_LOOPx 92 // at the end of the loop. 93 // 3rd Operand: Minimum count. 94 // 4th Operand: Max count, -1 for unbounded. 95 96 URX_DOTANY_UNIX = 27, // '.' operator in UNIX_LINES mode, only \n marks end of line. 97 98 URX_CTR_LOOP = 28, // Loop Ops for {interval} loops. 99 URX_CTR_LOOP_NG = 29, // Also in three flavors. 100 // Operand is loc of corresponding CTR_INIT. 101 102 URX_CARET_M_UNIX = 30, // '^' operator, test for start of line in multi-line 103 // plus UNIX_LINES mode. 104 105 URX_RELOC_OPRND = 31, // Operand value in multi-operand ops that refers 106 // back into compiled pattern code, and thus must 107 // be relocated when inserting/deleting ops in code. 108 109 URX_STO_SP = 32, // Store the stack ptr. Operand is location within 110 // matcher data (not stack data) to store it. 111 URX_LD_SP = 33, // Load the stack pointer. Operand is location 112 // to load from. 113 URX_BACKREF = 34, // Back Reference. Parameter is the index of the 114 // capture group variables in the state stack frame. 115 URX_STO_INP_LOC = 35, // Store the input location. Operand is location 116 // within the matcher stack frame. 117 URX_JMPX = 36, // Conditional JMP. 118 // First Operand: JMP target location. 119 // Second Operand: Data location containing an 120 // input position. If current input position == 121 // saved input position, FAIL rather than taking 122 // the JMP 123 URX_LA_START = 37, // Starting a LookAround expression. 124 // Save InputPos and SP in static data. 125 // Operand: Static data offset for the save 126 URX_LA_END = 38, // Ending a Lookaround expression. 127 // Restore InputPos and Stack to saved values. 128 // Operand: Static data offset for saved data. 129 URX_ONECHAR_I = 39, // Test for case-insensitive match of a literal character. 130 // Operand: the literal char. 131 URX_STRING_I = 40, // Case insensitive string compare. 132 // First Operand: Index of start of string in string literals 133 // Second Operand (next word in compiled code): 134 // the length of the string. 135 URX_BACKREF_I = 41, // Case insensitive back reference. 136 // Parameter is the index of the 137 // capture group variables in the state stack frame. 138 URX_DOLLAR_M = 42, // $ in multi-line mode. 139 URX_CARET_M = 43, // ^ in multi-line mode. 140 URX_LB_START = 44, // LookBehind Start. 141 // Paramater is data location 142 URX_LB_CONT = 45, // LookBehind Continue. 143 // Param 0: the data location 144 // Param 1: The minimum length of the look-behind match 145 // Param 2: The max length of the look-behind match 146 URX_LB_END = 46, // LookBehind End. 147 // Parameter is the data location. 148 // Check that match ended at the right spot, 149 // Restore original input string len. 150 URX_LBN_CONT = 47, // Negative LookBehind Continue 151 // Param 0: the data location 152 // Param 1: The minimum length of the look-behind match 153 // Param 2: The max length of the look-behind match 154 // Param 3: The pattern loc following the look-behind block. 155 URX_LBN_END = 48, // Negative LookBehind end 156 // Parameter is the data location. 157 // Check that the match ended at the right spot. 158 URX_STAT_SETREF_N = 49, // Reference to a prebuilt set (e.g. \w), negated 159 // Operand is index of set in array of sets. 160 URX_LOOP_SR_I = 50, // Init a [set]* loop. 161 // Operand is the sets index in array of user sets. 162 URX_LOOP_C = 51, // Continue a [set]* or OneChar* loop. 163 // Operand is a matcher static data location. 164 // Must always immediately follow LOOP_x_I instruction. 165 URX_LOOP_DOT_I = 52, // .*, initialization of the optimized loop. 166 // Operand value: 167 // bit 0: 168 // 0: Normal (. doesn't match new-line) mode. 169 // 1: . matches new-line mode. 170 // bit 1: controls what new-lines are recognized by this operation. 171 // 0: All Unicode New-lines 172 // 1: UNIX_LINES, \u000a only. 173 URX_BACKSLASH_BU = 53, // \b or \B in UREGEX_UWORD mode, using Unicode style 174 // word boundaries. 175 URX_DOLLAR_D = 54, // $ end of input test, in UNIX_LINES mode. 176 URX_DOLLAR_MD = 55 // $ end of input test, in MULTI_LINE and UNIX_LINES mode. 177 178}; 179 180// Keep this list of opcode names in sync with the above enum 181// Used for debug printing only. 182#define URX_OPCODE_NAMES \ 183 " ", \ 184 "BACKTRACK", \ 185 "END", \ 186 "ONECHAR", \ 187 "STRING", \ 188 "STRING_LEN", \ 189 "STATE_SAVE", \ 190 "NOP", \ 191 "START_CAPTURE", \ 192 "END_CAPTURE", \ 193 "URX_STATIC_SETREF", \ 194 "SETREF", \ 195 "DOTANY", \ 196 "JMP", \ 197 "FAIL", \ 198 "JMP_SAV", \ 199 "BACKSLASH_B", \ 200 "BACKSLASH_G", \ 201 "JMP_SAV_X", \ 202 "BACKSLASH_X", \ 203 "BACKSLASH_Z", \ 204 "DOTANY_ALL", \ 205 "BACKSLASH_D", \ 206 "CARET", \ 207 "DOLLAR", \ 208 "CTR_INIT", \ 209 "CTR_INIT_NG", \ 210 "DOTANY_UNIX", \ 211 "CTR_LOOP", \ 212 "CTR_LOOP_NG", \ 213 "URX_CARET_M_UNIX", \ 214 "RELOC_OPRND", \ 215 "STO_SP", \ 216 "LD_SP", \ 217 "BACKREF", \ 218 "STO_INP_LOC", \ 219 "JMPX", \ 220 "LA_START", \ 221 "LA_END", \ 222 "ONECHAR_I", \ 223 "STRING_I", \ 224 "BACKREF_I", \ 225 "DOLLAR_M", \ 226 "CARET_M", \ 227 "LB_START", \ 228 "LB_CONT", \ 229 "LB_END", \ 230 "LBN_CONT", \ 231 "LBN_END", \ 232 "STAT_SETREF_N", \ 233 "LOOP_SR_I", \ 234 "LOOP_C", \ 235 "LOOP_DOT_I", \ 236 "BACKSLASH_BU", \ 237 "DOLLAR_D", \ 238 "DOLLAR_MD" 239 240 241// 242// Convenience macros for assembling and disassembling a compiled operation. 243// 244#define URX_BUILD(type, val) (int32_t)((type << 24) | (val)) 245#define URX_TYPE(x) ((uint32_t)(x) >> 24) 246#define URX_VAL(x) ((x) & 0xffffff) 247 248 249// 250// Access to Unicode Sets composite character properties 251// The sets are accessed by the match engine for things like \w (word boundary) 252// 253enum { 254 URX_ISWORD_SET = 1, 255 URX_ISALNUM_SET = 2, 256 URX_ISALPHA_SET = 3, 257 URX_ISSPACE_SET = 4, 258 259 URX_GC_NORMAL, // Sets for finding grapheme cluster boundaries. 260 URX_GC_EXTEND, 261 URX_GC_CONTROL, 262 URX_GC_L, 263 URX_GC_LV, 264 URX_GC_LVT, 265 URX_GC_V, 266 URX_GC_T, 267 268 URX_LAST_SET, 269 270 URX_NEG_SET = 0x800000 // Flag bit to reverse sense of set 271 // membership test. 272}; 273 274 275// 276// Match Engine State Stack Frame Layout. 277// 278struct REStackFrame { 279 // Header 280 int64_t fInputIdx; // Position of next character in the input string 281 int64_t fPatIdx; // Position of next Op in the compiled pattern 282 // (int64_t for UVector64, values fit in an int32_t) 283 // Remainder 284 int64_t fExtra[1]; // Extra state, for capture group start/ends 285 // atomic parentheses, repeat counts, etc. 286 // Locations assigned at pattern compile time. 287 // Variable-length array. 288}; 289// number of UVector elements in the header 290#define RESTACKFRAME_HDRCOUNT 2 291 292// 293// Start-Of-Match type. Used by find() to quickly scan to positions where a 294// match might start before firing up the full match engine. 295// 296enum StartOfMatch { 297 START_NO_INFO, // No hint available. 298 START_CHAR, // Match starts with a literal code point. 299 START_SET, // Match starts with something matching a set. 300 START_START, // Match starts at start of buffer only (^ or \A) 301 START_LINE, // Match starts with ^ in multi-line mode. 302 START_STRING // Match starts with a literal string. 303}; 304 305#define START_OF_MATCH_STR(v) ((v)==START_NO_INFO? "START_NO_INFO" : \ 306 (v)==START_CHAR? "START_CHAR" : \ 307 (v)==START_SET? "START_SET" : \ 308 (v)==START_START? "START_START" : \ 309 (v)==START_LINE? "START_LINE" : \ 310 (v)==START_STRING? "START_STRING" : \ 311 "ILLEGAL") 312 313// 314// 8 bit set, to fast-path latin-1 set membership tests. 315// 316struct Regex8BitSet : public UMemory { 317 inline Regex8BitSet(); 318 inline void operator = (const Regex8BitSet &s); 319 inline void init(const UnicodeSet *src); 320 inline UBool contains(UChar32 c); 321 inline void add(UChar32 c); 322 int8_t d[32]; 323}; 324 325inline Regex8BitSet::Regex8BitSet() { 326 uprv_memset(d, 0, sizeof(d)); 327} 328 329inline UBool Regex8BitSet::contains(UChar32 c) { 330 // No bounds checking! This is deliberate. 331 return ((d[c>>3] & 1 <<(c&7)) != 0); 332} 333 334inline void Regex8BitSet::add(UChar32 c) { 335 d[c>>3] |= 1 << (c&7); 336} 337 338inline void Regex8BitSet::init(const UnicodeSet *s) { 339 if (s != NULL) { 340 for (int32_t i=0; i<=255; i++) { 341 if (s->contains(i)) { 342 this->add(i); 343 } 344 } 345 } 346} 347 348inline void Regex8BitSet::operator = (const Regex8BitSet &s) { 349 uprv_memcpy(d, s.d, sizeof(d)); 350} 351 352 353// Case folded UText Iterator helper class. 354// Wraps a UText, provides a case-folded enumeration over its contents. 355// Used in implementing case insensitive matching constructs. 356// Implementation in rematch.cpp 357 358class CaseFoldingUTextIterator: public UMemory { 359 public: 360 CaseFoldingUTextIterator(UText &text); 361 ~CaseFoldingUTextIterator(); 362 363 UChar32 next(); // Next case folded character 364 365 UBool inExpansion(); // True if last char returned from next() and the 366 // next to be returned both originated from a string 367 // folding of the same code point from the orignal UText. 368 private: 369 UText &fUText; 370 const UCaseProps *fcsp; 371 const UChar *fFoldChars; 372 int32_t fFoldLength; 373 int32_t fFoldIndex; 374 375}; 376 377 378// Case folded UChar * string iterator. 379// Wraps a UChar *, provides a case-folded enumeration over its contents. 380// Used in implementing case insensitive matching constructs. 381// Implementation in rematch.cpp 382 383class CaseFoldingUCharIterator: public UMemory { 384 public: 385 CaseFoldingUCharIterator(const UChar *chars, int64_t start, int64_t limit); 386 ~CaseFoldingUCharIterator(); 387 388 UChar32 next(); // Next case folded character 389 390 UBool inExpansion(); // True if last char returned from next() and the 391 // next to be returned both originated from a string 392 // folding of the same code point from the orignal UText. 393 394 int64_t getIndex(); // Return the current input buffer index. 395 396 private: 397 const UChar *fChars; 398 int64_t fIndex; 399 int64_t fLimit; 400 const UCaseProps *fcsp; 401 const UChar *fFoldChars; 402 int32_t fFoldLength; 403 int32_t fFoldIndex; 404 405}; 406 407U_NAMESPACE_END 408#endif 409 410