1 | /*
|
---|
2 | * Copyright 2001-2004 The Apache Software Foundation.
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 |
|
---|
17 | /*
|
---|
18 | * $Id: RegxParser.hpp,v 1.9 2004/09/08 13:56:47 peiyongz Exp $
|
---|
19 | */
|
---|
20 |
|
---|
21 | /*
|
---|
22 | * A regular expression parser
|
---|
23 | */
|
---|
24 | #if !defined(REGXPARSER_HPP)
|
---|
25 | #define REGXPARSER_HPP
|
---|
26 |
|
---|
27 | // ---------------------------------------------------------------------------
|
---|
28 | // Includes
|
---|
29 | // ---------------------------------------------------------------------------
|
---|
30 | #include <xercesc/util/RefVectorOf.hpp>
|
---|
31 | #include <xercesc/util/XMLUniDefs.hpp>
|
---|
32 | #include <xercesc/util/Mutexes.hpp>
|
---|
33 |
|
---|
34 | XERCES_CPP_NAMESPACE_BEGIN
|
---|
35 |
|
---|
36 | // ---------------------------------------------------------------------------
|
---|
37 | // Forward Declaration
|
---|
38 | // ---------------------------------------------------------------------------
|
---|
39 | class Token;
|
---|
40 | class RangeToken;
|
---|
41 | class TokenFactory;
|
---|
42 |
|
---|
43 | class XMLUTIL_EXPORT RegxParser : public XMemory
|
---|
44 | {
|
---|
45 | public:
|
---|
46 |
|
---|
47 | // -----------------------------------------------------------------------
|
---|
48 | // Public constant data
|
---|
49 | // -----------------------------------------------------------------------
|
---|
50 | // Parse tokens
|
---|
51 | enum {
|
---|
52 | REGX_T_CHAR = 0,
|
---|
53 | REGX_T_EOF = 1,
|
---|
54 | REGX_T_OR = 2,
|
---|
55 | REGX_T_STAR = 3,
|
---|
56 | REGX_T_PLUS = 4,
|
---|
57 | REGX_T_QUESTION = 5,
|
---|
58 | REGX_T_LPAREN = 6,
|
---|
59 | REGX_T_RPAREN = 7,
|
---|
60 | REGX_T_DOT = 8,
|
---|
61 | REGX_T_LBRACKET = 9,
|
---|
62 | REGX_T_BACKSOLIDUS = 10,
|
---|
63 | REGX_T_CARET = 11,
|
---|
64 | REGX_T_DOLLAR = 12,
|
---|
65 | REGX_T_LPAREN2 = 13,
|
---|
66 | REGX_T_LOOKAHEAD = 14,
|
---|
67 | REGX_T_NEGATIVELOOKAHEAD = 15,
|
---|
68 | REGX_T_LOOKBEHIND = 16,
|
---|
69 | REGX_T_NEGATIVELOOKBEHIND = 17,
|
---|
70 | REGX_T_INDEPENDENT = 18,
|
---|
71 | REGX_T_SET_OPERATIONS = 19,
|
---|
72 | REGX_T_POSIX_CHARCLASS_START = 20,
|
---|
73 | REGX_T_COMMENT = 21,
|
---|
74 | REGX_T_MODIFIERS = 22,
|
---|
75 | REGX_T_CONDITION = 23,
|
---|
76 | REGX_T_XMLSCHEMA_CC_SUBTRACTION = 24
|
---|
77 | };
|
---|
78 |
|
---|
79 | static const unsigned short S_NORMAL;
|
---|
80 | static const unsigned short S_INBRACKETS;
|
---|
81 | static const unsigned short S_INXBRACKETS;
|
---|
82 |
|
---|
83 | // -----------------------------------------------------------------------
|
---|
84 | // Public Constructors and Destructor
|
---|
85 | // -----------------------------------------------------------------------
|
---|
86 | RegxParser(MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager);
|
---|
87 | virtual ~RegxParser();
|
---|
88 |
|
---|
89 | // -----------------------------------------------------------------------
|
---|
90 | // Getter methods
|
---|
91 | // -----------------------------------------------------------------------
|
---|
92 | unsigned short getParseContext() const;
|
---|
93 | unsigned short getState() const;
|
---|
94 | XMLInt32 getCharData() const;
|
---|
95 | int getNoParen() const;
|
---|
96 | int getOffset() const;
|
---|
97 | bool hasBackReferences() const;
|
---|
98 | TokenFactory* getTokenFactory() const;
|
---|
99 |
|
---|
100 | // -----------------------------------------------------------------------
|
---|
101 | // Setter methods
|
---|
102 | // -----------------------------------------------------------------------
|
---|
103 | void setParseContext(const unsigned short value);
|
---|
104 | void setTokenFactory(TokenFactory* const tokFactory);
|
---|
105 |
|
---|
106 | // -----------------------------------------------------------------------
|
---|
107 | // Public Parsing methods
|
---|
108 | // -----------------------------------------------------------------------
|
---|
109 | Token* parse(const XMLCh* const regxStr, const int options);
|
---|
110 |
|
---|
111 | protected:
|
---|
112 | // -----------------------------------------------------------------------
|
---|
113 | // Protected Helper methods
|
---|
114 | // -----------------------------------------------------------------------
|
---|
115 | virtual bool checkQuestion(const int off);
|
---|
116 | virtual XMLInt32 decodeEscaped();
|
---|
117 | MemoryManager* getMemoryManager() const;
|
---|
118 | // -----------------------------------------------------------------------
|
---|
119 | // Protected Parsing/Processing methods
|
---|
120 | // -----------------------------------------------------------------------
|
---|
121 | void processNext();
|
---|
122 | Token* parseRegx(const bool matchingRParen = false);
|
---|
123 | virtual Token* processCaret();
|
---|
124 | virtual Token* processDollar();
|
---|
125 | virtual Token* processLook(const unsigned short tokType);
|
---|
126 | virtual Token* processBacksolidus_A();
|
---|
127 | virtual Token* processBacksolidus_z();
|
---|
128 | virtual Token* processBacksolidus_Z();
|
---|
129 | virtual Token* processBacksolidus_b();
|
---|
130 | virtual Token* processBacksolidus_B();
|
---|
131 | virtual Token* processBacksolidus_lt();
|
---|
132 | virtual Token* processBacksolidus_gt();
|
---|
133 | virtual Token* processBacksolidus_c();
|
---|
134 | virtual Token* processBacksolidus_C();
|
---|
135 | virtual Token* processBacksolidus_i();
|
---|
136 | virtual Token* processBacksolidus_I();
|
---|
137 | virtual Token* processBacksolidus_g();
|
---|
138 | virtual Token* processBacksolidus_X();
|
---|
139 | virtual Token* processBackReference();
|
---|
140 | virtual Token* processStar(Token* const tok);
|
---|
141 | virtual Token* processPlus(Token* const tok);
|
---|
142 | virtual Token* processQuestion(Token* const tok);
|
---|
143 | virtual Token* processParen();
|
---|
144 | virtual Token* processParen2();
|
---|
145 | virtual Token* processCondition();
|
---|
146 | virtual Token* processModifiers();
|
---|
147 | virtual Token* processIndependent();
|
---|
148 | virtual RangeToken* parseCharacterClass(const bool useNRange);
|
---|
149 | virtual RangeToken* parseSetOperations();
|
---|
150 | virtual XMLInt32 processCInCharacterClass(RangeToken* const tok,
|
---|
151 | const XMLInt32 ch);
|
---|
152 | RangeToken* processBacksolidus_pP(const XMLInt32 ch);
|
---|
153 |
|
---|
154 | // -----------------------------------------------------------------------
|
---|
155 | // Protected PreCreated RangeToken access methods
|
---|
156 | // -----------------------------------------------------------------------
|
---|
157 | virtual Token* getTokenForShorthand(const XMLInt32 ch);
|
---|
158 |
|
---|
159 | private:
|
---|
160 | // -----------------------------------------------------------------------
|
---|
161 | // Private parsing/processing methods
|
---|
162 | // -----------------------------------------------------------------------
|
---|
163 | Token* parseTerm(const bool matchingRParen = false);
|
---|
164 | Token* parseFactor();
|
---|
165 | Token* parseAtom();
|
---|
166 |
|
---|
167 | // -----------------------------------------------------------------------
|
---|
168 | // Unimplemented constructors and operators
|
---|
169 | // -----------------------------------------------------------------------
|
---|
170 | RegxParser(const RegxParser&);
|
---|
171 | RegxParser& operator=(const RegxParser&);
|
---|
172 |
|
---|
173 | // -----------------------------------------------------------------------
|
---|
174 | // Private data types
|
---|
175 | // -----------------------------------------------------------------------
|
---|
176 | class ReferencePosition : public XMemory
|
---|
177 | {
|
---|
178 | public :
|
---|
179 | ReferencePosition(const int refNo, const int position);
|
---|
180 |
|
---|
181 | int fReferenceNo;
|
---|
182 | int fPosition;
|
---|
183 | };
|
---|
184 |
|
---|
185 | // -----------------------------------------------------------------------
|
---|
186 | // Private Helper methods
|
---|
187 | // -----------------------------------------------------------------------
|
---|
188 | bool isSet(const int flag);
|
---|
189 | int hexChar(const XMLInt32 ch);
|
---|
190 |
|
---|
191 | // -----------------------------------------------------------------------
|
---|
192 | // Private data members
|
---|
193 | // -----------------------------------------------------------------------
|
---|
194 | MemoryManager* fMemoryManager;
|
---|
195 | bool fHasBackReferences;
|
---|
196 | int fOptions;
|
---|
197 | int fOffset;
|
---|
198 | int fNoGroups;
|
---|
199 | unsigned short fParseContext;
|
---|
200 | int fStringLen;
|
---|
201 | unsigned short fState;
|
---|
202 | XMLInt32 fCharData;
|
---|
203 | XMLCh* fString;
|
---|
204 | RefVectorOf<ReferencePosition>* fReferences;
|
---|
205 | TokenFactory* fTokenFactory;
|
---|
206 | XMLMutex fMutex;
|
---|
207 | };
|
---|
208 |
|
---|
209 |
|
---|
210 | // ---------------------------------------------------------------------------
|
---|
211 | // RegxParser: Getter Methods
|
---|
212 | // ---------------------------------------------------------------------------
|
---|
213 | inline unsigned short RegxParser::getParseContext() const {
|
---|
214 |
|
---|
215 | return fParseContext;
|
---|
216 | }
|
---|
217 |
|
---|
218 | inline unsigned short RegxParser::getState() const {
|
---|
219 |
|
---|
220 | return fState;
|
---|
221 | }
|
---|
222 |
|
---|
223 | inline XMLInt32 RegxParser::getCharData() const {
|
---|
224 |
|
---|
225 | return fCharData;
|
---|
226 | }
|
---|
227 |
|
---|
228 | inline int RegxParser::getNoParen() const {
|
---|
229 |
|
---|
230 | return fNoGroups;
|
---|
231 | }
|
---|
232 |
|
---|
233 | inline int RegxParser::getOffset() const {
|
---|
234 |
|
---|
235 | return fOffset;
|
---|
236 | }
|
---|
237 |
|
---|
238 | inline bool RegxParser::hasBackReferences() const {
|
---|
239 |
|
---|
240 | return fHasBackReferences;
|
---|
241 | }
|
---|
242 |
|
---|
243 | inline TokenFactory* RegxParser::getTokenFactory() const {
|
---|
244 |
|
---|
245 | return fTokenFactory;
|
---|
246 | }
|
---|
247 |
|
---|
248 | inline MemoryManager* RegxParser::getMemoryManager() const {
|
---|
249 | return fMemoryManager;
|
---|
250 | }
|
---|
251 | // ---------------------------------------------------------------------------
|
---|
252 | // RegxParser: Setter Methods
|
---|
253 | // ---------------------------------------------------------------------------
|
---|
254 | inline void RegxParser::setParseContext(const unsigned short value) {
|
---|
255 |
|
---|
256 | fParseContext = value;
|
---|
257 | }
|
---|
258 |
|
---|
259 | inline void RegxParser::setTokenFactory(TokenFactory* const tokFactory) {
|
---|
260 |
|
---|
261 | fTokenFactory = tokFactory;
|
---|
262 | }
|
---|
263 |
|
---|
264 | // ---------------------------------------------------------------------------
|
---|
265 | // RegxParser: Helper Methods
|
---|
266 | // ---------------------------------------------------------------------------
|
---|
267 | inline bool RegxParser::isSet(const int flag) {
|
---|
268 |
|
---|
269 | return (fOptions & flag) == flag;
|
---|
270 | }
|
---|
271 |
|
---|
272 |
|
---|
273 | inline int RegxParser::hexChar(const XMLInt32 ch) {
|
---|
274 |
|
---|
275 | if (ch < chDigit_0 || ch > chLatin_f)
|
---|
276 | return -1;
|
---|
277 |
|
---|
278 | if (ch <= chDigit_9)
|
---|
279 | return ch - chDigit_0;
|
---|
280 |
|
---|
281 | if (ch < chLatin_A)
|
---|
282 | return -1;
|
---|
283 |
|
---|
284 | if (ch <= chLatin_F)
|
---|
285 | return ch - chLatin_A + 10;
|
---|
286 |
|
---|
287 | if (ch < chLatin_a)
|
---|
288 | return -1;
|
---|
289 |
|
---|
290 | return ch - chLatin_a + 10;
|
---|
291 | }
|
---|
292 |
|
---|
293 | XERCES_CPP_NAMESPACE_END
|
---|
294 |
|
---|
295 | #endif
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * End file RegxParser.hpp
|
---|
299 | */
|
---|
300 |
|
---|