]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/regexcmp.h
ICU-57132.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / regexcmp.h
1 //
2 // regexcmp.h
3 //
4 // Copyright (C) 2002-2016, International Business Machines Corporation and others.
5 // All Rights Reserved.
6 //
7 // This file contains declarations for the class RegexCompile
8 //
9 // This class is internal to the regular expression implementation.
10 // For the public Regular Expression API, see the file "unicode/regex.h"
11 //
12
13
14 #ifndef RBBISCAN_H
15 #define RBBISCAN_H
16
17 #include "unicode/utypes.h"
18 #if !UCONFIG_NO_REGULAR_EXPRESSIONS
19
20 #include "unicode/parseerr.h"
21 #include "unicode/uniset.h"
22 #include "unicode/uobject.h"
23 #include "unicode/utext.h"
24 #include "uhash.h"
25 #include "uvector.h"
26 #include "uvectr32.h"
27
28
29
30 U_NAMESPACE_BEGIN
31
32
33 //--------------------------------------------------------------------------------
34 //
35 // class RegexCompile Contains the regular expression compiler.
36 //
37 //--------------------------------------------------------------------------------
38 struct RegexTableEl;
39 class RegexPattern;
40
41
42 class U_I18N_API RegexCompile : public UMemory {
43 public:
44
45 enum {
46 kStackSize = 100 // The size of the state stack for
47 }; // pattern parsing. Corresponds roughly
48 // to the depth of parentheses nesting
49 // that is allowed in the rules.
50
51 struct RegexPatternChar {
52 UChar32 fChar;
53 UBool fQuoted;
54 };
55
56 RegexCompile(RegexPattern *rp, UErrorCode &e);
57
58 void compile(const UnicodeString &pat, UParseError &pp, UErrorCode &e);
59 void compile(UText *pat, UParseError &pp, UErrorCode &e);
60
61
62 virtual ~RegexCompile();
63
64 void nextChar(RegexPatternChar &c); // Get the next char from the input stream.
65
66 static void cleanup(); // Memory cleanup
67
68
69
70 // Categories of parentheses in pattern.
71 // The category is saved in the compile-time parentheses stack frame, and
72 // determines the code to be generated when the matching close ) is encountered.
73 enum EParenClass {
74 plain = -1, // No special handling
75 capturing = -2,
76 atomic = -3,
77 lookAhead = -4,
78 negLookAhead = -5,
79 flags = -6,
80 lookBehind = -7,
81 lookBehindN = -8
82 };
83
84 private:
85
86
87 UBool doParseActions(int32_t a);
88 void error(UErrorCode e); // error reporting convenience function.
89
90 UChar32 nextCharLL();
91 UChar32 peekCharLL();
92 UnicodeSet *scanProp();
93 UnicodeSet *scanPosixProp();
94 void handleCloseParen();
95 int32_t blockTopLoc(UBool reserve); // Locate a position in the compiled pattern
96 // at the top of the just completed block
97 // or operation, and optionally ensure that
98 // there is space to add an opcode there.
99 void compileSet(UnicodeSet *theSet); // Generate the compiled pattern for
100 // a reference to a UnicodeSet.
101 void compileInterval(int32_t InitOp, // Generate the code for a {min,max} quantifier.
102 int32_t LoopOp);
103 UBool compileInlineInterval(); // Generate inline code for a {min,max} quantifier
104 void literalChar(UChar32 c); // Compile a literal char
105 void fixLiterals(UBool split=FALSE); // Generate code for pending literal characters.
106 void insertOp(int32_t where); // Open up a slot for a new op in the
107 // generated code at the specified location.
108 void appendOp(int32_t op); // Append a new op to the compiled pattern.
109 void appendOp(int32_t type, int32_t val); // Build & append a new op to the compiled pattern.
110 int32_t buildOp(int32_t type, int32_t val); // Construct a new pcode instruction.
111 int32_t allocateData(int32_t size); // Allocate space in the matcher data area.
112 // Return index of the newly allocated data.
113 int32_t allocateStackData(int32_t size); // Allocate space in the match back-track stack frame.
114 // Return offset index in the frame.
115 int32_t minMatchLength(int32_t start,
116 int32_t end);
117 int32_t maxMatchLength(int32_t start,
118 int32_t end);
119 void matchStartType();
120 void stripNOPs();
121
122 void setEval(int32_t op);
123 void setPushOp(int32_t op);
124 UChar32 scanNamedChar();
125 UnicodeSet *createSetForProperty(const UnicodeString &propName, UBool negated);
126
127 public: // Public for testing only.
128 static void U_EXPORT2 findCaseInsensitiveStarters(UChar32 c, UnicodeSet *starterChars);
129 private:
130
131
132 UErrorCode *fStatus;
133 RegexPattern *fRXPat;
134 UParseError *fParseErr;
135
136 //
137 // Data associated with low level character scanning
138 //
139 int64_t fScanIndex; // Index of current character being processed
140 // in the rule input string.
141 UBool fQuoteMode; // Scan is in a \Q...\E quoted region
142 UBool fInBackslashQuote; // Scan is between a '\' and the following char.
143 UBool fEOLComments; // When scan is just after '(?', inhibit #... to
144 // end of line comments, in favor of (?#...) comments.
145 int64_t fLineNum; // Line number in input file.
146 int64_t fCharNum; // Char position within the line.
147 UChar32 fLastChar; // Previous char, needed to count CR-LF
148 // as a single line, not two.
149 UChar32 fPeekChar; // Saved char, if we've scanned ahead.
150
151
152 RegexPatternChar fC; // Current char for parse state machine
153 // processing.
154
155 //
156 // Data for the state machine that parses the regular expression.
157 //
158 RegexTableEl **fStateTable; // State Transition Table for regex Rule
159 // parsing. index by p[state][char-class]
160
161 uint16_t fStack[kStackSize]; // State stack, holds state pushes
162 int32_t fStackPtr; // and pops as specified in the state
163 // transition rules.
164
165 //
166 // Data associated with the generation of the pcode for the match engine
167 //
168 int32_t fModeFlags; // Match Flags. (Case Insensitive, etc.)
169 // Always has high bit (31) set so that flag values
170 // on the paren stack are distinguished from relocatable
171 // pcode addresses.
172 int32_t fNewModeFlags; // New flags, while compiling (?i, holds state
173 // until last flag is scanned.
174 UBool fSetModeFlag; // true for (?ismx, false for (?-ismx
175
176 UnicodeString fLiteralChars; // Literal chars or strings from the pattern are accumulated here.
177 // Once completed, meaning that some non-literal pattern
178 // construct is encountered, the appropriate opcodes
179 // to match the literal will be generated, and this
180 // string will be cleared.
181
182 int64_t fPatternLength; // Length of the input pattern string.
183
184 UVector32 fParenStack; // parentheses stack. Each frame consists of
185 // the positions of compiled pattern operations
186 // needing fixup, followed by negative value. The
187 // first entry in each frame is the position of the
188 // spot reserved for use when a quantifier
189 // needs to add a SAVE at the start of a (block)
190 // The negative value (-1, -2,...) indicates
191 // the kind of paren that opened the frame. Some
192 // need special handling on close.
193
194
195 int32_t fMatchOpenParen; // The position in the compiled pattern
196 // of the slot reserved for a state save
197 // at the start of the most recently processed
198 // parenthesized block. Updated when processing
199 // a close to the location for the corresponding open.
200
201 int32_t fMatchCloseParen; // The position in the pattern of the first
202 // location after the most recently processed
203 // parenthesized block.
204
205 int32_t fIntervalLow; // {lower, upper} interval quantifier values.
206 int32_t fIntervalUpper; // Placed here temporarily, when pattern is
207 // initially scanned. Each new interval
208 // encountered overwrites these values.
209 // -1 for the upper interval value means none
210 // was specified (unlimited occurences.)
211
212 int64_t fNameStartPos; // Starting position of a \N{NAME} name in a
213 // pattern, valid while remainder of name is
214 // scanned.
215
216 UStack fSetStack; // Stack of UnicodeSets, used while evaluating
217 // (at compile time) set expressions within
218 // the pattern.
219 UStack fSetOpStack; // Stack of pending set operators (&&, --, union)
220
221 UChar32 fLastSetLiteral; // The last single code point added to a set.
222 // needed when "-y" is scanned, and we need
223 // to turn "x-y" into a range.
224
225 UnicodeString *fCaptureName; // Named Capture, the group name is built up
226 // in this string while being scanned.
227 };
228
229 // Constant values to be pushed onto fSetOpStack while scanning & evalueating [set expressions]
230 // The high 16 bits are the operator precedence, and the low 16 are a code for the operation itself.
231
232 enum SetOperations {
233 setStart = 0 << 16 | 1,
234 setEnd = 1 << 16 | 2,
235 setNegation = 2 << 16 | 3,
236 setCaseClose = 2 << 16 | 9,
237 setDifference2 = 3 << 16 | 4, // '--' set difference operator
238 setIntersection2 = 3 << 16 | 5, // '&&' set intersection operator
239 setUnion = 4 << 16 | 6, // implicit union of adjacent items
240 setDifference1 = 4 << 16 | 7, // '-', single dash difference op, for compatibility with old UnicodeSet.
241 setIntersection1 = 4 << 16 | 8 // '&', single amp intersection op, for compatibility with old UnicodeSet.
242 };
243
244 U_NAMESPACE_END
245 #endif // !UCONFIG_NO_REGULAR_EXPRESSIONS
246 #endif // RBBISCAN_H