[2674] | 1 | /* |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
| 3 | * contributor license agreements. See the NOTICE file distributed with |
---|
| 4 | * this work for additional information regarding copyright ownership. |
---|
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
| 6 | * (the "License"); you may not use this file except in compliance with |
---|
| 7 | * the License. You may obtain a copy of the License at |
---|
| 8 | * |
---|
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 10 | * |
---|
| 11 | * Unless required by applicable law or agreed to in writing, software |
---|
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 14 | * See the License for the specific language governing permissions and |
---|
| 15 | * limitations under the License. |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | /* |
---|
| 19 | * $Id: RegxParser.hpp 568078 2007-08-21 11:43:25Z amassari $ |
---|
| 20 | */ |
---|
| 21 | |
---|
| 22 | /* |
---|
| 23 | * A regular expression parser |
---|
| 24 | */ |
---|
| 25 | #if !defined(REGXPARSER_HPP) |
---|
| 26 | #define REGXPARSER_HPP |
---|
| 27 | |
---|
| 28 | // --------------------------------------------------------------------------- |
---|
| 29 | // Includes |
---|
| 30 | // --------------------------------------------------------------------------- |
---|
| 31 | #include <xercesc/util/RefVectorOf.hpp> |
---|
| 32 | #include <xercesc/util/XMLUniDefs.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 | }; |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | // --------------------------------------------------------------------------- |
---|
| 210 | // RegxParser: Getter Methods |
---|
| 211 | // --------------------------------------------------------------------------- |
---|
| 212 | inline unsigned short RegxParser::getParseContext() const { |
---|
| 213 | |
---|
| 214 | return fParseContext; |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | inline unsigned short RegxParser::getState() const { |
---|
| 218 | |
---|
| 219 | return fState; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | inline XMLInt32 RegxParser::getCharData() const { |
---|
| 223 | |
---|
| 224 | return fCharData; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | inline int RegxParser::getNoParen() const { |
---|
| 228 | |
---|
| 229 | return fNoGroups; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | inline int RegxParser::getOffset() const { |
---|
| 233 | |
---|
| 234 | return fOffset; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | inline bool RegxParser::hasBackReferences() const { |
---|
| 238 | |
---|
| 239 | return fHasBackReferences; |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | inline TokenFactory* RegxParser::getTokenFactory() const { |
---|
| 243 | |
---|
| 244 | return fTokenFactory; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | inline MemoryManager* RegxParser::getMemoryManager() const { |
---|
| 248 | return fMemoryManager; |
---|
| 249 | } |
---|
| 250 | // --------------------------------------------------------------------------- |
---|
| 251 | // RegxParser: Setter Methods |
---|
| 252 | // --------------------------------------------------------------------------- |
---|
| 253 | inline void RegxParser::setParseContext(const unsigned short value) { |
---|
| 254 | |
---|
| 255 | fParseContext = value; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | inline void RegxParser::setTokenFactory(TokenFactory* const tokFactory) { |
---|
| 259 | |
---|
| 260 | fTokenFactory = tokFactory; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | // --------------------------------------------------------------------------- |
---|
| 264 | // RegxParser: Helper Methods |
---|
| 265 | // --------------------------------------------------------------------------- |
---|
| 266 | inline bool RegxParser::isSet(const int flag) { |
---|
| 267 | |
---|
| 268 | return (fOptions & flag) == flag; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | |
---|
| 272 | inline int RegxParser::hexChar(const XMLInt32 ch) { |
---|
| 273 | |
---|
| 274 | if (ch < chDigit_0 || ch > chLatin_f) |
---|
| 275 | return -1; |
---|
| 276 | |
---|
| 277 | if (ch <= chDigit_9) |
---|
| 278 | return ch - chDigit_0; |
---|
| 279 | |
---|
| 280 | if (ch < chLatin_A) |
---|
| 281 | return -1; |
---|
| 282 | |
---|
| 283 | if (ch <= chLatin_F) |
---|
| 284 | return ch - chLatin_A + 10; |
---|
| 285 | |
---|
| 286 | if (ch < chLatin_a) |
---|
| 287 | return -1; |
---|
| 288 | |
---|
| 289 | return ch - chLatin_a + 10; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | XERCES_CPP_NAMESPACE_END |
---|
| 293 | |
---|
| 294 | #endif |
---|
| 295 | |
---|
| 296 | /** |
---|
| 297 | * End file RegxParser.hpp |
---|
| 298 | */ |
---|
| 299 | |
---|