Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * regexp.c: generic and extensible Regular Expression engine |
| 3 | * |
| 4 | * Basically designed with the purpose of compiling regexps for |
| 5 | * the variety of validation/shemas mechanisms now available in |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 6 | * XML related specifications these include: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 7 | * - XML-1.0 DTD validation |
| 8 | * - XML Schemas structure part 1 |
| 9 | * - XML Schemas Datatypes part 2 especially Appendix F |
| 10 | * - RELAX-NG/TREX i.e. the counter proposal |
| 11 | * |
| 12 | * See Copyright for the status of this software. |
| 13 | * |
| 14 | * Daniel Veillard <veillard@redhat.com> |
| 15 | */ |
| 16 | |
| 17 | #define IN_LIBXML |
| 18 | #include "libxml.h" |
| 19 | |
| 20 | #ifdef LIBXML_REGEXP_ENABLED |
| 21 | |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 22 | /* #define DEBUG_ERR */ |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 23 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <string.h> |
Daniel Veillard | ebe48c6 | 2003-12-03 12:12:27 +0000 | [diff] [blame] | 26 | #ifdef HAVE_LIMITS_H |
| 27 | #include <limits.h> |
| 28 | #endif |
| 29 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 30 | #include <libxml/tree.h> |
| 31 | #include <libxml/parserInternals.h> |
| 32 | #include <libxml/xmlregexp.h> |
| 33 | #include <libxml/xmlautomata.h> |
| 34 | #include <libxml/xmlunicode.h> |
| 35 | |
Daniel Veillard | ebe48c6 | 2003-12-03 12:12:27 +0000 | [diff] [blame] | 36 | #ifndef INT_MAX |
| 37 | #define INT_MAX 123456789 /* easy to flag and big enough for our needs */ |
| 38 | #endif |
| 39 | |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 40 | /* #define DEBUG_REGEXP_GRAPH */ |
Daniel Veillard | 1075228 | 2005-08-08 13:05:13 +0000 | [diff] [blame] | 41 | /* #define DEBUG_REGEXP_EXEC */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 42 | /* #define DEBUG_PUSH */ |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 43 | /* #define DEBUG_COMPACTION */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 44 | |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 45 | #define MAX_PUSH 10000000 |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 46 | |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 47 | #define ERROR(str) \ |
| 48 | ctxt->error = XML_REGEXP_COMPILE_ERROR; \ |
| 49 | xmlRegexpErrCompile(ctxt, str); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 50 | #define NEXT ctxt->cur++ |
| 51 | #define CUR (*(ctxt->cur)) |
| 52 | #define NXT(index) (ctxt->cur[index]) |
| 53 | |
| 54 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l) |
| 55 | #define NEXTL(l) ctxt->cur += l; |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 56 | #define XML_REG_STRING_SEPARATOR '|' |
William M. Brack | a9cbf28 | 2007-03-21 13:16:33 +0000 | [diff] [blame] | 57 | /* |
| 58 | * Need PREV to check on a '-' within a Character Group. May only be used |
| 59 | * when it's guaranteed that cur is not at the beginning of ctxt->string! |
| 60 | */ |
| 61 | #define PREV (ctxt->cur[-1]) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 62 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 63 | /** |
| 64 | * TODO: |
| 65 | * |
| 66 | * macro to flag unimplemented blocks |
| 67 | */ |
| 68 | #define TODO \ |
| 69 | xmlGenericError(xmlGenericErrorContext, \ |
| 70 | "Unimplemented block at %s:%d\n", \ |
| 71 | __FILE__, __LINE__); |
| 72 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 73 | /************************************************************************ |
| 74 | * * |
| 75 | * Datatypes and structures * |
| 76 | * * |
| 77 | ************************************************************************/ |
| 78 | |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 79 | /* |
| 80 | * Note: the order of the enums below is significant, do not shuffle |
| 81 | */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 82 | typedef enum { |
| 83 | XML_REGEXP_EPSILON = 1, |
| 84 | XML_REGEXP_CHARVAL, |
| 85 | XML_REGEXP_RANGES, |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 86 | XML_REGEXP_SUBREG, /* used for () sub regexps */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 87 | XML_REGEXP_STRING, |
| 88 | XML_REGEXP_ANYCHAR, /* . */ |
| 89 | XML_REGEXP_ANYSPACE, /* \s */ |
| 90 | XML_REGEXP_NOTSPACE, /* \S */ |
| 91 | XML_REGEXP_INITNAME, /* \l */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 92 | XML_REGEXP_NOTINITNAME, /* \L */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 93 | XML_REGEXP_NAMECHAR, /* \c */ |
| 94 | XML_REGEXP_NOTNAMECHAR, /* \C */ |
| 95 | XML_REGEXP_DECIMAL, /* \d */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 96 | XML_REGEXP_NOTDECIMAL, /* \D */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 97 | XML_REGEXP_REALCHAR, /* \w */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 98 | XML_REGEXP_NOTREALCHAR, /* \W */ |
| 99 | XML_REGEXP_LETTER = 100, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 100 | XML_REGEXP_LETTER_UPPERCASE, |
| 101 | XML_REGEXP_LETTER_LOWERCASE, |
| 102 | XML_REGEXP_LETTER_TITLECASE, |
| 103 | XML_REGEXP_LETTER_MODIFIER, |
| 104 | XML_REGEXP_LETTER_OTHERS, |
| 105 | XML_REGEXP_MARK, |
| 106 | XML_REGEXP_MARK_NONSPACING, |
| 107 | XML_REGEXP_MARK_SPACECOMBINING, |
| 108 | XML_REGEXP_MARK_ENCLOSING, |
| 109 | XML_REGEXP_NUMBER, |
| 110 | XML_REGEXP_NUMBER_DECIMAL, |
| 111 | XML_REGEXP_NUMBER_LETTER, |
| 112 | XML_REGEXP_NUMBER_OTHERS, |
| 113 | XML_REGEXP_PUNCT, |
| 114 | XML_REGEXP_PUNCT_CONNECTOR, |
| 115 | XML_REGEXP_PUNCT_DASH, |
| 116 | XML_REGEXP_PUNCT_OPEN, |
| 117 | XML_REGEXP_PUNCT_CLOSE, |
| 118 | XML_REGEXP_PUNCT_INITQUOTE, |
| 119 | XML_REGEXP_PUNCT_FINQUOTE, |
| 120 | XML_REGEXP_PUNCT_OTHERS, |
| 121 | XML_REGEXP_SEPAR, |
| 122 | XML_REGEXP_SEPAR_SPACE, |
| 123 | XML_REGEXP_SEPAR_LINE, |
| 124 | XML_REGEXP_SEPAR_PARA, |
| 125 | XML_REGEXP_SYMBOL, |
| 126 | XML_REGEXP_SYMBOL_MATH, |
| 127 | XML_REGEXP_SYMBOL_CURRENCY, |
| 128 | XML_REGEXP_SYMBOL_MODIFIER, |
| 129 | XML_REGEXP_SYMBOL_OTHERS, |
| 130 | XML_REGEXP_OTHER, |
| 131 | XML_REGEXP_OTHER_CONTROL, |
| 132 | XML_REGEXP_OTHER_FORMAT, |
| 133 | XML_REGEXP_OTHER_PRIVATE, |
| 134 | XML_REGEXP_OTHER_NA, |
| 135 | XML_REGEXP_BLOCK_NAME |
| 136 | } xmlRegAtomType; |
| 137 | |
| 138 | typedef enum { |
| 139 | XML_REGEXP_QUANT_EPSILON = 1, |
| 140 | XML_REGEXP_QUANT_ONCE, |
| 141 | XML_REGEXP_QUANT_OPT, |
| 142 | XML_REGEXP_QUANT_MULT, |
| 143 | XML_REGEXP_QUANT_PLUS, |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 144 | XML_REGEXP_QUANT_ONCEONLY, |
| 145 | XML_REGEXP_QUANT_ALL, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 146 | XML_REGEXP_QUANT_RANGE |
| 147 | } xmlRegQuantType; |
| 148 | |
| 149 | typedef enum { |
| 150 | XML_REGEXP_START_STATE = 1, |
| 151 | XML_REGEXP_FINAL_STATE, |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 152 | XML_REGEXP_TRANS_STATE, |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 153 | XML_REGEXP_SINK_STATE, |
| 154 | XML_REGEXP_UNREACH_STATE |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 155 | } xmlRegStateType; |
| 156 | |
| 157 | typedef enum { |
| 158 | XML_REGEXP_MARK_NORMAL = 0, |
| 159 | XML_REGEXP_MARK_START, |
| 160 | XML_REGEXP_MARK_VISITED |
| 161 | } xmlRegMarkedType; |
| 162 | |
| 163 | typedef struct _xmlRegRange xmlRegRange; |
| 164 | typedef xmlRegRange *xmlRegRangePtr; |
| 165 | |
| 166 | struct _xmlRegRange { |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 167 | int neg; /* 0 normal, 1 not, 2 exclude */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 168 | xmlRegAtomType type; |
| 169 | int start; |
| 170 | int end; |
| 171 | xmlChar *blockName; |
| 172 | }; |
| 173 | |
| 174 | typedef struct _xmlRegAtom xmlRegAtom; |
| 175 | typedef xmlRegAtom *xmlRegAtomPtr; |
| 176 | |
| 177 | typedef struct _xmlAutomataState xmlRegState; |
| 178 | typedef xmlRegState *xmlRegStatePtr; |
| 179 | |
| 180 | struct _xmlRegAtom { |
| 181 | int no; |
| 182 | xmlRegAtomType type; |
| 183 | xmlRegQuantType quant; |
| 184 | int min; |
| 185 | int max; |
| 186 | |
| 187 | void *valuep; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 188 | void *valuep2; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 189 | int neg; |
| 190 | int codepoint; |
| 191 | xmlRegStatePtr start; |
| 192 | xmlRegStatePtr stop; |
| 193 | int maxRanges; |
| 194 | int nbRanges; |
| 195 | xmlRegRangePtr *ranges; |
| 196 | void *data; |
| 197 | }; |
| 198 | |
| 199 | typedef struct _xmlRegCounter xmlRegCounter; |
| 200 | typedef xmlRegCounter *xmlRegCounterPtr; |
| 201 | |
| 202 | struct _xmlRegCounter { |
| 203 | int min; |
| 204 | int max; |
| 205 | }; |
| 206 | |
| 207 | typedef struct _xmlRegTrans xmlRegTrans; |
| 208 | typedef xmlRegTrans *xmlRegTransPtr; |
| 209 | |
| 210 | struct _xmlRegTrans { |
| 211 | xmlRegAtomPtr atom; |
| 212 | int to; |
| 213 | int counter; |
| 214 | int count; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 215 | int nd; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | struct _xmlAutomataState { |
| 219 | xmlRegStateType type; |
| 220 | xmlRegMarkedType mark; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 221 | xmlRegMarkedType reached; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 222 | int no; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 223 | int maxTrans; |
| 224 | int nbTrans; |
| 225 | xmlRegTrans *trans; |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 226 | /* knowing states ponting to us can speed things up */ |
| 227 | int maxTransTo; |
| 228 | int nbTransTo; |
| 229 | int *transTo; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 230 | }; |
| 231 | |
| 232 | typedef struct _xmlAutomata xmlRegParserCtxt; |
| 233 | typedef xmlRegParserCtxt *xmlRegParserCtxtPtr; |
| 234 | |
| 235 | struct _xmlAutomata { |
| 236 | xmlChar *string; |
| 237 | xmlChar *cur; |
| 238 | |
| 239 | int error; |
| 240 | int neg; |
| 241 | |
| 242 | xmlRegStatePtr start; |
| 243 | xmlRegStatePtr end; |
| 244 | xmlRegStatePtr state; |
| 245 | |
| 246 | xmlRegAtomPtr atom; |
| 247 | |
| 248 | int maxAtoms; |
| 249 | int nbAtoms; |
| 250 | xmlRegAtomPtr *atoms; |
| 251 | |
| 252 | int maxStates; |
| 253 | int nbStates; |
| 254 | xmlRegStatePtr *states; |
| 255 | |
| 256 | int maxCounters; |
| 257 | int nbCounters; |
| 258 | xmlRegCounter *counters; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 259 | |
| 260 | int determinist; |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 261 | int negs; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 262 | }; |
| 263 | |
| 264 | struct _xmlRegexp { |
| 265 | xmlChar *string; |
| 266 | int nbStates; |
| 267 | xmlRegStatePtr *states; |
| 268 | int nbAtoms; |
| 269 | xmlRegAtomPtr *atoms; |
| 270 | int nbCounters; |
| 271 | xmlRegCounter *counters; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 272 | int determinist; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 273 | /* |
| 274 | * That's the compact form for determinists automatas |
| 275 | */ |
| 276 | int nbstates; |
| 277 | int *compact; |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 278 | void **transdata; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 279 | int nbstrings; |
| 280 | xmlChar **stringMap; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | typedef struct _xmlRegExecRollback xmlRegExecRollback; |
| 284 | typedef xmlRegExecRollback *xmlRegExecRollbackPtr; |
| 285 | |
| 286 | struct _xmlRegExecRollback { |
| 287 | xmlRegStatePtr state;/* the current state */ |
| 288 | int index; /* the index in the input stack */ |
| 289 | int nextbranch; /* the next transition to explore in that state */ |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 290 | int *counts; /* save the automata state if it has some */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 291 | }; |
| 292 | |
| 293 | typedef struct _xmlRegInputToken xmlRegInputToken; |
| 294 | typedef xmlRegInputToken *xmlRegInputTokenPtr; |
| 295 | |
| 296 | struct _xmlRegInputToken { |
| 297 | xmlChar *value; |
| 298 | void *data; |
| 299 | }; |
| 300 | |
| 301 | struct _xmlRegExecCtxt { |
| 302 | int status; /* execution status != 0 indicate an error */ |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 303 | int determinist; /* did we find an indeterministic behaviour */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 304 | xmlRegexpPtr comp; /* the compiled regexp */ |
| 305 | xmlRegExecCallbacks callback; |
| 306 | void *data; |
| 307 | |
| 308 | xmlRegStatePtr state;/* the current state */ |
| 309 | int transno; /* the current transition on that state */ |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 310 | int transcount; /* the number of chars in char counted transitions */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 311 | |
| 312 | /* |
| 313 | * A stack of rollback states |
| 314 | */ |
| 315 | int maxRollbacks; |
| 316 | int nbRollbacks; |
| 317 | xmlRegExecRollback *rollbacks; |
| 318 | |
| 319 | /* |
| 320 | * The state of the automata if any |
| 321 | */ |
| 322 | int *counts; |
| 323 | |
| 324 | /* |
| 325 | * The input stack |
| 326 | */ |
| 327 | int inputStackMax; |
| 328 | int inputStackNr; |
| 329 | int index; |
| 330 | int *charStack; |
| 331 | const xmlChar *inputString; /* when operating on characters */ |
| 332 | xmlRegInputTokenPtr inputStack;/* when operating on strings */ |
| 333 | |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 334 | /* |
| 335 | * error handling |
| 336 | */ |
| 337 | int errStateNo; /* the error state number */ |
| 338 | xmlRegStatePtr errState; /* the error state */ |
| 339 | xmlChar *errString; /* the string raising the error */ |
| 340 | int *errCounts; /* counters at the error state */ |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 341 | int nbPush; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 342 | }; |
| 343 | |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 344 | #define REGEXP_ALL_COUNTER 0x123456 |
| 345 | #define REGEXP_ALL_LAX_COUNTER 0x123457 |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 346 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 347 | static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 348 | static void xmlRegFreeState(xmlRegStatePtr state); |
| 349 | static void xmlRegFreeAtom(xmlRegAtomPtr atom); |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 350 | static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 351 | static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint); |
| 352 | static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, |
| 353 | int neg, int start, int end, const xmlChar *blockName); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 354 | |
| 355 | /************************************************************************ |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 356 | * * |
| 357 | * Regexp memory error handler * |
| 358 | * * |
| 359 | ************************************************************************/ |
| 360 | /** |
| 361 | * xmlRegexpErrMemory: |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 362 | * @extra: extra information |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 363 | * |
| 364 | * Handle an out of memory condition |
| 365 | */ |
| 366 | static void |
| 367 | xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra) |
| 368 | { |
| 369 | const char *regexp = NULL; |
| 370 | if (ctxt != NULL) { |
| 371 | regexp = (const char *) ctxt->string; |
| 372 | ctxt->error = XML_ERR_NO_MEMORY; |
| 373 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 374 | __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP, |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 375 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 376 | regexp, NULL, 0, 0, |
| 377 | "Memory allocation failed : %s\n", extra); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * xmlRegexpErrCompile: |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 382 | * @extra: extra information |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 383 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 384 | * Handle a compilation failure |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 385 | */ |
| 386 | static void |
| 387 | xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra) |
| 388 | { |
| 389 | const char *regexp = NULL; |
| 390 | int idx = 0; |
| 391 | |
| 392 | if (ctxt != NULL) { |
| 393 | regexp = (const char *) ctxt->string; |
| 394 | idx = ctxt->cur - ctxt->string; |
| 395 | ctxt->error = XML_REGEXP_COMPILE_ERROR; |
| 396 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 397 | __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP, |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 398 | XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra, |
| 399 | regexp, NULL, idx, 0, |
| 400 | "failed to compile: %s\n", extra); |
| 401 | } |
| 402 | |
| 403 | /************************************************************************ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 404 | * * |
| 405 | * Allocation/Deallocation * |
| 406 | * * |
| 407 | ************************************************************************/ |
| 408 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 409 | static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 410 | /** |
| 411 | * xmlRegEpxFromParse: |
| 412 | * @ctxt: the parser context used to build it |
| 413 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 414 | * Allocate a new regexp and fill it with the result from the parser |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 415 | * |
| 416 | * Returns the new regexp or NULL in case of error |
| 417 | */ |
| 418 | static xmlRegexpPtr |
| 419 | xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) { |
| 420 | xmlRegexpPtr ret; |
| 421 | |
| 422 | ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 423 | if (ret == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 424 | xmlRegexpErrMemory(ctxt, "compiling regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 425 | return(NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 426 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 427 | memset(ret, 0, sizeof(xmlRegexp)); |
| 428 | ret->string = ctxt->string; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 429 | ret->nbStates = ctxt->nbStates; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 430 | ret->states = ctxt->states; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 431 | ret->nbAtoms = ctxt->nbAtoms; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 432 | ret->atoms = ctxt->atoms; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 433 | ret->nbCounters = ctxt->nbCounters; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 434 | ret->counters = ctxt->counters; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 435 | ret->determinist = ctxt->determinist; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 436 | if (ret->determinist == -1) { |
| 437 | xmlRegexpIsDeterminist(ret); |
| 438 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 439 | |
| 440 | if ((ret->determinist != 0) && |
| 441 | (ret->nbCounters == 0) && |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 442 | (ctxt->negs == 0) && |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 443 | (ret->atoms != NULL) && |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 444 | (ret->atoms[0] != NULL) && |
| 445 | (ret->atoms[0]->type == XML_REGEXP_STRING)) { |
| 446 | int i, j, nbstates = 0, nbatoms = 0; |
| 447 | int *stateRemap; |
| 448 | int *stringRemap; |
| 449 | int *transitions; |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 450 | void **transdata; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 451 | xmlChar **stringMap; |
| 452 | xmlChar *value; |
| 453 | |
| 454 | /* |
| 455 | * Switch to a compact representation |
| 456 | * 1/ counting the effective number of states left |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 457 | * 2/ counting the unique number of atoms, and check that |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 458 | * they are all of the string type |
| 459 | * 3/ build a table state x atom for the transitions |
| 460 | */ |
| 461 | |
| 462 | stateRemap = xmlMalloc(ret->nbStates * sizeof(int)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 463 | if (stateRemap == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 464 | xmlRegexpErrMemory(ctxt, "compiling regexp"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 465 | xmlFree(ret); |
| 466 | return(NULL); |
| 467 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 468 | for (i = 0;i < ret->nbStates;i++) { |
| 469 | if (ret->states[i] != NULL) { |
| 470 | stateRemap[i] = nbstates; |
| 471 | nbstates++; |
| 472 | } else { |
| 473 | stateRemap[i] = -1; |
| 474 | } |
| 475 | } |
| 476 | #ifdef DEBUG_COMPACTION |
| 477 | printf("Final: %d states\n", nbstates); |
| 478 | #endif |
| 479 | stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 480 | if (stringMap == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 481 | xmlRegexpErrMemory(ctxt, "compiling regexp"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 482 | xmlFree(stateRemap); |
| 483 | xmlFree(ret); |
| 484 | return(NULL); |
| 485 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 486 | stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 487 | if (stringRemap == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 488 | xmlRegexpErrMemory(ctxt, "compiling regexp"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 489 | xmlFree(stringMap); |
| 490 | xmlFree(stateRemap); |
| 491 | xmlFree(ret); |
| 492 | return(NULL); |
| 493 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 494 | for (i = 0;i < ret->nbAtoms;i++) { |
| 495 | if ((ret->atoms[i]->type == XML_REGEXP_STRING) && |
| 496 | (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) { |
| 497 | value = ret->atoms[i]->valuep; |
| 498 | for (j = 0;j < nbatoms;j++) { |
| 499 | if (xmlStrEqual(stringMap[j], value)) { |
| 500 | stringRemap[i] = j; |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | if (j >= nbatoms) { |
| 505 | stringRemap[i] = nbatoms; |
| 506 | stringMap[nbatoms] = xmlStrdup(value); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 507 | if (stringMap[nbatoms] == NULL) { |
| 508 | for (i = 0;i < nbatoms;i++) |
| 509 | xmlFree(stringMap[i]); |
| 510 | xmlFree(stringRemap); |
| 511 | xmlFree(stringMap); |
| 512 | xmlFree(stateRemap); |
| 513 | xmlFree(ret); |
| 514 | return(NULL); |
| 515 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 516 | nbatoms++; |
| 517 | } |
| 518 | } else { |
| 519 | xmlFree(stateRemap); |
| 520 | xmlFree(stringRemap); |
| 521 | for (i = 0;i < nbatoms;i++) |
| 522 | xmlFree(stringMap[i]); |
| 523 | xmlFree(stringMap); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 524 | xmlFree(ret); |
| 525 | return(NULL); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 526 | } |
| 527 | } |
| 528 | #ifdef DEBUG_COMPACTION |
| 529 | printf("Final: %d atoms\n", nbatoms); |
| 530 | #endif |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 531 | transitions = (int *) xmlMalloc((nbstates + 1) * |
| 532 | (nbatoms + 1) * sizeof(int)); |
| 533 | if (transitions == NULL) { |
| 534 | xmlFree(stateRemap); |
| 535 | xmlFree(stringRemap); |
| 536 | xmlFree(stringMap); |
| 537 | xmlFree(ret); |
| 538 | return(NULL); |
| 539 | } |
| 540 | memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int)); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 541 | |
| 542 | /* |
| 543 | * Allocate the transition table. The first entry for each |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 544 | * state corresponds to the state type. |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 545 | */ |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 546 | transdata = NULL; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 547 | |
| 548 | for (i = 0;i < ret->nbStates;i++) { |
| 549 | int stateno, atomno, targetno, prev; |
| 550 | xmlRegStatePtr state; |
| 551 | xmlRegTransPtr trans; |
| 552 | |
| 553 | stateno = stateRemap[i]; |
| 554 | if (stateno == -1) |
| 555 | continue; |
| 556 | state = ret->states[i]; |
| 557 | |
| 558 | transitions[stateno * (nbatoms + 1)] = state->type; |
| 559 | |
| 560 | for (j = 0;j < state->nbTrans;j++) { |
| 561 | trans = &(state->trans[j]); |
| 562 | if ((trans->to == -1) || (trans->atom == NULL)) |
| 563 | continue; |
| 564 | atomno = stringRemap[trans->atom->no]; |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 565 | if ((trans->atom->data != NULL) && (transdata == NULL)) { |
| 566 | transdata = (void **) xmlMalloc(nbstates * nbatoms * |
| 567 | sizeof(void *)); |
| 568 | if (transdata != NULL) |
| 569 | memset(transdata, 0, |
| 570 | nbstates * nbatoms * sizeof(void *)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 571 | else { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 572 | xmlRegexpErrMemory(ctxt, "compiling regexp"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 573 | break; |
| 574 | } |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 575 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 576 | targetno = stateRemap[trans->to]; |
| 577 | /* |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 578 | * if the same atom can generate transitions to 2 different |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 579 | * states then it means the automata is not determinist and |
| 580 | * the compact form can't be used ! |
| 581 | */ |
| 582 | prev = transitions[stateno * (nbatoms + 1) + atomno + 1]; |
| 583 | if (prev != 0) { |
| 584 | if (prev != targetno + 1) { |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 585 | ret->determinist = 0; |
| 586 | #ifdef DEBUG_COMPACTION |
| 587 | printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n", |
| 588 | i, j, trans->atom->no, trans->to, atomno, targetno); |
| 589 | printf(" previous to is %d\n", prev); |
| 590 | #endif |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 591 | if (transdata != NULL) |
| 592 | xmlFree(transdata); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 593 | xmlFree(transitions); |
| 594 | xmlFree(stateRemap); |
| 595 | xmlFree(stringRemap); |
| 596 | for (i = 0;i < nbatoms;i++) |
| 597 | xmlFree(stringMap[i]); |
| 598 | xmlFree(stringMap); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 599 | goto not_determ; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 600 | } |
| 601 | } else { |
| 602 | #if 0 |
| 603 | printf("State %d trans %d: atom %d to %d : %d to %d\n", |
| 604 | i, j, trans->atom->no, trans->to, atomno, targetno); |
| 605 | #endif |
| 606 | transitions[stateno * (nbatoms + 1) + atomno + 1] = |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 607 | targetno + 1; /* to avoid 0 */ |
| 608 | if (transdata != NULL) |
| 609 | transdata[stateno * nbatoms + atomno] = |
| 610 | trans->atom->data; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | } |
| 614 | ret->determinist = 1; |
| 615 | #ifdef DEBUG_COMPACTION |
| 616 | /* |
| 617 | * Debug |
| 618 | */ |
| 619 | for (i = 0;i < nbstates;i++) { |
| 620 | for (j = 0;j < nbatoms + 1;j++) { |
| 621 | printf("%02d ", transitions[i * (nbatoms + 1) + j]); |
| 622 | } |
| 623 | printf("\n"); |
| 624 | } |
| 625 | printf("\n"); |
| 626 | #endif |
| 627 | /* |
| 628 | * Cleanup of the old data |
| 629 | */ |
| 630 | if (ret->states != NULL) { |
| 631 | for (i = 0;i < ret->nbStates;i++) |
| 632 | xmlRegFreeState(ret->states[i]); |
| 633 | xmlFree(ret->states); |
| 634 | } |
| 635 | ret->states = NULL; |
| 636 | ret->nbStates = 0; |
| 637 | if (ret->atoms != NULL) { |
| 638 | for (i = 0;i < ret->nbAtoms;i++) |
| 639 | xmlRegFreeAtom(ret->atoms[i]); |
| 640 | xmlFree(ret->atoms); |
| 641 | } |
| 642 | ret->atoms = NULL; |
| 643 | ret->nbAtoms = 0; |
| 644 | |
| 645 | ret->compact = transitions; |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 646 | ret->transdata = transdata; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 647 | ret->stringMap = stringMap; |
| 648 | ret->nbstrings = nbatoms; |
| 649 | ret->nbstates = nbstates; |
| 650 | xmlFree(stateRemap); |
| 651 | xmlFree(stringRemap); |
| 652 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 653 | not_determ: |
| 654 | ctxt->string = NULL; |
| 655 | ctxt->nbStates = 0; |
| 656 | ctxt->states = NULL; |
| 657 | ctxt->nbAtoms = 0; |
| 658 | ctxt->atoms = NULL; |
| 659 | ctxt->nbCounters = 0; |
| 660 | ctxt->counters = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 661 | return(ret); |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * xmlRegNewParserCtxt: |
| 666 | * @string: the string to parse |
| 667 | * |
| 668 | * Allocate a new regexp parser context |
| 669 | * |
| 670 | * Returns the new context or NULL in case of error |
| 671 | */ |
| 672 | static xmlRegParserCtxtPtr |
| 673 | xmlRegNewParserCtxt(const xmlChar *string) { |
| 674 | xmlRegParserCtxtPtr ret; |
| 675 | |
| 676 | ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt)); |
| 677 | if (ret == NULL) |
| 678 | return(NULL); |
| 679 | memset(ret, 0, sizeof(xmlRegParserCtxt)); |
| 680 | if (string != NULL) |
| 681 | ret->string = xmlStrdup(string); |
| 682 | ret->cur = ret->string; |
| 683 | ret->neg = 0; |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 684 | ret->negs = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 685 | ret->error = 0; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 686 | ret->determinist = -1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 687 | return(ret); |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * xmlRegNewRange: |
| 692 | * @ctxt: the regexp parser context |
| 693 | * @neg: is that negative |
| 694 | * @type: the type of range |
| 695 | * @start: the start codepoint |
| 696 | * @end: the end codepoint |
| 697 | * |
| 698 | * Allocate a new regexp range |
| 699 | * |
| 700 | * Returns the new range or NULL in case of error |
| 701 | */ |
| 702 | static xmlRegRangePtr |
| 703 | xmlRegNewRange(xmlRegParserCtxtPtr ctxt, |
| 704 | int neg, xmlRegAtomType type, int start, int end) { |
| 705 | xmlRegRangePtr ret; |
| 706 | |
| 707 | ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange)); |
| 708 | if (ret == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 709 | xmlRegexpErrMemory(ctxt, "allocating range"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 710 | return(NULL); |
| 711 | } |
| 712 | ret->neg = neg; |
| 713 | ret->type = type; |
| 714 | ret->start = start; |
| 715 | ret->end = end; |
| 716 | return(ret); |
| 717 | } |
| 718 | |
| 719 | /** |
| 720 | * xmlRegFreeRange: |
| 721 | * @range: the regexp range |
| 722 | * |
| 723 | * Free a regexp range |
| 724 | */ |
| 725 | static void |
| 726 | xmlRegFreeRange(xmlRegRangePtr range) { |
| 727 | if (range == NULL) |
| 728 | return; |
| 729 | |
| 730 | if (range->blockName != NULL) |
| 731 | xmlFree(range->blockName); |
| 732 | xmlFree(range); |
| 733 | } |
| 734 | |
| 735 | /** |
| 736 | * xmlRegNewAtom: |
| 737 | * @ctxt: the regexp parser context |
| 738 | * @type: the type of atom |
| 739 | * |
| 740 | * Allocate a new regexp range |
| 741 | * |
| 742 | * Returns the new atom or NULL in case of error |
| 743 | */ |
| 744 | static xmlRegAtomPtr |
| 745 | xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) { |
| 746 | xmlRegAtomPtr ret; |
| 747 | |
| 748 | ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom)); |
| 749 | if (ret == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 750 | xmlRegexpErrMemory(ctxt, "allocating atom"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 751 | return(NULL); |
| 752 | } |
| 753 | memset(ret, 0, sizeof(xmlRegAtom)); |
| 754 | ret->type = type; |
| 755 | ret->quant = XML_REGEXP_QUANT_ONCE; |
| 756 | ret->min = 0; |
| 757 | ret->max = 0; |
| 758 | return(ret); |
| 759 | } |
| 760 | |
| 761 | /** |
| 762 | * xmlRegFreeAtom: |
| 763 | * @atom: the regexp atom |
| 764 | * |
| 765 | * Free a regexp atom |
| 766 | */ |
| 767 | static void |
| 768 | xmlRegFreeAtom(xmlRegAtomPtr atom) { |
| 769 | int i; |
| 770 | |
| 771 | if (atom == NULL) |
| 772 | return; |
| 773 | |
| 774 | for (i = 0;i < atom->nbRanges;i++) |
| 775 | xmlRegFreeRange(atom->ranges[i]); |
| 776 | if (atom->ranges != NULL) |
| 777 | xmlFree(atom->ranges); |
Daniel Veillard | de0e498 | 2005-07-03 14:35:44 +0000 | [diff] [blame] | 778 | if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL)) |
| 779 | xmlFree(atom->valuep); |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 780 | if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL)) |
| 781 | xmlFree(atom->valuep2); |
Daniel Veillard | de0e498 | 2005-07-03 14:35:44 +0000 | [diff] [blame] | 782 | if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL)) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 783 | xmlFree(atom->valuep); |
| 784 | xmlFree(atom); |
| 785 | } |
| 786 | |
| 787 | static xmlRegStatePtr |
| 788 | xmlRegNewState(xmlRegParserCtxtPtr ctxt) { |
| 789 | xmlRegStatePtr ret; |
| 790 | |
| 791 | ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState)); |
| 792 | if (ret == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 793 | xmlRegexpErrMemory(ctxt, "allocating state"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 794 | return(NULL); |
| 795 | } |
| 796 | memset(ret, 0, sizeof(xmlRegState)); |
| 797 | ret->type = XML_REGEXP_TRANS_STATE; |
| 798 | ret->mark = XML_REGEXP_MARK_NORMAL; |
| 799 | return(ret); |
| 800 | } |
| 801 | |
| 802 | /** |
| 803 | * xmlRegFreeState: |
| 804 | * @state: the regexp state |
| 805 | * |
| 806 | * Free a regexp state |
| 807 | */ |
| 808 | static void |
| 809 | xmlRegFreeState(xmlRegStatePtr state) { |
| 810 | if (state == NULL) |
| 811 | return; |
| 812 | |
| 813 | if (state->trans != NULL) |
| 814 | xmlFree(state->trans); |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 815 | if (state->transTo != NULL) |
| 816 | xmlFree(state->transTo); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 817 | xmlFree(state); |
| 818 | } |
| 819 | |
| 820 | /** |
| 821 | * xmlRegFreeParserCtxt: |
| 822 | * @ctxt: the regexp parser context |
| 823 | * |
| 824 | * Free a regexp parser context |
| 825 | */ |
| 826 | static void |
| 827 | xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) { |
| 828 | int i; |
| 829 | if (ctxt == NULL) |
| 830 | return; |
| 831 | |
| 832 | if (ctxt->string != NULL) |
| 833 | xmlFree(ctxt->string); |
| 834 | if (ctxt->states != NULL) { |
| 835 | for (i = 0;i < ctxt->nbStates;i++) |
| 836 | xmlRegFreeState(ctxt->states[i]); |
| 837 | xmlFree(ctxt->states); |
| 838 | } |
| 839 | if (ctxt->atoms != NULL) { |
| 840 | for (i = 0;i < ctxt->nbAtoms;i++) |
| 841 | xmlRegFreeAtom(ctxt->atoms[i]); |
| 842 | xmlFree(ctxt->atoms); |
| 843 | } |
| 844 | if (ctxt->counters != NULL) |
| 845 | xmlFree(ctxt->counters); |
| 846 | xmlFree(ctxt); |
| 847 | } |
| 848 | |
| 849 | /************************************************************************ |
| 850 | * * |
| 851 | * Display of Data structures * |
| 852 | * * |
| 853 | ************************************************************************/ |
| 854 | |
| 855 | static void |
| 856 | xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) { |
| 857 | switch (type) { |
| 858 | case XML_REGEXP_EPSILON: |
| 859 | fprintf(output, "epsilon "); break; |
| 860 | case XML_REGEXP_CHARVAL: |
| 861 | fprintf(output, "charval "); break; |
| 862 | case XML_REGEXP_RANGES: |
| 863 | fprintf(output, "ranges "); break; |
| 864 | case XML_REGEXP_SUBREG: |
| 865 | fprintf(output, "subexpr "); break; |
| 866 | case XML_REGEXP_STRING: |
| 867 | fprintf(output, "string "); break; |
| 868 | case XML_REGEXP_ANYCHAR: |
| 869 | fprintf(output, "anychar "); break; |
| 870 | case XML_REGEXP_ANYSPACE: |
| 871 | fprintf(output, "anyspace "); break; |
| 872 | case XML_REGEXP_NOTSPACE: |
| 873 | fprintf(output, "notspace "); break; |
| 874 | case XML_REGEXP_INITNAME: |
| 875 | fprintf(output, "initname "); break; |
| 876 | case XML_REGEXP_NOTINITNAME: |
| 877 | fprintf(output, "notinitname "); break; |
| 878 | case XML_REGEXP_NAMECHAR: |
| 879 | fprintf(output, "namechar "); break; |
| 880 | case XML_REGEXP_NOTNAMECHAR: |
| 881 | fprintf(output, "notnamechar "); break; |
| 882 | case XML_REGEXP_DECIMAL: |
| 883 | fprintf(output, "decimal "); break; |
| 884 | case XML_REGEXP_NOTDECIMAL: |
| 885 | fprintf(output, "notdecimal "); break; |
| 886 | case XML_REGEXP_REALCHAR: |
| 887 | fprintf(output, "realchar "); break; |
| 888 | case XML_REGEXP_NOTREALCHAR: |
| 889 | fprintf(output, "notrealchar "); break; |
| 890 | case XML_REGEXP_LETTER: |
| 891 | fprintf(output, "LETTER "); break; |
| 892 | case XML_REGEXP_LETTER_UPPERCASE: |
| 893 | fprintf(output, "LETTER_UPPERCASE "); break; |
| 894 | case XML_REGEXP_LETTER_LOWERCASE: |
| 895 | fprintf(output, "LETTER_LOWERCASE "); break; |
| 896 | case XML_REGEXP_LETTER_TITLECASE: |
| 897 | fprintf(output, "LETTER_TITLECASE "); break; |
| 898 | case XML_REGEXP_LETTER_MODIFIER: |
| 899 | fprintf(output, "LETTER_MODIFIER "); break; |
| 900 | case XML_REGEXP_LETTER_OTHERS: |
| 901 | fprintf(output, "LETTER_OTHERS "); break; |
| 902 | case XML_REGEXP_MARK: |
| 903 | fprintf(output, "MARK "); break; |
| 904 | case XML_REGEXP_MARK_NONSPACING: |
| 905 | fprintf(output, "MARK_NONSPACING "); break; |
| 906 | case XML_REGEXP_MARK_SPACECOMBINING: |
| 907 | fprintf(output, "MARK_SPACECOMBINING "); break; |
| 908 | case XML_REGEXP_MARK_ENCLOSING: |
| 909 | fprintf(output, "MARK_ENCLOSING "); break; |
| 910 | case XML_REGEXP_NUMBER: |
| 911 | fprintf(output, "NUMBER "); break; |
| 912 | case XML_REGEXP_NUMBER_DECIMAL: |
| 913 | fprintf(output, "NUMBER_DECIMAL "); break; |
| 914 | case XML_REGEXP_NUMBER_LETTER: |
| 915 | fprintf(output, "NUMBER_LETTER "); break; |
| 916 | case XML_REGEXP_NUMBER_OTHERS: |
| 917 | fprintf(output, "NUMBER_OTHERS "); break; |
| 918 | case XML_REGEXP_PUNCT: |
| 919 | fprintf(output, "PUNCT "); break; |
| 920 | case XML_REGEXP_PUNCT_CONNECTOR: |
| 921 | fprintf(output, "PUNCT_CONNECTOR "); break; |
| 922 | case XML_REGEXP_PUNCT_DASH: |
| 923 | fprintf(output, "PUNCT_DASH "); break; |
| 924 | case XML_REGEXP_PUNCT_OPEN: |
| 925 | fprintf(output, "PUNCT_OPEN "); break; |
| 926 | case XML_REGEXP_PUNCT_CLOSE: |
| 927 | fprintf(output, "PUNCT_CLOSE "); break; |
| 928 | case XML_REGEXP_PUNCT_INITQUOTE: |
| 929 | fprintf(output, "PUNCT_INITQUOTE "); break; |
| 930 | case XML_REGEXP_PUNCT_FINQUOTE: |
| 931 | fprintf(output, "PUNCT_FINQUOTE "); break; |
| 932 | case XML_REGEXP_PUNCT_OTHERS: |
| 933 | fprintf(output, "PUNCT_OTHERS "); break; |
| 934 | case XML_REGEXP_SEPAR: |
| 935 | fprintf(output, "SEPAR "); break; |
| 936 | case XML_REGEXP_SEPAR_SPACE: |
| 937 | fprintf(output, "SEPAR_SPACE "); break; |
| 938 | case XML_REGEXP_SEPAR_LINE: |
| 939 | fprintf(output, "SEPAR_LINE "); break; |
| 940 | case XML_REGEXP_SEPAR_PARA: |
| 941 | fprintf(output, "SEPAR_PARA "); break; |
| 942 | case XML_REGEXP_SYMBOL: |
| 943 | fprintf(output, "SYMBOL "); break; |
| 944 | case XML_REGEXP_SYMBOL_MATH: |
| 945 | fprintf(output, "SYMBOL_MATH "); break; |
| 946 | case XML_REGEXP_SYMBOL_CURRENCY: |
| 947 | fprintf(output, "SYMBOL_CURRENCY "); break; |
| 948 | case XML_REGEXP_SYMBOL_MODIFIER: |
| 949 | fprintf(output, "SYMBOL_MODIFIER "); break; |
| 950 | case XML_REGEXP_SYMBOL_OTHERS: |
| 951 | fprintf(output, "SYMBOL_OTHERS "); break; |
| 952 | case XML_REGEXP_OTHER: |
| 953 | fprintf(output, "OTHER "); break; |
| 954 | case XML_REGEXP_OTHER_CONTROL: |
| 955 | fprintf(output, "OTHER_CONTROL "); break; |
| 956 | case XML_REGEXP_OTHER_FORMAT: |
| 957 | fprintf(output, "OTHER_FORMAT "); break; |
| 958 | case XML_REGEXP_OTHER_PRIVATE: |
| 959 | fprintf(output, "OTHER_PRIVATE "); break; |
| 960 | case XML_REGEXP_OTHER_NA: |
| 961 | fprintf(output, "OTHER_NA "); break; |
| 962 | case XML_REGEXP_BLOCK_NAME: |
| 963 | fprintf(output, "BLOCK "); break; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | static void |
| 968 | xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) { |
| 969 | switch (type) { |
| 970 | case XML_REGEXP_QUANT_EPSILON: |
| 971 | fprintf(output, "epsilon "); break; |
| 972 | case XML_REGEXP_QUANT_ONCE: |
| 973 | fprintf(output, "once "); break; |
| 974 | case XML_REGEXP_QUANT_OPT: |
| 975 | fprintf(output, "? "); break; |
| 976 | case XML_REGEXP_QUANT_MULT: |
| 977 | fprintf(output, "* "); break; |
| 978 | case XML_REGEXP_QUANT_PLUS: |
| 979 | fprintf(output, "+ "); break; |
| 980 | case XML_REGEXP_QUANT_RANGE: |
| 981 | fprintf(output, "range "); break; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 982 | case XML_REGEXP_QUANT_ONCEONLY: |
| 983 | fprintf(output, "onceonly "); break; |
| 984 | case XML_REGEXP_QUANT_ALL: |
| 985 | fprintf(output, "all "); break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | static void |
| 989 | xmlRegPrintRange(FILE *output, xmlRegRangePtr range) { |
| 990 | fprintf(output, " range: "); |
| 991 | if (range->neg) |
| 992 | fprintf(output, "negative "); |
| 993 | xmlRegPrintAtomType(output, range->type); |
| 994 | fprintf(output, "%c - %c\n", range->start, range->end); |
| 995 | } |
| 996 | |
| 997 | static void |
| 998 | xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) { |
| 999 | fprintf(output, " atom: "); |
| 1000 | if (atom == NULL) { |
| 1001 | fprintf(output, "NULL\n"); |
| 1002 | return; |
| 1003 | } |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 1004 | if (atom->neg) |
| 1005 | fprintf(output, "not "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1006 | xmlRegPrintAtomType(output, atom->type); |
| 1007 | xmlRegPrintQuantType(output, atom->quant); |
| 1008 | if (atom->quant == XML_REGEXP_QUANT_RANGE) |
| 1009 | fprintf(output, "%d-%d ", atom->min, atom->max); |
| 1010 | if (atom->type == XML_REGEXP_STRING) |
| 1011 | fprintf(output, "'%s' ", (char *) atom->valuep); |
| 1012 | if (atom->type == XML_REGEXP_CHARVAL) |
| 1013 | fprintf(output, "char %c\n", atom->codepoint); |
| 1014 | else if (atom->type == XML_REGEXP_RANGES) { |
| 1015 | int i; |
| 1016 | fprintf(output, "%d entries\n", atom->nbRanges); |
| 1017 | for (i = 0; i < atom->nbRanges;i++) |
| 1018 | xmlRegPrintRange(output, atom->ranges[i]); |
| 1019 | } else if (atom->type == XML_REGEXP_SUBREG) { |
| 1020 | fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no); |
| 1021 | } else { |
| 1022 | fprintf(output, "\n"); |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | static void |
| 1027 | xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) { |
| 1028 | fprintf(output, " trans: "); |
| 1029 | if (trans == NULL) { |
| 1030 | fprintf(output, "NULL\n"); |
| 1031 | return; |
| 1032 | } |
| 1033 | if (trans->to < 0) { |
| 1034 | fprintf(output, "removed\n"); |
| 1035 | return; |
| 1036 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 1037 | if (trans->nd != 0) { |
| 1038 | if (trans->nd == 2) |
| 1039 | fprintf(output, "last not determinist, "); |
| 1040 | else |
| 1041 | fprintf(output, "not determinist, "); |
| 1042 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1043 | if (trans->counter >= 0) { |
| 1044 | fprintf(output, "counted %d, ", trans->counter); |
| 1045 | } |
Daniel Veillard | 8a001f6 | 2002-04-20 07:24:11 +0000 | [diff] [blame] | 1046 | if (trans->count == REGEXP_ALL_COUNTER) { |
| 1047 | fprintf(output, "all transition, "); |
| 1048 | } else if (trans->count >= 0) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1049 | fprintf(output, "count based %d, ", trans->count); |
| 1050 | } |
| 1051 | if (trans->atom == NULL) { |
| 1052 | fprintf(output, "epsilon to %d\n", trans->to); |
| 1053 | return; |
| 1054 | } |
| 1055 | if (trans->atom->type == XML_REGEXP_CHARVAL) |
| 1056 | fprintf(output, "char %c ", trans->atom->codepoint); |
| 1057 | fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to); |
| 1058 | } |
| 1059 | |
| 1060 | static void |
| 1061 | xmlRegPrintState(FILE *output, xmlRegStatePtr state) { |
| 1062 | int i; |
| 1063 | |
| 1064 | fprintf(output, " state: "); |
| 1065 | if (state == NULL) { |
| 1066 | fprintf(output, "NULL\n"); |
| 1067 | return; |
| 1068 | } |
| 1069 | if (state->type == XML_REGEXP_START_STATE) |
| 1070 | fprintf(output, "START "); |
| 1071 | if (state->type == XML_REGEXP_FINAL_STATE) |
| 1072 | fprintf(output, "FINAL "); |
| 1073 | |
| 1074 | fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans); |
| 1075 | for (i = 0;i < state->nbTrans; i++) { |
| 1076 | xmlRegPrintTrans(output, &(state->trans[i])); |
| 1077 | } |
| 1078 | } |
| 1079 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1080 | #ifdef DEBUG_REGEXP_GRAPH |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1081 | static void |
| 1082 | xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) { |
| 1083 | int i; |
| 1084 | |
| 1085 | fprintf(output, " ctxt: "); |
| 1086 | if (ctxt == NULL) { |
| 1087 | fprintf(output, "NULL\n"); |
| 1088 | return; |
| 1089 | } |
| 1090 | fprintf(output, "'%s' ", ctxt->string); |
| 1091 | if (ctxt->error) |
| 1092 | fprintf(output, "error "); |
| 1093 | if (ctxt->neg) |
| 1094 | fprintf(output, "neg "); |
| 1095 | fprintf(output, "\n"); |
| 1096 | fprintf(output, "%d atoms:\n", ctxt->nbAtoms); |
| 1097 | for (i = 0;i < ctxt->nbAtoms; i++) { |
| 1098 | fprintf(output, " %02d ", i); |
| 1099 | xmlRegPrintAtom(output, ctxt->atoms[i]); |
| 1100 | } |
| 1101 | if (ctxt->atom != NULL) { |
| 1102 | fprintf(output, "current atom:\n"); |
| 1103 | xmlRegPrintAtom(output, ctxt->atom); |
| 1104 | } |
| 1105 | fprintf(output, "%d states:", ctxt->nbStates); |
| 1106 | if (ctxt->start != NULL) |
| 1107 | fprintf(output, " start: %d", ctxt->start->no); |
| 1108 | if (ctxt->end != NULL) |
| 1109 | fprintf(output, " end: %d", ctxt->end->no); |
| 1110 | fprintf(output, "\n"); |
| 1111 | for (i = 0;i < ctxt->nbStates; i++) { |
| 1112 | xmlRegPrintState(output, ctxt->states[i]); |
| 1113 | } |
| 1114 | fprintf(output, "%d counters:\n", ctxt->nbCounters); |
| 1115 | for (i = 0;i < ctxt->nbCounters; i++) { |
| 1116 | fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min, |
| 1117 | ctxt->counters[i].max); |
| 1118 | } |
| 1119 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1120 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1121 | |
| 1122 | /************************************************************************ |
| 1123 | * * |
| 1124 | * Finite Automata structures manipulations * |
| 1125 | * * |
| 1126 | ************************************************************************/ |
| 1127 | |
| 1128 | static void |
| 1129 | xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom, |
| 1130 | int neg, xmlRegAtomType type, int start, int end, |
| 1131 | xmlChar *blockName) { |
| 1132 | xmlRegRangePtr range; |
| 1133 | |
| 1134 | if (atom == NULL) { |
| 1135 | ERROR("add range: atom is NULL"); |
| 1136 | return; |
| 1137 | } |
| 1138 | if (atom->type != XML_REGEXP_RANGES) { |
| 1139 | ERROR("add range: atom is not ranges"); |
| 1140 | return; |
| 1141 | } |
| 1142 | if (atom->maxRanges == 0) { |
| 1143 | atom->maxRanges = 4; |
| 1144 | atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges * |
| 1145 | sizeof(xmlRegRangePtr)); |
| 1146 | if (atom->ranges == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1147 | xmlRegexpErrMemory(ctxt, "adding ranges"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1148 | atom->maxRanges = 0; |
| 1149 | return; |
| 1150 | } |
| 1151 | } else if (atom->nbRanges >= atom->maxRanges) { |
| 1152 | xmlRegRangePtr *tmp; |
| 1153 | atom->maxRanges *= 2; |
| 1154 | tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges * |
| 1155 | sizeof(xmlRegRangePtr)); |
| 1156 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1157 | xmlRegexpErrMemory(ctxt, "adding ranges"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1158 | atom->maxRanges /= 2; |
| 1159 | return; |
| 1160 | } |
| 1161 | atom->ranges = tmp; |
| 1162 | } |
| 1163 | range = xmlRegNewRange(ctxt, neg, type, start, end); |
| 1164 | if (range == NULL) |
| 1165 | return; |
| 1166 | range->blockName = blockName; |
| 1167 | atom->ranges[atom->nbRanges++] = range; |
| 1168 | |
| 1169 | } |
| 1170 | |
| 1171 | static int |
| 1172 | xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) { |
| 1173 | if (ctxt->maxCounters == 0) { |
| 1174 | ctxt->maxCounters = 4; |
| 1175 | ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters * |
| 1176 | sizeof(xmlRegCounter)); |
| 1177 | if (ctxt->counters == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1178 | xmlRegexpErrMemory(ctxt, "allocating counter"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1179 | ctxt->maxCounters = 0; |
| 1180 | return(-1); |
| 1181 | } |
| 1182 | } else if (ctxt->nbCounters >= ctxt->maxCounters) { |
| 1183 | xmlRegCounter *tmp; |
| 1184 | ctxt->maxCounters *= 2; |
| 1185 | tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters * |
| 1186 | sizeof(xmlRegCounter)); |
| 1187 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1188 | xmlRegexpErrMemory(ctxt, "allocating counter"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1189 | ctxt->maxCounters /= 2; |
| 1190 | return(-1); |
| 1191 | } |
| 1192 | ctxt->counters = tmp; |
| 1193 | } |
| 1194 | ctxt->counters[ctxt->nbCounters].min = -1; |
| 1195 | ctxt->counters[ctxt->nbCounters].max = -1; |
| 1196 | return(ctxt->nbCounters++); |
| 1197 | } |
| 1198 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1199 | static int |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1200 | xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) { |
| 1201 | if (atom == NULL) { |
| 1202 | ERROR("atom push: atom is NULL"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1203 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1204 | } |
| 1205 | if (ctxt->maxAtoms == 0) { |
| 1206 | ctxt->maxAtoms = 4; |
| 1207 | ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms * |
| 1208 | sizeof(xmlRegAtomPtr)); |
| 1209 | if (ctxt->atoms == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1210 | xmlRegexpErrMemory(ctxt, "pushing atom"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1211 | ctxt->maxAtoms = 0; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1212 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1213 | } |
| 1214 | } else if (ctxt->nbAtoms >= ctxt->maxAtoms) { |
| 1215 | xmlRegAtomPtr *tmp; |
| 1216 | ctxt->maxAtoms *= 2; |
| 1217 | tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms * |
| 1218 | sizeof(xmlRegAtomPtr)); |
| 1219 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1220 | xmlRegexpErrMemory(ctxt, "allocating counter"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1221 | ctxt->maxAtoms /= 2; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1222 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1223 | } |
| 1224 | ctxt->atoms = tmp; |
| 1225 | } |
| 1226 | atom->no = ctxt->nbAtoms; |
| 1227 | ctxt->atoms[ctxt->nbAtoms++] = atom; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1228 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | static void |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1232 | xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target, |
| 1233 | int from) { |
| 1234 | if (target->maxTransTo == 0) { |
| 1235 | target->maxTransTo = 8; |
| 1236 | target->transTo = (int *) xmlMalloc(target->maxTransTo * |
| 1237 | sizeof(int)); |
| 1238 | if (target->transTo == NULL) { |
| 1239 | xmlRegexpErrMemory(ctxt, "adding transition"); |
| 1240 | target->maxTransTo = 0; |
| 1241 | return; |
| 1242 | } |
| 1243 | } else if (target->nbTransTo >= target->maxTransTo) { |
| 1244 | int *tmp; |
| 1245 | target->maxTransTo *= 2; |
| 1246 | tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo * |
| 1247 | sizeof(int)); |
| 1248 | if (tmp == NULL) { |
| 1249 | xmlRegexpErrMemory(ctxt, "adding transition"); |
| 1250 | target->maxTransTo /= 2; |
| 1251 | return; |
| 1252 | } |
| 1253 | target->transTo = tmp; |
| 1254 | } |
| 1255 | target->transTo[target->nbTransTo] = from; |
| 1256 | target->nbTransTo++; |
| 1257 | } |
| 1258 | |
| 1259 | static void |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1260 | xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state, |
| 1261 | xmlRegAtomPtr atom, xmlRegStatePtr target, |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1262 | int counter, int count) { |
William M. Brack | f9b5fa2 | 2004-05-10 07:52:15 +0000 | [diff] [blame] | 1263 | |
| 1264 | int nrtrans; |
| 1265 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1266 | if (state == NULL) { |
| 1267 | ERROR("add state: state is NULL"); |
| 1268 | return; |
| 1269 | } |
| 1270 | if (target == NULL) { |
| 1271 | ERROR("add state: target is NULL"); |
| 1272 | return; |
| 1273 | } |
William M. Brack | f9b5fa2 | 2004-05-10 07:52:15 +0000 | [diff] [blame] | 1274 | /* |
| 1275 | * Other routines follow the philosophy 'When in doubt, add a transition' |
| 1276 | * so we check here whether such a transition is already present and, if |
| 1277 | * so, silently ignore this request. |
| 1278 | */ |
| 1279 | |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1280 | for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) { |
| 1281 | xmlRegTransPtr trans = &(state->trans[nrtrans]); |
| 1282 | if ((trans->atom == atom) && |
| 1283 | (trans->to == target->no) && |
| 1284 | (trans->counter == counter) && |
| 1285 | (trans->count == count)) { |
William M. Brack | f9b5fa2 | 2004-05-10 07:52:15 +0000 | [diff] [blame] | 1286 | #ifdef DEBUG_REGEXP_GRAPH |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1287 | printf("Ignoring duplicate transition from %d to %d\n", |
| 1288 | state->no, target->no); |
William M. Brack | f9b5fa2 | 2004-05-10 07:52:15 +0000 | [diff] [blame] | 1289 | #endif |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1290 | return; |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1291 | } |
William M. Brack | f9b5fa2 | 2004-05-10 07:52:15 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1294 | if (state->maxTrans == 0) { |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1295 | state->maxTrans = 8; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1296 | state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans * |
| 1297 | sizeof(xmlRegTrans)); |
| 1298 | if (state->trans == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1299 | xmlRegexpErrMemory(ctxt, "adding transition"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1300 | state->maxTrans = 0; |
| 1301 | return; |
| 1302 | } |
| 1303 | } else if (state->nbTrans >= state->maxTrans) { |
| 1304 | xmlRegTrans *tmp; |
| 1305 | state->maxTrans *= 2; |
| 1306 | tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans * |
| 1307 | sizeof(xmlRegTrans)); |
| 1308 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1309 | xmlRegexpErrMemory(ctxt, "adding transition"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1310 | state->maxTrans /= 2; |
| 1311 | return; |
| 1312 | } |
| 1313 | state->trans = tmp; |
| 1314 | } |
| 1315 | #ifdef DEBUG_REGEXP_GRAPH |
| 1316 | printf("Add trans from %d to %d ", state->no, target->no); |
Daniel Veillard | 8a001f6 | 2002-04-20 07:24:11 +0000 | [diff] [blame] | 1317 | if (count == REGEXP_ALL_COUNTER) |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 1318 | printf("all transition\n"); |
Daniel Veillard | 4402ab4 | 2002-09-12 16:02:56 +0000 | [diff] [blame] | 1319 | else if (count >= 0) |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 1320 | printf("count based %d\n", count); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1321 | else if (counter >= 0) |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 1322 | printf("counted %d\n", counter); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1323 | else if (atom == NULL) |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 1324 | printf("epsilon transition\n"); |
| 1325 | else if (atom != NULL) |
| 1326 | xmlRegPrintAtom(stdout, atom); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1327 | #endif |
| 1328 | |
| 1329 | state->trans[state->nbTrans].atom = atom; |
| 1330 | state->trans[state->nbTrans].to = target->no; |
| 1331 | state->trans[state->nbTrans].counter = counter; |
| 1332 | state->trans[state->nbTrans].count = count; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 1333 | state->trans[state->nbTrans].nd = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1334 | state->nbTrans++; |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1335 | xmlRegStateAddTransTo(ctxt, target, state->no); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1338 | static int |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1339 | xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1340 | if (state == NULL) return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1341 | if (ctxt->maxStates == 0) { |
| 1342 | ctxt->maxStates = 4; |
| 1343 | ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates * |
| 1344 | sizeof(xmlRegStatePtr)); |
| 1345 | if (ctxt->states == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1346 | xmlRegexpErrMemory(ctxt, "adding state"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1347 | ctxt->maxStates = 0; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1348 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1349 | } |
| 1350 | } else if (ctxt->nbStates >= ctxt->maxStates) { |
| 1351 | xmlRegStatePtr *tmp; |
| 1352 | ctxt->maxStates *= 2; |
| 1353 | tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates * |
| 1354 | sizeof(xmlRegStatePtr)); |
| 1355 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1356 | xmlRegexpErrMemory(ctxt, "adding state"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1357 | ctxt->maxStates /= 2; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1358 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1359 | } |
| 1360 | ctxt->states = tmp; |
| 1361 | } |
| 1362 | state->no = ctxt->nbStates; |
| 1363 | ctxt->states[ctxt->nbStates++] = state; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1364 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | /** |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1368 | * xmlFAGenerateAllTransition: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1369 | * @ctxt: a regexp parser context |
| 1370 | * @from: the from state |
| 1371 | * @to: the target state or NULL for building a new one |
| 1372 | * @lax: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1373 | * |
| 1374 | */ |
| 1375 | static void |
| 1376 | xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt, |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1377 | xmlRegStatePtr from, xmlRegStatePtr to, |
| 1378 | int lax) { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1379 | if (to == NULL) { |
| 1380 | to = xmlRegNewState(ctxt); |
| 1381 | xmlRegStatePush(ctxt, to); |
| 1382 | ctxt->state = to; |
| 1383 | } |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1384 | if (lax) |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1385 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER); |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1386 | else |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1387 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1391 | * xmlFAGenerateEpsilonTransition: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1392 | * @ctxt: a regexp parser context |
| 1393 | * @from: the from state |
| 1394 | * @to: the target state or NULL for building a new one |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1395 | * |
| 1396 | */ |
| 1397 | static void |
| 1398 | xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt, |
| 1399 | xmlRegStatePtr from, xmlRegStatePtr to) { |
| 1400 | if (to == NULL) { |
| 1401 | to = xmlRegNewState(ctxt); |
| 1402 | xmlRegStatePush(ctxt, to); |
| 1403 | ctxt->state = to; |
| 1404 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1405 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | /** |
| 1409 | * xmlFAGenerateCountedEpsilonTransition: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1410 | * @ctxt: a regexp parser context |
| 1411 | * @from: the from state |
| 1412 | * @to: the target state or NULL for building a new one |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1413 | * counter: the counter for that transition |
| 1414 | * |
| 1415 | */ |
| 1416 | static void |
| 1417 | xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt, |
| 1418 | xmlRegStatePtr from, xmlRegStatePtr to, int counter) { |
| 1419 | if (to == NULL) { |
| 1420 | to = xmlRegNewState(ctxt); |
| 1421 | xmlRegStatePush(ctxt, to); |
| 1422 | ctxt->state = to; |
| 1423 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1424 | xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | /** |
| 1428 | * xmlFAGenerateCountedTransition: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1429 | * @ctxt: a regexp parser context |
| 1430 | * @from: the from state |
| 1431 | * @to: the target state or NULL for building a new one |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1432 | * counter: the counter for that transition |
| 1433 | * |
| 1434 | */ |
| 1435 | static void |
| 1436 | xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt, |
| 1437 | xmlRegStatePtr from, xmlRegStatePtr to, int counter) { |
| 1438 | if (to == NULL) { |
| 1439 | to = xmlRegNewState(ctxt); |
| 1440 | xmlRegStatePush(ctxt, to); |
| 1441 | ctxt->state = to; |
| 1442 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1443 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | /** |
| 1447 | * xmlFAGenerateTransitions: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1448 | * @ctxt: a regexp parser context |
| 1449 | * @from: the from state |
| 1450 | * @to: the target state or NULL for building a new one |
| 1451 | * @atom: the atom generating the transition |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1452 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1453 | * Returns 0 if success and -1 in case of error. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1454 | */ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1455 | static int |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1456 | xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from, |
| 1457 | xmlRegStatePtr to, xmlRegAtomPtr atom) { |
| 1458 | if (atom == NULL) { |
| 1459 | ERROR("genrate transition: atom == NULL"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1460 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1461 | } |
| 1462 | if (atom->type == XML_REGEXP_SUBREG) { |
| 1463 | /* |
| 1464 | * this is a subexpression handling one should not need to |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1465 | * create a new node except for XML_REGEXP_QUANT_RANGE. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1466 | */ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1467 | if (xmlRegAtomPush(ctxt, atom) < 0) { |
| 1468 | return(-1); |
| 1469 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1470 | if ((to != NULL) && (atom->stop != to) && |
| 1471 | (atom->quant != XML_REGEXP_QUANT_RANGE)) { |
| 1472 | /* |
| 1473 | * Generate an epsilon transition to link to the target |
| 1474 | */ |
| 1475 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to); |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 1476 | #ifdef DV |
| 1477 | } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) && |
| 1478 | (atom->quant != XML_REGEXP_QUANT_ONCE)) { |
| 1479 | to = xmlRegNewState(ctxt); |
| 1480 | xmlRegStatePush(ctxt, to); |
| 1481 | ctxt->state = to; |
| 1482 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to); |
| 1483 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1484 | } |
| 1485 | switch (atom->quant) { |
| 1486 | case XML_REGEXP_QUANT_OPT: |
| 1487 | atom->quant = XML_REGEXP_QUANT_ONCE; |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 1488 | /* |
| 1489 | * transition done to the state after end of atom. |
| 1490 | * 1. set transition from atom start to new state |
| 1491 | * 2. set transition from atom end to this state. |
| 1492 | */ |
| 1493 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0); |
| 1494 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, ctxt->state); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1495 | break; |
| 1496 | case XML_REGEXP_QUANT_MULT: |
| 1497 | atom->quant = XML_REGEXP_QUANT_ONCE; |
| 1498 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop); |
| 1499 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start); |
| 1500 | break; |
| 1501 | case XML_REGEXP_QUANT_PLUS: |
| 1502 | atom->quant = XML_REGEXP_QUANT_ONCE; |
| 1503 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start); |
| 1504 | break; |
| 1505 | case XML_REGEXP_QUANT_RANGE: { |
| 1506 | int counter; |
| 1507 | xmlRegStatePtr newstate; |
| 1508 | |
| 1509 | /* |
| 1510 | * This one is nasty: |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1511 | * 1/ if range has minOccurs == 0, create a new state |
| 1512 | * and create epsilon transitions from atom->start |
| 1513 | * to atom->stop, as well as atom->start to the new |
| 1514 | * state |
| 1515 | * 2/ register a new counter |
| 1516 | * 3/ register an epsilon transition associated to |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1517 | * this counter going from atom->stop to atom->start |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1518 | * 4/ create a new state |
| 1519 | * 5/ generate a counted transition from atom->stop to |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1520 | * that state |
| 1521 | */ |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1522 | if (atom->min == 0) { |
| 1523 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, |
| 1524 | atom->stop); |
| 1525 | newstate = xmlRegNewState(ctxt); |
| 1526 | xmlRegStatePush(ctxt, newstate); |
| 1527 | ctxt->state = newstate; |
| 1528 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, |
| 1529 | newstate); |
| 1530 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1531 | counter = xmlRegGetCounter(ctxt); |
| 1532 | ctxt->counters[counter].min = atom->min - 1; |
| 1533 | ctxt->counters[counter].max = atom->max - 1; |
| 1534 | atom->min = 0; |
| 1535 | atom->max = 0; |
| 1536 | atom->quant = XML_REGEXP_QUANT_ONCE; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1537 | if (to != NULL) { |
| 1538 | newstate = to; |
| 1539 | } else { |
| 1540 | newstate = xmlRegNewState(ctxt); |
| 1541 | xmlRegStatePush(ctxt, newstate); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1542 | } |
Daniel Veillard | 9a00fd2 | 2005-11-09 08:56:26 +0000 | [diff] [blame] | 1543 | ctxt->state = newstate; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1544 | xmlFAGenerateCountedTransition(ctxt, atom->stop, |
| 1545 | newstate, counter); |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 1546 | |
| 1547 | /* |
| 1548 | * first check count and if OK jump forward; |
| 1549 | * if checking fail increment count and jump back |
| 1550 | */ |
| 1551 | xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop, |
| 1552 | atom->start, counter); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1553 | } |
| 1554 | default: |
| 1555 | break; |
| 1556 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1557 | return(0); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 1558 | } |
| 1559 | if ((atom->min == 0) && (atom->max == 0) && |
Daniel Veillard | 99c394d | 2005-07-14 12:58:49 +0000 | [diff] [blame] | 1560 | (atom->quant == XML_REGEXP_QUANT_RANGE)) { |
| 1561 | /* |
| 1562 | * we can discard the atom and generate an epsilon transition instead |
| 1563 | */ |
| 1564 | if (to == NULL) { |
| 1565 | to = xmlRegNewState(ctxt); |
| 1566 | if (to != NULL) |
| 1567 | xmlRegStatePush(ctxt, to); |
| 1568 | else { |
| 1569 | return(-1); |
| 1570 | } |
| 1571 | } |
| 1572 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
| 1573 | ctxt->state = to; |
| 1574 | xmlRegFreeAtom(atom); |
| 1575 | return(0); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 1576 | } |
| 1577 | if (to == NULL) { |
| 1578 | to = xmlRegNewState(ctxt); |
| 1579 | if (to != NULL) |
| 1580 | xmlRegStatePush(ctxt, to); |
| 1581 | else { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1582 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1583 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1584 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 1585 | if (xmlRegAtomPush(ctxt, atom) < 0) { |
| 1586 | return(-1); |
| 1587 | } |
| 1588 | xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1); |
| 1589 | ctxt->state = to; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1590 | switch (atom->quant) { |
| 1591 | case XML_REGEXP_QUANT_OPT: |
| 1592 | atom->quant = XML_REGEXP_QUANT_ONCE; |
| 1593 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
| 1594 | break; |
| 1595 | case XML_REGEXP_QUANT_MULT: |
| 1596 | atom->quant = XML_REGEXP_QUANT_ONCE; |
| 1597 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1598 | xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1599 | break; |
| 1600 | case XML_REGEXP_QUANT_PLUS: |
| 1601 | atom->quant = XML_REGEXP_QUANT_ONCE; |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1602 | xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1603 | break; |
William M. Brack | 5657837 | 2007-04-11 14:33:46 +0000 | [diff] [blame] | 1604 | case XML_REGEXP_QUANT_RANGE: |
| 1605 | if (atom->min == 0) { |
| 1606 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
| 1607 | } |
| 1608 | break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1609 | default: |
| 1610 | break; |
| 1611 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1612 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /** |
| 1616 | * xmlFAReduceEpsilonTransitions: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1617 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1618 | * @fromnr: the from state |
| 1619 | * @tonr: the to state |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1620 | * @counter: should that transition be associated to a counted |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1621 | * |
| 1622 | */ |
| 1623 | static void |
| 1624 | xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr, |
| 1625 | int tonr, int counter) { |
| 1626 | int transnr; |
| 1627 | xmlRegStatePtr from; |
| 1628 | xmlRegStatePtr to; |
| 1629 | |
| 1630 | #ifdef DEBUG_REGEXP_GRAPH |
| 1631 | printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr); |
| 1632 | #endif |
| 1633 | from = ctxt->states[fromnr]; |
| 1634 | if (from == NULL) |
| 1635 | return; |
| 1636 | to = ctxt->states[tonr]; |
| 1637 | if (to == NULL) |
| 1638 | return; |
| 1639 | if ((to->mark == XML_REGEXP_MARK_START) || |
| 1640 | (to->mark == XML_REGEXP_MARK_VISITED)) |
| 1641 | return; |
| 1642 | |
| 1643 | to->mark = XML_REGEXP_MARK_VISITED; |
| 1644 | if (to->type == XML_REGEXP_FINAL_STATE) { |
| 1645 | #ifdef DEBUG_REGEXP_GRAPH |
| 1646 | printf("State %d is final, so %d becomes final\n", tonr, fromnr); |
| 1647 | #endif |
| 1648 | from->type = XML_REGEXP_FINAL_STATE; |
| 1649 | } |
| 1650 | for (transnr = 0;transnr < to->nbTrans;transnr++) { |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1651 | if (to->trans[transnr].to < 0) |
| 1652 | continue; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1653 | if (to->trans[transnr].atom == NULL) { |
| 1654 | /* |
| 1655 | * Don't remove counted transitions |
| 1656 | * Don't loop either |
| 1657 | */ |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1658 | if (to->trans[transnr].to != fromnr) { |
| 1659 | if (to->trans[transnr].count >= 0) { |
| 1660 | int newto = to->trans[transnr].to; |
| 1661 | |
| 1662 | xmlRegStateAddTrans(ctxt, from, NULL, |
| 1663 | ctxt->states[newto], |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1664 | -1, to->trans[transnr].count); |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1665 | } else { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1666 | #ifdef DEBUG_REGEXP_GRAPH |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1667 | printf("Found epsilon trans %d from %d to %d\n", |
| 1668 | transnr, tonr, to->trans[transnr].to); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1669 | #endif |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1670 | if (to->trans[transnr].counter >= 0) { |
| 1671 | xmlFAReduceEpsilonTransitions(ctxt, fromnr, |
| 1672 | to->trans[transnr].to, |
| 1673 | to->trans[transnr].counter); |
| 1674 | } else { |
| 1675 | xmlFAReduceEpsilonTransitions(ctxt, fromnr, |
| 1676 | to->trans[transnr].to, |
| 1677 | counter); |
| 1678 | } |
| 1679 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1680 | } |
| 1681 | } else { |
| 1682 | int newto = to->trans[transnr].to; |
| 1683 | |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1684 | if (to->trans[transnr].counter >= 0) { |
| 1685 | xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom, |
| 1686 | ctxt->states[newto], |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1687 | to->trans[transnr].counter, -1); |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1688 | } else { |
| 1689 | xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom, |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1690 | ctxt->states[newto], counter, -1); |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1691 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1692 | } |
| 1693 | } |
| 1694 | to->mark = XML_REGEXP_MARK_NORMAL; |
| 1695 | } |
| 1696 | |
| 1697 | /** |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1698 | * xmlFAEliminateSimpleEpsilonTransitions: |
| 1699 | * @ctxt: a regexp parser context |
| 1700 | * |
| 1701 | * Eliminating general epsilon transitions can get costly in the general |
| 1702 | * algorithm due to the large amount of generated new transitions and |
| 1703 | * associated comparisons. However for simple epsilon transition used just |
| 1704 | * to separate building blocks when generating the automata this can be |
| 1705 | * reduced to state elimination: |
| 1706 | * - if there exists an epsilon from X to Y |
| 1707 | * - if there is no other transition from X |
| 1708 | * then X and Y are semantically equivalent and X can be eliminated |
| 1709 | * If X is the start state then make Y the start state, else replace the |
| 1710 | * target of all transitions to X by transitions to Y. |
| 1711 | */ |
| 1712 | static void |
| 1713 | xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) { |
| 1714 | int statenr, i, j, newto; |
| 1715 | xmlRegStatePtr state, tmp; |
| 1716 | |
| 1717 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1718 | state = ctxt->states[statenr]; |
| 1719 | if (state == NULL) |
| 1720 | continue; |
| 1721 | if (state->nbTrans != 1) |
| 1722 | continue; |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 1723 | if (state->type == XML_REGEXP_UNREACH_STATE) |
| 1724 | continue; |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1725 | /* is the only transition out a basic transition */ |
| 1726 | if ((state->trans[0].atom == NULL) && |
| 1727 | (state->trans[0].to >= 0) && |
| 1728 | (state->trans[0].to != statenr) && |
| 1729 | (state->trans[0].counter < 0) && |
| 1730 | (state->trans[0].count < 0)) { |
| 1731 | newto = state->trans[0].to; |
| 1732 | |
| 1733 | if (state->type == XML_REGEXP_START_STATE) { |
| 1734 | #ifdef DEBUG_REGEXP_GRAPH |
| 1735 | printf("Found simple epsilon trans from start %d to %d\n", |
| 1736 | statenr, newto); |
| 1737 | #endif |
| 1738 | } else { |
| 1739 | #ifdef DEBUG_REGEXP_GRAPH |
| 1740 | printf("Found simple epsilon trans from %d to %d\n", |
| 1741 | statenr, newto); |
| 1742 | #endif |
| 1743 | for (i = 0;i < state->nbTransTo;i++) { |
| 1744 | tmp = ctxt->states[state->transTo[i]]; |
| 1745 | for (j = 0;j < tmp->nbTrans;j++) { |
| 1746 | if (tmp->trans[j].to == statenr) { |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1747 | #ifdef DEBUG_REGEXP_GRAPH |
| 1748 | printf("Changed transition %d on %d to go to %d\n", |
| 1749 | j, tmp->no, newto); |
| 1750 | #endif |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 1751 | tmp->trans[j].to = -1; |
| 1752 | xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom, |
| 1753 | ctxt->states[newto], |
| 1754 | tmp->trans[j].counter, |
| 1755 | tmp->trans[j].count); |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1756 | } |
| 1757 | } |
| 1758 | } |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1759 | if (state->type == XML_REGEXP_FINAL_STATE) |
| 1760 | ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE; |
| 1761 | /* eliminate the transition completely */ |
| 1762 | state->nbTrans = 0; |
| 1763 | |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 1764 | state->type = XML_REGEXP_UNREACH_STATE; |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1765 | |
| 1766 | } |
| 1767 | |
| 1768 | } |
| 1769 | } |
| 1770 | } |
| 1771 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1772 | * xmlFAEliminateEpsilonTransitions: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1773 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1774 | * |
| 1775 | */ |
| 1776 | static void |
| 1777 | xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) { |
| 1778 | int statenr, transnr; |
| 1779 | xmlRegStatePtr state; |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1780 | int has_epsilon; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1781 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1782 | if (ctxt->states == NULL) return; |
| 1783 | |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 1784 | /* |
| 1785 | * Eliminate simple epsilon transition and the associated unreachable |
| 1786 | * states. |
| 1787 | */ |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1788 | xmlFAEliminateSimpleEpsilonTransitions(ctxt); |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 1789 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1790 | state = ctxt->states[statenr]; |
| 1791 | if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) { |
| 1792 | #ifdef DEBUG_REGEXP_GRAPH |
| 1793 | printf("Removed unreachable state %d\n", statenr); |
| 1794 | #endif |
| 1795 | xmlRegFreeState(state); |
| 1796 | ctxt->states[statenr] = NULL; |
| 1797 | } |
| 1798 | } |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1799 | |
| 1800 | has_epsilon = 0; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1801 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1802 | /* |
Daniel Veillard | fcd18ff | 2006-11-02 10:28:04 +0000 | [diff] [blame] | 1803 | * Build the completed transitions bypassing the epsilons |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1804 | * Use a marking algorithm to avoid loops |
Daniel Veillard | fcd18ff | 2006-11-02 10:28:04 +0000 | [diff] [blame] | 1805 | * Mark sink states too. |
| 1806 | * Process from the latests states backward to the start when |
| 1807 | * there is long cascading epsilon chains this minimize the |
| 1808 | * recursions and transition compares when adding the new ones |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1809 | */ |
Daniel Veillard | fcd18ff | 2006-11-02 10:28:04 +0000 | [diff] [blame] | 1810 | for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1811 | state = ctxt->states[statenr]; |
| 1812 | if (state == NULL) |
| 1813 | continue; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 1814 | if ((state->nbTrans == 0) && |
| 1815 | (state->type != XML_REGEXP_FINAL_STATE)) { |
| 1816 | state->type = XML_REGEXP_SINK_STATE; |
| 1817 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1818 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 1819 | if ((state->trans[transnr].atom == NULL) && |
| 1820 | (state->trans[transnr].to >= 0)) { |
| 1821 | if (state->trans[transnr].to == statenr) { |
| 1822 | state->trans[transnr].to = -1; |
| 1823 | #ifdef DEBUG_REGEXP_GRAPH |
| 1824 | printf("Removed loopback epsilon trans %d on %d\n", |
| 1825 | transnr, statenr); |
| 1826 | #endif |
| 1827 | } else if (state->trans[transnr].count < 0) { |
| 1828 | int newto = state->trans[transnr].to; |
| 1829 | |
| 1830 | #ifdef DEBUG_REGEXP_GRAPH |
| 1831 | printf("Found epsilon trans %d from %d to %d\n", |
| 1832 | transnr, statenr, newto); |
| 1833 | #endif |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1834 | has_epsilon = 1; |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 1835 | state->trans[transnr].to = -2; |
| 1836 | state->mark = XML_REGEXP_MARK_START; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1837 | xmlFAReduceEpsilonTransitions(ctxt, statenr, |
| 1838 | newto, state->trans[transnr].counter); |
| 1839 | state->mark = XML_REGEXP_MARK_NORMAL; |
| 1840 | #ifdef DEBUG_REGEXP_GRAPH |
| 1841 | } else { |
| 1842 | printf("Found counted transition %d on %d\n", |
| 1843 | transnr, statenr); |
| 1844 | #endif |
| 1845 | } |
| 1846 | } |
| 1847 | } |
| 1848 | } |
| 1849 | /* |
| 1850 | * Eliminate the epsilon transitions |
| 1851 | */ |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1852 | if (has_epsilon) { |
| 1853 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1854 | state = ctxt->states[statenr]; |
| 1855 | if (state == NULL) |
| 1856 | continue; |
| 1857 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 1858 | xmlRegTransPtr trans = &(state->trans[transnr]); |
| 1859 | if ((trans->atom == NULL) && |
| 1860 | (trans->count < 0) && |
| 1861 | (trans->to >= 0)) { |
| 1862 | trans->to = -1; |
| 1863 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1864 | } |
| 1865 | } |
| 1866 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1867 | |
| 1868 | /* |
| 1869 | * Use this pass to detect unreachable states too |
| 1870 | */ |
| 1871 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1872 | state = ctxt->states[statenr]; |
| 1873 | if (state != NULL) |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1874 | state->reached = XML_REGEXP_MARK_NORMAL; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1875 | } |
| 1876 | state = ctxt->states[0]; |
| 1877 | if (state != NULL) |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1878 | state->reached = XML_REGEXP_MARK_START; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1879 | while (state != NULL) { |
| 1880 | xmlRegStatePtr target = NULL; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1881 | state->reached = XML_REGEXP_MARK_VISITED; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1882 | /* |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1883 | * Mark all states reachable from the current reachable state |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1884 | */ |
| 1885 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 1886 | if ((state->trans[transnr].to >= 0) && |
| 1887 | ((state->trans[transnr].atom != NULL) || |
| 1888 | (state->trans[transnr].count >= 0))) { |
| 1889 | int newto = state->trans[transnr].to; |
| 1890 | |
| 1891 | if (ctxt->states[newto] == NULL) |
| 1892 | continue; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1893 | if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) { |
| 1894 | ctxt->states[newto]->reached = XML_REGEXP_MARK_START; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1895 | target = ctxt->states[newto]; |
| 1896 | } |
| 1897 | } |
| 1898 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 1899 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1900 | /* |
| 1901 | * find the next accessible state not explored |
| 1902 | */ |
| 1903 | if (target == NULL) { |
| 1904 | for (statenr = 1;statenr < ctxt->nbStates;statenr++) { |
| 1905 | state = ctxt->states[statenr]; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1906 | if ((state != NULL) && (state->reached == |
| 1907 | XML_REGEXP_MARK_START)) { |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1908 | target = state; |
| 1909 | break; |
| 1910 | } |
| 1911 | } |
| 1912 | } |
| 1913 | state = target; |
| 1914 | } |
| 1915 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1916 | state = ctxt->states[statenr]; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1917 | if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) { |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1918 | #ifdef DEBUG_REGEXP_GRAPH |
| 1919 | printf("Removed unreachable state %d\n", statenr); |
| 1920 | #endif |
| 1921 | xmlRegFreeState(state); |
| 1922 | ctxt->states[statenr] = NULL; |
| 1923 | } |
| 1924 | } |
| 1925 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1926 | } |
| 1927 | |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 1928 | static int |
| 1929 | xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) { |
| 1930 | int ret = 0; |
| 1931 | |
| 1932 | if ((range1->type == XML_REGEXP_RANGES) || |
| 1933 | (range2->type == XML_REGEXP_RANGES) || |
| 1934 | (range2->type == XML_REGEXP_SUBREG) || |
| 1935 | (range1->type == XML_REGEXP_SUBREG) || |
| 1936 | (range1->type == XML_REGEXP_STRING) || |
| 1937 | (range2->type == XML_REGEXP_STRING)) |
| 1938 | return(-1); |
| 1939 | |
| 1940 | /* put them in order */ |
| 1941 | if (range1->type > range2->type) { |
| 1942 | xmlRegRangePtr tmp; |
| 1943 | |
| 1944 | tmp = range1; |
| 1945 | range1 = range2; |
| 1946 | range2 = tmp; |
| 1947 | } |
| 1948 | if ((range1->type == XML_REGEXP_ANYCHAR) || |
| 1949 | (range2->type == XML_REGEXP_ANYCHAR)) { |
| 1950 | ret = 1; |
| 1951 | } else if ((range1->type == XML_REGEXP_EPSILON) || |
| 1952 | (range2->type == XML_REGEXP_EPSILON)) { |
| 1953 | return(0); |
| 1954 | } else if (range1->type == range2->type) { |
| 1955 | if ((range1->type != XML_REGEXP_CHARVAL) || |
| 1956 | (range1->end < range2->start) || |
| 1957 | (range2->end < range1->start)) |
| 1958 | ret = 1; |
| 1959 | else |
| 1960 | ret = 0; |
| 1961 | } else if (range1->type == XML_REGEXP_CHARVAL) { |
| 1962 | int codepoint; |
| 1963 | int neg = 0; |
| 1964 | |
| 1965 | /* |
| 1966 | * just check all codepoints in the range for acceptance, |
| 1967 | * this is usually way cheaper since done only once at |
| 1968 | * compilation than testing over and over at runtime or |
| 1969 | * pushing too many states when evaluating. |
| 1970 | */ |
| 1971 | if (((range1->neg == 0) && (range2->neg != 0)) || |
| 1972 | ((range1->neg != 0) && (range2->neg == 0))) |
| 1973 | neg = 1; |
| 1974 | |
| 1975 | for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) { |
| 1976 | ret = xmlRegCheckCharacterRange(range2->type, codepoint, |
| 1977 | 0, range2->start, range2->end, |
| 1978 | range2->blockName); |
| 1979 | if (ret < 0) |
| 1980 | return(-1); |
| 1981 | if (((neg == 1) && (ret == 0)) || |
| 1982 | ((neg == 0) && (ret == 1))) |
| 1983 | return(1); |
| 1984 | } |
| 1985 | return(0); |
| 1986 | } else if ((range1->type == XML_REGEXP_BLOCK_NAME) || |
| 1987 | (range2->type == XML_REGEXP_BLOCK_NAME)) { |
| 1988 | if (range1->type == range2->type) { |
| 1989 | ret = xmlStrEqual(range1->blockName, range2->blockName); |
| 1990 | } else { |
| 1991 | /* |
| 1992 | * comparing a block range with anything else is way |
| 1993 | * too costly, and maintining the table is like too much |
| 1994 | * memory too, so let's force the automata to save state |
| 1995 | * here. |
| 1996 | */ |
| 1997 | return(1); |
| 1998 | } |
| 1999 | } else if ((range1->type < XML_REGEXP_LETTER) || |
| 2000 | (range2->type < XML_REGEXP_LETTER)) { |
| 2001 | if ((range1->type == XML_REGEXP_ANYSPACE) && |
| 2002 | (range2->type == XML_REGEXP_NOTSPACE)) |
| 2003 | ret = 0; |
| 2004 | else if ((range1->type == XML_REGEXP_INITNAME) && |
| 2005 | (range2->type == XML_REGEXP_NOTINITNAME)) |
| 2006 | ret = 0; |
| 2007 | else if ((range1->type == XML_REGEXP_NAMECHAR) && |
| 2008 | (range2->type == XML_REGEXP_NOTNAMECHAR)) |
| 2009 | ret = 0; |
| 2010 | else if ((range1->type == XML_REGEXP_DECIMAL) && |
| 2011 | (range2->type == XML_REGEXP_NOTDECIMAL)) |
| 2012 | ret = 0; |
| 2013 | else if ((range1->type == XML_REGEXP_REALCHAR) && |
| 2014 | (range2->type == XML_REGEXP_NOTREALCHAR)) |
| 2015 | ret = 0; |
| 2016 | else { |
| 2017 | /* same thing to limit complexity */ |
| 2018 | return(1); |
| 2019 | } |
| 2020 | } else { |
| 2021 | ret = 0; |
| 2022 | /* range1->type < range2->type here */ |
| 2023 | switch (range1->type) { |
| 2024 | case XML_REGEXP_LETTER: |
| 2025 | /* all disjoint except in the subgroups */ |
| 2026 | if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) || |
| 2027 | (range2->type == XML_REGEXP_LETTER_LOWERCASE) || |
| 2028 | (range2->type == XML_REGEXP_LETTER_TITLECASE) || |
| 2029 | (range2->type == XML_REGEXP_LETTER_MODIFIER) || |
| 2030 | (range2->type == XML_REGEXP_LETTER_OTHERS)) |
| 2031 | ret = 1; |
| 2032 | break; |
| 2033 | case XML_REGEXP_MARK: |
| 2034 | if ((range2->type == XML_REGEXP_MARK_NONSPACING) || |
| 2035 | (range2->type == XML_REGEXP_MARK_SPACECOMBINING) || |
| 2036 | (range2->type == XML_REGEXP_MARK_ENCLOSING)) |
| 2037 | ret = 1; |
| 2038 | break; |
| 2039 | case XML_REGEXP_NUMBER: |
| 2040 | if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) || |
| 2041 | (range2->type == XML_REGEXP_NUMBER_LETTER) || |
| 2042 | (range2->type == XML_REGEXP_NUMBER_OTHERS)) |
| 2043 | ret = 1; |
| 2044 | break; |
| 2045 | case XML_REGEXP_PUNCT: |
| 2046 | if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) || |
| 2047 | (range2->type == XML_REGEXP_PUNCT_DASH) || |
| 2048 | (range2->type == XML_REGEXP_PUNCT_OPEN) || |
| 2049 | (range2->type == XML_REGEXP_PUNCT_CLOSE) || |
| 2050 | (range2->type == XML_REGEXP_PUNCT_INITQUOTE) || |
| 2051 | (range2->type == XML_REGEXP_PUNCT_FINQUOTE) || |
| 2052 | (range2->type == XML_REGEXP_PUNCT_OTHERS)) |
| 2053 | ret = 1; |
| 2054 | break; |
| 2055 | case XML_REGEXP_SEPAR: |
| 2056 | if ((range2->type == XML_REGEXP_SEPAR_SPACE) || |
| 2057 | (range2->type == XML_REGEXP_SEPAR_LINE) || |
| 2058 | (range2->type == XML_REGEXP_SEPAR_PARA)) |
| 2059 | ret = 1; |
| 2060 | break; |
| 2061 | case XML_REGEXP_SYMBOL: |
| 2062 | if ((range2->type == XML_REGEXP_SYMBOL_MATH) || |
| 2063 | (range2->type == XML_REGEXP_SYMBOL_CURRENCY) || |
| 2064 | (range2->type == XML_REGEXP_SYMBOL_MODIFIER) || |
| 2065 | (range2->type == XML_REGEXP_SYMBOL_OTHERS)) |
| 2066 | ret = 1; |
| 2067 | break; |
| 2068 | case XML_REGEXP_OTHER: |
| 2069 | if ((range2->type == XML_REGEXP_OTHER_CONTROL) || |
| 2070 | (range2->type == XML_REGEXP_OTHER_FORMAT) || |
| 2071 | (range2->type == XML_REGEXP_OTHER_PRIVATE)) |
| 2072 | ret = 1; |
| 2073 | break; |
| 2074 | default: |
| 2075 | if ((range2->type >= XML_REGEXP_LETTER) && |
| 2076 | (range2->type < XML_REGEXP_BLOCK_NAME)) |
| 2077 | ret = 0; |
| 2078 | else { |
| 2079 | /* safety net ! */ |
| 2080 | return(1); |
| 2081 | } |
| 2082 | } |
| 2083 | } |
| 2084 | if (((range1->neg == 0) && (range2->neg != 0)) || |
| 2085 | ((range1->neg != 0) && (range2->neg == 0))) |
| 2086 | ret = !ret; |
| 2087 | return(1); |
| 2088 | } |
| 2089 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2090 | /** |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2091 | * xmlFACompareAtomTypes: |
| 2092 | * @type1: an atom type |
| 2093 | * @type2: an atom type |
| 2094 | * |
| 2095 | * Compares two atoms type to check whether they intersect in some ways, |
| 2096 | * this is used by xmlFACompareAtoms only |
| 2097 | * |
| 2098 | * Returns 1 if they may intersect and 0 otherwise |
| 2099 | */ |
| 2100 | static int |
| 2101 | xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) { |
| 2102 | if ((type1 == XML_REGEXP_EPSILON) || |
| 2103 | (type1 == XML_REGEXP_CHARVAL) || |
| 2104 | (type1 == XML_REGEXP_RANGES) || |
| 2105 | (type1 == XML_REGEXP_SUBREG) || |
| 2106 | (type1 == XML_REGEXP_STRING) || |
| 2107 | (type1 == XML_REGEXP_ANYCHAR)) |
| 2108 | return(1); |
| 2109 | if ((type2 == XML_REGEXP_EPSILON) || |
| 2110 | (type2 == XML_REGEXP_CHARVAL) || |
| 2111 | (type2 == XML_REGEXP_RANGES) || |
| 2112 | (type2 == XML_REGEXP_SUBREG) || |
| 2113 | (type2 == XML_REGEXP_STRING) || |
| 2114 | (type2 == XML_REGEXP_ANYCHAR)) |
| 2115 | return(1); |
| 2116 | |
| 2117 | if (type1 == type2) return(1); |
| 2118 | |
| 2119 | /* simplify subsequent compares by making sure type1 < type2 */ |
| 2120 | if (type1 > type2) { |
| 2121 | xmlRegAtomType tmp = type1; |
| 2122 | type1 = type2; |
| 2123 | type2 = tmp; |
| 2124 | } |
| 2125 | switch (type1) { |
| 2126 | case XML_REGEXP_ANYSPACE: /* \s */ |
| 2127 | /* can't be a letter, number, mark, pontuation, symbol */ |
| 2128 | if ((type2 == XML_REGEXP_NOTSPACE) || |
| 2129 | ((type2 >= XML_REGEXP_LETTER) && |
| 2130 | (type2 <= XML_REGEXP_LETTER_OTHERS)) || |
| 2131 | ((type2 >= XML_REGEXP_NUMBER) && |
| 2132 | (type2 <= XML_REGEXP_NUMBER_OTHERS)) || |
| 2133 | ((type2 >= XML_REGEXP_MARK) && |
| 2134 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
| 2135 | ((type2 >= XML_REGEXP_PUNCT) && |
| 2136 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
| 2137 | ((type2 >= XML_REGEXP_SYMBOL) && |
| 2138 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) |
| 2139 | ) return(0); |
| 2140 | break; |
| 2141 | case XML_REGEXP_NOTSPACE: /* \S */ |
| 2142 | break; |
| 2143 | case XML_REGEXP_INITNAME: /* \l */ |
| 2144 | /* can't be a number, mark, separator, pontuation, symbol or other */ |
| 2145 | if ((type2 == XML_REGEXP_NOTINITNAME) || |
| 2146 | ((type2 >= XML_REGEXP_NUMBER) && |
| 2147 | (type2 <= XML_REGEXP_NUMBER_OTHERS)) || |
| 2148 | ((type2 >= XML_REGEXP_MARK) && |
| 2149 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
| 2150 | ((type2 >= XML_REGEXP_SEPAR) && |
| 2151 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
| 2152 | ((type2 >= XML_REGEXP_PUNCT) && |
| 2153 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
| 2154 | ((type2 >= XML_REGEXP_SYMBOL) && |
| 2155 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
| 2156 | ((type2 >= XML_REGEXP_OTHER) && |
| 2157 | (type2 <= XML_REGEXP_OTHER_NA)) |
| 2158 | ) return(0); |
| 2159 | break; |
| 2160 | case XML_REGEXP_NOTINITNAME: /* \L */ |
| 2161 | break; |
| 2162 | case XML_REGEXP_NAMECHAR: /* \c */ |
| 2163 | /* can't be a mark, separator, pontuation, symbol or other */ |
| 2164 | if ((type2 == XML_REGEXP_NOTNAMECHAR) || |
| 2165 | ((type2 >= XML_REGEXP_MARK) && |
| 2166 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
| 2167 | ((type2 >= XML_REGEXP_PUNCT) && |
| 2168 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
| 2169 | ((type2 >= XML_REGEXP_SEPAR) && |
| 2170 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
| 2171 | ((type2 >= XML_REGEXP_SYMBOL) && |
| 2172 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
| 2173 | ((type2 >= XML_REGEXP_OTHER) && |
| 2174 | (type2 <= XML_REGEXP_OTHER_NA)) |
| 2175 | ) return(0); |
| 2176 | break; |
| 2177 | case XML_REGEXP_NOTNAMECHAR: /* \C */ |
| 2178 | break; |
| 2179 | case XML_REGEXP_DECIMAL: /* \d */ |
| 2180 | /* can't be a letter, mark, separator, pontuation, symbol or other */ |
| 2181 | if ((type2 == XML_REGEXP_NOTDECIMAL) || |
| 2182 | (type2 == XML_REGEXP_REALCHAR) || |
| 2183 | ((type2 >= XML_REGEXP_LETTER) && |
| 2184 | (type2 <= XML_REGEXP_LETTER_OTHERS)) || |
| 2185 | ((type2 >= XML_REGEXP_MARK) && |
| 2186 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
| 2187 | ((type2 >= XML_REGEXP_PUNCT) && |
| 2188 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
| 2189 | ((type2 >= XML_REGEXP_SEPAR) && |
| 2190 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
| 2191 | ((type2 >= XML_REGEXP_SYMBOL) && |
| 2192 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
| 2193 | ((type2 >= XML_REGEXP_OTHER) && |
| 2194 | (type2 <= XML_REGEXP_OTHER_NA)) |
| 2195 | )return(0); |
| 2196 | break; |
| 2197 | case XML_REGEXP_NOTDECIMAL: /* \D */ |
| 2198 | break; |
| 2199 | case XML_REGEXP_REALCHAR: /* \w */ |
| 2200 | /* can't be a mark, separator, pontuation, symbol or other */ |
| 2201 | if ((type2 == XML_REGEXP_NOTDECIMAL) || |
| 2202 | ((type2 >= XML_REGEXP_MARK) && |
| 2203 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
| 2204 | ((type2 >= XML_REGEXP_PUNCT) && |
| 2205 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
| 2206 | ((type2 >= XML_REGEXP_SEPAR) && |
| 2207 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
| 2208 | ((type2 >= XML_REGEXP_SYMBOL) && |
| 2209 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
| 2210 | ((type2 >= XML_REGEXP_OTHER) && |
| 2211 | (type2 <= XML_REGEXP_OTHER_NA)) |
| 2212 | )return(0); |
| 2213 | break; |
| 2214 | case XML_REGEXP_NOTREALCHAR: /* \W */ |
| 2215 | break; |
| 2216 | /* |
| 2217 | * at that point we know both type 1 and type2 are from |
| 2218 | * character categories are ordered and are different, |
| 2219 | * it becomes simple because this is a partition |
| 2220 | */ |
| 2221 | case XML_REGEXP_LETTER: |
| 2222 | if (type2 <= XML_REGEXP_LETTER_OTHERS) |
| 2223 | return(1); |
| 2224 | return(0); |
| 2225 | case XML_REGEXP_LETTER_UPPERCASE: |
| 2226 | case XML_REGEXP_LETTER_LOWERCASE: |
| 2227 | case XML_REGEXP_LETTER_TITLECASE: |
| 2228 | case XML_REGEXP_LETTER_MODIFIER: |
| 2229 | case XML_REGEXP_LETTER_OTHERS: |
| 2230 | return(0); |
| 2231 | case XML_REGEXP_MARK: |
| 2232 | if (type2 <= XML_REGEXP_MARK_ENCLOSING) |
| 2233 | return(1); |
| 2234 | return(0); |
| 2235 | case XML_REGEXP_MARK_NONSPACING: |
| 2236 | case XML_REGEXP_MARK_SPACECOMBINING: |
| 2237 | case XML_REGEXP_MARK_ENCLOSING: |
| 2238 | return(0); |
| 2239 | case XML_REGEXP_NUMBER: |
| 2240 | if (type2 <= XML_REGEXP_NUMBER_OTHERS) |
| 2241 | return(1); |
| 2242 | return(0); |
| 2243 | case XML_REGEXP_NUMBER_DECIMAL: |
| 2244 | case XML_REGEXP_NUMBER_LETTER: |
| 2245 | case XML_REGEXP_NUMBER_OTHERS: |
| 2246 | return(0); |
| 2247 | case XML_REGEXP_PUNCT: |
| 2248 | if (type2 <= XML_REGEXP_PUNCT_OTHERS) |
| 2249 | return(1); |
| 2250 | return(0); |
| 2251 | case XML_REGEXP_PUNCT_CONNECTOR: |
| 2252 | case XML_REGEXP_PUNCT_DASH: |
| 2253 | case XML_REGEXP_PUNCT_OPEN: |
| 2254 | case XML_REGEXP_PUNCT_CLOSE: |
| 2255 | case XML_REGEXP_PUNCT_INITQUOTE: |
| 2256 | case XML_REGEXP_PUNCT_FINQUOTE: |
| 2257 | case XML_REGEXP_PUNCT_OTHERS: |
| 2258 | return(0); |
| 2259 | case XML_REGEXP_SEPAR: |
| 2260 | if (type2 <= XML_REGEXP_SEPAR_PARA) |
| 2261 | return(1); |
| 2262 | return(0); |
| 2263 | case XML_REGEXP_SEPAR_SPACE: |
| 2264 | case XML_REGEXP_SEPAR_LINE: |
| 2265 | case XML_REGEXP_SEPAR_PARA: |
| 2266 | return(0); |
| 2267 | case XML_REGEXP_SYMBOL: |
| 2268 | if (type2 <= XML_REGEXP_SYMBOL_OTHERS) |
| 2269 | return(1); |
| 2270 | return(0); |
| 2271 | case XML_REGEXP_SYMBOL_MATH: |
| 2272 | case XML_REGEXP_SYMBOL_CURRENCY: |
| 2273 | case XML_REGEXP_SYMBOL_MODIFIER: |
| 2274 | case XML_REGEXP_SYMBOL_OTHERS: |
| 2275 | return(0); |
| 2276 | case XML_REGEXP_OTHER: |
| 2277 | if (type2 <= XML_REGEXP_OTHER_NA) |
| 2278 | return(1); |
| 2279 | return(0); |
| 2280 | case XML_REGEXP_OTHER_CONTROL: |
| 2281 | case XML_REGEXP_OTHER_FORMAT: |
| 2282 | case XML_REGEXP_OTHER_PRIVATE: |
| 2283 | case XML_REGEXP_OTHER_NA: |
| 2284 | return(0); |
| 2285 | default: |
| 2286 | break; |
| 2287 | } |
| 2288 | return(1); |
| 2289 | } |
| 2290 | |
| 2291 | /** |
| 2292 | * xmlFAEqualAtoms: |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2293 | * @atom1: an atom |
| 2294 | * @atom2: an atom |
| 2295 | * |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2296 | * Compares two atoms to check whether they are the same exactly |
| 2297 | * this is used to remove equivalent transitions |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2298 | * |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2299 | * Returns 1 if same and 0 otherwise |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2300 | */ |
| 2301 | static int |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2302 | xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) { |
| 2303 | int ret = 0; |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 2304 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2305 | if (atom1 == atom2) |
| 2306 | return(1); |
| 2307 | if ((atom1 == NULL) || (atom2 == NULL)) |
| 2308 | return(0); |
| 2309 | |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2310 | if (atom1->type != atom2->type) |
| 2311 | return(0); |
| 2312 | switch (atom1->type) { |
| 2313 | case XML_REGEXP_EPSILON: |
| 2314 | ret = 0; |
| 2315 | break; |
| 2316 | case XML_REGEXP_STRING: |
| 2317 | ret = xmlStrEqual((xmlChar *)atom1->valuep, |
| 2318 | (xmlChar *)atom2->valuep); |
| 2319 | break; |
| 2320 | case XML_REGEXP_CHARVAL: |
| 2321 | ret = (atom1->codepoint == atom2->codepoint); |
| 2322 | break; |
| 2323 | case XML_REGEXP_RANGES: |
| 2324 | /* too hard to do in the general case */ |
| 2325 | ret = 0; |
| 2326 | default: |
| 2327 | break; |
| 2328 | } |
| 2329 | return(ret); |
| 2330 | } |
| 2331 | |
| 2332 | /** |
| 2333 | * xmlFACompareAtoms: |
| 2334 | * @atom1: an atom |
| 2335 | * @atom2: an atom |
| 2336 | * |
| 2337 | * Compares two atoms to check whether they intersect in some ways, |
| 2338 | * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only |
| 2339 | * |
| 2340 | * Returns 1 if yes and 0 otherwise |
| 2341 | */ |
| 2342 | static int |
| 2343 | xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) { |
| 2344 | int ret = 1; |
| 2345 | |
| 2346 | if (atom1 == atom2) |
| 2347 | return(1); |
| 2348 | if ((atom1 == NULL) || (atom2 == NULL)) |
| 2349 | return(0); |
| 2350 | |
| 2351 | if ((atom1->type == XML_REGEXP_ANYCHAR) || |
| 2352 | (atom2->type == XML_REGEXP_ANYCHAR)) |
| 2353 | return(1); |
| 2354 | |
| 2355 | if (atom1->type > atom2->type) { |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2356 | xmlRegAtomPtr tmp; |
| 2357 | tmp = atom1; |
| 2358 | atom1 = atom2; |
| 2359 | atom2 = tmp; |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2360 | } |
| 2361 | if (atom1->type != atom2->type) { |
| 2362 | ret = xmlFACompareAtomTypes(atom1->type, atom2->type); |
| 2363 | /* if they can't intersect at the type level break now */ |
| 2364 | if (ret == 0) |
| 2365 | return(0); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2366 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2367 | switch (atom1->type) { |
| 2368 | case XML_REGEXP_STRING: |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 2369 | ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep, |
| 2370 | (xmlChar *)atom2->valuep); |
| 2371 | break; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2372 | case XML_REGEXP_EPSILON: |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2373 | goto not_determinist; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2374 | case XML_REGEXP_CHARVAL: |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2375 | if (atom2->type == XML_REGEXP_CHARVAL) { |
| 2376 | ret = (atom1->codepoint == atom2->codepoint); |
| 2377 | } else { |
| 2378 | ret = xmlRegCheckCharacter(atom2, atom1->codepoint); |
| 2379 | if (ret < 0) |
| 2380 | ret = 1; |
| 2381 | } |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 2382 | break; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2383 | case XML_REGEXP_RANGES: |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2384 | if (atom2->type == XML_REGEXP_RANGES) { |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2385 | int i, j, res; |
| 2386 | xmlRegRangePtr r1, r2; |
| 2387 | |
| 2388 | /* |
| 2389 | * need to check that none of the ranges eventually matches |
| 2390 | */ |
| 2391 | for (i = 0;i < atom1->nbRanges;i++) { |
| 2392 | for (j = 0;j < atom2->nbRanges;j++) { |
| 2393 | r1 = atom1->ranges[i]; |
| 2394 | r2 = atom2->ranges[j]; |
| 2395 | res = xmlFACompareRanges(r1, r2); |
| 2396 | if (res == 1) { |
| 2397 | ret = 1; |
| 2398 | goto done; |
| 2399 | } |
| 2400 | } |
| 2401 | } |
| 2402 | ret = 0; |
| 2403 | } |
| 2404 | break; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2405 | default: |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2406 | goto not_determinist; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2407 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2408 | done: |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 2409 | if (atom1->neg != atom2->neg) { |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 2410 | ret = !ret; |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 2411 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2412 | if (ret == 0) |
| 2413 | return(0); |
| 2414 | not_determinist: |
| 2415 | return(1); |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | /** |
| 2419 | * xmlFARecurseDeterminism: |
| 2420 | * @ctxt: a regexp parser context |
| 2421 | * |
| 2422 | * Check whether the associated regexp is determinist, |
| 2423 | * should be called after xmlFAEliminateEpsilonTransitions() |
| 2424 | * |
| 2425 | */ |
| 2426 | static int |
| 2427 | xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state, |
| 2428 | int to, xmlRegAtomPtr atom) { |
| 2429 | int ret = 1; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2430 | int res; |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 2431 | int transnr, nbTrans; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2432 | xmlRegTransPtr t1; |
| 2433 | |
| 2434 | if (state == NULL) |
| 2435 | return(ret); |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 2436 | /* |
| 2437 | * don't recurse on transitions potentially added in the course of |
| 2438 | * the elimination. |
| 2439 | */ |
| 2440 | nbTrans = state->nbTrans; |
| 2441 | for (transnr = 0;transnr < nbTrans;transnr++) { |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2442 | t1 = &(state->trans[transnr]); |
| 2443 | /* |
| 2444 | * check transitions conflicting with the one looked at |
| 2445 | */ |
| 2446 | if (t1->atom == NULL) { |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 2447 | if (t1->to < 0) |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2448 | continue; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2449 | res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to], |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2450 | to, atom); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2451 | if (res == 0) { |
| 2452 | ret = 0; |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 2453 | /* t1->nd = 1; */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2454 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2455 | continue; |
| 2456 | } |
| 2457 | if (t1->to != to) |
| 2458 | continue; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2459 | if (xmlFACompareAtoms(t1->atom, atom)) { |
| 2460 | ret = 0; |
| 2461 | /* mark the transition as non-deterministic */ |
| 2462 | t1->nd = 1; |
| 2463 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2464 | } |
| 2465 | return(ret); |
| 2466 | } |
| 2467 | |
| 2468 | /** |
| 2469 | * xmlFAComputesDeterminism: |
| 2470 | * @ctxt: a regexp parser context |
| 2471 | * |
| 2472 | * Check whether the associated regexp is determinist, |
| 2473 | * should be called after xmlFAEliminateEpsilonTransitions() |
| 2474 | * |
| 2475 | */ |
| 2476 | static int |
| 2477 | xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) { |
| 2478 | int statenr, transnr; |
| 2479 | xmlRegStatePtr state; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2480 | xmlRegTransPtr t1, t2, last; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2481 | int i; |
| 2482 | int ret = 1; |
| 2483 | |
Daniel Veillard | 4402ab4 | 2002-09-12 16:02:56 +0000 | [diff] [blame] | 2484 | #ifdef DEBUG_REGEXP_GRAPH |
| 2485 | printf("xmlFAComputesDeterminism\n"); |
| 2486 | xmlRegPrintCtxt(stdout, ctxt); |
| 2487 | #endif |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2488 | if (ctxt->determinist != -1) |
| 2489 | return(ctxt->determinist); |
| 2490 | |
| 2491 | /* |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2492 | * First cleanup the automata removing cancelled transitions |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2493 | */ |
| 2494 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 2495 | state = ctxt->states[statenr]; |
| 2496 | if (state == NULL) |
| 2497 | continue; |
Daniel Veillard | 4f82c8a | 2005-08-09 21:40:08 +0000 | [diff] [blame] | 2498 | if (state->nbTrans < 2) |
| 2499 | continue; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2500 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 2501 | t1 = &(state->trans[transnr]); |
| 2502 | /* |
| 2503 | * Determinism checks in case of counted or all transitions |
| 2504 | * will have to be handled separately |
| 2505 | */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2506 | if (t1->atom == NULL) { |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 2507 | /* t1->nd = 1; */ |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2508 | continue; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2509 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2510 | if (t1->to == -1) /* eliminated */ |
| 2511 | continue; |
| 2512 | for (i = 0;i < transnr;i++) { |
| 2513 | t2 = &(state->trans[i]); |
| 2514 | if (t2->to == -1) /* eliminated */ |
| 2515 | continue; |
| 2516 | if (t2->atom != NULL) { |
| 2517 | if (t1->to == t2->to) { |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2518 | if (xmlFAEqualAtoms(t1->atom, t2->atom)) |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 2519 | t2->to = -1; /* eliminated */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2520 | } |
| 2521 | } |
| 2522 | } |
| 2523 | } |
| 2524 | } |
| 2525 | |
| 2526 | /* |
| 2527 | * Check for all states that there aren't 2 transitions |
| 2528 | * with the same atom and a different target. |
| 2529 | */ |
| 2530 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 2531 | state = ctxt->states[statenr]; |
| 2532 | if (state == NULL) |
| 2533 | continue; |
| 2534 | if (state->nbTrans < 2) |
| 2535 | continue; |
| 2536 | last = NULL; |
| 2537 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 2538 | t1 = &(state->trans[transnr]); |
| 2539 | /* |
| 2540 | * Determinism checks in case of counted or all transitions |
| 2541 | * will have to be handled separately |
| 2542 | */ |
| 2543 | if (t1->atom == NULL) { |
| 2544 | continue; |
| 2545 | } |
| 2546 | if (t1->to == -1) /* eliminated */ |
| 2547 | continue; |
| 2548 | for (i = 0;i < transnr;i++) { |
| 2549 | t2 = &(state->trans[i]); |
| 2550 | if (t2->to == -1) /* eliminated */ |
| 2551 | continue; |
| 2552 | if (t2->atom != NULL) { |
| 2553 | /* not determinist ! */ |
| 2554 | if (xmlFACompareAtoms(t1->atom, t2->atom)) { |
| 2555 | ret = 0; |
| 2556 | /* mark the transitions as non-deterministic ones */ |
| 2557 | t1->nd = 1; |
| 2558 | t2->nd = 1; |
| 2559 | last = t1; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2560 | } |
| 2561 | } else if (t1->to != -1) { |
| 2562 | /* |
| 2563 | * do the closure in case of remaining specific |
| 2564 | * epsilon transitions like choices or all |
| 2565 | */ |
| 2566 | ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to], |
| 2567 | t2->to, t2->atom); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2568 | /* don't shortcut the computation so all non deterministic |
| 2569 | transition get marked down |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2570 | if (ret == 0) |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 2571 | return(0); |
| 2572 | */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2573 | if (ret == 0) { |
| 2574 | t1->nd = 1; |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 2575 | /* t2->nd = 1; */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2576 | last = t1; |
| 2577 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2578 | } |
| 2579 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2580 | /* don't shortcut the computation so all non deterministic |
| 2581 | transition get marked down |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2582 | if (ret == 0) |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2583 | break; */ |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2584 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2585 | |
| 2586 | /* |
| 2587 | * mark specifically the last non-deterministic transition |
| 2588 | * from a state since there is no need to set-up rollback |
| 2589 | * from it |
| 2590 | */ |
| 2591 | if (last != NULL) { |
| 2592 | last->nd = 2; |
| 2593 | } |
| 2594 | |
| 2595 | /* don't shortcut the computation so all non deterministic |
| 2596 | transition get marked down |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2597 | if (ret == 0) |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2598 | break; */ |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2599 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2600 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2601 | ctxt->determinist = ret; |
| 2602 | return(ret); |
| 2603 | } |
| 2604 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2605 | /************************************************************************ |
| 2606 | * * |
| 2607 | * Routines to check input against transition atoms * |
| 2608 | * * |
| 2609 | ************************************************************************/ |
| 2610 | |
| 2611 | static int |
| 2612 | xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg, |
| 2613 | int start, int end, const xmlChar *blockName) { |
| 2614 | int ret = 0; |
| 2615 | |
| 2616 | switch (type) { |
| 2617 | case XML_REGEXP_STRING: |
| 2618 | case XML_REGEXP_SUBREG: |
| 2619 | case XML_REGEXP_RANGES: |
| 2620 | case XML_REGEXP_EPSILON: |
| 2621 | return(-1); |
| 2622 | case XML_REGEXP_ANYCHAR: |
| 2623 | ret = ((codepoint != '\n') && (codepoint != '\r')); |
| 2624 | break; |
| 2625 | case XML_REGEXP_CHARVAL: |
| 2626 | ret = ((codepoint >= start) && (codepoint <= end)); |
| 2627 | break; |
| 2628 | case XML_REGEXP_NOTSPACE: |
| 2629 | neg = !neg; |
| 2630 | case XML_REGEXP_ANYSPACE: |
| 2631 | ret = ((codepoint == '\n') || (codepoint == '\r') || |
| 2632 | (codepoint == '\t') || (codepoint == ' ')); |
| 2633 | break; |
| 2634 | case XML_REGEXP_NOTINITNAME: |
| 2635 | neg = !neg; |
| 2636 | case XML_REGEXP_INITNAME: |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2637 | ret = (IS_LETTER(codepoint) || |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2638 | (codepoint == '_') || (codepoint == ':')); |
| 2639 | break; |
| 2640 | case XML_REGEXP_NOTNAMECHAR: |
| 2641 | neg = !neg; |
| 2642 | case XML_REGEXP_NAMECHAR: |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2643 | ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) || |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2644 | (codepoint == '.') || (codepoint == '-') || |
| 2645 | (codepoint == '_') || (codepoint == ':') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2646 | IS_COMBINING(codepoint) || IS_EXTENDER(codepoint)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2647 | break; |
| 2648 | case XML_REGEXP_NOTDECIMAL: |
| 2649 | neg = !neg; |
| 2650 | case XML_REGEXP_DECIMAL: |
| 2651 | ret = xmlUCSIsCatNd(codepoint); |
| 2652 | break; |
| 2653 | case XML_REGEXP_REALCHAR: |
| 2654 | neg = !neg; |
| 2655 | case XML_REGEXP_NOTREALCHAR: |
| 2656 | ret = xmlUCSIsCatP(codepoint); |
| 2657 | if (ret == 0) |
| 2658 | ret = xmlUCSIsCatZ(codepoint); |
| 2659 | if (ret == 0) |
| 2660 | ret = xmlUCSIsCatC(codepoint); |
| 2661 | break; |
| 2662 | case XML_REGEXP_LETTER: |
| 2663 | ret = xmlUCSIsCatL(codepoint); |
| 2664 | break; |
| 2665 | case XML_REGEXP_LETTER_UPPERCASE: |
| 2666 | ret = xmlUCSIsCatLu(codepoint); |
| 2667 | break; |
| 2668 | case XML_REGEXP_LETTER_LOWERCASE: |
| 2669 | ret = xmlUCSIsCatLl(codepoint); |
| 2670 | break; |
| 2671 | case XML_REGEXP_LETTER_TITLECASE: |
| 2672 | ret = xmlUCSIsCatLt(codepoint); |
| 2673 | break; |
| 2674 | case XML_REGEXP_LETTER_MODIFIER: |
| 2675 | ret = xmlUCSIsCatLm(codepoint); |
| 2676 | break; |
| 2677 | case XML_REGEXP_LETTER_OTHERS: |
| 2678 | ret = xmlUCSIsCatLo(codepoint); |
| 2679 | break; |
| 2680 | case XML_REGEXP_MARK: |
| 2681 | ret = xmlUCSIsCatM(codepoint); |
| 2682 | break; |
| 2683 | case XML_REGEXP_MARK_NONSPACING: |
| 2684 | ret = xmlUCSIsCatMn(codepoint); |
| 2685 | break; |
| 2686 | case XML_REGEXP_MARK_SPACECOMBINING: |
| 2687 | ret = xmlUCSIsCatMc(codepoint); |
| 2688 | break; |
| 2689 | case XML_REGEXP_MARK_ENCLOSING: |
| 2690 | ret = xmlUCSIsCatMe(codepoint); |
| 2691 | break; |
| 2692 | case XML_REGEXP_NUMBER: |
| 2693 | ret = xmlUCSIsCatN(codepoint); |
| 2694 | break; |
| 2695 | case XML_REGEXP_NUMBER_DECIMAL: |
| 2696 | ret = xmlUCSIsCatNd(codepoint); |
| 2697 | break; |
| 2698 | case XML_REGEXP_NUMBER_LETTER: |
| 2699 | ret = xmlUCSIsCatNl(codepoint); |
| 2700 | break; |
| 2701 | case XML_REGEXP_NUMBER_OTHERS: |
| 2702 | ret = xmlUCSIsCatNo(codepoint); |
| 2703 | break; |
| 2704 | case XML_REGEXP_PUNCT: |
| 2705 | ret = xmlUCSIsCatP(codepoint); |
| 2706 | break; |
| 2707 | case XML_REGEXP_PUNCT_CONNECTOR: |
| 2708 | ret = xmlUCSIsCatPc(codepoint); |
| 2709 | break; |
| 2710 | case XML_REGEXP_PUNCT_DASH: |
| 2711 | ret = xmlUCSIsCatPd(codepoint); |
| 2712 | break; |
| 2713 | case XML_REGEXP_PUNCT_OPEN: |
| 2714 | ret = xmlUCSIsCatPs(codepoint); |
| 2715 | break; |
| 2716 | case XML_REGEXP_PUNCT_CLOSE: |
| 2717 | ret = xmlUCSIsCatPe(codepoint); |
| 2718 | break; |
| 2719 | case XML_REGEXP_PUNCT_INITQUOTE: |
| 2720 | ret = xmlUCSIsCatPi(codepoint); |
| 2721 | break; |
| 2722 | case XML_REGEXP_PUNCT_FINQUOTE: |
| 2723 | ret = xmlUCSIsCatPf(codepoint); |
| 2724 | break; |
| 2725 | case XML_REGEXP_PUNCT_OTHERS: |
| 2726 | ret = xmlUCSIsCatPo(codepoint); |
| 2727 | break; |
| 2728 | case XML_REGEXP_SEPAR: |
| 2729 | ret = xmlUCSIsCatZ(codepoint); |
| 2730 | break; |
| 2731 | case XML_REGEXP_SEPAR_SPACE: |
| 2732 | ret = xmlUCSIsCatZs(codepoint); |
| 2733 | break; |
| 2734 | case XML_REGEXP_SEPAR_LINE: |
| 2735 | ret = xmlUCSIsCatZl(codepoint); |
| 2736 | break; |
| 2737 | case XML_REGEXP_SEPAR_PARA: |
| 2738 | ret = xmlUCSIsCatZp(codepoint); |
| 2739 | break; |
| 2740 | case XML_REGEXP_SYMBOL: |
| 2741 | ret = xmlUCSIsCatS(codepoint); |
| 2742 | break; |
| 2743 | case XML_REGEXP_SYMBOL_MATH: |
| 2744 | ret = xmlUCSIsCatSm(codepoint); |
| 2745 | break; |
| 2746 | case XML_REGEXP_SYMBOL_CURRENCY: |
| 2747 | ret = xmlUCSIsCatSc(codepoint); |
| 2748 | break; |
| 2749 | case XML_REGEXP_SYMBOL_MODIFIER: |
| 2750 | ret = xmlUCSIsCatSk(codepoint); |
| 2751 | break; |
| 2752 | case XML_REGEXP_SYMBOL_OTHERS: |
| 2753 | ret = xmlUCSIsCatSo(codepoint); |
| 2754 | break; |
| 2755 | case XML_REGEXP_OTHER: |
| 2756 | ret = xmlUCSIsCatC(codepoint); |
| 2757 | break; |
| 2758 | case XML_REGEXP_OTHER_CONTROL: |
| 2759 | ret = xmlUCSIsCatCc(codepoint); |
| 2760 | break; |
| 2761 | case XML_REGEXP_OTHER_FORMAT: |
| 2762 | ret = xmlUCSIsCatCf(codepoint); |
| 2763 | break; |
| 2764 | case XML_REGEXP_OTHER_PRIVATE: |
| 2765 | ret = xmlUCSIsCatCo(codepoint); |
| 2766 | break; |
| 2767 | case XML_REGEXP_OTHER_NA: |
| 2768 | /* ret = xmlUCSIsCatCn(codepoint); */ |
| 2769 | /* Seems it doesn't exist anymore in recent Unicode releases */ |
| 2770 | ret = 0; |
| 2771 | break; |
| 2772 | case XML_REGEXP_BLOCK_NAME: |
| 2773 | ret = xmlUCSIsBlock(codepoint, (const char *) blockName); |
| 2774 | break; |
| 2775 | } |
| 2776 | if (neg) |
| 2777 | return(!ret); |
| 2778 | return(ret); |
| 2779 | } |
| 2780 | |
| 2781 | static int |
| 2782 | xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) { |
| 2783 | int i, ret = 0; |
| 2784 | xmlRegRangePtr range; |
| 2785 | |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2786 | if ((atom == NULL) || (!IS_CHAR(codepoint))) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2787 | return(-1); |
| 2788 | |
| 2789 | switch (atom->type) { |
| 2790 | case XML_REGEXP_SUBREG: |
| 2791 | case XML_REGEXP_EPSILON: |
| 2792 | return(-1); |
| 2793 | case XML_REGEXP_CHARVAL: |
| 2794 | return(codepoint == atom->codepoint); |
| 2795 | case XML_REGEXP_RANGES: { |
| 2796 | int accept = 0; |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 2797 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2798 | for (i = 0;i < atom->nbRanges;i++) { |
| 2799 | range = atom->ranges[i]; |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 2800 | if (range->neg == 2) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2801 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
| 2802 | 0, range->start, range->end, |
| 2803 | range->blockName); |
| 2804 | if (ret != 0) |
| 2805 | return(0); /* excluded char */ |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 2806 | } else if (range->neg) { |
| 2807 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
| 2808 | 0, range->start, range->end, |
| 2809 | range->blockName); |
| 2810 | if (ret == 0) |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 2811 | accept = 1; |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 2812 | else |
| 2813 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2814 | } else { |
| 2815 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
| 2816 | 0, range->start, range->end, |
| 2817 | range->blockName); |
| 2818 | if (ret != 0) |
| 2819 | accept = 1; /* might still be excluded */ |
| 2820 | } |
| 2821 | } |
| 2822 | return(accept); |
| 2823 | } |
| 2824 | case XML_REGEXP_STRING: |
| 2825 | printf("TODO: XML_REGEXP_STRING\n"); |
| 2826 | return(-1); |
| 2827 | case XML_REGEXP_ANYCHAR: |
| 2828 | case XML_REGEXP_ANYSPACE: |
| 2829 | case XML_REGEXP_NOTSPACE: |
| 2830 | case XML_REGEXP_INITNAME: |
| 2831 | case XML_REGEXP_NOTINITNAME: |
| 2832 | case XML_REGEXP_NAMECHAR: |
| 2833 | case XML_REGEXP_NOTNAMECHAR: |
| 2834 | case XML_REGEXP_DECIMAL: |
| 2835 | case XML_REGEXP_NOTDECIMAL: |
| 2836 | case XML_REGEXP_REALCHAR: |
| 2837 | case XML_REGEXP_NOTREALCHAR: |
| 2838 | case XML_REGEXP_LETTER: |
| 2839 | case XML_REGEXP_LETTER_UPPERCASE: |
| 2840 | case XML_REGEXP_LETTER_LOWERCASE: |
| 2841 | case XML_REGEXP_LETTER_TITLECASE: |
| 2842 | case XML_REGEXP_LETTER_MODIFIER: |
| 2843 | case XML_REGEXP_LETTER_OTHERS: |
| 2844 | case XML_REGEXP_MARK: |
| 2845 | case XML_REGEXP_MARK_NONSPACING: |
| 2846 | case XML_REGEXP_MARK_SPACECOMBINING: |
| 2847 | case XML_REGEXP_MARK_ENCLOSING: |
| 2848 | case XML_REGEXP_NUMBER: |
| 2849 | case XML_REGEXP_NUMBER_DECIMAL: |
| 2850 | case XML_REGEXP_NUMBER_LETTER: |
| 2851 | case XML_REGEXP_NUMBER_OTHERS: |
| 2852 | case XML_REGEXP_PUNCT: |
| 2853 | case XML_REGEXP_PUNCT_CONNECTOR: |
| 2854 | case XML_REGEXP_PUNCT_DASH: |
| 2855 | case XML_REGEXP_PUNCT_OPEN: |
| 2856 | case XML_REGEXP_PUNCT_CLOSE: |
| 2857 | case XML_REGEXP_PUNCT_INITQUOTE: |
| 2858 | case XML_REGEXP_PUNCT_FINQUOTE: |
| 2859 | case XML_REGEXP_PUNCT_OTHERS: |
| 2860 | case XML_REGEXP_SEPAR: |
| 2861 | case XML_REGEXP_SEPAR_SPACE: |
| 2862 | case XML_REGEXP_SEPAR_LINE: |
| 2863 | case XML_REGEXP_SEPAR_PARA: |
| 2864 | case XML_REGEXP_SYMBOL: |
| 2865 | case XML_REGEXP_SYMBOL_MATH: |
| 2866 | case XML_REGEXP_SYMBOL_CURRENCY: |
| 2867 | case XML_REGEXP_SYMBOL_MODIFIER: |
| 2868 | case XML_REGEXP_SYMBOL_OTHERS: |
| 2869 | case XML_REGEXP_OTHER: |
| 2870 | case XML_REGEXP_OTHER_CONTROL: |
| 2871 | case XML_REGEXP_OTHER_FORMAT: |
| 2872 | case XML_REGEXP_OTHER_PRIVATE: |
| 2873 | case XML_REGEXP_OTHER_NA: |
| 2874 | case XML_REGEXP_BLOCK_NAME: |
| 2875 | ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0, |
| 2876 | (const xmlChar *)atom->valuep); |
| 2877 | if (atom->neg) |
| 2878 | ret = !ret; |
| 2879 | break; |
| 2880 | } |
| 2881 | return(ret); |
| 2882 | } |
| 2883 | |
| 2884 | /************************************************************************ |
| 2885 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 2886 | * Saving and restoring state of an execution context * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2887 | * * |
| 2888 | ************************************************************************/ |
| 2889 | |
| 2890 | #ifdef DEBUG_REGEXP_EXEC |
| 2891 | static void |
| 2892 | xmlFARegDebugExec(xmlRegExecCtxtPtr exec) { |
| 2893 | printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index); |
| 2894 | if (exec->inputStack != NULL) { |
| 2895 | int i; |
| 2896 | printf(": "); |
| 2897 | for (i = 0;(i < 3) && (i < exec->inputStackNr);i++) |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 2898 | printf("%s ", (const char *) |
| 2899 | exec->inputStack[exec->inputStackNr - (i + 1)].value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2900 | } else { |
| 2901 | printf(": %s", &(exec->inputString[exec->index])); |
| 2902 | } |
| 2903 | printf("\n"); |
| 2904 | } |
| 2905 | #endif |
| 2906 | |
| 2907 | static void |
| 2908 | xmlFARegExecSave(xmlRegExecCtxtPtr exec) { |
| 2909 | #ifdef DEBUG_REGEXP_EXEC |
| 2910 | printf("saving "); |
| 2911 | exec->transno++; |
| 2912 | xmlFARegDebugExec(exec); |
| 2913 | exec->transno--; |
| 2914 | #endif |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 2915 | #ifdef MAX_PUSH |
| 2916 | if (exec->nbPush > MAX_PUSH) { |
| 2917 | return; |
| 2918 | } |
| 2919 | exec->nbPush++; |
| 2920 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2921 | |
| 2922 | if (exec->maxRollbacks == 0) { |
| 2923 | exec->maxRollbacks = 4; |
| 2924 | exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks * |
| 2925 | sizeof(xmlRegExecRollback)); |
| 2926 | if (exec->rollbacks == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2927 | xmlRegexpErrMemory(NULL, "saving regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2928 | exec->maxRollbacks = 0; |
| 2929 | return; |
| 2930 | } |
| 2931 | memset(exec->rollbacks, 0, |
| 2932 | exec->maxRollbacks * sizeof(xmlRegExecRollback)); |
| 2933 | } else if (exec->nbRollbacks >= exec->maxRollbacks) { |
| 2934 | xmlRegExecRollback *tmp; |
| 2935 | int len = exec->maxRollbacks; |
| 2936 | |
| 2937 | exec->maxRollbacks *= 2; |
| 2938 | tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks, |
| 2939 | exec->maxRollbacks * sizeof(xmlRegExecRollback)); |
| 2940 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2941 | xmlRegexpErrMemory(NULL, "saving regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2942 | exec->maxRollbacks /= 2; |
| 2943 | return; |
| 2944 | } |
| 2945 | exec->rollbacks = tmp; |
| 2946 | tmp = &exec->rollbacks[len]; |
| 2947 | memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback)); |
| 2948 | } |
| 2949 | exec->rollbacks[exec->nbRollbacks].state = exec->state; |
| 2950 | exec->rollbacks[exec->nbRollbacks].index = exec->index; |
| 2951 | exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1; |
| 2952 | if (exec->comp->nbCounters > 0) { |
| 2953 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
| 2954 | exec->rollbacks[exec->nbRollbacks].counts = (int *) |
| 2955 | xmlMalloc(exec->comp->nbCounters * sizeof(int)); |
| 2956 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2957 | xmlRegexpErrMemory(NULL, "saving regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2958 | exec->status = -5; |
| 2959 | return; |
| 2960 | } |
| 2961 | } |
| 2962 | memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts, |
| 2963 | exec->comp->nbCounters * sizeof(int)); |
| 2964 | } |
| 2965 | exec->nbRollbacks++; |
| 2966 | } |
| 2967 | |
| 2968 | static void |
| 2969 | xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) { |
| 2970 | if (exec->nbRollbacks <= 0) { |
| 2971 | exec->status = -1; |
| 2972 | #ifdef DEBUG_REGEXP_EXEC |
| 2973 | printf("rollback failed on empty stack\n"); |
| 2974 | #endif |
| 2975 | return; |
| 2976 | } |
| 2977 | exec->nbRollbacks--; |
| 2978 | exec->state = exec->rollbacks[exec->nbRollbacks].state; |
| 2979 | exec->index = exec->rollbacks[exec->nbRollbacks].index; |
| 2980 | exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch; |
| 2981 | if (exec->comp->nbCounters > 0) { |
| 2982 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
| 2983 | fprintf(stderr, "exec save: allocation failed"); |
| 2984 | exec->status = -6; |
| 2985 | return; |
| 2986 | } |
| 2987 | memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts, |
| 2988 | exec->comp->nbCounters * sizeof(int)); |
| 2989 | } |
| 2990 | |
| 2991 | #ifdef DEBUG_REGEXP_EXEC |
| 2992 | printf("restored "); |
| 2993 | xmlFARegDebugExec(exec); |
| 2994 | #endif |
| 2995 | } |
| 2996 | |
| 2997 | /************************************************************************ |
| 2998 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 2999 | * Verifier, running an input against a compiled regexp * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3000 | * * |
| 3001 | ************************************************************************/ |
| 3002 | |
| 3003 | static int |
| 3004 | xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) { |
| 3005 | xmlRegExecCtxt execval; |
| 3006 | xmlRegExecCtxtPtr exec = &execval; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 3007 | int ret, codepoint = 0, len, deter; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3008 | |
| 3009 | exec->inputString = content; |
| 3010 | exec->index = 0; |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 3011 | exec->nbPush = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3012 | exec->determinist = 1; |
| 3013 | exec->maxRollbacks = 0; |
| 3014 | exec->nbRollbacks = 0; |
| 3015 | exec->rollbacks = NULL; |
| 3016 | exec->status = 0; |
| 3017 | exec->comp = comp; |
| 3018 | exec->state = comp->states[0]; |
| 3019 | exec->transno = 0; |
| 3020 | exec->transcount = 0; |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 3021 | exec->inputStack = NULL; |
| 3022 | exec->inputStackMax = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3023 | if (comp->nbCounters > 0) { |
| 3024 | exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)); |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3025 | if (exec->counts == NULL) { |
| 3026 | xmlRegexpErrMemory(NULL, "running regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3027 | return(-1); |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3028 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3029 | memset(exec->counts, 0, comp->nbCounters * sizeof(int)); |
| 3030 | } else |
| 3031 | exec->counts = NULL; |
| 3032 | while ((exec->status == 0) && |
| 3033 | ((exec->inputString[exec->index] != 0) || |
| 3034 | (exec->state->type != XML_REGEXP_FINAL_STATE))) { |
| 3035 | xmlRegTransPtr trans; |
| 3036 | xmlRegAtomPtr atom; |
| 3037 | |
| 3038 | /* |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3039 | * If end of input on non-terminal state, rollback, however we may |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3040 | * still have epsilon like transition for counted transitions |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3041 | * on counters, in that case don't break too early. Additionally, |
| 3042 | * if we are working on a range like "AB{0,2}", where B is not present, |
| 3043 | * we don't want to break. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3044 | */ |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3045 | len = 1; |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3046 | if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) { |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 3047 | /* |
| 3048 | * if there is a transition, we must check if |
| 3049 | * atom allows minOccurs of 0 |
| 3050 | */ |
| 3051 | if (exec->transno < exec->state->nbTrans) { |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3052 | trans = &exec->state->trans[exec->transno]; |
| 3053 | if (trans->to >=0) { |
| 3054 | atom = trans->atom; |
| 3055 | if (!((atom->min == 0) && (atom->max > 0))) |
| 3056 | goto rollback; |
| 3057 | } |
| 3058 | } else |
| 3059 | goto rollback; |
| 3060 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3061 | |
| 3062 | exec->transcount = 0; |
| 3063 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
| 3064 | trans = &exec->state->trans[exec->transno]; |
| 3065 | if (trans->to < 0) |
| 3066 | continue; |
| 3067 | atom = trans->atom; |
| 3068 | ret = 0; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 3069 | deter = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3070 | if (trans->count >= 0) { |
| 3071 | int count; |
| 3072 | xmlRegCounterPtr counter; |
| 3073 | |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3074 | if (exec->counts == NULL) { |
| 3075 | exec->status = -1; |
| 3076 | goto error; |
| 3077 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3078 | /* |
| 3079 | * A counted transition. |
| 3080 | */ |
| 3081 | |
| 3082 | count = exec->counts[trans->count]; |
| 3083 | counter = &exec->comp->counters[trans->count]; |
| 3084 | #ifdef DEBUG_REGEXP_EXEC |
| 3085 | printf("testing count %d: val %d, min %d, max %d\n", |
| 3086 | trans->count, count, counter->min, counter->max); |
| 3087 | #endif |
| 3088 | ret = ((count >= counter->min) && (count <= counter->max)); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 3089 | if ((ret) && (counter->min != counter->max)) |
| 3090 | deter = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3091 | } else if (atom == NULL) { |
| 3092 | fprintf(stderr, "epsilon transition left at runtime\n"); |
| 3093 | exec->status = -2; |
| 3094 | break; |
| 3095 | } else if (exec->inputString[exec->index] != 0) { |
| 3096 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len); |
| 3097 | ret = xmlRegCheckCharacter(atom, codepoint); |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3098 | if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3099 | xmlRegStatePtr to = comp->states[trans->to]; |
| 3100 | |
| 3101 | /* |
| 3102 | * this is a multiple input sequence |
Daniel Veillard | fc6eca0 | 2005-11-01 15:24:02 +0000 | [diff] [blame] | 3103 | * If there is a counter associated increment it now. |
| 3104 | * before potentially saving and rollback |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3105 | */ |
Daniel Veillard | fc6eca0 | 2005-11-01 15:24:02 +0000 | [diff] [blame] | 3106 | if (trans->counter >= 0) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3107 | if (exec->counts == NULL) { |
| 3108 | exec->status = -1; |
| 3109 | goto error; |
| 3110 | } |
Daniel Veillard | fc6eca0 | 2005-11-01 15:24:02 +0000 | [diff] [blame] | 3111 | #ifdef DEBUG_REGEXP_EXEC |
| 3112 | printf("Increasing count %d\n", trans->counter); |
| 3113 | #endif |
| 3114 | exec->counts[trans->counter]++; |
| 3115 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3116 | if (exec->state->nbTrans > exec->transno + 1) { |
| 3117 | xmlFARegExecSave(exec); |
| 3118 | } |
| 3119 | exec->transcount = 1; |
| 3120 | do { |
| 3121 | /* |
| 3122 | * Try to progress as much as possible on the input |
| 3123 | */ |
| 3124 | if (exec->transcount == atom->max) { |
| 3125 | break; |
| 3126 | } |
| 3127 | exec->index += len; |
| 3128 | /* |
| 3129 | * End of input: stop here |
| 3130 | */ |
| 3131 | if (exec->inputString[exec->index] == 0) { |
| 3132 | exec->index -= len; |
| 3133 | break; |
| 3134 | } |
| 3135 | if (exec->transcount >= atom->min) { |
| 3136 | int transno = exec->transno; |
| 3137 | xmlRegStatePtr state = exec->state; |
| 3138 | |
| 3139 | /* |
| 3140 | * The transition is acceptable save it |
| 3141 | */ |
| 3142 | exec->transno = -1; /* trick */ |
| 3143 | exec->state = to; |
| 3144 | xmlFARegExecSave(exec); |
| 3145 | exec->transno = transno; |
| 3146 | exec->state = state; |
| 3147 | } |
| 3148 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), |
| 3149 | len); |
| 3150 | ret = xmlRegCheckCharacter(atom, codepoint); |
| 3151 | exec->transcount++; |
| 3152 | } while (ret == 1); |
| 3153 | if (exec->transcount < atom->min) |
| 3154 | ret = 0; |
| 3155 | |
| 3156 | /* |
| 3157 | * If the last check failed but one transition was found |
| 3158 | * possible, rollback |
| 3159 | */ |
| 3160 | if (ret < 0) |
| 3161 | ret = 0; |
| 3162 | if (ret == 0) { |
| 3163 | goto rollback; |
| 3164 | } |
Daniel Veillard | fc6eca0 | 2005-11-01 15:24:02 +0000 | [diff] [blame] | 3165 | if (trans->counter >= 0) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3166 | if (exec->counts == NULL) { |
| 3167 | exec->status = -1; |
| 3168 | goto error; |
| 3169 | } |
Daniel Veillard | fc6eca0 | 2005-11-01 15:24:02 +0000 | [diff] [blame] | 3170 | #ifdef DEBUG_REGEXP_EXEC |
| 3171 | printf("Decreasing count %d\n", trans->counter); |
| 3172 | #endif |
| 3173 | exec->counts[trans->counter]--; |
| 3174 | } |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3175 | } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) { |
| 3176 | /* |
| 3177 | * we don't match on the codepoint, but minOccurs of 0 |
| 3178 | * says that's ok. Setting len to 0 inhibits stepping |
| 3179 | * over the codepoint. |
| 3180 | */ |
| 3181 | exec->transcount = 1; |
| 3182 | len = 0; |
| 3183 | ret = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3184 | } |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3185 | } else if ((atom->min == 0) && (atom->max > 0)) { |
| 3186 | /* another spot to match when minOccurs is 0 */ |
| 3187 | exec->transcount = 1; |
| 3188 | len = 0; |
| 3189 | ret = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3190 | } |
| 3191 | if (ret == 1) { |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 3192 | if ((trans->nd == 1) || |
| 3193 | ((trans->count >= 0) && (deter == 0) && |
| 3194 | (exec->state->nbTrans > exec->transno + 1))) { |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 3195 | #ifdef DEBUG_REGEXP_EXEC |
| 3196 | if (trans->nd == 1) |
| 3197 | printf("Saving on nd transition atom %d for %c at %d\n", |
| 3198 | trans->atom->no, codepoint, exec->index); |
| 3199 | else |
| 3200 | printf("Saving on counted transition count %d for %c at %d\n", |
| 3201 | trans->count, codepoint, exec->index); |
| 3202 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3203 | xmlFARegExecSave(exec); |
| 3204 | } |
| 3205 | if (trans->counter >= 0) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3206 | if (exec->counts == NULL) { |
| 3207 | exec->status = -1; |
| 3208 | goto error; |
| 3209 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3210 | #ifdef DEBUG_REGEXP_EXEC |
| 3211 | printf("Increasing count %d\n", trans->counter); |
| 3212 | #endif |
| 3213 | exec->counts[trans->counter]++; |
| 3214 | } |
Daniel Veillard | 1075228 | 2005-08-08 13:05:13 +0000 | [diff] [blame] | 3215 | if ((trans->count >= 0) && |
| 3216 | (trans->count < REGEXP_ALL_COUNTER)) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3217 | if (exec->counts == NULL) { |
| 3218 | exec->status = -1; |
| 3219 | goto error; |
| 3220 | } |
Daniel Veillard | 1075228 | 2005-08-08 13:05:13 +0000 | [diff] [blame] | 3221 | #ifdef DEBUG_REGEXP_EXEC |
| 3222 | printf("resetting count %d on transition\n", |
| 3223 | trans->count); |
| 3224 | #endif |
| 3225 | exec->counts[trans->count] = 0; |
| 3226 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3227 | #ifdef DEBUG_REGEXP_EXEC |
| 3228 | printf("entering state %d\n", trans->to); |
| 3229 | #endif |
| 3230 | exec->state = comp->states[trans->to]; |
| 3231 | exec->transno = 0; |
| 3232 | if (trans->atom != NULL) { |
| 3233 | exec->index += len; |
| 3234 | } |
| 3235 | goto progress; |
| 3236 | } else if (ret < 0) { |
| 3237 | exec->status = -4; |
| 3238 | break; |
| 3239 | } |
| 3240 | } |
| 3241 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
| 3242 | rollback: |
| 3243 | /* |
| 3244 | * Failed to find a way out |
| 3245 | */ |
| 3246 | exec->determinist = 0; |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 3247 | #ifdef DEBUG_REGEXP_EXEC |
| 3248 | printf("rollback from state %d on %d:%c\n", exec->state->no, |
| 3249 | codepoint,codepoint); |
| 3250 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3251 | xmlFARegExecRollBack(exec); |
| 3252 | } |
| 3253 | progress: |
| 3254 | continue; |
| 3255 | } |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3256 | error: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3257 | if (exec->rollbacks != NULL) { |
| 3258 | if (exec->counts != NULL) { |
| 3259 | int i; |
| 3260 | |
| 3261 | for (i = 0;i < exec->maxRollbacks;i++) |
| 3262 | if (exec->rollbacks[i].counts != NULL) |
| 3263 | xmlFree(exec->rollbacks[i].counts); |
| 3264 | } |
| 3265 | xmlFree(exec->rollbacks); |
| 3266 | } |
| 3267 | if (exec->counts != NULL) |
| 3268 | xmlFree(exec->counts); |
| 3269 | if (exec->status == 0) |
| 3270 | return(1); |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 3271 | if (exec->status == -1) { |
| 3272 | if (exec->nbPush > MAX_PUSH) |
| 3273 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3274 | return(0); |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 3275 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3276 | return(exec->status); |
| 3277 | } |
| 3278 | |
| 3279 | /************************************************************************ |
| 3280 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 3281 | * Progressive interface to the verifier one atom at a time * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3282 | * * |
| 3283 | ************************************************************************/ |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3284 | #ifdef DEBUG_ERR |
| 3285 | static void testerr(xmlRegExecCtxtPtr exec); |
| 3286 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3287 | |
| 3288 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3289 | * xmlRegNewExecCtxt: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3290 | * @comp: a precompiled regular expression |
| 3291 | * @callback: a callback function used for handling progresses in the |
| 3292 | * automata matching phase |
| 3293 | * @data: the context data associated to the callback in this context |
| 3294 | * |
| 3295 | * Build a context used for progressive evaluation of a regexp. |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3296 | * |
| 3297 | * Returns the new context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3298 | */ |
| 3299 | xmlRegExecCtxtPtr |
| 3300 | xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) { |
| 3301 | xmlRegExecCtxtPtr exec; |
| 3302 | |
| 3303 | if (comp == NULL) |
| 3304 | return(NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 3305 | if ((comp->compact == NULL) && (comp->states == NULL)) |
| 3306 | return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3307 | exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt)); |
| 3308 | if (exec == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3309 | xmlRegexpErrMemory(NULL, "creating execution context"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3310 | return(NULL); |
| 3311 | } |
| 3312 | memset(exec, 0, sizeof(xmlRegExecCtxt)); |
| 3313 | exec->inputString = NULL; |
| 3314 | exec->index = 0; |
| 3315 | exec->determinist = 1; |
| 3316 | exec->maxRollbacks = 0; |
| 3317 | exec->nbRollbacks = 0; |
| 3318 | exec->rollbacks = NULL; |
| 3319 | exec->status = 0; |
| 3320 | exec->comp = comp; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3321 | if (comp->compact == NULL) |
| 3322 | exec->state = comp->states[0]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3323 | exec->transno = 0; |
| 3324 | exec->transcount = 0; |
| 3325 | exec->callback = callback; |
| 3326 | exec->data = data; |
| 3327 | if (comp->nbCounters > 0) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3328 | /* |
| 3329 | * For error handling, exec->counts is allocated twice the size |
| 3330 | * the second half is used to store the data in case of rollback |
| 3331 | */ |
| 3332 | exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int) |
| 3333 | * 2); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3334 | if (exec->counts == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3335 | xmlRegexpErrMemory(NULL, "creating execution context"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3336 | xmlFree(exec); |
| 3337 | return(NULL); |
| 3338 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3339 | memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2); |
| 3340 | exec->errCounts = &exec->counts[comp->nbCounters]; |
| 3341 | } else { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3342 | exec->counts = NULL; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3343 | exec->errCounts = NULL; |
| 3344 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3345 | exec->inputStackMax = 0; |
| 3346 | exec->inputStackNr = 0; |
| 3347 | exec->inputStack = NULL; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3348 | exec->errStateNo = -1; |
| 3349 | exec->errString = NULL; |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 3350 | exec->nbPush = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3351 | return(exec); |
| 3352 | } |
| 3353 | |
| 3354 | /** |
| 3355 | * xmlRegFreeExecCtxt: |
| 3356 | * @exec: a regular expression evaulation context |
| 3357 | * |
| 3358 | * Free the structures associated to a regular expression evaulation context. |
| 3359 | */ |
| 3360 | void |
| 3361 | xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) { |
| 3362 | if (exec == NULL) |
| 3363 | return; |
| 3364 | |
| 3365 | if (exec->rollbacks != NULL) { |
| 3366 | if (exec->counts != NULL) { |
| 3367 | int i; |
| 3368 | |
| 3369 | for (i = 0;i < exec->maxRollbacks;i++) |
| 3370 | if (exec->rollbacks[i].counts != NULL) |
| 3371 | xmlFree(exec->rollbacks[i].counts); |
| 3372 | } |
| 3373 | xmlFree(exec->rollbacks); |
| 3374 | } |
| 3375 | if (exec->counts != NULL) |
| 3376 | xmlFree(exec->counts); |
| 3377 | if (exec->inputStack != NULL) { |
| 3378 | int i; |
| 3379 | |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3380 | for (i = 0;i < exec->inputStackNr;i++) { |
| 3381 | if (exec->inputStack[i].value != NULL) |
| 3382 | xmlFree(exec->inputStack[i].value); |
| 3383 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3384 | xmlFree(exec->inputStack); |
| 3385 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3386 | if (exec->errString != NULL) |
| 3387 | xmlFree(exec->errString); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3388 | xmlFree(exec); |
| 3389 | } |
| 3390 | |
| 3391 | static void |
| 3392 | xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value, |
| 3393 | void *data) { |
| 3394 | #ifdef DEBUG_PUSH |
| 3395 | printf("saving value: %d:%s\n", exec->inputStackNr, value); |
| 3396 | #endif |
| 3397 | if (exec->inputStackMax == 0) { |
| 3398 | exec->inputStackMax = 4; |
| 3399 | exec->inputStack = (xmlRegInputTokenPtr) |
| 3400 | xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken)); |
| 3401 | if (exec->inputStack == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3402 | xmlRegexpErrMemory(NULL, "pushing input string"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3403 | exec->inputStackMax = 0; |
| 3404 | return; |
| 3405 | } |
| 3406 | } else if (exec->inputStackNr + 1 >= exec->inputStackMax) { |
| 3407 | xmlRegInputTokenPtr tmp; |
| 3408 | |
| 3409 | exec->inputStackMax *= 2; |
| 3410 | tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack, |
| 3411 | exec->inputStackMax * sizeof(xmlRegInputToken)); |
| 3412 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3413 | xmlRegexpErrMemory(NULL, "pushing input string"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3414 | exec->inputStackMax /= 2; |
| 3415 | return; |
| 3416 | } |
| 3417 | exec->inputStack = tmp; |
| 3418 | } |
| 3419 | exec->inputStack[exec->inputStackNr].value = xmlStrdup(value); |
| 3420 | exec->inputStack[exec->inputStackNr].data = data; |
| 3421 | exec->inputStackNr++; |
| 3422 | exec->inputStack[exec->inputStackNr].value = NULL; |
| 3423 | exec->inputStack[exec->inputStackNr].data = NULL; |
| 3424 | } |
| 3425 | |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3426 | /** |
| 3427 | * xmlRegStrEqualWildcard: |
| 3428 | * @expStr: the string to be evaluated |
| 3429 | * @valStr: the validation string |
| 3430 | * |
| 3431 | * Checks if both strings are equal or have the same content. "*" |
| 3432 | * can be used as a wildcard in @valStr; "|" is used as a seperator of |
| 3433 | * substrings in both @expStr and @valStr. |
| 3434 | * |
| 3435 | * Returns 1 if the comparison is satisfied and the number of substrings |
| 3436 | * is equal, 0 otherwise. |
| 3437 | */ |
| 3438 | |
| 3439 | static int |
| 3440 | xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) { |
| 3441 | if (expStr == valStr) return(1); |
| 3442 | if (expStr == NULL) return(0); |
| 3443 | if (valStr == NULL) return(0); |
| 3444 | do { |
| 3445 | /* |
| 3446 | * Eval if we have a wildcard for the current item. |
| 3447 | */ |
| 3448 | if (*expStr != *valStr) { |
Daniel Veillard | 4f82c8a | 2005-08-09 21:40:08 +0000 | [diff] [blame] | 3449 | /* if one of them starts with a wildcard make valStr be it */ |
| 3450 | if (*valStr == '*') { |
| 3451 | const xmlChar *tmp; |
| 3452 | |
| 3453 | tmp = valStr; |
| 3454 | valStr = expStr; |
| 3455 | expStr = tmp; |
| 3456 | } |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3457 | if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) { |
| 3458 | do { |
| 3459 | if (*valStr == XML_REG_STRING_SEPARATOR) |
| 3460 | break; |
Kasimier T. Buchcik | c0e833f | 2005-04-19 15:02:20 +0000 | [diff] [blame] | 3461 | valStr++; |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3462 | } while (*valStr != 0); |
| 3463 | continue; |
| 3464 | } else |
| 3465 | return(0); |
| 3466 | } |
Kasimier T. Buchcik | c0e833f | 2005-04-19 15:02:20 +0000 | [diff] [blame] | 3467 | expStr++; |
| 3468 | valStr++; |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3469 | } while (*valStr != 0); |
| 3470 | if (*expStr != 0) |
| 3471 | return (0); |
| 3472 | else |
| 3473 | return (1); |
| 3474 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3475 | |
| 3476 | /** |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3477 | * xmlRegCompactPushString: |
| 3478 | * @exec: a regexp execution context |
| 3479 | * @comp: the precompiled exec with a compact table |
| 3480 | * @value: a string token input |
| 3481 | * @data: data associated to the token to reuse in callbacks |
| 3482 | * |
| 3483 | * Push one input token in the execution context |
| 3484 | * |
| 3485 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
| 3486 | * a negative value in case of error. |
| 3487 | */ |
| 3488 | static int |
| 3489 | xmlRegCompactPushString(xmlRegExecCtxtPtr exec, |
| 3490 | xmlRegexpPtr comp, |
| 3491 | const xmlChar *value, |
| 3492 | void *data) { |
| 3493 | int state = exec->index; |
| 3494 | int i, target; |
| 3495 | |
| 3496 | if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL)) |
| 3497 | return(-1); |
| 3498 | |
| 3499 | if (value == NULL) { |
| 3500 | /* |
| 3501 | * are we at a final state ? |
| 3502 | */ |
| 3503 | if (comp->compact[state * (comp->nbstrings + 1)] == |
| 3504 | XML_REGEXP_FINAL_STATE) |
| 3505 | return(1); |
| 3506 | return(0); |
| 3507 | } |
| 3508 | |
| 3509 | #ifdef DEBUG_PUSH |
| 3510 | printf("value pushed: %s\n", value); |
| 3511 | #endif |
| 3512 | |
| 3513 | /* |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 3514 | * Examine all outside transitions from current state |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3515 | */ |
| 3516 | for (i = 0;i < comp->nbstrings;i++) { |
| 3517 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
| 3518 | if ((target > 0) && (target <= comp->nbstates)) { |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3519 | target--; /* to avoid 0 */ |
| 3520 | if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) { |
| 3521 | exec->index = target; |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 3522 | if ((exec->callback != NULL) && (comp->transdata != NULL)) { |
| 3523 | exec->callback(exec->data, value, |
| 3524 | comp->transdata[state * comp->nbstrings + i], data); |
| 3525 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3526 | #ifdef DEBUG_PUSH |
| 3527 | printf("entering state %d\n", target); |
| 3528 | #endif |
| 3529 | if (comp->compact[target * (comp->nbstrings + 1)] == |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3530 | XML_REGEXP_SINK_STATE) |
| 3531 | goto error; |
| 3532 | |
| 3533 | if (comp->compact[target * (comp->nbstrings + 1)] == |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3534 | XML_REGEXP_FINAL_STATE) |
| 3535 | return(1); |
| 3536 | return(0); |
| 3537 | } |
| 3538 | } |
| 3539 | } |
| 3540 | /* |
| 3541 | * Failed to find an exit transition out from current state for the |
| 3542 | * current token |
| 3543 | */ |
| 3544 | #ifdef DEBUG_PUSH |
| 3545 | printf("failed to find a transition for %s on state %d\n", value, state); |
| 3546 | #endif |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3547 | error: |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3548 | if (exec->errString != NULL) |
| 3549 | xmlFree(exec->errString); |
| 3550 | exec->errString = xmlStrdup(value); |
| 3551 | exec->errStateNo = state; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3552 | exec->status = -1; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3553 | #ifdef DEBUG_ERR |
| 3554 | testerr(exec); |
| 3555 | #endif |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3556 | return(-1); |
| 3557 | } |
| 3558 | |
| 3559 | /** |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3560 | * xmlRegExecPushStringInternal: |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 3561 | * @exec: a regexp execution context or NULL to indicate the end |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3562 | * @value: a string token input |
| 3563 | * @data: data associated to the token to reuse in callbacks |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3564 | * @compound: value was assembled from 2 strings |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3565 | * |
| 3566 | * Push one input token in the execution context |
| 3567 | * |
| 3568 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
| 3569 | * a negative value in case of error. |
| 3570 | */ |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3571 | static int |
| 3572 | xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value, |
| 3573 | void *data, int compound) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3574 | xmlRegTransPtr trans; |
| 3575 | xmlRegAtomPtr atom; |
| 3576 | int ret; |
| 3577 | int final = 0; |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3578 | int progress = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3579 | |
| 3580 | if (exec == NULL) |
| 3581 | return(-1); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3582 | if (exec->comp == NULL) |
| 3583 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3584 | if (exec->status != 0) |
| 3585 | return(exec->status); |
| 3586 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3587 | if (exec->comp->compact != NULL) |
| 3588 | return(xmlRegCompactPushString(exec, exec->comp, value, data)); |
| 3589 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3590 | if (value == NULL) { |
| 3591 | if (exec->state->type == XML_REGEXP_FINAL_STATE) |
| 3592 | return(1); |
| 3593 | final = 1; |
| 3594 | } |
| 3595 | |
| 3596 | #ifdef DEBUG_PUSH |
| 3597 | printf("value pushed: %s\n", value); |
| 3598 | #endif |
| 3599 | /* |
| 3600 | * If we have an active rollback stack push the new value there |
| 3601 | * and get back to where we were left |
| 3602 | */ |
| 3603 | if ((value != NULL) && (exec->inputStackNr > 0)) { |
| 3604 | xmlFARegExecSaveInputString(exec, value, data); |
| 3605 | value = exec->inputStack[exec->index].value; |
| 3606 | data = exec->inputStack[exec->index].data; |
| 3607 | #ifdef DEBUG_PUSH |
| 3608 | printf("value loaded: %s\n", value); |
| 3609 | #endif |
| 3610 | } |
| 3611 | |
| 3612 | while ((exec->status == 0) && |
| 3613 | ((value != NULL) || |
| 3614 | ((final == 1) && |
| 3615 | (exec->state->type != XML_REGEXP_FINAL_STATE)))) { |
| 3616 | |
| 3617 | /* |
| 3618 | * End of input on non-terminal state, rollback, however we may |
| 3619 | * still have epsilon like transition for counted transitions |
| 3620 | * on counters, in that case don't break too early. |
| 3621 | */ |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 3622 | if ((value == NULL) && (exec->counts == NULL)) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3623 | goto rollback; |
| 3624 | |
| 3625 | exec->transcount = 0; |
| 3626 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
| 3627 | trans = &exec->state->trans[exec->transno]; |
| 3628 | if (trans->to < 0) |
| 3629 | continue; |
| 3630 | atom = trans->atom; |
| 3631 | ret = 0; |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 3632 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
| 3633 | int i; |
| 3634 | int count; |
| 3635 | xmlRegTransPtr t; |
| 3636 | xmlRegCounterPtr counter; |
| 3637 | |
| 3638 | ret = 0; |
| 3639 | |
| 3640 | #ifdef DEBUG_PUSH |
| 3641 | printf("testing all lax %d\n", trans->count); |
| 3642 | #endif |
| 3643 | /* |
| 3644 | * Check all counted transitions from the current state |
| 3645 | */ |
| 3646 | if ((value == NULL) && (final)) { |
| 3647 | ret = 1; |
| 3648 | } else if (value != NULL) { |
| 3649 | for (i = 0;i < exec->state->nbTrans;i++) { |
| 3650 | t = &exec->state->trans[i]; |
| 3651 | if ((t->counter < 0) || (t == trans)) |
| 3652 | continue; |
| 3653 | counter = &exec->comp->counters[t->counter]; |
| 3654 | count = exec->counts[t->counter]; |
| 3655 | if ((count < counter->max) && |
| 3656 | (t->atom != NULL) && |
| 3657 | (xmlStrEqual(value, t->atom->valuep))) { |
| 3658 | ret = 0; |
| 3659 | break; |
| 3660 | } |
| 3661 | if ((count >= counter->min) && |
| 3662 | (count < counter->max) && |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3663 | (t->atom != NULL) && |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 3664 | (xmlStrEqual(value, t->atom->valuep))) { |
| 3665 | ret = 1; |
| 3666 | break; |
| 3667 | } |
| 3668 | } |
| 3669 | } |
| 3670 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
Daniel Veillard | 8a001f6 | 2002-04-20 07:24:11 +0000 | [diff] [blame] | 3671 | int i; |
| 3672 | int count; |
| 3673 | xmlRegTransPtr t; |
| 3674 | xmlRegCounterPtr counter; |
| 3675 | |
| 3676 | ret = 1; |
| 3677 | |
| 3678 | #ifdef DEBUG_PUSH |
| 3679 | printf("testing all %d\n", trans->count); |
| 3680 | #endif |
| 3681 | /* |
| 3682 | * Check all counted transitions from the current state |
| 3683 | */ |
| 3684 | for (i = 0;i < exec->state->nbTrans;i++) { |
| 3685 | t = &exec->state->trans[i]; |
| 3686 | if ((t->counter < 0) || (t == trans)) |
| 3687 | continue; |
| 3688 | counter = &exec->comp->counters[t->counter]; |
| 3689 | count = exec->counts[t->counter]; |
| 3690 | if ((count < counter->min) || (count > counter->max)) { |
| 3691 | ret = 0; |
| 3692 | break; |
| 3693 | } |
| 3694 | } |
| 3695 | } else if (trans->count >= 0) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3696 | int count; |
| 3697 | xmlRegCounterPtr counter; |
| 3698 | |
| 3699 | /* |
| 3700 | * A counted transition. |
| 3701 | */ |
| 3702 | |
| 3703 | count = exec->counts[trans->count]; |
| 3704 | counter = &exec->comp->counters[trans->count]; |
| 3705 | #ifdef DEBUG_PUSH |
| 3706 | printf("testing count %d: val %d, min %d, max %d\n", |
| 3707 | trans->count, count, counter->min, counter->max); |
| 3708 | #endif |
| 3709 | ret = ((count >= counter->min) && (count <= counter->max)); |
| 3710 | } else if (atom == NULL) { |
| 3711 | fprintf(stderr, "epsilon transition left at runtime\n"); |
| 3712 | exec->status = -2; |
| 3713 | break; |
| 3714 | } else if (value != NULL) { |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3715 | ret = xmlRegStrEqualWildcard(atom->valuep, value); |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3716 | if (atom->neg) { |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 3717 | ret = !ret; |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3718 | if (!compound) |
| 3719 | ret = 0; |
| 3720 | } |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 3721 | if ((ret == 1) && (trans->counter >= 0)) { |
| 3722 | xmlRegCounterPtr counter; |
| 3723 | int count; |
| 3724 | |
| 3725 | count = exec->counts[trans->counter]; |
| 3726 | counter = &exec->comp->counters[trans->counter]; |
| 3727 | if (count >= counter->max) |
| 3728 | ret = 0; |
| 3729 | } |
| 3730 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3731 | if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) { |
| 3732 | xmlRegStatePtr to = exec->comp->states[trans->to]; |
| 3733 | |
| 3734 | /* |
| 3735 | * this is a multiple input sequence |
| 3736 | */ |
| 3737 | if (exec->state->nbTrans > exec->transno + 1) { |
| 3738 | if (exec->inputStackNr <= 0) { |
| 3739 | xmlFARegExecSaveInputString(exec, value, data); |
| 3740 | } |
| 3741 | xmlFARegExecSave(exec); |
| 3742 | } |
| 3743 | exec->transcount = 1; |
| 3744 | do { |
| 3745 | /* |
| 3746 | * Try to progress as much as possible on the input |
| 3747 | */ |
| 3748 | if (exec->transcount == atom->max) { |
| 3749 | break; |
| 3750 | } |
| 3751 | exec->index++; |
| 3752 | value = exec->inputStack[exec->index].value; |
| 3753 | data = exec->inputStack[exec->index].data; |
| 3754 | #ifdef DEBUG_PUSH |
| 3755 | printf("value loaded: %s\n", value); |
| 3756 | #endif |
| 3757 | |
| 3758 | /* |
| 3759 | * End of input: stop here |
| 3760 | */ |
| 3761 | if (value == NULL) { |
| 3762 | exec->index --; |
| 3763 | break; |
| 3764 | } |
| 3765 | if (exec->transcount >= atom->min) { |
| 3766 | int transno = exec->transno; |
| 3767 | xmlRegStatePtr state = exec->state; |
| 3768 | |
| 3769 | /* |
| 3770 | * The transition is acceptable save it |
| 3771 | */ |
| 3772 | exec->transno = -1; /* trick */ |
| 3773 | exec->state = to; |
| 3774 | if (exec->inputStackNr <= 0) { |
| 3775 | xmlFARegExecSaveInputString(exec, value, data); |
| 3776 | } |
| 3777 | xmlFARegExecSave(exec); |
| 3778 | exec->transno = transno; |
| 3779 | exec->state = state; |
| 3780 | } |
| 3781 | ret = xmlStrEqual(value, atom->valuep); |
| 3782 | exec->transcount++; |
| 3783 | } while (ret == 1); |
| 3784 | if (exec->transcount < atom->min) |
| 3785 | ret = 0; |
| 3786 | |
| 3787 | /* |
| 3788 | * If the last check failed but one transition was found |
| 3789 | * possible, rollback |
| 3790 | */ |
| 3791 | if (ret < 0) |
| 3792 | ret = 0; |
| 3793 | if (ret == 0) { |
| 3794 | goto rollback; |
| 3795 | } |
| 3796 | } |
| 3797 | } |
| 3798 | if (ret == 1) { |
William M. Brack | 9887395 | 2003-12-26 06:03:14 +0000 | [diff] [blame] | 3799 | if ((exec->callback != NULL) && (atom != NULL) && |
| 3800 | (data != NULL)) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3801 | exec->callback(exec->data, atom->valuep, |
| 3802 | atom->data, data); |
| 3803 | } |
| 3804 | if (exec->state->nbTrans > exec->transno + 1) { |
| 3805 | if (exec->inputStackNr <= 0) { |
| 3806 | xmlFARegExecSaveInputString(exec, value, data); |
| 3807 | } |
| 3808 | xmlFARegExecSave(exec); |
| 3809 | } |
| 3810 | if (trans->counter >= 0) { |
| 3811 | #ifdef DEBUG_PUSH |
| 3812 | printf("Increasing count %d\n", trans->counter); |
| 3813 | #endif |
| 3814 | exec->counts[trans->counter]++; |
| 3815 | } |
Daniel Veillard | 1075228 | 2005-08-08 13:05:13 +0000 | [diff] [blame] | 3816 | if ((trans->count >= 0) && |
| 3817 | (trans->count < REGEXP_ALL_COUNTER)) { |
| 3818 | #ifdef DEBUG_REGEXP_EXEC |
| 3819 | printf("resetting count %d on transition\n", |
| 3820 | trans->count); |
| 3821 | #endif |
| 3822 | exec->counts[trans->count] = 0; |
| 3823 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3824 | #ifdef DEBUG_PUSH |
| 3825 | printf("entering state %d\n", trans->to); |
| 3826 | #endif |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3827 | if ((exec->comp->states[trans->to] != NULL) && |
| 3828 | (exec->comp->states[trans->to]->type == |
| 3829 | XML_REGEXP_SINK_STATE)) { |
| 3830 | /* |
| 3831 | * entering a sink state, save the current state as error |
| 3832 | * state. |
| 3833 | */ |
| 3834 | if (exec->errString != NULL) |
| 3835 | xmlFree(exec->errString); |
| 3836 | exec->errString = xmlStrdup(value); |
| 3837 | exec->errState = exec->state; |
| 3838 | memcpy(exec->errCounts, exec->counts, |
| 3839 | exec->comp->nbCounters * sizeof(int)); |
| 3840 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3841 | exec->state = exec->comp->states[trans->to]; |
| 3842 | exec->transno = 0; |
| 3843 | if (trans->atom != NULL) { |
| 3844 | if (exec->inputStack != NULL) { |
| 3845 | exec->index++; |
| 3846 | if (exec->index < exec->inputStackNr) { |
| 3847 | value = exec->inputStack[exec->index].value; |
| 3848 | data = exec->inputStack[exec->index].data; |
| 3849 | #ifdef DEBUG_PUSH |
| 3850 | printf("value loaded: %s\n", value); |
| 3851 | #endif |
| 3852 | } else { |
| 3853 | value = NULL; |
| 3854 | data = NULL; |
| 3855 | #ifdef DEBUG_PUSH |
| 3856 | printf("end of input\n"); |
| 3857 | #endif |
| 3858 | } |
| 3859 | } else { |
| 3860 | value = NULL; |
| 3861 | data = NULL; |
| 3862 | #ifdef DEBUG_PUSH |
| 3863 | printf("end of input\n"); |
| 3864 | #endif |
| 3865 | } |
| 3866 | } |
| 3867 | goto progress; |
| 3868 | } else if (ret < 0) { |
| 3869 | exec->status = -4; |
| 3870 | break; |
| 3871 | } |
| 3872 | } |
| 3873 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
| 3874 | rollback: |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3875 | /* |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3876 | * if we didn't yet rollback on the current input |
| 3877 | * store the current state as the error state. |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3878 | */ |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3879 | if ((progress) && (exec->state != NULL) && |
| 3880 | (exec->state->type != XML_REGEXP_SINK_STATE)) { |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3881 | progress = 0; |
| 3882 | if (exec->errString != NULL) |
| 3883 | xmlFree(exec->errString); |
| 3884 | exec->errString = xmlStrdup(value); |
| 3885 | exec->errState = exec->state; |
| 3886 | memcpy(exec->errCounts, exec->counts, |
| 3887 | exec->comp->nbCounters * sizeof(int)); |
| 3888 | } |
| 3889 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3890 | /* |
| 3891 | * Failed to find a way out |
| 3892 | */ |
| 3893 | exec->determinist = 0; |
| 3894 | xmlFARegExecRollBack(exec); |
| 3895 | if (exec->status == 0) { |
| 3896 | value = exec->inputStack[exec->index].value; |
| 3897 | data = exec->inputStack[exec->index].data; |
| 3898 | #ifdef DEBUG_PUSH |
| 3899 | printf("value loaded: %s\n", value); |
| 3900 | #endif |
| 3901 | } |
| 3902 | } |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3903 | continue; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3904 | progress: |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3905 | progress = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3906 | continue; |
| 3907 | } |
| 3908 | if (exec->status == 0) { |
| 3909 | return(exec->state->type == XML_REGEXP_FINAL_STATE); |
| 3910 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3911 | #ifdef DEBUG_ERR |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3912 | if (exec->status < 0) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3913 | testerr(exec); |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3914 | } |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3915 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3916 | return(exec->status); |
| 3917 | } |
| 3918 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3919 | /** |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3920 | * xmlRegExecPushString: |
| 3921 | * @exec: a regexp execution context or NULL to indicate the end |
| 3922 | * @value: a string token input |
| 3923 | * @data: data associated to the token to reuse in callbacks |
| 3924 | * |
| 3925 | * Push one input token in the execution context |
| 3926 | * |
| 3927 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
| 3928 | * a negative value in case of error. |
| 3929 | */ |
| 3930 | int |
| 3931 | xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value, |
| 3932 | void *data) { |
| 3933 | return(xmlRegExecPushStringInternal(exec, value, data, 0)); |
| 3934 | } |
| 3935 | |
| 3936 | /** |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3937 | * xmlRegExecPushString2: |
| 3938 | * @exec: a regexp execution context or NULL to indicate the end |
| 3939 | * @value: the first string token input |
| 3940 | * @value2: the second string token input |
| 3941 | * @data: data associated to the token to reuse in callbacks |
| 3942 | * |
| 3943 | * Push one input token in the execution context |
| 3944 | * |
| 3945 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
| 3946 | * a negative value in case of error. |
| 3947 | */ |
| 3948 | int |
| 3949 | xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value, |
| 3950 | const xmlChar *value2, void *data) { |
| 3951 | xmlChar buf[150]; |
| 3952 | int lenn, lenp, ret; |
| 3953 | xmlChar *str; |
| 3954 | |
| 3955 | if (exec == NULL) |
| 3956 | return(-1); |
| 3957 | if (exec->comp == NULL) |
| 3958 | return(-1); |
| 3959 | if (exec->status != 0) |
| 3960 | return(exec->status); |
| 3961 | |
| 3962 | if (value2 == NULL) |
| 3963 | return(xmlRegExecPushString(exec, value, data)); |
| 3964 | |
| 3965 | lenn = strlen((char *) value2); |
| 3966 | lenp = strlen((char *) value); |
| 3967 | |
| 3968 | if (150 < lenn + lenp + 2) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3969 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3970 | if (str == NULL) { |
| 3971 | exec->status = -1; |
| 3972 | return(-1); |
| 3973 | } |
| 3974 | } else { |
| 3975 | str = buf; |
| 3976 | } |
| 3977 | memcpy(&str[0], value, lenp); |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3978 | str[lenp] = XML_REG_STRING_SEPARATOR; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3979 | memcpy(&str[lenp + 1], value2, lenn); |
| 3980 | str[lenn + lenp + 1] = 0; |
| 3981 | |
| 3982 | if (exec->comp->compact != NULL) |
| 3983 | ret = xmlRegCompactPushString(exec, exec->comp, str, data); |
| 3984 | else |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3985 | ret = xmlRegExecPushStringInternal(exec, str, data, 1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3986 | |
| 3987 | if (str != buf) |
Daniel Veillard | 0b1ff14 | 2005-12-28 21:13:33 +0000 | [diff] [blame] | 3988 | xmlFree(str); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3989 | return(ret); |
| 3990 | } |
| 3991 | |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3992 | /** |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 3993 | * xmlRegExecGetValues: |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3994 | * @exec: a regexp execution context |
| 3995 | * @err: error extraction or normal one |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3996 | * @nbval: pointer to the number of accepted values IN/OUT |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3997 | * @nbneg: return number of negative transitions |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3998 | * @values: pointer to the array of acceptable values |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3999 | * @terminal: return value if this was a terminal state |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4000 | * |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4001 | * Extract informations from the regexp execution, internal routine to |
| 4002 | * implement xmlRegExecNextValues() and xmlRegExecErrInfo() |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4003 | * |
| 4004 | * Returns: 0 in case of success or -1 in case of error. |
| 4005 | */ |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4006 | static int |
| 4007 | xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err, |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4008 | int *nbval, int *nbneg, |
| 4009 | xmlChar **values, int *terminal) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4010 | int maxval; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4011 | int nb = 0; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4012 | |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4013 | if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) || |
| 4014 | (values == NULL) || (*nbval <= 0)) |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4015 | return(-1); |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4016 | |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4017 | maxval = *nbval; |
| 4018 | *nbval = 0; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4019 | *nbneg = 0; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4020 | if ((exec->comp != NULL) && (exec->comp->compact != NULL)) { |
| 4021 | xmlRegexpPtr comp; |
| 4022 | int target, i, state; |
| 4023 | |
| 4024 | comp = exec->comp; |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4025 | |
| 4026 | if (err) { |
| 4027 | if (exec->errStateNo == -1) return(-1); |
| 4028 | state = exec->errStateNo; |
| 4029 | } else { |
| 4030 | state = exec->index; |
| 4031 | } |
| 4032 | if (terminal != NULL) { |
| 4033 | if (comp->compact[state * (comp->nbstrings + 1)] == |
| 4034 | XML_REGEXP_FINAL_STATE) |
| 4035 | *terminal = 1; |
| 4036 | else |
| 4037 | *terminal = 0; |
| 4038 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4039 | for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4040 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4041 | if ((target > 0) && (target <= comp->nbstates) && |
| 4042 | (comp->compact[(target - 1) * (comp->nbstrings + 1)] != |
| 4043 | XML_REGEXP_SINK_STATE)) { |
| 4044 | values[nb++] = comp->stringMap[i]; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4045 | (*nbval)++; |
| 4046 | } |
| 4047 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4048 | for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) { |
| 4049 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
| 4050 | if ((target > 0) && (target <= comp->nbstates) && |
| 4051 | (comp->compact[(target - 1) * (comp->nbstrings + 1)] == |
| 4052 | XML_REGEXP_SINK_STATE)) { |
| 4053 | values[nb++] = comp->stringMap[i]; |
| 4054 | (*nbneg)++; |
| 4055 | } |
| 4056 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4057 | } else { |
| 4058 | int transno; |
| 4059 | xmlRegTransPtr trans; |
| 4060 | xmlRegAtomPtr atom; |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4061 | xmlRegStatePtr state; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4062 | |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4063 | if (terminal != NULL) { |
| 4064 | if (exec->state->type == XML_REGEXP_FINAL_STATE) |
| 4065 | *terminal = 1; |
| 4066 | else |
| 4067 | *terminal = 0; |
| 4068 | } |
| 4069 | |
| 4070 | if (err) { |
| 4071 | if (exec->errState == NULL) return(-1); |
| 4072 | state = exec->errState; |
| 4073 | } else { |
| 4074 | if (exec->state == NULL) return(-1); |
| 4075 | state = exec->state; |
| 4076 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4077 | for (transno = 0; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4078 | (transno < state->nbTrans) && (nb < maxval); |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4079 | transno++) { |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4080 | trans = &state->trans[transno]; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4081 | if (trans->to < 0) |
| 4082 | continue; |
| 4083 | atom = trans->atom; |
| 4084 | if ((atom == NULL) || (atom->valuep == NULL)) |
| 4085 | continue; |
| 4086 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4087 | /* this should not be reached but ... */ |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4088 | TODO; |
| 4089 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4090 | /* this should not be reached but ... */ |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4091 | TODO; |
| 4092 | } else if (trans->counter >= 0) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 4093 | xmlRegCounterPtr counter = NULL; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4094 | int count; |
| 4095 | |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4096 | if (err) |
| 4097 | count = exec->errCounts[trans->counter]; |
| 4098 | else |
| 4099 | count = exec->counts[trans->counter]; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 4100 | if (exec->comp != NULL) |
| 4101 | counter = &exec->comp->counters[trans->counter]; |
| 4102 | if ((counter == NULL) || (count < counter->max)) { |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 4103 | if (atom->neg) |
| 4104 | values[nb++] = (xmlChar *) atom->valuep2; |
| 4105 | else |
| 4106 | values[nb++] = (xmlChar *) atom->valuep; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4107 | (*nbval)++; |
| 4108 | } |
| 4109 | } else { |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4110 | if ((exec->comp->states[trans->to] != NULL) && |
| 4111 | (exec->comp->states[trans->to]->type != |
| 4112 | XML_REGEXP_SINK_STATE)) { |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 4113 | if (atom->neg) |
| 4114 | values[nb++] = (xmlChar *) atom->valuep2; |
| 4115 | else |
| 4116 | values[nb++] = (xmlChar *) atom->valuep; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4117 | (*nbval)++; |
| 4118 | } |
| 4119 | } |
| 4120 | } |
| 4121 | for (transno = 0; |
| 4122 | (transno < state->nbTrans) && (nb < maxval); |
| 4123 | transno++) { |
| 4124 | trans = &state->trans[transno]; |
| 4125 | if (trans->to < 0) |
| 4126 | continue; |
| 4127 | atom = trans->atom; |
| 4128 | if ((atom == NULL) || (atom->valuep == NULL)) |
| 4129 | continue; |
| 4130 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
| 4131 | continue; |
| 4132 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
| 4133 | continue; |
| 4134 | } else if (trans->counter >= 0) { |
| 4135 | continue; |
| 4136 | } else { |
| 4137 | if ((exec->comp->states[trans->to] != NULL) && |
| 4138 | (exec->comp->states[trans->to]->type == |
| 4139 | XML_REGEXP_SINK_STATE)) { |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 4140 | if (atom->neg) |
| 4141 | values[nb++] = (xmlChar *) atom->valuep2; |
| 4142 | else |
| 4143 | values[nb++] = (xmlChar *) atom->valuep; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4144 | (*nbneg)++; |
| 4145 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4146 | } |
| 4147 | } |
| 4148 | } |
| 4149 | return(0); |
| 4150 | } |
| 4151 | |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4152 | /** |
| 4153 | * xmlRegExecNextValues: |
| 4154 | * @exec: a regexp execution context |
| 4155 | * @nbval: pointer to the number of accepted values IN/OUT |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4156 | * @nbneg: return number of negative transitions |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4157 | * @values: pointer to the array of acceptable values |
| 4158 | * @terminal: return value if this was a terminal state |
| 4159 | * |
| 4160 | * Extract informations from the regexp execution, |
| 4161 | * the parameter @values must point to an array of @nbval string pointers |
| 4162 | * on return nbval will contain the number of possible strings in that |
| 4163 | * state and the @values array will be updated with them. The string values |
| 4164 | * returned will be freed with the @exec context and don't need to be |
| 4165 | * deallocated. |
| 4166 | * |
| 4167 | * Returns: 0 in case of success or -1 in case of error. |
| 4168 | */ |
| 4169 | int |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4170 | xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg, |
| 4171 | xmlChar **values, int *terminal) { |
| 4172 | return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal)); |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
| 4175 | /** |
| 4176 | * xmlRegExecErrInfo: |
| 4177 | * @exec: a regexp execution context generating an error |
| 4178 | * @string: return value for the error string |
| 4179 | * @nbval: pointer to the number of accepted values IN/OUT |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4180 | * @nbneg: return number of negative transitions |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4181 | * @values: pointer to the array of acceptable values |
| 4182 | * @terminal: return value if this was a terminal state |
| 4183 | * |
| 4184 | * Extract error informations from the regexp execution, the parameter |
| 4185 | * @string will be updated with the value pushed and not accepted, |
| 4186 | * the parameter @values must point to an array of @nbval string pointers |
| 4187 | * on return nbval will contain the number of possible strings in that |
| 4188 | * state and the @values array will be updated with them. The string values |
| 4189 | * returned will be freed with the @exec context and don't need to be |
| 4190 | * deallocated. |
| 4191 | * |
| 4192 | * Returns: 0 in case of success or -1 in case of error. |
| 4193 | */ |
| 4194 | int |
| 4195 | xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string, |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4196 | int *nbval, int *nbneg, xmlChar **values, int *terminal) { |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4197 | if (exec == NULL) |
| 4198 | return(-1); |
| 4199 | if (string != NULL) { |
| 4200 | if (exec->status != 0) |
| 4201 | *string = exec->errString; |
| 4202 | else |
| 4203 | *string = NULL; |
| 4204 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4205 | return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal)); |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4206 | } |
| 4207 | |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4208 | #ifdef DEBUG_ERR |
| 4209 | static void testerr(xmlRegExecCtxtPtr exec) { |
| 4210 | const xmlChar *string; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4211 | xmlChar *values[5]; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4212 | int nb = 5; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4213 | int nbneg; |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4214 | int terminal; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4215 | xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal); |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4216 | } |
| 4217 | #endif |
| 4218 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4219 | #if 0 |
| 4220 | static int |
| 4221 | xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) { |
| 4222 | xmlRegTransPtr trans; |
| 4223 | xmlRegAtomPtr atom; |
| 4224 | int ret; |
| 4225 | int codepoint, len; |
| 4226 | |
| 4227 | if (exec == NULL) |
| 4228 | return(-1); |
| 4229 | if (exec->status != 0) |
| 4230 | return(exec->status); |
| 4231 | |
| 4232 | while ((exec->status == 0) && |
| 4233 | ((exec->inputString[exec->index] != 0) || |
| 4234 | (exec->state->type != XML_REGEXP_FINAL_STATE))) { |
| 4235 | |
| 4236 | /* |
| 4237 | * End of input on non-terminal state, rollback, however we may |
| 4238 | * still have epsilon like transition for counted transitions |
| 4239 | * on counters, in that case don't break too early. |
| 4240 | */ |
| 4241 | if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) |
| 4242 | goto rollback; |
| 4243 | |
| 4244 | exec->transcount = 0; |
| 4245 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
| 4246 | trans = &exec->state->trans[exec->transno]; |
| 4247 | if (trans->to < 0) |
| 4248 | continue; |
| 4249 | atom = trans->atom; |
| 4250 | ret = 0; |
| 4251 | if (trans->count >= 0) { |
| 4252 | int count; |
| 4253 | xmlRegCounterPtr counter; |
| 4254 | |
| 4255 | /* |
| 4256 | * A counted transition. |
| 4257 | */ |
| 4258 | |
| 4259 | count = exec->counts[trans->count]; |
| 4260 | counter = &exec->comp->counters[trans->count]; |
| 4261 | #ifdef DEBUG_REGEXP_EXEC |
| 4262 | printf("testing count %d: val %d, min %d, max %d\n", |
| 4263 | trans->count, count, counter->min, counter->max); |
| 4264 | #endif |
| 4265 | ret = ((count >= counter->min) && (count <= counter->max)); |
| 4266 | } else if (atom == NULL) { |
| 4267 | fprintf(stderr, "epsilon transition left at runtime\n"); |
| 4268 | exec->status = -2; |
| 4269 | break; |
| 4270 | } else if (exec->inputString[exec->index] != 0) { |
| 4271 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len); |
| 4272 | ret = xmlRegCheckCharacter(atom, codepoint); |
| 4273 | if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) { |
| 4274 | xmlRegStatePtr to = exec->comp->states[trans->to]; |
| 4275 | |
| 4276 | /* |
| 4277 | * this is a multiple input sequence |
| 4278 | */ |
| 4279 | if (exec->state->nbTrans > exec->transno + 1) { |
| 4280 | xmlFARegExecSave(exec); |
| 4281 | } |
| 4282 | exec->transcount = 1; |
| 4283 | do { |
| 4284 | /* |
| 4285 | * Try to progress as much as possible on the input |
| 4286 | */ |
| 4287 | if (exec->transcount == atom->max) { |
| 4288 | break; |
| 4289 | } |
| 4290 | exec->index += len; |
| 4291 | /* |
| 4292 | * End of input: stop here |
| 4293 | */ |
| 4294 | if (exec->inputString[exec->index] == 0) { |
| 4295 | exec->index -= len; |
| 4296 | break; |
| 4297 | } |
| 4298 | if (exec->transcount >= atom->min) { |
| 4299 | int transno = exec->transno; |
| 4300 | xmlRegStatePtr state = exec->state; |
| 4301 | |
| 4302 | /* |
| 4303 | * The transition is acceptable save it |
| 4304 | */ |
| 4305 | exec->transno = -1; /* trick */ |
| 4306 | exec->state = to; |
| 4307 | xmlFARegExecSave(exec); |
| 4308 | exec->transno = transno; |
| 4309 | exec->state = state; |
| 4310 | } |
| 4311 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), |
| 4312 | len); |
| 4313 | ret = xmlRegCheckCharacter(atom, codepoint); |
| 4314 | exec->transcount++; |
| 4315 | } while (ret == 1); |
| 4316 | if (exec->transcount < atom->min) |
| 4317 | ret = 0; |
| 4318 | |
| 4319 | /* |
| 4320 | * If the last check failed but one transition was found |
| 4321 | * possible, rollback |
| 4322 | */ |
| 4323 | if (ret < 0) |
| 4324 | ret = 0; |
| 4325 | if (ret == 0) { |
| 4326 | goto rollback; |
| 4327 | } |
| 4328 | } |
| 4329 | } |
| 4330 | if (ret == 1) { |
| 4331 | if (exec->state->nbTrans > exec->transno + 1) { |
| 4332 | xmlFARegExecSave(exec); |
| 4333 | } |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 4334 | /* |
| 4335 | * restart count for expressions like this ((abc){2})* |
| 4336 | */ |
| 4337 | if (trans->count >= 0) { |
| 4338 | #ifdef DEBUG_REGEXP_EXEC |
| 4339 | printf("Reset count %d\n", trans->count); |
| 4340 | #endif |
| 4341 | exec->counts[trans->count] = 0; |
| 4342 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4343 | if (trans->counter >= 0) { |
| 4344 | #ifdef DEBUG_REGEXP_EXEC |
| 4345 | printf("Increasing count %d\n", trans->counter); |
| 4346 | #endif |
| 4347 | exec->counts[trans->counter]++; |
| 4348 | } |
| 4349 | #ifdef DEBUG_REGEXP_EXEC |
| 4350 | printf("entering state %d\n", trans->to); |
| 4351 | #endif |
| 4352 | exec->state = exec->comp->states[trans->to]; |
| 4353 | exec->transno = 0; |
| 4354 | if (trans->atom != NULL) { |
| 4355 | exec->index += len; |
| 4356 | } |
| 4357 | goto progress; |
| 4358 | } else if (ret < 0) { |
| 4359 | exec->status = -4; |
| 4360 | break; |
| 4361 | } |
| 4362 | } |
| 4363 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
| 4364 | rollback: |
| 4365 | /* |
| 4366 | * Failed to find a way out |
| 4367 | */ |
| 4368 | exec->determinist = 0; |
| 4369 | xmlFARegExecRollBack(exec); |
| 4370 | } |
| 4371 | progress: |
| 4372 | continue; |
| 4373 | } |
| 4374 | } |
| 4375 | #endif |
| 4376 | /************************************************************************ |
| 4377 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 4378 | * Parser for the Schemas Datatype Regular Expressions * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4379 | * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs * |
| 4380 | * * |
| 4381 | ************************************************************************/ |
| 4382 | |
| 4383 | /** |
| 4384 | * xmlFAIsChar: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4385 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4386 | * |
| 4387 | * [10] Char ::= [^.\?*+()|#x5B#x5D] |
| 4388 | */ |
| 4389 | static int |
| 4390 | xmlFAIsChar(xmlRegParserCtxtPtr ctxt) { |
| 4391 | int cur; |
| 4392 | int len; |
| 4393 | |
| 4394 | cur = CUR_SCHAR(ctxt->cur, len); |
| 4395 | if ((cur == '.') || (cur == '\\') || (cur == '?') || |
| 4396 | (cur == '*') || (cur == '+') || (cur == '(') || |
| 4397 | (cur == ')') || (cur == '|') || (cur == 0x5B) || |
| 4398 | (cur == 0x5D) || (cur == 0)) |
| 4399 | return(-1); |
| 4400 | return(cur); |
| 4401 | } |
| 4402 | |
| 4403 | /** |
| 4404 | * xmlFAParseCharProp: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4405 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4406 | * |
| 4407 | * [27] charProp ::= IsCategory | IsBlock |
| 4408 | * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation | |
| 4409 | * Separators | Symbols | Others |
| 4410 | * [29] Letters ::= 'L' [ultmo]? |
| 4411 | * [30] Marks ::= 'M' [nce]? |
| 4412 | * [31] Numbers ::= 'N' [dlo]? |
| 4413 | * [32] Punctuation ::= 'P' [cdseifo]? |
| 4414 | * [33] Separators ::= 'Z' [slp]? |
| 4415 | * [34] Symbols ::= 'S' [mcko]? |
| 4416 | * [35] Others ::= 'C' [cfon]? |
| 4417 | * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+ |
| 4418 | */ |
| 4419 | static void |
| 4420 | xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) { |
| 4421 | int cur; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 4422 | xmlRegAtomType type = (xmlRegAtomType) 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4423 | xmlChar *blockName = NULL; |
| 4424 | |
| 4425 | cur = CUR; |
| 4426 | if (cur == 'L') { |
| 4427 | NEXT; |
| 4428 | cur = CUR; |
| 4429 | if (cur == 'u') { |
| 4430 | NEXT; |
| 4431 | type = XML_REGEXP_LETTER_UPPERCASE; |
| 4432 | } else if (cur == 'l') { |
| 4433 | NEXT; |
| 4434 | type = XML_REGEXP_LETTER_LOWERCASE; |
| 4435 | } else if (cur == 't') { |
| 4436 | NEXT; |
| 4437 | type = XML_REGEXP_LETTER_TITLECASE; |
| 4438 | } else if (cur == 'm') { |
| 4439 | NEXT; |
| 4440 | type = XML_REGEXP_LETTER_MODIFIER; |
| 4441 | } else if (cur == 'o') { |
| 4442 | NEXT; |
| 4443 | type = XML_REGEXP_LETTER_OTHERS; |
| 4444 | } else { |
| 4445 | type = XML_REGEXP_LETTER; |
| 4446 | } |
| 4447 | } else if (cur == 'M') { |
| 4448 | NEXT; |
| 4449 | cur = CUR; |
| 4450 | if (cur == 'n') { |
| 4451 | NEXT; |
| 4452 | /* nonspacing */ |
| 4453 | type = XML_REGEXP_MARK_NONSPACING; |
| 4454 | } else if (cur == 'c') { |
| 4455 | NEXT; |
| 4456 | /* spacing combining */ |
| 4457 | type = XML_REGEXP_MARK_SPACECOMBINING; |
| 4458 | } else if (cur == 'e') { |
| 4459 | NEXT; |
| 4460 | /* enclosing */ |
| 4461 | type = XML_REGEXP_MARK_ENCLOSING; |
| 4462 | } else { |
| 4463 | /* all marks */ |
| 4464 | type = XML_REGEXP_MARK; |
| 4465 | } |
| 4466 | } else if (cur == 'N') { |
| 4467 | NEXT; |
| 4468 | cur = CUR; |
| 4469 | if (cur == 'd') { |
| 4470 | NEXT; |
| 4471 | /* digital */ |
| 4472 | type = XML_REGEXP_NUMBER_DECIMAL; |
| 4473 | } else if (cur == 'l') { |
| 4474 | NEXT; |
| 4475 | /* letter */ |
| 4476 | type = XML_REGEXP_NUMBER_LETTER; |
| 4477 | } else if (cur == 'o') { |
| 4478 | NEXT; |
| 4479 | /* other */ |
| 4480 | type = XML_REGEXP_NUMBER_OTHERS; |
| 4481 | } else { |
| 4482 | /* all numbers */ |
| 4483 | type = XML_REGEXP_NUMBER; |
| 4484 | } |
| 4485 | } else if (cur == 'P') { |
| 4486 | NEXT; |
| 4487 | cur = CUR; |
| 4488 | if (cur == 'c') { |
| 4489 | NEXT; |
| 4490 | /* connector */ |
| 4491 | type = XML_REGEXP_PUNCT_CONNECTOR; |
| 4492 | } else if (cur == 'd') { |
| 4493 | NEXT; |
| 4494 | /* dash */ |
| 4495 | type = XML_REGEXP_PUNCT_DASH; |
| 4496 | } else if (cur == 's') { |
| 4497 | NEXT; |
| 4498 | /* open */ |
| 4499 | type = XML_REGEXP_PUNCT_OPEN; |
| 4500 | } else if (cur == 'e') { |
| 4501 | NEXT; |
| 4502 | /* close */ |
| 4503 | type = XML_REGEXP_PUNCT_CLOSE; |
| 4504 | } else if (cur == 'i') { |
| 4505 | NEXT; |
| 4506 | /* initial quote */ |
| 4507 | type = XML_REGEXP_PUNCT_INITQUOTE; |
| 4508 | } else if (cur == 'f') { |
| 4509 | NEXT; |
| 4510 | /* final quote */ |
| 4511 | type = XML_REGEXP_PUNCT_FINQUOTE; |
| 4512 | } else if (cur == 'o') { |
| 4513 | NEXT; |
| 4514 | /* other */ |
| 4515 | type = XML_REGEXP_PUNCT_OTHERS; |
| 4516 | } else { |
| 4517 | /* all punctuation */ |
| 4518 | type = XML_REGEXP_PUNCT; |
| 4519 | } |
| 4520 | } else if (cur == 'Z') { |
| 4521 | NEXT; |
| 4522 | cur = CUR; |
| 4523 | if (cur == 's') { |
| 4524 | NEXT; |
| 4525 | /* space */ |
| 4526 | type = XML_REGEXP_SEPAR_SPACE; |
| 4527 | } else if (cur == 'l') { |
| 4528 | NEXT; |
| 4529 | /* line */ |
| 4530 | type = XML_REGEXP_SEPAR_LINE; |
| 4531 | } else if (cur == 'p') { |
| 4532 | NEXT; |
| 4533 | /* paragraph */ |
| 4534 | type = XML_REGEXP_SEPAR_PARA; |
| 4535 | } else { |
| 4536 | /* all separators */ |
| 4537 | type = XML_REGEXP_SEPAR; |
| 4538 | } |
| 4539 | } else if (cur == 'S') { |
| 4540 | NEXT; |
| 4541 | cur = CUR; |
| 4542 | if (cur == 'm') { |
| 4543 | NEXT; |
| 4544 | type = XML_REGEXP_SYMBOL_MATH; |
| 4545 | /* math */ |
| 4546 | } else if (cur == 'c') { |
| 4547 | NEXT; |
| 4548 | type = XML_REGEXP_SYMBOL_CURRENCY; |
| 4549 | /* currency */ |
| 4550 | } else if (cur == 'k') { |
| 4551 | NEXT; |
| 4552 | type = XML_REGEXP_SYMBOL_MODIFIER; |
| 4553 | /* modifiers */ |
| 4554 | } else if (cur == 'o') { |
| 4555 | NEXT; |
| 4556 | type = XML_REGEXP_SYMBOL_OTHERS; |
| 4557 | /* other */ |
| 4558 | } else { |
| 4559 | /* all symbols */ |
| 4560 | type = XML_REGEXP_SYMBOL; |
| 4561 | } |
| 4562 | } else if (cur == 'C') { |
| 4563 | NEXT; |
| 4564 | cur = CUR; |
| 4565 | if (cur == 'c') { |
| 4566 | NEXT; |
| 4567 | /* control */ |
| 4568 | type = XML_REGEXP_OTHER_CONTROL; |
| 4569 | } else if (cur == 'f') { |
| 4570 | NEXT; |
| 4571 | /* format */ |
| 4572 | type = XML_REGEXP_OTHER_FORMAT; |
| 4573 | } else if (cur == 'o') { |
| 4574 | NEXT; |
| 4575 | /* private use */ |
| 4576 | type = XML_REGEXP_OTHER_PRIVATE; |
| 4577 | } else if (cur == 'n') { |
| 4578 | NEXT; |
| 4579 | /* not assigned */ |
| 4580 | type = XML_REGEXP_OTHER_NA; |
| 4581 | } else { |
| 4582 | /* all others */ |
| 4583 | type = XML_REGEXP_OTHER; |
| 4584 | } |
| 4585 | } else if (cur == 'I') { |
| 4586 | const xmlChar *start; |
| 4587 | NEXT; |
| 4588 | cur = CUR; |
| 4589 | if (cur != 's') { |
| 4590 | ERROR("IsXXXX expected"); |
| 4591 | return; |
| 4592 | } |
| 4593 | NEXT; |
| 4594 | start = ctxt->cur; |
| 4595 | cur = CUR; |
| 4596 | if (((cur >= 'a') && (cur <= 'z')) || |
| 4597 | ((cur >= 'A') && (cur <= 'Z')) || |
| 4598 | ((cur >= '0') && (cur <= '9')) || |
| 4599 | (cur == 0x2D)) { |
| 4600 | NEXT; |
| 4601 | cur = CUR; |
| 4602 | while (((cur >= 'a') && (cur <= 'z')) || |
| 4603 | ((cur >= 'A') && (cur <= 'Z')) || |
| 4604 | ((cur >= '0') && (cur <= '9')) || |
| 4605 | (cur == 0x2D)) { |
| 4606 | NEXT; |
| 4607 | cur = CUR; |
| 4608 | } |
| 4609 | } |
| 4610 | type = XML_REGEXP_BLOCK_NAME; |
| 4611 | blockName = xmlStrndup(start, ctxt->cur - start); |
| 4612 | } else { |
| 4613 | ERROR("Unknown char property"); |
| 4614 | return; |
| 4615 | } |
| 4616 | if (ctxt->atom == NULL) { |
| 4617 | ctxt->atom = xmlRegNewAtom(ctxt, type); |
| 4618 | if (ctxt->atom != NULL) |
| 4619 | ctxt->atom->valuep = blockName; |
| 4620 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 4621 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4622 | type, 0, 0, blockName); |
| 4623 | } |
| 4624 | } |
| 4625 | |
| 4626 | /** |
| 4627 | * xmlFAParseCharClassEsc: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4628 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4629 | * |
| 4630 | * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc ) |
| 4631 | * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E] |
| 4632 | * [25] catEsc ::= '\p{' charProp '}' |
| 4633 | * [26] complEsc ::= '\P{' charProp '}' |
| 4634 | * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW]) |
| 4635 | */ |
| 4636 | static void |
| 4637 | xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) { |
| 4638 | int cur; |
| 4639 | |
| 4640 | if (CUR == '.') { |
| 4641 | if (ctxt->atom == NULL) { |
| 4642 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR); |
| 4643 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 4644 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4645 | XML_REGEXP_ANYCHAR, 0, 0, NULL); |
| 4646 | } |
| 4647 | NEXT; |
| 4648 | return; |
| 4649 | } |
| 4650 | if (CUR != '\\') { |
| 4651 | ERROR("Escaped sequence: expecting \\"); |
| 4652 | return; |
| 4653 | } |
| 4654 | NEXT; |
| 4655 | cur = CUR; |
| 4656 | if (cur == 'p') { |
| 4657 | NEXT; |
| 4658 | if (CUR != '{') { |
| 4659 | ERROR("Expecting '{'"); |
| 4660 | return; |
| 4661 | } |
| 4662 | NEXT; |
| 4663 | xmlFAParseCharProp(ctxt); |
| 4664 | if (CUR != '}') { |
| 4665 | ERROR("Expecting '}'"); |
| 4666 | return; |
| 4667 | } |
| 4668 | NEXT; |
| 4669 | } else if (cur == 'P') { |
| 4670 | NEXT; |
| 4671 | if (CUR != '{') { |
| 4672 | ERROR("Expecting '{'"); |
| 4673 | return; |
| 4674 | } |
| 4675 | NEXT; |
| 4676 | xmlFAParseCharProp(ctxt); |
| 4677 | ctxt->atom->neg = 1; |
| 4678 | if (CUR != '}') { |
| 4679 | ERROR("Expecting '}'"); |
| 4680 | return; |
| 4681 | } |
| 4682 | NEXT; |
| 4683 | } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') || |
| 4684 | (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') || |
| 4685 | (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') || |
| 4686 | (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) || |
| 4687 | (cur == 0x5E)) { |
| 4688 | if (ctxt->atom == NULL) { |
| 4689 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL); |
Daniel Veillard | 99c394d | 2005-07-14 12:58:49 +0000 | [diff] [blame] | 4690 | if (ctxt->atom != NULL) { |
| 4691 | switch (cur) { |
| 4692 | case 'n': |
| 4693 | ctxt->atom->codepoint = '\n'; |
| 4694 | break; |
| 4695 | case 'r': |
| 4696 | ctxt->atom->codepoint = '\r'; |
| 4697 | break; |
| 4698 | case 't': |
| 4699 | ctxt->atom->codepoint = '\t'; |
| 4700 | break; |
| 4701 | default: |
| 4702 | ctxt->atom->codepoint = cur; |
| 4703 | } |
| 4704 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4705 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 4706 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4707 | XML_REGEXP_CHARVAL, cur, cur, NULL); |
| 4708 | } |
| 4709 | NEXT; |
| 4710 | } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') || |
| 4711 | (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') || |
| 4712 | (cur == 'w') || (cur == 'W')) { |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 4713 | xmlRegAtomType type = XML_REGEXP_ANYSPACE; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4714 | |
| 4715 | switch (cur) { |
| 4716 | case 's': |
| 4717 | type = XML_REGEXP_ANYSPACE; |
| 4718 | break; |
| 4719 | case 'S': |
| 4720 | type = XML_REGEXP_NOTSPACE; |
| 4721 | break; |
| 4722 | case 'i': |
| 4723 | type = XML_REGEXP_INITNAME; |
| 4724 | break; |
| 4725 | case 'I': |
| 4726 | type = XML_REGEXP_NOTINITNAME; |
| 4727 | break; |
| 4728 | case 'c': |
| 4729 | type = XML_REGEXP_NAMECHAR; |
| 4730 | break; |
| 4731 | case 'C': |
| 4732 | type = XML_REGEXP_NOTNAMECHAR; |
| 4733 | break; |
| 4734 | case 'd': |
| 4735 | type = XML_REGEXP_DECIMAL; |
| 4736 | break; |
| 4737 | case 'D': |
| 4738 | type = XML_REGEXP_NOTDECIMAL; |
| 4739 | break; |
| 4740 | case 'w': |
| 4741 | type = XML_REGEXP_REALCHAR; |
| 4742 | break; |
| 4743 | case 'W': |
| 4744 | type = XML_REGEXP_NOTREALCHAR; |
| 4745 | break; |
| 4746 | } |
| 4747 | NEXT; |
| 4748 | if (ctxt->atom == NULL) { |
| 4749 | ctxt->atom = xmlRegNewAtom(ctxt, type); |
| 4750 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 4751 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4752 | type, 0, 0, NULL); |
| 4753 | } |
Daniel Veillard | cb4284e | 2007-04-25 13:55:20 +0000 | [diff] [blame^] | 4754 | } else { |
| 4755 | ERROR("Wrong escape sequence, misuse of character '\\'"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4756 | } |
| 4757 | } |
| 4758 | |
| 4759 | /** |
| 4760 | * xmlFAParseCharRef: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4761 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4762 | * |
| 4763 | * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' ) |
| 4764 | */ |
| 4765 | static int |
| 4766 | xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) { |
| 4767 | int ret = 0, cur; |
| 4768 | |
| 4769 | if ((CUR != '&') || (NXT(1) != '#')) |
| 4770 | return(-1); |
| 4771 | NEXT; |
| 4772 | NEXT; |
| 4773 | cur = CUR; |
| 4774 | if (cur == 'x') { |
| 4775 | NEXT; |
| 4776 | cur = CUR; |
| 4777 | if (((cur >= '0') && (cur <= '9')) || |
| 4778 | ((cur >= 'a') && (cur <= 'f')) || |
| 4779 | ((cur >= 'A') && (cur <= 'F'))) { |
| 4780 | while (((cur >= '0') && (cur <= '9')) || |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 4781 | ((cur >= 'a') && (cur <= 'f')) || |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4782 | ((cur >= 'A') && (cur <= 'F'))) { |
| 4783 | if ((cur >= '0') && (cur <= '9')) |
| 4784 | ret = ret * 16 + cur - '0'; |
| 4785 | else if ((cur >= 'a') && (cur <= 'f')) |
| 4786 | ret = ret * 16 + 10 + (cur - 'a'); |
| 4787 | else |
| 4788 | ret = ret * 16 + 10 + (cur - 'A'); |
| 4789 | NEXT; |
| 4790 | cur = CUR; |
| 4791 | } |
| 4792 | } else { |
| 4793 | ERROR("Char ref: expecting [0-9A-F]"); |
| 4794 | return(-1); |
| 4795 | } |
| 4796 | } else { |
| 4797 | if ((cur >= '0') && (cur <= '9')) { |
| 4798 | while ((cur >= '0') && (cur <= '9')) { |
| 4799 | ret = ret * 10 + cur - '0'; |
| 4800 | NEXT; |
| 4801 | cur = CUR; |
| 4802 | } |
| 4803 | } else { |
| 4804 | ERROR("Char ref: expecting [0-9]"); |
| 4805 | return(-1); |
| 4806 | } |
| 4807 | } |
| 4808 | if (cur != ';') { |
| 4809 | ERROR("Char ref: expecting ';'"); |
| 4810 | return(-1); |
| 4811 | } else { |
| 4812 | NEXT; |
| 4813 | } |
| 4814 | return(ret); |
| 4815 | } |
| 4816 | |
| 4817 | /** |
| 4818 | * xmlFAParseCharRange: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4819 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4820 | * |
| 4821 | * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash |
| 4822 | * [18] seRange ::= charOrEsc '-' charOrEsc |
| 4823 | * [20] charOrEsc ::= XmlChar | SingleCharEsc |
| 4824 | * [21] XmlChar ::= [^\#x2D#x5B#x5D] |
| 4825 | * [22] XmlCharIncDash ::= [^\#x5B#x5D] |
| 4826 | */ |
| 4827 | static void |
| 4828 | xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) { |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4829 | int cur, len; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4830 | int start = -1; |
| 4831 | int end = -1; |
| 4832 | |
Daniel Veillard | 777737e | 2006-10-17 21:23:17 +0000 | [diff] [blame] | 4833 | if (CUR == '\0') { |
| 4834 | ERROR("Expecting ']'"); |
| 4835 | return; |
| 4836 | } |
| 4837 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4838 | if ((CUR == '&') && (NXT(1) == '#')) { |
| 4839 | end = start = xmlFAParseCharRef(ctxt); |
| 4840 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4841 | XML_REGEXP_CHARVAL, start, end, NULL); |
| 4842 | return; |
| 4843 | } |
| 4844 | cur = CUR; |
| 4845 | if (cur == '\\') { |
| 4846 | NEXT; |
| 4847 | cur = CUR; |
| 4848 | switch (cur) { |
| 4849 | case 'n': start = 0xA; break; |
| 4850 | case 'r': start = 0xD; break; |
| 4851 | case 't': start = 0x9; break; |
| 4852 | case '\\': case '|': case '.': case '-': case '^': case '?': |
| 4853 | case '*': case '+': case '{': case '}': case '(': case ')': |
| 4854 | case '[': case ']': |
| 4855 | start = cur; break; |
| 4856 | default: |
| 4857 | ERROR("Invalid escape value"); |
| 4858 | return; |
| 4859 | } |
| 4860 | end = start; |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4861 | len = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4862 | } else if ((cur != 0x5B) && (cur != 0x5D)) { |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4863 | end = start = CUR_SCHAR(ctxt->cur, len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4864 | } else { |
| 4865 | ERROR("Expecting a char range"); |
| 4866 | return; |
| 4867 | } |
William M. Brack | a9cbf28 | 2007-03-21 13:16:33 +0000 | [diff] [blame] | 4868 | /* |
| 4869 | * Since we are "inside" a range, we can assume ctxt->cur is past |
| 4870 | * the start of ctxt->string, and PREV should be safe |
| 4871 | */ |
| 4872 | if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) { |
| 4873 | NEXTL(len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4874 | return; |
| 4875 | } |
William M. Brack | a9cbf28 | 2007-03-21 13:16:33 +0000 | [diff] [blame] | 4876 | NEXTL(len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4877 | cur = CUR; |
William M. Brack | 10f1ef4 | 2004-03-20 14:51:25 +0000 | [diff] [blame] | 4878 | if ((cur != '-') || (NXT(1) == ']')) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4879 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4880 | XML_REGEXP_CHARVAL, start, end, NULL); |
| 4881 | return; |
| 4882 | } |
| 4883 | NEXT; |
| 4884 | cur = CUR; |
| 4885 | if (cur == '\\') { |
| 4886 | NEXT; |
| 4887 | cur = CUR; |
| 4888 | switch (cur) { |
| 4889 | case 'n': end = 0xA; break; |
| 4890 | case 'r': end = 0xD; break; |
| 4891 | case 't': end = 0x9; break; |
| 4892 | case '\\': case '|': case '.': case '-': case '^': case '?': |
| 4893 | case '*': case '+': case '{': case '}': case '(': case ')': |
| 4894 | case '[': case ']': |
| 4895 | end = cur; break; |
| 4896 | default: |
| 4897 | ERROR("Invalid escape value"); |
| 4898 | return; |
| 4899 | } |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4900 | len = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4901 | } else if ((cur != 0x5B) && (cur != 0x5D)) { |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4902 | end = CUR_SCHAR(ctxt->cur, len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4903 | } else { |
| 4904 | ERROR("Expecting the end of a char range"); |
| 4905 | return; |
| 4906 | } |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4907 | NEXTL(len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4908 | /* TODO check that the values are acceptable character ranges for XML */ |
| 4909 | if (end < start) { |
| 4910 | ERROR("End of range is before start of range"); |
| 4911 | } else { |
| 4912 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4913 | XML_REGEXP_CHARVAL, start, end, NULL); |
| 4914 | } |
| 4915 | return; |
| 4916 | } |
| 4917 | |
| 4918 | /** |
| 4919 | * xmlFAParsePosCharGroup: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4920 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4921 | * |
| 4922 | * [14] posCharGroup ::= ( charRange | charClassEsc )+ |
| 4923 | */ |
| 4924 | static void |
| 4925 | xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) { |
| 4926 | do { |
| 4927 | if ((CUR == '\\') || (CUR == '.')) { |
| 4928 | xmlFAParseCharClassEsc(ctxt); |
| 4929 | } else { |
| 4930 | xmlFAParseCharRange(ctxt); |
| 4931 | } |
| 4932 | } while ((CUR != ']') && (CUR != '^') && (CUR != '-') && |
Daniel Veillard | 777737e | 2006-10-17 21:23:17 +0000 | [diff] [blame] | 4933 | (CUR != 0) && (ctxt->error == 0)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4934 | } |
| 4935 | |
| 4936 | /** |
| 4937 | * xmlFAParseCharGroup: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4938 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4939 | * |
| 4940 | * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub |
| 4941 | * [15] negCharGroup ::= '^' posCharGroup |
| 4942 | * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr |
| 4943 | * [12] charClassExpr ::= '[' charGroup ']' |
| 4944 | */ |
| 4945 | static void |
| 4946 | xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) { |
| 4947 | int n = ctxt->neg; |
| 4948 | while ((CUR != ']') && (ctxt->error == 0)) { |
| 4949 | if (CUR == '^') { |
| 4950 | int neg = ctxt->neg; |
| 4951 | |
| 4952 | NEXT; |
| 4953 | ctxt->neg = !ctxt->neg; |
| 4954 | xmlFAParsePosCharGroup(ctxt); |
| 4955 | ctxt->neg = neg; |
William M. Brack | 10f1ef4 | 2004-03-20 14:51:25 +0000 | [diff] [blame] | 4956 | } else if ((CUR == '-') && (NXT(1) == '[')) { |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 4957 | int neg = ctxt->neg; |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 4958 | ctxt->neg = 2; |
William M. Brack | 10f1ef4 | 2004-03-20 14:51:25 +0000 | [diff] [blame] | 4959 | NEXT; /* eat the '-' */ |
| 4960 | NEXT; /* eat the '[' */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4961 | xmlFAParseCharGroup(ctxt); |
| 4962 | if (CUR == ']') { |
| 4963 | NEXT; |
| 4964 | } else { |
| 4965 | ERROR("charClassExpr: ']' expected"); |
| 4966 | break; |
| 4967 | } |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 4968 | ctxt->neg = neg; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4969 | break; |
| 4970 | } else if (CUR != ']') { |
| 4971 | xmlFAParsePosCharGroup(ctxt); |
| 4972 | } |
| 4973 | } |
| 4974 | ctxt->neg = n; |
| 4975 | } |
| 4976 | |
| 4977 | /** |
| 4978 | * xmlFAParseCharClass: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4979 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4980 | * |
| 4981 | * [11] charClass ::= charClassEsc | charClassExpr |
| 4982 | * [12] charClassExpr ::= '[' charGroup ']' |
| 4983 | */ |
| 4984 | static void |
| 4985 | xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) { |
| 4986 | if (CUR == '[') { |
| 4987 | NEXT; |
| 4988 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES); |
| 4989 | if (ctxt->atom == NULL) |
| 4990 | return; |
| 4991 | xmlFAParseCharGroup(ctxt); |
| 4992 | if (CUR == ']') { |
| 4993 | NEXT; |
| 4994 | } else { |
| 4995 | ERROR("xmlFAParseCharClass: ']' expected"); |
| 4996 | } |
| 4997 | } else { |
| 4998 | xmlFAParseCharClassEsc(ctxt); |
| 4999 | } |
| 5000 | } |
| 5001 | |
| 5002 | /** |
| 5003 | * xmlFAParseQuantExact: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5004 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5005 | * |
| 5006 | * [8] QuantExact ::= [0-9]+ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5007 | * |
| 5008 | * Returns 0 if success or -1 in case of error |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5009 | */ |
| 5010 | static int |
| 5011 | xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) { |
| 5012 | int ret = 0; |
| 5013 | int ok = 0; |
| 5014 | |
| 5015 | while ((CUR >= '0') && (CUR <= '9')) { |
| 5016 | ret = ret * 10 + (CUR - '0'); |
| 5017 | ok = 1; |
| 5018 | NEXT; |
| 5019 | } |
| 5020 | if (ok != 1) { |
| 5021 | return(-1); |
| 5022 | } |
| 5023 | return(ret); |
| 5024 | } |
| 5025 | |
| 5026 | /** |
| 5027 | * xmlFAParseQuantifier: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5028 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5029 | * |
| 5030 | * [4] quantifier ::= [?*+] | ( '{' quantity '}' ) |
| 5031 | * [5] quantity ::= quantRange | quantMin | QuantExact |
| 5032 | * [6] quantRange ::= QuantExact ',' QuantExact |
| 5033 | * [7] quantMin ::= QuantExact ',' |
| 5034 | * [8] QuantExact ::= [0-9]+ |
| 5035 | */ |
| 5036 | static int |
| 5037 | xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) { |
| 5038 | int cur; |
| 5039 | |
| 5040 | cur = CUR; |
| 5041 | if ((cur == '?') || (cur == '*') || (cur == '+')) { |
| 5042 | if (ctxt->atom != NULL) { |
| 5043 | if (cur == '?') |
| 5044 | ctxt->atom->quant = XML_REGEXP_QUANT_OPT; |
| 5045 | else if (cur == '*') |
| 5046 | ctxt->atom->quant = XML_REGEXP_QUANT_MULT; |
| 5047 | else if (cur == '+') |
| 5048 | ctxt->atom->quant = XML_REGEXP_QUANT_PLUS; |
| 5049 | } |
| 5050 | NEXT; |
| 5051 | return(1); |
| 5052 | } |
| 5053 | if (cur == '{') { |
| 5054 | int min = 0, max = 0; |
| 5055 | |
| 5056 | NEXT; |
| 5057 | cur = xmlFAParseQuantExact(ctxt); |
| 5058 | if (cur >= 0) |
| 5059 | min = cur; |
| 5060 | if (CUR == ',') { |
| 5061 | NEXT; |
Daniel Veillard | ebe48c6 | 2003-12-03 12:12:27 +0000 | [diff] [blame] | 5062 | if (CUR == '}') |
| 5063 | max = INT_MAX; |
| 5064 | else { |
| 5065 | cur = xmlFAParseQuantExact(ctxt); |
| 5066 | if (cur >= 0) |
| 5067 | max = cur; |
| 5068 | else { |
| 5069 | ERROR("Improper quantifier"); |
| 5070 | } |
| 5071 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5072 | } |
| 5073 | if (CUR == '}') { |
| 5074 | NEXT; |
| 5075 | } else { |
| 5076 | ERROR("Unterminated quantifier"); |
| 5077 | } |
| 5078 | if (max == 0) |
| 5079 | max = min; |
| 5080 | if (ctxt->atom != NULL) { |
| 5081 | ctxt->atom->quant = XML_REGEXP_QUANT_RANGE; |
| 5082 | ctxt->atom->min = min; |
| 5083 | ctxt->atom->max = max; |
| 5084 | } |
| 5085 | return(1); |
| 5086 | } |
| 5087 | return(0); |
| 5088 | } |
| 5089 | |
| 5090 | /** |
| 5091 | * xmlFAParseAtom: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5092 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5093 | * |
| 5094 | * [9] atom ::= Char | charClass | ( '(' regExp ')' ) |
| 5095 | */ |
| 5096 | static int |
| 5097 | xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) { |
| 5098 | int codepoint, len; |
| 5099 | |
| 5100 | codepoint = xmlFAIsChar(ctxt); |
| 5101 | if (codepoint > 0) { |
| 5102 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL); |
| 5103 | if (ctxt->atom == NULL) |
| 5104 | return(-1); |
| 5105 | codepoint = CUR_SCHAR(ctxt->cur, len); |
| 5106 | ctxt->atom->codepoint = codepoint; |
| 5107 | NEXTL(len); |
| 5108 | return(1); |
| 5109 | } else if (CUR == '|') { |
| 5110 | return(0); |
| 5111 | } else if (CUR == 0) { |
| 5112 | return(0); |
| 5113 | } else if (CUR == ')') { |
| 5114 | return(0); |
| 5115 | } else if (CUR == '(') { |
| 5116 | xmlRegStatePtr start, oldend; |
| 5117 | |
| 5118 | NEXT; |
| 5119 | xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL); |
| 5120 | start = ctxt->state; |
| 5121 | oldend = ctxt->end; |
| 5122 | ctxt->end = NULL; |
| 5123 | ctxt->atom = NULL; |
| 5124 | xmlFAParseRegExp(ctxt, 0); |
| 5125 | if (CUR == ')') { |
| 5126 | NEXT; |
| 5127 | } else { |
| 5128 | ERROR("xmlFAParseAtom: expecting ')'"); |
| 5129 | } |
| 5130 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG); |
| 5131 | if (ctxt->atom == NULL) |
| 5132 | return(-1); |
| 5133 | ctxt->atom->start = start; |
| 5134 | ctxt->atom->stop = ctxt->state; |
| 5135 | ctxt->end = oldend; |
| 5136 | return(1); |
| 5137 | } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) { |
| 5138 | xmlFAParseCharClass(ctxt); |
| 5139 | return(1); |
| 5140 | } |
| 5141 | return(0); |
| 5142 | } |
| 5143 | |
| 5144 | /** |
| 5145 | * xmlFAParsePiece: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5146 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5147 | * |
| 5148 | * [3] piece ::= atom quantifier? |
| 5149 | */ |
| 5150 | static int |
| 5151 | xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) { |
| 5152 | int ret; |
| 5153 | |
| 5154 | ctxt->atom = NULL; |
| 5155 | ret = xmlFAParseAtom(ctxt); |
| 5156 | if (ret == 0) |
| 5157 | return(0); |
| 5158 | if (ctxt->atom == NULL) { |
| 5159 | ERROR("internal: no atom generated"); |
| 5160 | } |
| 5161 | xmlFAParseQuantifier(ctxt); |
| 5162 | return(1); |
| 5163 | } |
| 5164 | |
| 5165 | /** |
| 5166 | * xmlFAParseBranch: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5167 | * @ctxt: a regexp parser context |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5168 | * @to: optional target to the end of the branch |
| 5169 | * |
| 5170 | * @to is used to optimize by removing duplicate path in automata |
| 5171 | * in expressions like (a|b)(c|d) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5172 | * |
| 5173 | * [2] branch ::= piece* |
| 5174 | */ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5175 | static int |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5176 | xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5177 | xmlRegStatePtr previous; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5178 | int ret; |
| 5179 | |
| 5180 | previous = ctxt->state; |
| 5181 | ret = xmlFAParsePiece(ctxt); |
| 5182 | if (ret != 0) { |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5183 | if (xmlFAGenerateTransitions(ctxt, previous, |
| 5184 | (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0) |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5185 | return(-1); |
| 5186 | previous = ctxt->state; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5187 | ctxt->atom = NULL; |
| 5188 | } |
| 5189 | while ((ret != 0) && (ctxt->error == 0)) { |
| 5190 | ret = xmlFAParsePiece(ctxt); |
| 5191 | if (ret != 0) { |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5192 | if (xmlFAGenerateTransitions(ctxt, previous, |
| 5193 | (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5194 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5195 | previous = ctxt->state; |
| 5196 | ctxt->atom = NULL; |
| 5197 | } |
| 5198 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5199 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5200 | } |
| 5201 | |
| 5202 | /** |
| 5203 | * xmlFAParseRegExp: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5204 | * @ctxt: a regexp parser context |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5205 | * @top: is this the top-level expression ? |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5206 | * |
| 5207 | * [1] regExp ::= branch ( '|' branch )* |
| 5208 | */ |
| 5209 | static void |
| 5210 | xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) { |
Daniel Veillard | c7e3cc4 | 2004-09-28 12:33:52 +0000 | [diff] [blame] | 5211 | xmlRegStatePtr start, end; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5212 | |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5213 | /* if not top start should have been generated by an epsilon trans */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5214 | start = ctxt->state; |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5215 | ctxt->end = NULL; |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5216 | xmlFAParseBranch(ctxt, NULL); |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5217 | if (top) { |
| 5218 | #ifdef DEBUG_REGEXP_GRAPH |
| 5219 | printf("State %d is final\n", ctxt->state->no); |
| 5220 | #endif |
| 5221 | ctxt->state->type = XML_REGEXP_FINAL_STATE; |
| 5222 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5223 | if (CUR != '|') { |
| 5224 | ctxt->end = ctxt->state; |
| 5225 | return; |
| 5226 | } |
| 5227 | end = ctxt->state; |
| 5228 | while ((CUR == '|') && (ctxt->error == 0)) { |
| 5229 | NEXT; |
| 5230 | ctxt->state = start; |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5231 | ctxt->end = NULL; |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5232 | xmlFAParseBranch(ctxt, end); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5233 | } |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5234 | if (!top) { |
| 5235 | ctxt->state = end; |
| 5236 | ctxt->end = end; |
| 5237 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5238 | } |
| 5239 | |
| 5240 | /************************************************************************ |
| 5241 | * * |
| 5242 | * The basic API * |
| 5243 | * * |
| 5244 | ************************************************************************/ |
| 5245 | |
| 5246 | /** |
| 5247 | * xmlRegexpPrint: |
| 5248 | * @output: the file for the output debug |
| 5249 | * @regexp: the compiled regexp |
| 5250 | * |
| 5251 | * Print the content of the compiled regular expression |
| 5252 | */ |
| 5253 | void |
| 5254 | xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) { |
| 5255 | int i; |
| 5256 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5257 | if (output == NULL) |
| 5258 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5259 | fprintf(output, " regexp: "); |
| 5260 | if (regexp == NULL) { |
| 5261 | fprintf(output, "NULL\n"); |
| 5262 | return; |
| 5263 | } |
| 5264 | fprintf(output, "'%s' ", regexp->string); |
| 5265 | fprintf(output, "\n"); |
| 5266 | fprintf(output, "%d atoms:\n", regexp->nbAtoms); |
| 5267 | for (i = 0;i < regexp->nbAtoms; i++) { |
| 5268 | fprintf(output, " %02d ", i); |
| 5269 | xmlRegPrintAtom(output, regexp->atoms[i]); |
| 5270 | } |
| 5271 | fprintf(output, "%d states:", regexp->nbStates); |
| 5272 | fprintf(output, "\n"); |
| 5273 | for (i = 0;i < regexp->nbStates; i++) { |
| 5274 | xmlRegPrintState(output, regexp->states[i]); |
| 5275 | } |
| 5276 | fprintf(output, "%d counters:\n", regexp->nbCounters); |
| 5277 | for (i = 0;i < regexp->nbCounters; i++) { |
| 5278 | fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min, |
| 5279 | regexp->counters[i].max); |
| 5280 | } |
| 5281 | } |
| 5282 | |
| 5283 | /** |
| 5284 | * xmlRegexpCompile: |
| 5285 | * @regexp: a regular expression string |
| 5286 | * |
| 5287 | * Parses a regular expression conforming to XML Schemas Part 2 Datatype |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5288 | * Appendix F and builds an automata suitable for testing strings against |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5289 | * that regular expression |
| 5290 | * |
| 5291 | * Returns the compiled expression or NULL in case of error |
| 5292 | */ |
| 5293 | xmlRegexpPtr |
| 5294 | xmlRegexpCompile(const xmlChar *regexp) { |
| 5295 | xmlRegexpPtr ret; |
| 5296 | xmlRegParserCtxtPtr ctxt; |
| 5297 | |
| 5298 | ctxt = xmlRegNewParserCtxt(regexp); |
| 5299 | if (ctxt == NULL) |
| 5300 | return(NULL); |
| 5301 | |
| 5302 | /* initialize the parser */ |
| 5303 | ctxt->end = NULL; |
| 5304 | ctxt->start = ctxt->state = xmlRegNewState(ctxt); |
| 5305 | xmlRegStatePush(ctxt, ctxt->start); |
| 5306 | |
| 5307 | /* parse the expression building an automata */ |
| 5308 | xmlFAParseRegExp(ctxt, 1); |
| 5309 | if (CUR != 0) { |
| 5310 | ERROR("xmlFAParseRegExp: extra characters"); |
| 5311 | } |
Daniel Veillard | cb4284e | 2007-04-25 13:55:20 +0000 | [diff] [blame^] | 5312 | if (ctxt->error != 0) { |
| 5313 | xmlRegFreeParserCtxt(ctxt); |
| 5314 | return(NULL); |
| 5315 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5316 | ctxt->end = ctxt->state; |
| 5317 | ctxt->start->type = XML_REGEXP_START_STATE; |
| 5318 | ctxt->end->type = XML_REGEXP_FINAL_STATE; |
| 5319 | |
| 5320 | /* remove the Epsilon except for counted transitions */ |
| 5321 | xmlFAEliminateEpsilonTransitions(ctxt); |
| 5322 | |
| 5323 | |
| 5324 | if (ctxt->error != 0) { |
| 5325 | xmlRegFreeParserCtxt(ctxt); |
| 5326 | return(NULL); |
| 5327 | } |
| 5328 | ret = xmlRegEpxFromParse(ctxt); |
| 5329 | xmlRegFreeParserCtxt(ctxt); |
| 5330 | return(ret); |
| 5331 | } |
| 5332 | |
| 5333 | /** |
| 5334 | * xmlRegexpExec: |
| 5335 | * @comp: the compiled regular expression |
| 5336 | * @content: the value to check against the regular expression |
| 5337 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5338 | * Check if the regular expression generates the value |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5339 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5340 | * Returns 1 if it matches, 0 if not and a negative value in case of error |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5341 | */ |
| 5342 | int |
| 5343 | xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) { |
| 5344 | if ((comp == NULL) || (content == NULL)) |
| 5345 | return(-1); |
| 5346 | return(xmlFARegExec(comp, content)); |
| 5347 | } |
| 5348 | |
| 5349 | /** |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5350 | * xmlRegexpIsDeterminist: |
| 5351 | * @comp: the compiled regular expression |
| 5352 | * |
| 5353 | * Check if the regular expression is determinist |
| 5354 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5355 | * Returns 1 if it yes, 0 if not and a negative value in case of error |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5356 | */ |
| 5357 | int |
| 5358 | xmlRegexpIsDeterminist(xmlRegexpPtr comp) { |
| 5359 | xmlAutomataPtr am; |
| 5360 | int ret; |
| 5361 | |
| 5362 | if (comp == NULL) |
| 5363 | return(-1); |
| 5364 | if (comp->determinist != -1) |
| 5365 | return(comp->determinist); |
| 5366 | |
| 5367 | am = xmlNewAutomata(); |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5368 | if (am->states != NULL) { |
| 5369 | int i; |
| 5370 | |
| 5371 | for (i = 0;i < am->nbStates;i++) |
| 5372 | xmlRegFreeState(am->states[i]); |
| 5373 | xmlFree(am->states); |
| 5374 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5375 | am->nbAtoms = comp->nbAtoms; |
| 5376 | am->atoms = comp->atoms; |
| 5377 | am->nbStates = comp->nbStates; |
| 5378 | am->states = comp->states; |
| 5379 | am->determinist = -1; |
| 5380 | ret = xmlFAComputesDeterminism(am); |
| 5381 | am->atoms = NULL; |
| 5382 | am->states = NULL; |
| 5383 | xmlFreeAutomata(am); |
| 5384 | return(ret); |
| 5385 | } |
| 5386 | |
| 5387 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5388 | * xmlRegFreeRegexp: |
| 5389 | * @regexp: the regexp |
| 5390 | * |
| 5391 | * Free a regexp |
| 5392 | */ |
| 5393 | void |
| 5394 | xmlRegFreeRegexp(xmlRegexpPtr regexp) { |
| 5395 | int i; |
| 5396 | if (regexp == NULL) |
| 5397 | return; |
| 5398 | |
| 5399 | if (regexp->string != NULL) |
| 5400 | xmlFree(regexp->string); |
| 5401 | if (regexp->states != NULL) { |
| 5402 | for (i = 0;i < regexp->nbStates;i++) |
| 5403 | xmlRegFreeState(regexp->states[i]); |
| 5404 | xmlFree(regexp->states); |
| 5405 | } |
| 5406 | if (regexp->atoms != NULL) { |
| 5407 | for (i = 0;i < regexp->nbAtoms;i++) |
| 5408 | xmlRegFreeAtom(regexp->atoms[i]); |
| 5409 | xmlFree(regexp->atoms); |
| 5410 | } |
| 5411 | if (regexp->counters != NULL) |
| 5412 | xmlFree(regexp->counters); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5413 | if (regexp->compact != NULL) |
| 5414 | xmlFree(regexp->compact); |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 5415 | if (regexp->transdata != NULL) |
| 5416 | xmlFree(regexp->transdata); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5417 | if (regexp->stringMap != NULL) { |
| 5418 | for (i = 0; i < regexp->nbstrings;i++) |
| 5419 | xmlFree(regexp->stringMap[i]); |
| 5420 | xmlFree(regexp->stringMap); |
| 5421 | } |
| 5422 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5423 | xmlFree(regexp); |
| 5424 | } |
| 5425 | |
| 5426 | #ifdef LIBXML_AUTOMATA_ENABLED |
| 5427 | /************************************************************************ |
| 5428 | * * |
| 5429 | * The Automata interface * |
| 5430 | * * |
| 5431 | ************************************************************************/ |
| 5432 | |
| 5433 | /** |
| 5434 | * xmlNewAutomata: |
| 5435 | * |
| 5436 | * Create a new automata |
| 5437 | * |
| 5438 | * Returns the new object or NULL in case of failure |
| 5439 | */ |
| 5440 | xmlAutomataPtr |
| 5441 | xmlNewAutomata(void) { |
| 5442 | xmlAutomataPtr ctxt; |
| 5443 | |
| 5444 | ctxt = xmlRegNewParserCtxt(NULL); |
| 5445 | if (ctxt == NULL) |
| 5446 | return(NULL); |
| 5447 | |
| 5448 | /* initialize the parser */ |
| 5449 | ctxt->end = NULL; |
| 5450 | ctxt->start = ctxt->state = xmlRegNewState(ctxt); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5451 | if (ctxt->start == NULL) { |
| 5452 | xmlFreeAutomata(ctxt); |
| 5453 | return(NULL); |
| 5454 | } |
Daniel Veillard | d027147 | 2006-01-02 10:22:02 +0000 | [diff] [blame] | 5455 | ctxt->start->type = XML_REGEXP_START_STATE; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5456 | if (xmlRegStatePush(ctxt, ctxt->start) < 0) { |
| 5457 | xmlRegFreeState(ctxt->start); |
| 5458 | xmlFreeAutomata(ctxt); |
| 5459 | return(NULL); |
| 5460 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5461 | |
| 5462 | return(ctxt); |
| 5463 | } |
| 5464 | |
| 5465 | /** |
| 5466 | * xmlFreeAutomata: |
| 5467 | * @am: an automata |
| 5468 | * |
| 5469 | * Free an automata |
| 5470 | */ |
| 5471 | void |
| 5472 | xmlFreeAutomata(xmlAutomataPtr am) { |
| 5473 | if (am == NULL) |
| 5474 | return; |
| 5475 | xmlRegFreeParserCtxt(am); |
| 5476 | } |
| 5477 | |
| 5478 | /** |
| 5479 | * xmlAutomataGetInitState: |
| 5480 | * @am: an automata |
| 5481 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5482 | * Initial state lookup |
| 5483 | * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5484 | * Returns the initial state of the automata |
| 5485 | */ |
| 5486 | xmlAutomataStatePtr |
| 5487 | xmlAutomataGetInitState(xmlAutomataPtr am) { |
| 5488 | if (am == NULL) |
| 5489 | return(NULL); |
| 5490 | return(am->start); |
| 5491 | } |
| 5492 | |
| 5493 | /** |
| 5494 | * xmlAutomataSetFinalState: |
| 5495 | * @am: an automata |
| 5496 | * @state: a state in this automata |
| 5497 | * |
| 5498 | * Makes that state a final state |
| 5499 | * |
| 5500 | * Returns 0 or -1 in case of error |
| 5501 | */ |
| 5502 | int |
| 5503 | xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) { |
| 5504 | if ((am == NULL) || (state == NULL)) |
| 5505 | return(-1); |
| 5506 | state->type = XML_REGEXP_FINAL_STATE; |
| 5507 | return(0); |
| 5508 | } |
| 5509 | |
| 5510 | /** |
| 5511 | * xmlAutomataNewTransition: |
| 5512 | * @am: an automata |
| 5513 | * @from: the starting point of the transition |
| 5514 | * @to: the target point of the transition or NULL |
| 5515 | * @token: the input string associated to that transition |
| 5516 | * @data: data passed to the callback function if the transition is activated |
| 5517 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5518 | * If @to is NULL, this creates first a new target state in the automata |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5519 | * and then adds a transition from the @from state to the target state |
| 5520 | * activated by the value of @token |
| 5521 | * |
| 5522 | * Returns the target state or NULL in case of error |
| 5523 | */ |
| 5524 | xmlAutomataStatePtr |
| 5525 | xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5526 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5527 | void *data) { |
| 5528 | xmlRegAtomPtr atom; |
| 5529 | |
| 5530 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5531 | return(NULL); |
| 5532 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5533 | if (atom == NULL) |
| 5534 | return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5535 | atom->data = data; |
| 5536 | if (atom == NULL) |
| 5537 | return(NULL); |
| 5538 | atom->valuep = xmlStrdup(token); |
| 5539 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5540 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
| 5541 | xmlRegFreeAtom(atom); |
| 5542 | return(NULL); |
| 5543 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5544 | if (to == NULL) |
| 5545 | return(am->state); |
| 5546 | return(to); |
| 5547 | } |
| 5548 | |
| 5549 | /** |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5550 | * xmlAutomataNewTransition2: |
| 5551 | * @am: an automata |
| 5552 | * @from: the starting point of the transition |
| 5553 | * @to: the target point of the transition or NULL |
| 5554 | * @token: the first input string associated to that transition |
| 5555 | * @token2: the second input string associated to that transition |
| 5556 | * @data: data passed to the callback function if the transition is activated |
| 5557 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5558 | * If @to is NULL, this creates first a new target state in the automata |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5559 | * and then adds a transition from the @from state to the target state |
| 5560 | * activated by the value of @token |
| 5561 | * |
| 5562 | * Returns the target state or NULL in case of error |
| 5563 | */ |
| 5564 | xmlAutomataStatePtr |
| 5565 | xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5566 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5567 | const xmlChar *token2, void *data) { |
| 5568 | xmlRegAtomPtr atom; |
| 5569 | |
| 5570 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5571 | return(NULL); |
| 5572 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5573 | if (atom == NULL) |
| 5574 | return(NULL); |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5575 | atom->data = data; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5576 | if ((token2 == NULL) || (*token2 == 0)) { |
| 5577 | atom->valuep = xmlStrdup(token); |
| 5578 | } else { |
| 5579 | int lenn, lenp; |
| 5580 | xmlChar *str; |
| 5581 | |
| 5582 | lenn = strlen((char *) token2); |
| 5583 | lenp = strlen((char *) token); |
| 5584 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 5585 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5586 | if (str == NULL) { |
| 5587 | xmlRegFreeAtom(atom); |
| 5588 | return(NULL); |
| 5589 | } |
| 5590 | memcpy(&str[0], token, lenp); |
| 5591 | str[lenp] = '|'; |
| 5592 | memcpy(&str[lenp + 1], token2, lenn); |
| 5593 | str[lenn + lenp + 1] = 0; |
| 5594 | |
| 5595 | atom->valuep = str; |
| 5596 | } |
| 5597 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5598 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
| 5599 | xmlRegFreeAtom(atom); |
| 5600 | return(NULL); |
| 5601 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5602 | if (to == NULL) |
| 5603 | return(am->state); |
| 5604 | return(to); |
| 5605 | } |
| 5606 | |
| 5607 | /** |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 5608 | * xmlAutomataNewNegTrans: |
| 5609 | * @am: an automata |
| 5610 | * @from: the starting point of the transition |
| 5611 | * @to: the target point of the transition or NULL |
| 5612 | * @token: the first input string associated to that transition |
| 5613 | * @token2: the second input string associated to that transition |
| 5614 | * @data: data passed to the callback function if the transition is activated |
| 5615 | * |
| 5616 | * If @to is NULL, this creates first a new target state in the automata |
| 5617 | * and then adds a transition from the @from state to the target state |
| 5618 | * activated by any value except (@token,@token2) |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 5619 | * Note that if @token2 is not NULL, then (X, NULL) won't match to follow |
| 5620 | # the semantic of XSD ##other |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 5621 | * |
| 5622 | * Returns the target state or NULL in case of error |
| 5623 | */ |
| 5624 | xmlAutomataStatePtr |
| 5625 | xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5626 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5627 | const xmlChar *token2, void *data) { |
| 5628 | xmlRegAtomPtr atom; |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 5629 | xmlChar err_msg[200]; |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 5630 | |
| 5631 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5632 | return(NULL); |
| 5633 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5634 | if (atom == NULL) |
| 5635 | return(NULL); |
| 5636 | atom->data = data; |
| 5637 | atom->neg = 1; |
| 5638 | if ((token2 == NULL) || (*token2 == 0)) { |
| 5639 | atom->valuep = xmlStrdup(token); |
| 5640 | } else { |
| 5641 | int lenn, lenp; |
| 5642 | xmlChar *str; |
| 5643 | |
| 5644 | lenn = strlen((char *) token2); |
| 5645 | lenp = strlen((char *) token); |
| 5646 | |
| 5647 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
| 5648 | if (str == NULL) { |
| 5649 | xmlRegFreeAtom(atom); |
| 5650 | return(NULL); |
| 5651 | } |
| 5652 | memcpy(&str[0], token, lenp); |
| 5653 | str[lenp] = '|'; |
| 5654 | memcpy(&str[lenp + 1], token2, lenn); |
| 5655 | str[lenn + lenp + 1] = 0; |
| 5656 | |
| 5657 | atom->valuep = str; |
| 5658 | } |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 5659 | snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep); |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 5660 | err_msg[199] = 0; |
| 5661 | atom->valuep2 = xmlStrdup(err_msg); |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 5662 | |
| 5663 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
| 5664 | xmlRegFreeAtom(atom); |
| 5665 | return(NULL); |
| 5666 | } |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 5667 | am->negs++; |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 5668 | if (to == NULL) |
| 5669 | return(am->state); |
| 5670 | return(to); |
| 5671 | } |
| 5672 | |
| 5673 | /** |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 5674 | * xmlAutomataNewCountTrans2: |
| 5675 | * @am: an automata |
| 5676 | * @from: the starting point of the transition |
| 5677 | * @to: the target point of the transition or NULL |
| 5678 | * @token: the input string associated to that transition |
| 5679 | * @token2: the second input string associated to that transition |
| 5680 | * @min: the minimum successive occurences of token |
| 5681 | * @max: the maximum successive occurences of token |
| 5682 | * @data: data associated to the transition |
| 5683 | * |
| 5684 | * If @to is NULL, this creates first a new target state in the automata |
| 5685 | * and then adds a transition from the @from state to the target state |
| 5686 | * activated by a succession of input of value @token and @token2 and |
| 5687 | * whose number is between @min and @max |
| 5688 | * |
| 5689 | * Returns the target state or NULL in case of error |
| 5690 | */ |
| 5691 | xmlAutomataStatePtr |
| 5692 | xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5693 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5694 | const xmlChar *token2, |
| 5695 | int min, int max, void *data) { |
| 5696 | xmlRegAtomPtr atom; |
| 5697 | int counter; |
| 5698 | |
| 5699 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5700 | return(NULL); |
| 5701 | if (min < 0) |
| 5702 | return(NULL); |
| 5703 | if ((max < min) || (max < 1)) |
| 5704 | return(NULL); |
| 5705 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5706 | if (atom == NULL) |
| 5707 | return(NULL); |
| 5708 | if ((token2 == NULL) || (*token2 == 0)) { |
| 5709 | atom->valuep = xmlStrdup(token); |
| 5710 | } else { |
| 5711 | int lenn, lenp; |
| 5712 | xmlChar *str; |
| 5713 | |
| 5714 | lenn = strlen((char *) token2); |
| 5715 | lenp = strlen((char *) token); |
| 5716 | |
| 5717 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
| 5718 | if (str == NULL) { |
| 5719 | xmlRegFreeAtom(atom); |
| 5720 | return(NULL); |
| 5721 | } |
| 5722 | memcpy(&str[0], token, lenp); |
| 5723 | str[lenp] = '|'; |
| 5724 | memcpy(&str[lenp + 1], token2, lenn); |
| 5725 | str[lenn + lenp + 1] = 0; |
| 5726 | |
| 5727 | atom->valuep = str; |
| 5728 | } |
| 5729 | atom->data = data; |
| 5730 | if (min == 0) |
| 5731 | atom->min = 1; |
| 5732 | else |
| 5733 | atom->min = min; |
| 5734 | atom->max = max; |
| 5735 | |
| 5736 | /* |
| 5737 | * associate a counter to the transition. |
| 5738 | */ |
| 5739 | counter = xmlRegGetCounter(am); |
| 5740 | am->counters[counter].min = min; |
| 5741 | am->counters[counter].max = max; |
| 5742 | |
| 5743 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 5744 | if (to == NULL) { |
| 5745 | to = xmlRegNewState(am); |
| 5746 | xmlRegStatePush(am, to); |
| 5747 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 5748 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 5749 | xmlRegAtomPush(am, atom); |
| 5750 | am->state = to; |
| 5751 | |
| 5752 | if (to == NULL) |
| 5753 | to = am->state; |
| 5754 | if (to == NULL) |
| 5755 | return(NULL); |
| 5756 | if (min == 0) |
| 5757 | xmlFAGenerateEpsilonTransition(am, from, to); |
| 5758 | return(to); |
| 5759 | } |
| 5760 | |
| 5761 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5762 | * xmlAutomataNewCountTrans: |
| 5763 | * @am: an automata |
| 5764 | * @from: the starting point of the transition |
| 5765 | * @to: the target point of the transition or NULL |
| 5766 | * @token: the input string associated to that transition |
| 5767 | * @min: the minimum successive occurences of token |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5768 | * @max: the maximum successive occurences of token |
| 5769 | * @data: data associated to the transition |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5770 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5771 | * If @to is NULL, this creates first a new target state in the automata |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5772 | * and then adds a transition from the @from state to the target state |
| 5773 | * activated by a succession of input of value @token and whose number |
| 5774 | * is between @min and @max |
| 5775 | * |
| 5776 | * Returns the target state or NULL in case of error |
| 5777 | */ |
| 5778 | xmlAutomataStatePtr |
| 5779 | xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5780 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5781 | int min, int max, void *data) { |
| 5782 | xmlRegAtomPtr atom; |
Daniel Veillard | 0ddb21c | 2004-02-12 12:43:49 +0000 | [diff] [blame] | 5783 | int counter; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5784 | |
| 5785 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5786 | return(NULL); |
| 5787 | if (min < 0) |
| 5788 | return(NULL); |
| 5789 | if ((max < min) || (max < 1)) |
| 5790 | return(NULL); |
| 5791 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5792 | if (atom == NULL) |
| 5793 | return(NULL); |
| 5794 | atom->valuep = xmlStrdup(token); |
| 5795 | atom->data = data; |
| 5796 | if (min == 0) |
| 5797 | atom->min = 1; |
| 5798 | else |
| 5799 | atom->min = min; |
| 5800 | atom->max = max; |
| 5801 | |
Daniel Veillard | 0ddb21c | 2004-02-12 12:43:49 +0000 | [diff] [blame] | 5802 | /* |
| 5803 | * associate a counter to the transition. |
| 5804 | */ |
| 5805 | counter = xmlRegGetCounter(am); |
| 5806 | am->counters[counter].min = min; |
| 5807 | am->counters[counter].max = max; |
| 5808 | |
| 5809 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 5810 | if (to == NULL) { |
| 5811 | to = xmlRegNewState(am); |
| 5812 | xmlRegStatePush(am, to); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5813 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 5814 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
Daniel Veillard | 0ddb21c | 2004-02-12 12:43:49 +0000 | [diff] [blame] | 5815 | xmlRegAtomPush(am, atom); |
| 5816 | am->state = to; |
| 5817 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5818 | if (to == NULL) |
| 5819 | to = am->state; |
| 5820 | if (to == NULL) |
| 5821 | return(NULL); |
| 5822 | if (min == 0) |
| 5823 | xmlFAGenerateEpsilonTransition(am, from, to); |
| 5824 | return(to); |
| 5825 | } |
| 5826 | |
| 5827 | /** |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 5828 | * xmlAutomataNewOnceTrans2: |
| 5829 | * @am: an automata |
| 5830 | * @from: the starting point of the transition |
| 5831 | * @to: the target point of the transition or NULL |
| 5832 | * @token: the input string associated to that transition |
| 5833 | * @token2: the second input string associated to that transition |
| 5834 | * @min: the minimum successive occurences of token |
| 5835 | * @max: the maximum successive occurences of token |
| 5836 | * @data: data associated to the transition |
| 5837 | * |
| 5838 | * If @to is NULL, this creates first a new target state in the automata |
| 5839 | * and then adds a transition from the @from state to the target state |
| 5840 | * activated by a succession of input of value @token and @token2 and whose |
| 5841 | * number is between @min and @max, moreover that transition can only be |
| 5842 | * crossed once. |
| 5843 | * |
| 5844 | * Returns the target state or NULL in case of error |
| 5845 | */ |
| 5846 | xmlAutomataStatePtr |
| 5847 | xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5848 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5849 | const xmlChar *token2, |
| 5850 | int min, int max, void *data) { |
| 5851 | xmlRegAtomPtr atom; |
| 5852 | int counter; |
| 5853 | |
| 5854 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5855 | return(NULL); |
| 5856 | if (min < 1) |
| 5857 | return(NULL); |
| 5858 | if ((max < min) || (max < 1)) |
| 5859 | return(NULL); |
| 5860 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5861 | if (atom == NULL) |
| 5862 | return(NULL); |
| 5863 | if ((token2 == NULL) || (*token2 == 0)) { |
| 5864 | atom->valuep = xmlStrdup(token); |
| 5865 | } else { |
| 5866 | int lenn, lenp; |
| 5867 | xmlChar *str; |
| 5868 | |
| 5869 | lenn = strlen((char *) token2); |
| 5870 | lenp = strlen((char *) token); |
| 5871 | |
| 5872 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
| 5873 | if (str == NULL) { |
| 5874 | xmlRegFreeAtom(atom); |
| 5875 | return(NULL); |
| 5876 | } |
| 5877 | memcpy(&str[0], token, lenp); |
| 5878 | str[lenp] = '|'; |
| 5879 | memcpy(&str[lenp + 1], token2, lenn); |
| 5880 | str[lenn + lenp + 1] = 0; |
| 5881 | |
| 5882 | atom->valuep = str; |
| 5883 | } |
| 5884 | atom->data = data; |
| 5885 | atom->quant = XML_REGEXP_QUANT_ONCEONLY; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5886 | atom->min = min; |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 5887 | atom->max = max; |
| 5888 | /* |
| 5889 | * associate a counter to the transition. |
| 5890 | */ |
| 5891 | counter = xmlRegGetCounter(am); |
| 5892 | am->counters[counter].min = 1; |
| 5893 | am->counters[counter].max = 1; |
| 5894 | |
| 5895 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 5896 | if (to == NULL) { |
| 5897 | to = xmlRegNewState(am); |
| 5898 | xmlRegStatePush(am, to); |
| 5899 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 5900 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 5901 | xmlRegAtomPush(am, atom); |
| 5902 | am->state = to; |
| 5903 | return(to); |
| 5904 | } |
| 5905 | |
| 5906 | |
| 5907 | |
| 5908 | /** |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5909 | * xmlAutomataNewOnceTrans: |
| 5910 | * @am: an automata |
| 5911 | * @from: the starting point of the transition |
| 5912 | * @to: the target point of the transition or NULL |
| 5913 | * @token: the input string associated to that transition |
| 5914 | * @min: the minimum successive occurences of token |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5915 | * @max: the maximum successive occurences of token |
| 5916 | * @data: data associated to the transition |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5917 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5918 | * If @to is NULL, this creates first a new target state in the automata |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5919 | * and then adds a transition from the @from state to the target state |
| 5920 | * activated by a succession of input of value @token and whose number |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5921 | * is between @min and @max, moreover that transition can only be crossed |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5922 | * once. |
| 5923 | * |
| 5924 | * Returns the target state or NULL in case of error |
| 5925 | */ |
| 5926 | xmlAutomataStatePtr |
| 5927 | xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5928 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5929 | int min, int max, void *data) { |
| 5930 | xmlRegAtomPtr atom; |
| 5931 | int counter; |
| 5932 | |
| 5933 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5934 | return(NULL); |
| 5935 | if (min < 1) |
| 5936 | return(NULL); |
| 5937 | if ((max < min) || (max < 1)) |
| 5938 | return(NULL); |
| 5939 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5940 | if (atom == NULL) |
| 5941 | return(NULL); |
| 5942 | atom->valuep = xmlStrdup(token); |
| 5943 | atom->data = data; |
| 5944 | atom->quant = XML_REGEXP_QUANT_ONCEONLY; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5945 | atom->min = min; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5946 | atom->max = max; |
| 5947 | /* |
| 5948 | * associate a counter to the transition. |
| 5949 | */ |
| 5950 | counter = xmlRegGetCounter(am); |
| 5951 | am->counters[counter].min = 1; |
| 5952 | am->counters[counter].max = 1; |
| 5953 | |
| 5954 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 5955 | if (to == NULL) { |
| 5956 | to = xmlRegNewState(am); |
| 5957 | xmlRegStatePush(am, to); |
| 5958 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 5959 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5960 | xmlRegAtomPush(am, atom); |
| 5961 | am->state = to; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5962 | return(to); |
| 5963 | } |
| 5964 | |
| 5965 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5966 | * xmlAutomataNewState: |
| 5967 | * @am: an automata |
| 5968 | * |
| 5969 | * Create a new disconnected state in the automata |
| 5970 | * |
| 5971 | * Returns the new state or NULL in case of error |
| 5972 | */ |
| 5973 | xmlAutomataStatePtr |
| 5974 | xmlAutomataNewState(xmlAutomataPtr am) { |
| 5975 | xmlAutomataStatePtr to; |
| 5976 | |
| 5977 | if (am == NULL) |
| 5978 | return(NULL); |
| 5979 | to = xmlRegNewState(am); |
| 5980 | xmlRegStatePush(am, to); |
| 5981 | return(to); |
| 5982 | } |
| 5983 | |
| 5984 | /** |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5985 | * xmlAutomataNewEpsilon: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5986 | * @am: an automata |
| 5987 | * @from: the starting point of the transition |
| 5988 | * @to: the target point of the transition or NULL |
| 5989 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5990 | * If @to is NULL, this creates first a new target state in the automata |
| 5991 | * and then adds an epsilon transition from the @from state to the |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5992 | * target state |
| 5993 | * |
| 5994 | * Returns the target state or NULL in case of error |
| 5995 | */ |
| 5996 | xmlAutomataStatePtr |
| 5997 | xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5998 | xmlAutomataStatePtr to) { |
| 5999 | if ((am == NULL) || (from == NULL)) |
| 6000 | return(NULL); |
| 6001 | xmlFAGenerateEpsilonTransition(am, from, to); |
| 6002 | if (to == NULL) |
| 6003 | return(am->state); |
| 6004 | return(to); |
| 6005 | } |
| 6006 | |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 6007 | /** |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 6008 | * xmlAutomataNewAllTrans: |
| 6009 | * @am: an automata |
| 6010 | * @from: the starting point of the transition |
| 6011 | * @to: the target point of the transition or NULL |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 6012 | * @lax: allow to transition if not all all transitions have been activated |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 6013 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 6014 | * If @to is NULL, this creates first a new target state in the automata |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 6015 | * and then adds a an ALL transition from the @from state to the |
| 6016 | * target state. That transition is an epsilon transition allowed only when |
| 6017 | * all transitions from the @from node have been activated. |
| 6018 | * |
| 6019 | * Returns the target state or NULL in case of error |
| 6020 | */ |
| 6021 | xmlAutomataStatePtr |
| 6022 | xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 6023 | xmlAutomataStatePtr to, int lax) { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 6024 | if ((am == NULL) || (from == NULL)) |
| 6025 | return(NULL); |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 6026 | xmlFAGenerateAllTransition(am, from, to, lax); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 6027 | if (to == NULL) |
| 6028 | return(am->state); |
| 6029 | return(to); |
| 6030 | } |
| 6031 | |
| 6032 | /** |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 6033 | * xmlAutomataNewCounter: |
| 6034 | * @am: an automata |
| 6035 | * @min: the minimal value on the counter |
| 6036 | * @max: the maximal value on the counter |
| 6037 | * |
| 6038 | * Create a new counter |
| 6039 | * |
| 6040 | * Returns the counter number or -1 in case of error |
| 6041 | */ |
| 6042 | int |
| 6043 | xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) { |
| 6044 | int ret; |
| 6045 | |
| 6046 | if (am == NULL) |
| 6047 | return(-1); |
| 6048 | |
| 6049 | ret = xmlRegGetCounter(am); |
| 6050 | if (ret < 0) |
| 6051 | return(-1); |
| 6052 | am->counters[ret].min = min; |
| 6053 | am->counters[ret].max = max; |
| 6054 | return(ret); |
| 6055 | } |
| 6056 | |
| 6057 | /** |
| 6058 | * xmlAutomataNewCountedTrans: |
| 6059 | * @am: an automata |
| 6060 | * @from: the starting point of the transition |
| 6061 | * @to: the target point of the transition or NULL |
| 6062 | * @counter: the counter associated to that transition |
| 6063 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 6064 | * If @to is NULL, this creates first a new target state in the automata |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 6065 | * and then adds an epsilon transition from the @from state to the target state |
| 6066 | * which will increment the counter provided |
| 6067 | * |
| 6068 | * Returns the target state or NULL in case of error |
| 6069 | */ |
| 6070 | xmlAutomataStatePtr |
| 6071 | xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 6072 | xmlAutomataStatePtr to, int counter) { |
| 6073 | if ((am == NULL) || (from == NULL) || (counter < 0)) |
| 6074 | return(NULL); |
| 6075 | xmlFAGenerateCountedEpsilonTransition(am, from, to, counter); |
| 6076 | if (to == NULL) |
| 6077 | return(am->state); |
| 6078 | return(to); |
| 6079 | } |
| 6080 | |
| 6081 | /** |
| 6082 | * xmlAutomataNewCounterTrans: |
| 6083 | * @am: an automata |
| 6084 | * @from: the starting point of the transition |
| 6085 | * @to: the target point of the transition or NULL |
| 6086 | * @counter: the counter associated to that transition |
| 6087 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 6088 | * If @to is NULL, this creates first a new target state in the automata |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 6089 | * and then adds an epsilon transition from the @from state to the target state |
| 6090 | * which will be allowed only if the counter is within the right range. |
| 6091 | * |
| 6092 | * Returns the target state or NULL in case of error |
| 6093 | */ |
| 6094 | xmlAutomataStatePtr |
| 6095 | xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 6096 | xmlAutomataStatePtr to, int counter) { |
| 6097 | if ((am == NULL) || (from == NULL) || (counter < 0)) |
| 6098 | return(NULL); |
| 6099 | xmlFAGenerateCountedTransition(am, from, to, counter); |
| 6100 | if (to == NULL) |
| 6101 | return(am->state); |
| 6102 | return(to); |
| 6103 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6104 | |
| 6105 | /** |
| 6106 | * xmlAutomataCompile: |
| 6107 | * @am: an automata |
| 6108 | * |
| 6109 | * Compile the automata into a Reg Exp ready for being executed. |
| 6110 | * The automata should be free after this point. |
| 6111 | * |
| 6112 | * Returns the compiled regexp or NULL in case of error |
| 6113 | */ |
| 6114 | xmlRegexpPtr |
| 6115 | xmlAutomataCompile(xmlAutomataPtr am) { |
| 6116 | xmlRegexpPtr ret; |
| 6117 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6118 | if ((am == NULL) || (am->error != 0)) return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6119 | xmlFAEliminateEpsilonTransitions(am); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 6120 | /* xmlFAComputesDeterminism(am); */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6121 | ret = xmlRegEpxFromParse(am); |
| 6122 | |
| 6123 | return(ret); |
| 6124 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 6125 | |
| 6126 | /** |
| 6127 | * xmlAutomataIsDeterminist: |
| 6128 | * @am: an automata |
| 6129 | * |
| 6130 | * Checks if an automata is determinist. |
| 6131 | * |
| 6132 | * Returns 1 if true, 0 if not, and -1 in case of error |
| 6133 | */ |
| 6134 | int |
| 6135 | xmlAutomataIsDeterminist(xmlAutomataPtr am) { |
| 6136 | int ret; |
| 6137 | |
| 6138 | if (am == NULL) |
| 6139 | return(-1); |
| 6140 | |
| 6141 | ret = xmlFAComputesDeterminism(am); |
| 6142 | return(ret); |
| 6143 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6144 | #endif /* LIBXML_AUTOMATA_ENABLED */ |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6145 | |
| 6146 | #ifdef LIBXML_EXPR_ENABLED |
| 6147 | /************************************************************************ |
| 6148 | * * |
| 6149 | * Formal Expression handling code * |
| 6150 | * * |
| 6151 | ************************************************************************/ |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6152 | /************************************************************************ |
| 6153 | * * |
| 6154 | * Expression handling context * |
| 6155 | * * |
| 6156 | ************************************************************************/ |
| 6157 | |
| 6158 | struct _xmlExpCtxt { |
| 6159 | xmlDictPtr dict; |
| 6160 | xmlExpNodePtr *table; |
| 6161 | int size; |
| 6162 | int nbElems; |
| 6163 | int nb_nodes; |
| 6164 | const char *expr; |
| 6165 | const char *cur; |
| 6166 | int nb_cons; |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6167 | int tabSize; |
| 6168 | }; |
| 6169 | |
| 6170 | /** |
| 6171 | * xmlExpNewCtxt: |
| 6172 | * @maxNodes: the maximum number of nodes |
| 6173 | * @dict: optional dictionnary to use internally |
| 6174 | * |
| 6175 | * Creates a new context for manipulating expressions |
| 6176 | * |
| 6177 | * Returns the context or NULL in case of error |
| 6178 | */ |
| 6179 | xmlExpCtxtPtr |
| 6180 | xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) { |
| 6181 | xmlExpCtxtPtr ret; |
| 6182 | int size = 256; |
| 6183 | |
| 6184 | if (maxNodes <= 4096) |
| 6185 | maxNodes = 4096; |
| 6186 | |
| 6187 | ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt)); |
| 6188 | if (ret == NULL) |
| 6189 | return(NULL); |
| 6190 | memset(ret, 0, sizeof(xmlExpCtxt)); |
| 6191 | ret->size = size; |
| 6192 | ret->nbElems = 0; |
| 6193 | ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr)); |
| 6194 | if (ret->table == NULL) { |
| 6195 | xmlFree(ret); |
| 6196 | return(NULL); |
| 6197 | } |
| 6198 | memset(ret->table, 0, size * sizeof(xmlExpNodePtr)); |
| 6199 | if (dict == NULL) { |
| 6200 | ret->dict = xmlDictCreate(); |
| 6201 | if (ret->dict == NULL) { |
| 6202 | xmlFree(ret->table); |
| 6203 | xmlFree(ret); |
| 6204 | return(NULL); |
| 6205 | } |
| 6206 | } else { |
| 6207 | ret->dict = dict; |
| 6208 | xmlDictReference(ret->dict); |
| 6209 | } |
| 6210 | return(ret); |
| 6211 | } |
| 6212 | |
| 6213 | /** |
| 6214 | * xmlExpFreeCtxt: |
| 6215 | * @ctxt: an expression context |
| 6216 | * |
| 6217 | * Free an expression context |
| 6218 | */ |
| 6219 | void |
| 6220 | xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) { |
| 6221 | if (ctxt == NULL) |
| 6222 | return; |
| 6223 | xmlDictFree(ctxt->dict); |
| 6224 | if (ctxt->table != NULL) |
| 6225 | xmlFree(ctxt->table); |
| 6226 | xmlFree(ctxt); |
| 6227 | } |
| 6228 | |
| 6229 | /************************************************************************ |
| 6230 | * * |
| 6231 | * Structure associated to an expression node * |
| 6232 | * * |
| 6233 | ************************************************************************/ |
Daniel Veillard | 465a000 | 2005-08-22 12:07:04 +0000 | [diff] [blame] | 6234 | #define MAX_NODES 10000 |
| 6235 | |
| 6236 | /* #define DEBUG_DERIV */ |
| 6237 | |
| 6238 | /* |
| 6239 | * TODO: |
| 6240 | * - Wildcards |
| 6241 | * - public API for creation |
| 6242 | * |
| 6243 | * Started |
| 6244 | * - regression testing |
| 6245 | * |
| 6246 | * Done |
| 6247 | * - split into module and test tool |
| 6248 | * - memleaks |
| 6249 | */ |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6250 | |
| 6251 | typedef enum { |
| 6252 | XML_EXP_NILABLE = (1 << 0) |
| 6253 | } xmlExpNodeInfo; |
| 6254 | |
| 6255 | #define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE) |
| 6256 | |
| 6257 | struct _xmlExpNode { |
| 6258 | unsigned char type;/* xmlExpNodeType */ |
| 6259 | unsigned char info;/* OR of xmlExpNodeInfo */ |
| 6260 | unsigned short key; /* the hash key */ |
| 6261 | unsigned int ref; /* The number of references */ |
| 6262 | int c_max; /* the maximum length it can consume */ |
| 6263 | xmlExpNodePtr exp_left; |
| 6264 | xmlExpNodePtr next;/* the next node in the hash table or free list */ |
| 6265 | union { |
| 6266 | struct { |
| 6267 | int f_min; |
| 6268 | int f_max; |
| 6269 | } count; |
| 6270 | struct { |
| 6271 | xmlExpNodePtr f_right; |
| 6272 | } children; |
| 6273 | const xmlChar *f_str; |
| 6274 | } field; |
| 6275 | }; |
| 6276 | |
| 6277 | #define exp_min field.count.f_min |
| 6278 | #define exp_max field.count.f_max |
| 6279 | /* #define exp_left field.children.f_left */ |
| 6280 | #define exp_right field.children.f_right |
| 6281 | #define exp_str field.f_str |
| 6282 | |
| 6283 | static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type); |
| 6284 | static xmlExpNode forbiddenExpNode = { |
| 6285 | XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}} |
| 6286 | }; |
| 6287 | xmlExpNodePtr forbiddenExp = &forbiddenExpNode; |
| 6288 | static xmlExpNode emptyExpNode = { |
| 6289 | XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}} |
| 6290 | }; |
| 6291 | xmlExpNodePtr emptyExp = &emptyExpNode; |
| 6292 | |
| 6293 | /************************************************************************ |
| 6294 | * * |
| 6295 | * The custom hash table for unicity and canonicalization * |
| 6296 | * of sub-expressions pointers * |
| 6297 | * * |
| 6298 | ************************************************************************/ |
| 6299 | /* |
| 6300 | * xmlExpHashNameComputeKey: |
| 6301 | * Calculate the hash key for a token |
| 6302 | */ |
| 6303 | static unsigned short |
| 6304 | xmlExpHashNameComputeKey(const xmlChar *name) { |
| 6305 | unsigned short value = 0L; |
| 6306 | char ch; |
| 6307 | |
| 6308 | if (name != NULL) { |
| 6309 | value += 30 * (*name); |
| 6310 | while ((ch = *name++) != 0) { |
| 6311 | value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch); |
| 6312 | } |
| 6313 | } |
| 6314 | return (value); |
| 6315 | } |
| 6316 | |
| 6317 | /* |
| 6318 | * xmlExpHashComputeKey: |
| 6319 | * Calculate the hash key for a compound expression |
| 6320 | */ |
| 6321 | static unsigned short |
| 6322 | xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left, |
| 6323 | xmlExpNodePtr right) { |
| 6324 | unsigned long value; |
| 6325 | unsigned short ret; |
| 6326 | |
| 6327 | switch (type) { |
| 6328 | case XML_EXP_SEQ: |
| 6329 | value = left->key; |
| 6330 | value += right->key; |
| 6331 | value *= 3; |
| 6332 | ret = (unsigned short) value; |
| 6333 | break; |
| 6334 | case XML_EXP_OR: |
| 6335 | value = left->key; |
| 6336 | value += right->key; |
| 6337 | value *= 7; |
| 6338 | ret = (unsigned short) value; |
| 6339 | break; |
| 6340 | case XML_EXP_COUNT: |
| 6341 | value = left->key; |
| 6342 | value += right->key; |
| 6343 | ret = (unsigned short) value; |
| 6344 | break; |
| 6345 | default: |
| 6346 | ret = 0; |
| 6347 | } |
| 6348 | return(ret); |
| 6349 | } |
| 6350 | |
| 6351 | |
| 6352 | static xmlExpNodePtr |
| 6353 | xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) { |
| 6354 | xmlExpNodePtr ret; |
| 6355 | |
| 6356 | if (ctxt->nb_nodes >= MAX_NODES) |
| 6357 | return(NULL); |
| 6358 | ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode)); |
| 6359 | if (ret == NULL) |
| 6360 | return(NULL); |
| 6361 | memset(ret, 0, sizeof(xmlExpNode)); |
| 6362 | ret->type = type; |
| 6363 | ret->next = NULL; |
| 6364 | ctxt->nb_nodes++; |
| 6365 | ctxt->nb_cons++; |
| 6366 | return(ret); |
| 6367 | } |
| 6368 | |
| 6369 | /** |
| 6370 | * xmlExpHashGetEntry: |
| 6371 | * @table: the hash table |
| 6372 | * |
| 6373 | * Get the unique entry from the hash table. The entry is created if |
| 6374 | * needed. @left and @right are consumed, i.e. their ref count will |
| 6375 | * be decremented by the operation. |
| 6376 | * |
| 6377 | * Returns the pointer or NULL in case of error |
| 6378 | */ |
| 6379 | static xmlExpNodePtr |
| 6380 | xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type, |
| 6381 | xmlExpNodePtr left, xmlExpNodePtr right, |
| 6382 | const xmlChar *name, int min, int max) { |
| 6383 | unsigned short kbase, key; |
| 6384 | xmlExpNodePtr entry; |
| 6385 | xmlExpNodePtr insert; |
| 6386 | |
| 6387 | if (ctxt == NULL) |
| 6388 | return(NULL); |
| 6389 | |
| 6390 | /* |
| 6391 | * Check for duplicate and insertion location. |
| 6392 | */ |
| 6393 | if (type == XML_EXP_ATOM) { |
| 6394 | kbase = xmlExpHashNameComputeKey(name); |
| 6395 | } else if (type == XML_EXP_COUNT) { |
| 6396 | /* COUNT reduction rule 1 */ |
| 6397 | /* a{1} -> a */ |
| 6398 | if (min == max) { |
| 6399 | if (min == 1) { |
| 6400 | return(left); |
| 6401 | } |
| 6402 | if (min == 0) { |
| 6403 | xmlExpFree(ctxt, left); |
| 6404 | return(emptyExp); |
| 6405 | } |
| 6406 | } |
| 6407 | if (min < 0) { |
| 6408 | xmlExpFree(ctxt, left); |
| 6409 | return(forbiddenExp); |
| 6410 | } |
| 6411 | if (max == -1) |
| 6412 | kbase = min + 79; |
| 6413 | else |
| 6414 | kbase = max - min; |
| 6415 | kbase += left->key; |
| 6416 | } else if (type == XML_EXP_OR) { |
| 6417 | /* Forbid reduction rules */ |
| 6418 | if (left->type == XML_EXP_FORBID) { |
| 6419 | xmlExpFree(ctxt, left); |
| 6420 | return(right); |
| 6421 | } |
| 6422 | if (right->type == XML_EXP_FORBID) { |
| 6423 | xmlExpFree(ctxt, right); |
| 6424 | return(left); |
| 6425 | } |
| 6426 | |
| 6427 | /* OR reduction rule 1 */ |
| 6428 | /* a | a reduced to a */ |
| 6429 | if (left == right) { |
| 6430 | left->ref--; |
| 6431 | return(left); |
| 6432 | } |
| 6433 | /* OR canonicalization rule 1 */ |
| 6434 | /* linearize (a | b) | c into a | (b | c) */ |
| 6435 | if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) { |
| 6436 | xmlExpNodePtr tmp = left; |
| 6437 | left = right; |
| 6438 | right = tmp; |
| 6439 | } |
| 6440 | /* OR reduction rule 2 */ |
| 6441 | /* a | (a | b) and b | (a | b) are reduced to a | b */ |
| 6442 | if (right->type == XML_EXP_OR) { |
| 6443 | if ((left == right->exp_left) || |
| 6444 | (left == right->exp_right)) { |
| 6445 | xmlExpFree(ctxt, left); |
| 6446 | return(right); |
| 6447 | } |
| 6448 | } |
| 6449 | /* OR canonicalization rule 2 */ |
| 6450 | /* linearize (a | b) | c into a | (b | c) */ |
| 6451 | if (left->type == XML_EXP_OR) { |
| 6452 | xmlExpNodePtr tmp; |
| 6453 | |
| 6454 | /* OR canonicalization rule 2 */ |
| 6455 | if ((left->exp_right->type != XML_EXP_OR) && |
| 6456 | (left->exp_right->key < left->exp_left->key)) { |
| 6457 | tmp = left->exp_right; |
| 6458 | left->exp_right = left->exp_left; |
| 6459 | left->exp_left = tmp; |
| 6460 | } |
| 6461 | left->exp_right->ref++; |
| 6462 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right, |
| 6463 | NULL, 0, 0); |
| 6464 | left->exp_left->ref++; |
| 6465 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp, |
| 6466 | NULL, 0, 0); |
| 6467 | |
| 6468 | xmlExpFree(ctxt, left); |
| 6469 | return(tmp); |
| 6470 | } |
| 6471 | if (right->type == XML_EXP_OR) { |
| 6472 | /* Ordering in the tree */ |
| 6473 | /* C | (A | B) -> A | (B | C) */ |
| 6474 | if (left->key > right->exp_right->key) { |
| 6475 | xmlExpNodePtr tmp; |
| 6476 | right->exp_right->ref++; |
| 6477 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right, |
| 6478 | left, NULL, 0, 0); |
| 6479 | right->exp_left->ref++; |
| 6480 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left, |
| 6481 | tmp, NULL, 0, 0); |
| 6482 | xmlExpFree(ctxt, right); |
| 6483 | return(tmp); |
| 6484 | } |
| 6485 | /* Ordering in the tree */ |
| 6486 | /* B | (A | C) -> A | (B | C) */ |
| 6487 | if (left->key > right->exp_left->key) { |
| 6488 | xmlExpNodePtr tmp; |
| 6489 | right->exp_right->ref++; |
| 6490 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, |
| 6491 | right->exp_right, NULL, 0, 0); |
| 6492 | right->exp_left->ref++; |
| 6493 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left, |
| 6494 | tmp, NULL, 0, 0); |
| 6495 | xmlExpFree(ctxt, right); |
| 6496 | return(tmp); |
| 6497 | } |
| 6498 | } |
| 6499 | /* we know both types are != XML_EXP_OR here */ |
| 6500 | else if (left->key > right->key) { |
| 6501 | xmlExpNodePtr tmp = left; |
| 6502 | left = right; |
| 6503 | right = tmp; |
| 6504 | } |
| 6505 | kbase = xmlExpHashComputeKey(type, left, right); |
| 6506 | } else if (type == XML_EXP_SEQ) { |
| 6507 | /* Forbid reduction rules */ |
| 6508 | if (left->type == XML_EXP_FORBID) { |
| 6509 | xmlExpFree(ctxt, right); |
| 6510 | return(left); |
| 6511 | } |
| 6512 | if (right->type == XML_EXP_FORBID) { |
| 6513 | xmlExpFree(ctxt, left); |
| 6514 | return(right); |
| 6515 | } |
| 6516 | /* Empty reduction rules */ |
| 6517 | if (right->type == XML_EXP_EMPTY) { |
| 6518 | return(left); |
| 6519 | } |
| 6520 | if (left->type == XML_EXP_EMPTY) { |
| 6521 | return(right); |
| 6522 | } |
| 6523 | kbase = xmlExpHashComputeKey(type, left, right); |
| 6524 | } else |
| 6525 | return(NULL); |
| 6526 | |
| 6527 | key = kbase % ctxt->size; |
| 6528 | if (ctxt->table[key] != NULL) { |
| 6529 | for (insert = ctxt->table[key]; insert != NULL; |
| 6530 | insert = insert->next) { |
| 6531 | if ((insert->key == kbase) && |
| 6532 | (insert->type == type)) { |
| 6533 | if (type == XML_EXP_ATOM) { |
| 6534 | if (name == insert->exp_str) { |
| 6535 | insert->ref++; |
| 6536 | return(insert); |
| 6537 | } |
| 6538 | } else if (type == XML_EXP_COUNT) { |
| 6539 | if ((insert->exp_min == min) && (insert->exp_max == max) && |
| 6540 | (insert->exp_left == left)) { |
| 6541 | insert->ref++; |
| 6542 | left->ref--; |
| 6543 | return(insert); |
| 6544 | } |
| 6545 | } else if ((insert->exp_left == left) && |
| 6546 | (insert->exp_right == right)) { |
| 6547 | insert->ref++; |
| 6548 | left->ref--; |
| 6549 | right->ref--; |
| 6550 | return(insert); |
| 6551 | } |
| 6552 | } |
| 6553 | } |
| 6554 | } |
| 6555 | |
| 6556 | entry = xmlExpNewNode(ctxt, type); |
| 6557 | if (entry == NULL) |
| 6558 | return(NULL); |
| 6559 | entry->key = kbase; |
| 6560 | if (type == XML_EXP_ATOM) { |
| 6561 | entry->exp_str = name; |
| 6562 | entry->c_max = 1; |
| 6563 | } else if (type == XML_EXP_COUNT) { |
| 6564 | entry->exp_min = min; |
| 6565 | entry->exp_max = max; |
| 6566 | entry->exp_left = left; |
| 6567 | if ((min == 0) || (IS_NILLABLE(left))) |
| 6568 | entry->info |= XML_EXP_NILABLE; |
| 6569 | if (max < 0) |
| 6570 | entry->c_max = -1; |
| 6571 | else |
| 6572 | entry->c_max = max * entry->exp_left->c_max; |
| 6573 | } else { |
| 6574 | entry->exp_left = left; |
| 6575 | entry->exp_right = right; |
| 6576 | if (type == XML_EXP_OR) { |
| 6577 | if ((IS_NILLABLE(left)) || (IS_NILLABLE(right))) |
| 6578 | entry->info |= XML_EXP_NILABLE; |
| 6579 | if ((entry->exp_left->c_max == -1) || |
| 6580 | (entry->exp_right->c_max == -1)) |
| 6581 | entry->c_max = -1; |
| 6582 | else if (entry->exp_left->c_max > entry->exp_right->c_max) |
| 6583 | entry->c_max = entry->exp_left->c_max; |
| 6584 | else |
| 6585 | entry->c_max = entry->exp_right->c_max; |
| 6586 | } else { |
| 6587 | if ((IS_NILLABLE(left)) && (IS_NILLABLE(right))) |
| 6588 | entry->info |= XML_EXP_NILABLE; |
| 6589 | if ((entry->exp_left->c_max == -1) || |
| 6590 | (entry->exp_right->c_max == -1)) |
| 6591 | entry->c_max = -1; |
| 6592 | else |
| 6593 | entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max; |
| 6594 | } |
| 6595 | } |
| 6596 | entry->ref = 1; |
| 6597 | if (ctxt->table[key] != NULL) |
| 6598 | entry->next = ctxt->table[key]; |
| 6599 | |
| 6600 | ctxt->table[key] = entry; |
| 6601 | ctxt->nbElems++; |
| 6602 | |
| 6603 | return(entry); |
| 6604 | } |
| 6605 | |
| 6606 | /** |
| 6607 | * xmlExpFree: |
| 6608 | * @ctxt: the expression context |
| 6609 | * @exp: the expression |
| 6610 | * |
| 6611 | * Dereference the expression |
| 6612 | */ |
| 6613 | void |
| 6614 | xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) { |
| 6615 | if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp)) |
| 6616 | return; |
| 6617 | exp->ref--; |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6618 | if (exp->ref == 0) { |
| 6619 | unsigned short key; |
| 6620 | |
| 6621 | /* Unlink it first from the hash table */ |
| 6622 | key = exp->key % ctxt->size; |
| 6623 | if (ctxt->table[key] == exp) { |
| 6624 | ctxt->table[key] = exp->next; |
| 6625 | } else { |
| 6626 | xmlExpNodePtr tmp; |
| 6627 | |
| 6628 | tmp = ctxt->table[key]; |
| 6629 | while (tmp != NULL) { |
| 6630 | if (tmp->next == exp) { |
| 6631 | tmp->next = exp->next; |
| 6632 | break; |
| 6633 | } |
| 6634 | tmp = tmp->next; |
| 6635 | } |
| 6636 | } |
| 6637 | |
| 6638 | if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) { |
| 6639 | xmlExpFree(ctxt, exp->exp_left); |
| 6640 | xmlExpFree(ctxt, exp->exp_right); |
| 6641 | } else if (exp->type == XML_EXP_COUNT) { |
| 6642 | xmlExpFree(ctxt, exp->exp_left); |
| 6643 | } |
| 6644 | xmlFree(exp); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6645 | ctxt->nb_nodes--; |
| 6646 | } |
| 6647 | } |
| 6648 | |
| 6649 | /** |
| 6650 | * xmlExpRef: |
| 6651 | * @exp: the expression |
| 6652 | * |
| 6653 | * Increase the reference count of the expression |
| 6654 | */ |
| 6655 | void |
| 6656 | xmlExpRef(xmlExpNodePtr exp) { |
| 6657 | if (exp != NULL) |
| 6658 | exp->ref++; |
| 6659 | } |
| 6660 | |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 6661 | /** |
| 6662 | * xmlExpNewAtom: |
| 6663 | * @ctxt: the expression context |
| 6664 | * @name: the atom name |
| 6665 | * @len: the atom name lenght in byte (or -1); |
| 6666 | * |
| 6667 | * Get the atom associated to this name from that context |
| 6668 | * |
| 6669 | * Returns the node or NULL in case of error |
| 6670 | */ |
| 6671 | xmlExpNodePtr |
| 6672 | xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) { |
| 6673 | if ((ctxt == NULL) || (name == NULL)) |
| 6674 | return(NULL); |
| 6675 | name = xmlDictLookup(ctxt->dict, name, len); |
| 6676 | if (name == NULL) |
| 6677 | return(NULL); |
| 6678 | return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0)); |
| 6679 | } |
| 6680 | |
| 6681 | /** |
| 6682 | * xmlExpNewOr: |
| 6683 | * @ctxt: the expression context |
| 6684 | * @left: left expression |
| 6685 | * @right: right expression |
| 6686 | * |
| 6687 | * Get the atom associated to the choice @left | @right |
| 6688 | * Note that @left and @right are consumed in the operation, to keep |
| 6689 | * an handle on them use xmlExpRef() and use xmlExpFree() to release them, |
| 6690 | * this is true even in case of failure (unless ctxt == NULL). |
| 6691 | * |
| 6692 | * Returns the node or NULL in case of error |
| 6693 | */ |
| 6694 | xmlExpNodePtr |
| 6695 | xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 6696 | if (ctxt == NULL) |
| 6697 | return(NULL); |
| 6698 | if ((left == NULL) || (right == NULL)) { |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 6699 | xmlExpFree(ctxt, left); |
| 6700 | xmlExpFree(ctxt, right); |
| 6701 | return(NULL); |
| 6702 | } |
| 6703 | return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0)); |
| 6704 | } |
| 6705 | |
| 6706 | /** |
| 6707 | * xmlExpNewSeq: |
| 6708 | * @ctxt: the expression context |
| 6709 | * @left: left expression |
| 6710 | * @right: right expression |
| 6711 | * |
| 6712 | * Get the atom associated to the sequence @left , @right |
| 6713 | * Note that @left and @right are consumed in the operation, to keep |
| 6714 | * an handle on them use xmlExpRef() and use xmlExpFree() to release them, |
| 6715 | * this is true even in case of failure (unless ctxt == NULL). |
| 6716 | * |
| 6717 | * Returns the node or NULL in case of error |
| 6718 | */ |
| 6719 | xmlExpNodePtr |
| 6720 | xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 6721 | if (ctxt == NULL) |
| 6722 | return(NULL); |
| 6723 | if ((left == NULL) || (right == NULL)) { |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 6724 | xmlExpFree(ctxt, left); |
| 6725 | xmlExpFree(ctxt, right); |
| 6726 | return(NULL); |
| 6727 | } |
| 6728 | return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0)); |
| 6729 | } |
| 6730 | |
| 6731 | /** |
| 6732 | * xmlExpNewRange: |
| 6733 | * @ctxt: the expression context |
| 6734 | * @subset: the expression to be repeated |
| 6735 | * @min: the lower bound for the repetition |
| 6736 | * @max: the upper bound for the repetition, -1 means infinite |
| 6737 | * |
| 6738 | * Get the atom associated to the range (@subset){@min, @max} |
| 6739 | * Note that @subset is consumed in the operation, to keep |
| 6740 | * an handle on it use xmlExpRef() and use xmlExpFree() to release it, |
| 6741 | * this is true even in case of failure (unless ctxt == NULL). |
| 6742 | * |
| 6743 | * Returns the node or NULL in case of error |
| 6744 | */ |
| 6745 | xmlExpNodePtr |
| 6746 | xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 6747 | if (ctxt == NULL) |
| 6748 | return(NULL); |
| 6749 | if ((subset == NULL) || (min < 0) || (max < -1) || |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 6750 | ((max >= 0) && (min > max))) { |
| 6751 | xmlExpFree(ctxt, subset); |
| 6752 | return(NULL); |
| 6753 | } |
| 6754 | return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset, |
| 6755 | NULL, NULL, min, max)); |
| 6756 | } |
| 6757 | |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6758 | /************************************************************************ |
| 6759 | * * |
| 6760 | * Public API for operations on expressions * |
| 6761 | * * |
| 6762 | ************************************************************************/ |
| 6763 | |
| 6764 | static int |
| 6765 | xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
| 6766 | const xmlChar**list, int len, int nb) { |
| 6767 | int tmp, tmp2; |
| 6768 | tail: |
| 6769 | switch (exp->type) { |
| 6770 | case XML_EXP_EMPTY: |
| 6771 | return(0); |
| 6772 | case XML_EXP_ATOM: |
| 6773 | for (tmp = 0;tmp < nb;tmp++) |
| 6774 | if (list[tmp] == exp->exp_str) |
| 6775 | return(0); |
| 6776 | if (nb >= len) |
| 6777 | return(-2); |
| 6778 | list[nb++] = exp->exp_str; |
| 6779 | return(1); |
| 6780 | case XML_EXP_COUNT: |
| 6781 | exp = exp->exp_left; |
| 6782 | goto tail; |
| 6783 | case XML_EXP_SEQ: |
| 6784 | case XML_EXP_OR: |
| 6785 | tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb); |
| 6786 | if (tmp < 0) |
| 6787 | return(tmp); |
| 6788 | tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len, |
| 6789 | nb + tmp); |
| 6790 | if (tmp2 < 0) |
| 6791 | return(tmp2); |
| 6792 | return(tmp + tmp2); |
| 6793 | } |
| 6794 | return(-1); |
| 6795 | } |
| 6796 | |
| 6797 | /** |
| 6798 | * xmlExpGetLanguage: |
| 6799 | * @ctxt: the expression context |
| 6800 | * @exp: the expression |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6801 | * @langList: where to store the tokens |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6802 | * @len: the allocated lenght of @list |
| 6803 | * |
| 6804 | * Find all the strings used in @exp and store them in @list |
| 6805 | * |
| 6806 | * Returns the number of unique strings found, -1 in case of errors and |
| 6807 | * -2 if there is more than @len strings |
| 6808 | */ |
| 6809 | int |
| 6810 | xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6811 | const xmlChar**langList, int len) { |
| 6812 | if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0)) |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6813 | return(-1); |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6814 | return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0)); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6815 | } |
| 6816 | |
| 6817 | static int |
| 6818 | xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
| 6819 | const xmlChar**list, int len, int nb) { |
| 6820 | int tmp, tmp2; |
| 6821 | tail: |
| 6822 | switch (exp->type) { |
| 6823 | case XML_EXP_FORBID: |
| 6824 | return(0); |
| 6825 | case XML_EXP_EMPTY: |
| 6826 | return(0); |
| 6827 | case XML_EXP_ATOM: |
| 6828 | for (tmp = 0;tmp < nb;tmp++) |
| 6829 | if (list[tmp] == exp->exp_str) |
| 6830 | return(0); |
| 6831 | if (nb >= len) |
| 6832 | return(-2); |
| 6833 | list[nb++] = exp->exp_str; |
| 6834 | return(1); |
| 6835 | case XML_EXP_COUNT: |
| 6836 | exp = exp->exp_left; |
| 6837 | goto tail; |
| 6838 | case XML_EXP_SEQ: |
| 6839 | tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb); |
| 6840 | if (tmp < 0) |
| 6841 | return(tmp); |
| 6842 | if (IS_NILLABLE(exp->exp_left)) { |
| 6843 | tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len, |
| 6844 | nb + tmp); |
| 6845 | if (tmp2 < 0) |
| 6846 | return(tmp2); |
| 6847 | tmp += tmp2; |
| 6848 | } |
| 6849 | return(tmp); |
| 6850 | case XML_EXP_OR: |
| 6851 | tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb); |
| 6852 | if (tmp < 0) |
| 6853 | return(tmp); |
| 6854 | tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len, |
| 6855 | nb + tmp); |
| 6856 | if (tmp2 < 0) |
| 6857 | return(tmp2); |
| 6858 | return(tmp + tmp2); |
| 6859 | } |
| 6860 | return(-1); |
| 6861 | } |
| 6862 | |
| 6863 | /** |
| 6864 | * xmlExpGetStart: |
| 6865 | * @ctxt: the expression context |
| 6866 | * @exp: the expression |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6867 | * @tokList: where to store the tokens |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6868 | * @len: the allocated lenght of @list |
| 6869 | * |
| 6870 | * Find all the strings that appears at the start of the languages |
| 6871 | * accepted by @exp and store them in @list. E.g. for (a, b) | c |
| 6872 | * it will return the list [a, c] |
| 6873 | * |
| 6874 | * Returns the number of unique strings found, -1 in case of errors and |
| 6875 | * -2 if there is more than @len strings |
| 6876 | */ |
| 6877 | int |
| 6878 | xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6879 | const xmlChar**tokList, int len) { |
| 6880 | if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0)) |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6881 | return(-1); |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6882 | return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0)); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6883 | } |
| 6884 | |
| 6885 | /** |
| 6886 | * xmlExpIsNillable: |
| 6887 | * @exp: the expression |
| 6888 | * |
| 6889 | * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce |
| 6890 | * |
| 6891 | * Returns 1 if nillable, 0 if not and -1 in case of error |
| 6892 | */ |
| 6893 | int |
| 6894 | xmlExpIsNillable(xmlExpNodePtr exp) { |
| 6895 | if (exp == NULL) |
| 6896 | return(-1); |
| 6897 | return(IS_NILLABLE(exp) != 0); |
| 6898 | } |
| 6899 | |
| 6900 | static xmlExpNodePtr |
| 6901 | xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str) |
| 6902 | { |
| 6903 | xmlExpNodePtr ret; |
| 6904 | |
| 6905 | switch (exp->type) { |
| 6906 | case XML_EXP_EMPTY: |
| 6907 | return(forbiddenExp); |
| 6908 | case XML_EXP_FORBID: |
| 6909 | return(forbiddenExp); |
| 6910 | case XML_EXP_ATOM: |
| 6911 | if (exp->exp_str == str) { |
| 6912 | #ifdef DEBUG_DERIV |
| 6913 | printf("deriv atom: equal => Empty\n"); |
| 6914 | #endif |
| 6915 | ret = emptyExp; |
| 6916 | } else { |
| 6917 | #ifdef DEBUG_DERIV |
| 6918 | printf("deriv atom: mismatch => forbid\n"); |
| 6919 | #endif |
| 6920 | /* TODO wildcards here */ |
| 6921 | ret = forbiddenExp; |
| 6922 | } |
| 6923 | return(ret); |
| 6924 | case XML_EXP_OR: { |
| 6925 | xmlExpNodePtr tmp; |
| 6926 | |
| 6927 | #ifdef DEBUG_DERIV |
| 6928 | printf("deriv or: => or(derivs)\n"); |
| 6929 | #endif |
| 6930 | tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str); |
| 6931 | if (tmp == NULL) { |
| 6932 | return(NULL); |
| 6933 | } |
| 6934 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str); |
| 6935 | if (ret == NULL) { |
| 6936 | xmlExpFree(ctxt, tmp); |
| 6937 | return(NULL); |
| 6938 | } |
| 6939 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, |
| 6940 | NULL, 0, 0); |
| 6941 | return(ret); |
| 6942 | } |
| 6943 | case XML_EXP_SEQ: |
| 6944 | #ifdef DEBUG_DERIV |
| 6945 | printf("deriv seq: starting with left\n"); |
| 6946 | #endif |
| 6947 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str); |
| 6948 | if (ret == NULL) { |
| 6949 | return(NULL); |
| 6950 | } else if (ret == forbiddenExp) { |
| 6951 | if (IS_NILLABLE(exp->exp_left)) { |
| 6952 | #ifdef DEBUG_DERIV |
| 6953 | printf("deriv seq: left failed but nillable\n"); |
| 6954 | #endif |
| 6955 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str); |
| 6956 | } |
| 6957 | } else { |
| 6958 | #ifdef DEBUG_DERIV |
| 6959 | printf("deriv seq: left match => sequence\n"); |
| 6960 | #endif |
| 6961 | exp->exp_right->ref++; |
| 6962 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right, |
| 6963 | NULL, 0, 0); |
| 6964 | } |
| 6965 | return(ret); |
| 6966 | case XML_EXP_COUNT: { |
| 6967 | int min, max; |
| 6968 | xmlExpNodePtr tmp; |
| 6969 | |
| 6970 | if (exp->exp_max == 0) |
| 6971 | return(forbiddenExp); |
| 6972 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str); |
| 6973 | if (ret == NULL) |
| 6974 | return(NULL); |
| 6975 | if (ret == forbiddenExp) { |
| 6976 | #ifdef DEBUG_DERIV |
| 6977 | printf("deriv count: pattern mismatch => forbid\n"); |
| 6978 | #endif |
| 6979 | return(ret); |
| 6980 | } |
| 6981 | if (exp->exp_max == 1) |
| 6982 | return(ret); |
| 6983 | if (exp->exp_max < 0) /* unbounded */ |
| 6984 | max = -1; |
| 6985 | else |
| 6986 | max = exp->exp_max - 1; |
| 6987 | if (exp->exp_min > 0) |
| 6988 | min = exp->exp_min - 1; |
| 6989 | else |
| 6990 | min = 0; |
| 6991 | exp->exp_left->ref++; |
| 6992 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL, |
| 6993 | NULL, min, max); |
| 6994 | if (ret == emptyExp) { |
| 6995 | #ifdef DEBUG_DERIV |
| 6996 | printf("deriv count: match to empty => new count\n"); |
| 6997 | #endif |
| 6998 | return(tmp); |
| 6999 | } |
| 7000 | #ifdef DEBUG_DERIV |
| 7001 | printf("deriv count: match => sequence with new count\n"); |
| 7002 | #endif |
| 7003 | return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp, |
| 7004 | NULL, 0, 0)); |
| 7005 | } |
| 7006 | } |
| 7007 | return(NULL); |
| 7008 | } |
| 7009 | |
| 7010 | /** |
| 7011 | * xmlExpStringDerive: |
| 7012 | * @ctxt: the expression context |
| 7013 | * @exp: the expression |
| 7014 | * @str: the string |
| 7015 | * @len: the string len in bytes if available |
| 7016 | * |
| 7017 | * Do one step of Brzozowski derivation of the expression @exp with |
| 7018 | * respect to the input string |
| 7019 | * |
| 7020 | * Returns the resulting expression or NULL in case of internal error |
| 7021 | */ |
| 7022 | xmlExpNodePtr |
| 7023 | xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
| 7024 | const xmlChar *str, int len) { |
| 7025 | const xmlChar *input; |
| 7026 | |
| 7027 | if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) { |
| 7028 | return(NULL); |
| 7029 | } |
| 7030 | /* |
| 7031 | * check the string is in the dictionnary, if yes use an interned |
| 7032 | * copy, otherwise we know it's not an acceptable input |
| 7033 | */ |
| 7034 | input = xmlDictExists(ctxt->dict, str, len); |
| 7035 | if (input == NULL) { |
| 7036 | return(forbiddenExp); |
| 7037 | } |
| 7038 | return(xmlExpStringDeriveInt(ctxt, exp, input)); |
| 7039 | } |
| 7040 | |
| 7041 | static int |
| 7042 | xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) { |
| 7043 | int ret = 1; |
| 7044 | |
| 7045 | if (sub->c_max == -1) { |
| 7046 | if (exp->c_max != -1) |
| 7047 | ret = 0; |
| 7048 | } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) { |
| 7049 | ret = 0; |
| 7050 | } |
| 7051 | #if 0 |
| 7052 | if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp))) |
| 7053 | ret = 0; |
| 7054 | #endif |
| 7055 | return(ret); |
| 7056 | } |
| 7057 | |
| 7058 | static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
| 7059 | xmlExpNodePtr sub); |
| 7060 | /** |
| 7061 | * xmlExpDivide: |
| 7062 | * @ctxt: the expressions context |
| 7063 | * @exp: the englobing expression |
| 7064 | * @sub: the subexpression |
| 7065 | * @mult: the multiple expression |
| 7066 | * @remain: the remain from the derivation of the multiple |
| 7067 | * |
| 7068 | * Check if exp is a multiple of sub, i.e. if there is a finite number n |
| 7069 | * so that sub{n} subsume exp |
| 7070 | * |
| 7071 | * Returns the multiple value if successful, 0 if it is not a multiple |
| 7072 | * and -1 in case of internel error. |
| 7073 | */ |
| 7074 | |
| 7075 | static int |
| 7076 | xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub, |
| 7077 | xmlExpNodePtr *mult, xmlExpNodePtr *remain) { |
| 7078 | int i; |
| 7079 | xmlExpNodePtr tmp, tmp2; |
| 7080 | |
| 7081 | if (mult != NULL) *mult = NULL; |
| 7082 | if (remain != NULL) *remain = NULL; |
| 7083 | if (exp->c_max == -1) return(0); |
| 7084 | if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0); |
| 7085 | |
| 7086 | for (i = 1;i <= exp->c_max;i++) { |
| 7087 | sub->ref++; |
| 7088 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, |
| 7089 | sub, NULL, NULL, i, i); |
| 7090 | if (tmp == NULL) { |
| 7091 | return(-1); |
| 7092 | } |
| 7093 | if (!xmlExpCheckCard(tmp, exp)) { |
| 7094 | xmlExpFree(ctxt, tmp); |
| 7095 | continue; |
| 7096 | } |
| 7097 | tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp); |
| 7098 | if (tmp2 == NULL) { |
| 7099 | xmlExpFree(ctxt, tmp); |
| 7100 | return(-1); |
| 7101 | } |
| 7102 | if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) { |
| 7103 | if (remain != NULL) |
| 7104 | *remain = tmp2; |
| 7105 | else |
| 7106 | xmlExpFree(ctxt, tmp2); |
| 7107 | if (mult != NULL) |
| 7108 | *mult = tmp; |
| 7109 | else |
| 7110 | xmlExpFree(ctxt, tmp); |
| 7111 | #ifdef DEBUG_DERIV |
| 7112 | printf("Divide succeeded %d\n", i); |
| 7113 | #endif |
| 7114 | return(i); |
| 7115 | } |
| 7116 | xmlExpFree(ctxt, tmp); |
| 7117 | xmlExpFree(ctxt, tmp2); |
| 7118 | } |
| 7119 | #ifdef DEBUG_DERIV |
| 7120 | printf("Divide failed\n"); |
| 7121 | #endif |
| 7122 | return(0); |
| 7123 | } |
| 7124 | |
| 7125 | /** |
| 7126 | * xmlExpExpDeriveInt: |
| 7127 | * @ctxt: the expressions context |
| 7128 | * @exp: the englobing expression |
| 7129 | * @sub: the subexpression |
| 7130 | * |
| 7131 | * Try to do a step of Brzozowski derivation but at a higher level |
| 7132 | * the input being a subexpression. |
| 7133 | * |
| 7134 | * Returns the resulting expression or NULL in case of internal error |
| 7135 | */ |
| 7136 | static xmlExpNodePtr |
| 7137 | xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) { |
| 7138 | xmlExpNodePtr ret, tmp, tmp2, tmp3; |
| 7139 | const xmlChar **tab; |
| 7140 | int len, i; |
| 7141 | |
| 7142 | /* |
| 7143 | * In case of equality and if the expression can only consume a finite |
| 7144 | * amount, then the derivation is empty |
| 7145 | */ |
| 7146 | if ((exp == sub) && (exp->c_max >= 0)) { |
| 7147 | #ifdef DEBUG_DERIV |
| 7148 | printf("Equal(exp, sub) and finite -> Empty\n"); |
| 7149 | #endif |
| 7150 | return(emptyExp); |
| 7151 | } |
| 7152 | /* |
| 7153 | * decompose sub sequence first |
| 7154 | */ |
| 7155 | if (sub->type == XML_EXP_EMPTY) { |
| 7156 | #ifdef DEBUG_DERIV |
| 7157 | printf("Empty(sub) -> Empty\n"); |
| 7158 | #endif |
| 7159 | exp->ref++; |
| 7160 | return(exp); |
| 7161 | } |
| 7162 | if (sub->type == XML_EXP_SEQ) { |
| 7163 | #ifdef DEBUG_DERIV |
| 7164 | printf("Seq(sub) -> decompose\n"); |
| 7165 | #endif |
| 7166 | tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left); |
| 7167 | if (tmp == NULL) |
| 7168 | return(NULL); |
| 7169 | if (tmp == forbiddenExp) |
| 7170 | return(tmp); |
| 7171 | ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right); |
| 7172 | xmlExpFree(ctxt, tmp); |
| 7173 | return(ret); |
| 7174 | } |
| 7175 | if (sub->type == XML_EXP_OR) { |
| 7176 | #ifdef DEBUG_DERIV |
| 7177 | printf("Or(sub) -> decompose\n"); |
| 7178 | #endif |
| 7179 | tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left); |
| 7180 | if (tmp == forbiddenExp) |
| 7181 | return(tmp); |
| 7182 | if (tmp == NULL) |
| 7183 | return(NULL); |
| 7184 | ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right); |
| 7185 | if ((ret == NULL) || (ret == forbiddenExp)) { |
| 7186 | xmlExpFree(ctxt, tmp); |
| 7187 | return(ret); |
| 7188 | } |
| 7189 | return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0)); |
| 7190 | } |
| 7191 | if (!xmlExpCheckCard(exp, sub)) { |
| 7192 | #ifdef DEBUG_DERIV |
| 7193 | printf("CheckCard(exp, sub) failed -> Forbid\n"); |
| 7194 | #endif |
| 7195 | return(forbiddenExp); |
| 7196 | } |
| 7197 | switch (exp->type) { |
| 7198 | case XML_EXP_EMPTY: |
| 7199 | if (sub == emptyExp) |
| 7200 | return(emptyExp); |
| 7201 | #ifdef DEBUG_DERIV |
| 7202 | printf("Empty(exp) -> Forbid\n"); |
| 7203 | #endif |
| 7204 | return(forbiddenExp); |
| 7205 | case XML_EXP_FORBID: |
| 7206 | #ifdef DEBUG_DERIV |
| 7207 | printf("Forbid(exp) -> Forbid\n"); |
| 7208 | #endif |
| 7209 | return(forbiddenExp); |
| 7210 | case XML_EXP_ATOM: |
| 7211 | if (sub->type == XML_EXP_ATOM) { |
| 7212 | /* TODO: handle wildcards */ |
| 7213 | if (exp->exp_str == sub->exp_str) { |
| 7214 | #ifdef DEBUG_DERIV |
| 7215 | printf("Atom match -> Empty\n"); |
| 7216 | #endif |
| 7217 | return(emptyExp); |
| 7218 | } |
| 7219 | #ifdef DEBUG_DERIV |
| 7220 | printf("Atom mismatch -> Forbid\n"); |
| 7221 | #endif |
| 7222 | return(forbiddenExp); |
| 7223 | } |
| 7224 | if ((sub->type == XML_EXP_COUNT) && |
| 7225 | (sub->exp_max == 1) && |
| 7226 | (sub->exp_left->type == XML_EXP_ATOM)) { |
| 7227 | /* TODO: handle wildcards */ |
| 7228 | if (exp->exp_str == sub->exp_left->exp_str) { |
| 7229 | #ifdef DEBUG_DERIV |
| 7230 | printf("Atom match -> Empty\n"); |
| 7231 | #endif |
| 7232 | return(emptyExp); |
| 7233 | } |
| 7234 | #ifdef DEBUG_DERIV |
| 7235 | printf("Atom mismatch -> Forbid\n"); |
| 7236 | #endif |
| 7237 | return(forbiddenExp); |
| 7238 | } |
| 7239 | #ifdef DEBUG_DERIV |
| 7240 | printf("Compex exp vs Atom -> Forbid\n"); |
| 7241 | #endif |
| 7242 | return(forbiddenExp); |
| 7243 | case XML_EXP_SEQ: |
| 7244 | /* try to get the sequence consumed only if possible */ |
| 7245 | if (xmlExpCheckCard(exp->exp_left, sub)) { |
| 7246 | /* See if the sequence can be consumed directly */ |
| 7247 | #ifdef DEBUG_DERIV |
| 7248 | printf("Seq trying left only\n"); |
| 7249 | #endif |
| 7250 | ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub); |
| 7251 | if ((ret != forbiddenExp) && (ret != NULL)) { |
| 7252 | #ifdef DEBUG_DERIV |
| 7253 | printf("Seq trying left only worked\n"); |
| 7254 | #endif |
| 7255 | /* |
| 7256 | * TODO: assumption here that we are determinist |
| 7257 | * i.e. we won't get to a nillable exp left |
| 7258 | * subset which could be matched by the right |
| 7259 | * part too. |
| 7260 | * e.g.: (a | b)+,(a | c) and 'a+,a' |
| 7261 | */ |
| 7262 | exp->exp_right->ref++; |
| 7263 | return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, |
| 7264 | exp->exp_right, NULL, 0, 0)); |
| 7265 | } |
| 7266 | #ifdef DEBUG_DERIV |
| 7267 | } else { |
| 7268 | printf("Seq: left too short\n"); |
| 7269 | #endif |
| 7270 | } |
| 7271 | /* Try instead to decompose */ |
| 7272 | if (sub->type == XML_EXP_COUNT) { |
| 7273 | int min, max; |
| 7274 | |
| 7275 | #ifdef DEBUG_DERIV |
| 7276 | printf("Seq: sub is a count\n"); |
| 7277 | #endif |
| 7278 | ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left); |
| 7279 | if (ret == NULL) |
| 7280 | return(NULL); |
| 7281 | if (ret != forbiddenExp) { |
| 7282 | #ifdef DEBUG_DERIV |
| 7283 | printf("Seq , Count match on left\n"); |
| 7284 | #endif |
| 7285 | if (sub->exp_max < 0) |
| 7286 | max = -1; |
| 7287 | else |
| 7288 | max = sub->exp_max -1; |
| 7289 | if (sub->exp_min > 0) |
| 7290 | min = sub->exp_min -1; |
| 7291 | else |
| 7292 | min = 0; |
| 7293 | exp->exp_right->ref++; |
| 7294 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, |
| 7295 | exp->exp_right, NULL, 0, 0); |
| 7296 | if (tmp == NULL) |
| 7297 | return(NULL); |
| 7298 | |
| 7299 | sub->exp_left->ref++; |
| 7300 | tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, |
| 7301 | sub->exp_left, NULL, NULL, min, max); |
| 7302 | if (tmp2 == NULL) { |
| 7303 | xmlExpFree(ctxt, tmp); |
| 7304 | return(NULL); |
| 7305 | } |
| 7306 | ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2); |
| 7307 | xmlExpFree(ctxt, tmp); |
| 7308 | xmlExpFree(ctxt, tmp2); |
| 7309 | return(ret); |
| 7310 | } |
| 7311 | } |
| 7312 | /* we made no progress on structured operations */ |
| 7313 | break; |
| 7314 | case XML_EXP_OR: |
| 7315 | #ifdef DEBUG_DERIV |
| 7316 | printf("Or , trying both side\n"); |
| 7317 | #endif |
| 7318 | ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub); |
| 7319 | if (ret == NULL) |
| 7320 | return(NULL); |
| 7321 | tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub); |
| 7322 | if (tmp == NULL) { |
| 7323 | xmlExpFree(ctxt, ret); |
| 7324 | return(NULL); |
| 7325 | } |
| 7326 | return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0)); |
| 7327 | case XML_EXP_COUNT: { |
| 7328 | int min, max; |
| 7329 | |
| 7330 | if (sub->type == XML_EXP_COUNT) { |
| 7331 | /* |
| 7332 | * Try to see if the loop is completely subsumed |
| 7333 | */ |
| 7334 | tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left); |
| 7335 | if (tmp == NULL) |
| 7336 | return(NULL); |
| 7337 | if (tmp == forbiddenExp) { |
| 7338 | int mult; |
| 7339 | |
| 7340 | #ifdef DEBUG_DERIV |
| 7341 | printf("Count, Count inner don't subsume\n"); |
| 7342 | #endif |
| 7343 | mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left, |
| 7344 | NULL, &tmp); |
| 7345 | if (mult <= 0) { |
| 7346 | #ifdef DEBUG_DERIV |
| 7347 | printf("Count, Count not multiple => forbidden\n"); |
| 7348 | #endif |
| 7349 | return(forbiddenExp); |
| 7350 | } |
| 7351 | if (sub->exp_max == -1) { |
| 7352 | max = -1; |
| 7353 | if (exp->exp_max == -1) { |
| 7354 | if (exp->exp_min <= sub->exp_min * mult) |
| 7355 | min = 0; |
| 7356 | else |
| 7357 | min = exp->exp_min - sub->exp_min * mult; |
| 7358 | } else { |
| 7359 | #ifdef DEBUG_DERIV |
| 7360 | printf("Count, Count finite can't subsume infinite\n"); |
| 7361 | #endif |
| 7362 | xmlExpFree(ctxt, tmp); |
| 7363 | return(forbiddenExp); |
| 7364 | } |
| 7365 | } else { |
| 7366 | if (exp->exp_max == -1) { |
| 7367 | #ifdef DEBUG_DERIV |
| 7368 | printf("Infinite loop consume mult finite loop\n"); |
| 7369 | #endif |
| 7370 | if (exp->exp_min > sub->exp_min * mult) { |
| 7371 | max = -1; |
| 7372 | min = exp->exp_min - sub->exp_min * mult; |
| 7373 | } else { |
| 7374 | max = -1; |
| 7375 | min = 0; |
| 7376 | } |
| 7377 | } else { |
| 7378 | if (exp->exp_max < sub->exp_max * mult) { |
| 7379 | #ifdef DEBUG_DERIV |
| 7380 | printf("loops max mult mismatch => forbidden\n"); |
| 7381 | #endif |
| 7382 | xmlExpFree(ctxt, tmp); |
| 7383 | return(forbiddenExp); |
| 7384 | } |
| 7385 | if (sub->exp_max * mult > exp->exp_min) |
| 7386 | min = 0; |
| 7387 | else |
| 7388 | min = exp->exp_min - sub->exp_max * mult; |
| 7389 | max = exp->exp_max - sub->exp_max * mult; |
| 7390 | } |
| 7391 | } |
| 7392 | } else if (!IS_NILLABLE(tmp)) { |
| 7393 | /* |
| 7394 | * TODO: loop here to try to grow if working on finite |
| 7395 | * blocks. |
| 7396 | */ |
| 7397 | #ifdef DEBUG_DERIV |
| 7398 | printf("Count, Count remain not nillable => forbidden\n"); |
| 7399 | #endif |
| 7400 | xmlExpFree(ctxt, tmp); |
| 7401 | return(forbiddenExp); |
| 7402 | } else if (sub->exp_max == -1) { |
| 7403 | if (exp->exp_max == -1) { |
| 7404 | if (exp->exp_min <= sub->exp_min) { |
| 7405 | #ifdef DEBUG_DERIV |
| 7406 | printf("Infinite loops Okay => COUNT(0,Inf)\n"); |
| 7407 | #endif |
| 7408 | max = -1; |
| 7409 | min = 0; |
| 7410 | } else { |
| 7411 | #ifdef DEBUG_DERIV |
| 7412 | printf("Infinite loops min => Count(X,Inf)\n"); |
| 7413 | #endif |
| 7414 | max = -1; |
| 7415 | min = exp->exp_min - sub->exp_min; |
| 7416 | } |
| 7417 | } else if (exp->exp_min > sub->exp_min) { |
| 7418 | #ifdef DEBUG_DERIV |
| 7419 | printf("loops min mismatch 1 => forbidden ???\n"); |
| 7420 | #endif |
| 7421 | xmlExpFree(ctxt, tmp); |
| 7422 | return(forbiddenExp); |
| 7423 | } else { |
| 7424 | max = -1; |
| 7425 | min = 0; |
| 7426 | } |
| 7427 | } else { |
| 7428 | if (exp->exp_max == -1) { |
| 7429 | #ifdef DEBUG_DERIV |
| 7430 | printf("Infinite loop consume finite loop\n"); |
| 7431 | #endif |
| 7432 | if (exp->exp_min > sub->exp_min) { |
| 7433 | max = -1; |
| 7434 | min = exp->exp_min - sub->exp_min; |
| 7435 | } else { |
| 7436 | max = -1; |
| 7437 | min = 0; |
| 7438 | } |
| 7439 | } else { |
| 7440 | if (exp->exp_max < sub->exp_max) { |
| 7441 | #ifdef DEBUG_DERIV |
| 7442 | printf("loops max mismatch => forbidden\n"); |
| 7443 | #endif |
| 7444 | xmlExpFree(ctxt, tmp); |
| 7445 | return(forbiddenExp); |
| 7446 | } |
| 7447 | if (sub->exp_max > exp->exp_min) |
| 7448 | min = 0; |
| 7449 | else |
| 7450 | min = exp->exp_min - sub->exp_max; |
| 7451 | max = exp->exp_max - sub->exp_max; |
| 7452 | } |
| 7453 | } |
| 7454 | #ifdef DEBUG_DERIV |
| 7455 | printf("loops match => SEQ(COUNT())\n"); |
| 7456 | #endif |
| 7457 | exp->exp_left->ref++; |
| 7458 | tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, |
| 7459 | NULL, NULL, min, max); |
| 7460 | if (tmp2 == NULL) { |
| 7461 | return(NULL); |
| 7462 | } |
| 7463 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2, |
| 7464 | NULL, 0, 0); |
| 7465 | return(ret); |
| 7466 | } |
| 7467 | tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub); |
| 7468 | if (tmp == NULL) |
| 7469 | return(NULL); |
| 7470 | if (tmp == forbiddenExp) { |
| 7471 | #ifdef DEBUG_DERIV |
| 7472 | printf("loop mismatch => forbidden\n"); |
| 7473 | #endif |
| 7474 | return(forbiddenExp); |
| 7475 | } |
| 7476 | if (exp->exp_min > 0) |
| 7477 | min = exp->exp_min - 1; |
| 7478 | else |
| 7479 | min = 0; |
| 7480 | if (exp->exp_max < 0) |
| 7481 | max = -1; |
| 7482 | else |
| 7483 | max = exp->exp_max - 1; |
| 7484 | |
| 7485 | #ifdef DEBUG_DERIV |
| 7486 | printf("loop match => SEQ(COUNT())\n"); |
| 7487 | #endif |
| 7488 | exp->exp_left->ref++; |
| 7489 | tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, |
| 7490 | NULL, NULL, min, max); |
| 7491 | if (tmp2 == NULL) |
| 7492 | return(NULL); |
| 7493 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2, |
| 7494 | NULL, 0, 0); |
| 7495 | return(ret); |
| 7496 | } |
| 7497 | } |
| 7498 | |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 7499 | #ifdef DEBUG_DERIV |
| 7500 | printf("Fallback to derivative\n"); |
| 7501 | #endif |
| 7502 | if (IS_NILLABLE(sub)) { |
| 7503 | if (!(IS_NILLABLE(exp))) |
| 7504 | return(forbiddenExp); |
| 7505 | else |
| 7506 | ret = emptyExp; |
| 7507 | } else |
| 7508 | ret = NULL; |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7509 | /* |
| 7510 | * here the structured derivation made no progress so |
| 7511 | * we use the default token based derivation to force one more step |
| 7512 | */ |
| 7513 | if (ctxt->tabSize == 0) |
| 7514 | ctxt->tabSize = 40; |
| 7515 | |
| 7516 | tab = (const xmlChar **) xmlMalloc(ctxt->tabSize * |
| 7517 | sizeof(const xmlChar *)); |
| 7518 | if (tab == NULL) { |
| 7519 | return(NULL); |
| 7520 | } |
| 7521 | |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7522 | /* |
| 7523 | * collect all the strings accepted by the subexpression on input |
| 7524 | */ |
| 7525 | len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0); |
| 7526 | while (len < 0) { |
| 7527 | const xmlChar **temp; |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7528 | temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 * |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7529 | sizeof(const xmlChar *)); |
| 7530 | if (temp == NULL) { |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7531 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7532 | return(NULL); |
| 7533 | } |
| 7534 | tab = temp; |
| 7535 | ctxt->tabSize *= 2; |
| 7536 | len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0); |
| 7537 | } |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7538 | for (i = 0;i < len;i++) { |
| 7539 | tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]); |
| 7540 | if ((tmp == NULL) || (tmp == forbiddenExp)) { |
| 7541 | xmlExpFree(ctxt, ret); |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7542 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7543 | return(tmp); |
| 7544 | } |
| 7545 | tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]); |
| 7546 | if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) { |
| 7547 | xmlExpFree(ctxt, tmp); |
| 7548 | xmlExpFree(ctxt, ret); |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7549 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7550 | return(tmp); |
| 7551 | } |
| 7552 | tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2); |
| 7553 | xmlExpFree(ctxt, tmp); |
| 7554 | xmlExpFree(ctxt, tmp2); |
| 7555 | |
| 7556 | if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) { |
| 7557 | xmlExpFree(ctxt, ret); |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7558 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7559 | return(tmp3); |
| 7560 | } |
| 7561 | |
| 7562 | if (ret == NULL) |
| 7563 | ret = tmp3; |
| 7564 | else { |
| 7565 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0); |
| 7566 | if (ret == NULL) { |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7567 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7568 | return(NULL); |
| 7569 | } |
| 7570 | } |
| 7571 | } |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7572 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7573 | return(ret); |
| 7574 | } |
| 7575 | |
| 7576 | /** |
Daniel Veillard | 0090bd5 | 2005-08-22 14:43:43 +0000 | [diff] [blame] | 7577 | * xmlExpExpDerive: |
| 7578 | * @ctxt: the expressions context |
| 7579 | * @exp: the englobing expression |
| 7580 | * @sub: the subexpression |
| 7581 | * |
| 7582 | * Evaluates the expression resulting from @exp consuming a sub expression @sub |
| 7583 | * Based on algebraic derivation and sometimes direct Brzozowski derivation |
| 7584 | * it usually tatkes less than linear time and can handle expressions generating |
| 7585 | * infinite languages. |
| 7586 | * |
| 7587 | * Returns the resulting expression or NULL in case of internal error, the |
| 7588 | * result must be freed |
| 7589 | */ |
| 7590 | xmlExpNodePtr |
| 7591 | xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) { |
| 7592 | if ((exp == NULL) || (ctxt == NULL) || (sub == NULL)) |
| 7593 | return(NULL); |
| 7594 | |
| 7595 | /* |
| 7596 | * O(1) speedups |
| 7597 | */ |
| 7598 | if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) { |
| 7599 | #ifdef DEBUG_DERIV |
| 7600 | printf("Sub nillable and not exp : can't subsume\n"); |
| 7601 | #endif |
| 7602 | return(forbiddenExp); |
| 7603 | } |
| 7604 | if (xmlExpCheckCard(exp, sub) == 0) { |
| 7605 | #ifdef DEBUG_DERIV |
| 7606 | printf("sub generate longuer sequances than exp : can't subsume\n"); |
| 7607 | #endif |
| 7608 | return(forbiddenExp); |
| 7609 | } |
| 7610 | return(xmlExpExpDeriveInt(ctxt, exp, sub)); |
| 7611 | } |
| 7612 | |
| 7613 | /** |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7614 | * xmlExpSubsume: |
| 7615 | * @ctxt: the expressions context |
| 7616 | * @exp: the englobing expression |
| 7617 | * @sub: the subexpression |
| 7618 | * |
| 7619 | * Check whether @exp accepts all the languages accexpted by @sub |
| 7620 | * the input being a subexpression. |
| 7621 | * |
| 7622 | * Returns 1 if true 0 if false and -1 in case of failure. |
| 7623 | */ |
| 7624 | int |
| 7625 | xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) { |
| 7626 | xmlExpNodePtr tmp; |
| 7627 | |
| 7628 | if ((exp == NULL) || (ctxt == NULL) || (sub == NULL)) |
| 7629 | return(-1); |
| 7630 | |
| 7631 | /* |
| 7632 | * TODO: speedup by checking the language of sub is a subset of the |
| 7633 | * language of exp |
| 7634 | */ |
| 7635 | /* |
| 7636 | * O(1) speedups |
| 7637 | */ |
| 7638 | if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) { |
| 7639 | #ifdef DEBUG_DERIV |
| 7640 | printf("Sub nillable and not exp : can't subsume\n"); |
| 7641 | #endif |
| 7642 | return(0); |
| 7643 | } |
| 7644 | if (xmlExpCheckCard(exp, sub) == 0) { |
| 7645 | #ifdef DEBUG_DERIV |
| 7646 | printf("sub generate longuer sequances than exp : can't subsume\n"); |
| 7647 | #endif |
| 7648 | return(0); |
| 7649 | } |
| 7650 | tmp = xmlExpExpDeriveInt(ctxt, exp, sub); |
| 7651 | #ifdef DEBUG_DERIV |
| 7652 | printf("Result derivation :\n"); |
| 7653 | PRINT_EXP(tmp); |
| 7654 | #endif |
| 7655 | if (tmp == NULL) |
| 7656 | return(-1); |
| 7657 | if (tmp == forbiddenExp) |
| 7658 | return(0); |
| 7659 | if (tmp == emptyExp) |
| 7660 | return(1); |
| 7661 | if ((tmp != NULL) && (IS_NILLABLE(tmp))) { |
| 7662 | xmlExpFree(ctxt, tmp); |
| 7663 | return(1); |
| 7664 | } |
| 7665 | xmlExpFree(ctxt, tmp); |
| 7666 | return(0); |
| 7667 | } |
Daniel Veillard | 465a000 | 2005-08-22 12:07:04 +0000 | [diff] [blame] | 7668 | |
| 7669 | /************************************************************************ |
| 7670 | * * |
| 7671 | * Parsing expression * |
| 7672 | * * |
| 7673 | ************************************************************************/ |
| 7674 | |
| 7675 | static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt); |
| 7676 | |
| 7677 | #undef CUR |
| 7678 | #define CUR (*ctxt->cur) |
| 7679 | #undef NEXT |
| 7680 | #define NEXT ctxt->cur++; |
| 7681 | #undef IS_BLANK |
| 7682 | #define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t')) |
| 7683 | #define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++; |
| 7684 | |
| 7685 | static int |
| 7686 | xmlExpParseNumber(xmlExpCtxtPtr ctxt) { |
| 7687 | int ret = 0; |
| 7688 | |
| 7689 | SKIP_BLANKS |
| 7690 | if (CUR == '*') { |
| 7691 | NEXT |
| 7692 | return(-1); |
| 7693 | } |
| 7694 | if ((CUR < '0') || (CUR > '9')) |
| 7695 | return(-1); |
| 7696 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7697 | ret = ret * 10 + (CUR - '0'); |
| 7698 | NEXT |
| 7699 | } |
| 7700 | return(ret); |
| 7701 | } |
| 7702 | |
| 7703 | static xmlExpNodePtr |
| 7704 | xmlExpParseOr(xmlExpCtxtPtr ctxt) { |
| 7705 | const char *base; |
| 7706 | xmlExpNodePtr ret; |
| 7707 | const xmlChar *val; |
| 7708 | |
| 7709 | SKIP_BLANKS |
| 7710 | base = ctxt->cur; |
| 7711 | if (*ctxt->cur == '(') { |
| 7712 | NEXT |
| 7713 | ret = xmlExpParseExpr(ctxt); |
| 7714 | SKIP_BLANKS |
| 7715 | if (*ctxt->cur != ')') { |
| 7716 | fprintf(stderr, "unbalanced '(' : %s\n", base); |
| 7717 | xmlExpFree(ctxt, ret); |
| 7718 | return(NULL); |
| 7719 | } |
| 7720 | NEXT; |
| 7721 | SKIP_BLANKS |
| 7722 | goto parse_quantifier; |
| 7723 | } |
| 7724 | while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') && |
| 7725 | (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') && |
| 7726 | (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}')) |
| 7727 | NEXT; |
| 7728 | val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base); |
| 7729 | if (val == NULL) |
| 7730 | return(NULL); |
| 7731 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0); |
| 7732 | if (ret == NULL) |
| 7733 | return(NULL); |
| 7734 | SKIP_BLANKS |
| 7735 | parse_quantifier: |
| 7736 | if (CUR == '{') { |
| 7737 | int min, max; |
| 7738 | |
| 7739 | NEXT |
| 7740 | min = xmlExpParseNumber(ctxt); |
| 7741 | if (min < 0) { |
| 7742 | xmlExpFree(ctxt, ret); |
| 7743 | return(NULL); |
| 7744 | } |
| 7745 | SKIP_BLANKS |
| 7746 | if (CUR == ',') { |
| 7747 | NEXT |
| 7748 | max = xmlExpParseNumber(ctxt); |
| 7749 | SKIP_BLANKS |
| 7750 | } else |
| 7751 | max = min; |
| 7752 | if (CUR != '}') { |
| 7753 | xmlExpFree(ctxt, ret); |
| 7754 | return(NULL); |
| 7755 | } |
| 7756 | NEXT |
| 7757 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
| 7758 | min, max); |
| 7759 | SKIP_BLANKS |
| 7760 | } else if (CUR == '?') { |
| 7761 | NEXT |
| 7762 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
| 7763 | 0, 1); |
| 7764 | SKIP_BLANKS |
| 7765 | } else if (CUR == '+') { |
| 7766 | NEXT |
| 7767 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
| 7768 | 1, -1); |
| 7769 | SKIP_BLANKS |
| 7770 | } else if (CUR == '*') { |
| 7771 | NEXT |
| 7772 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
| 7773 | 0, -1); |
| 7774 | SKIP_BLANKS |
| 7775 | } |
| 7776 | return(ret); |
| 7777 | } |
| 7778 | |
| 7779 | |
| 7780 | static xmlExpNodePtr |
| 7781 | xmlExpParseSeq(xmlExpCtxtPtr ctxt) { |
| 7782 | xmlExpNodePtr ret, right; |
| 7783 | |
| 7784 | ret = xmlExpParseOr(ctxt); |
| 7785 | SKIP_BLANKS |
| 7786 | while (CUR == '|') { |
| 7787 | NEXT |
| 7788 | right = xmlExpParseOr(ctxt); |
| 7789 | if (right == NULL) { |
| 7790 | xmlExpFree(ctxt, ret); |
| 7791 | return(NULL); |
| 7792 | } |
| 7793 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0); |
| 7794 | if (ret == NULL) |
| 7795 | return(NULL); |
| 7796 | } |
| 7797 | return(ret); |
| 7798 | } |
| 7799 | |
| 7800 | static xmlExpNodePtr |
| 7801 | xmlExpParseExpr(xmlExpCtxtPtr ctxt) { |
| 7802 | xmlExpNodePtr ret, right; |
| 7803 | |
| 7804 | ret = xmlExpParseSeq(ctxt); |
| 7805 | SKIP_BLANKS |
| 7806 | while (CUR == ',') { |
| 7807 | NEXT |
| 7808 | right = xmlExpParseSeq(ctxt); |
| 7809 | if (right == NULL) { |
| 7810 | xmlExpFree(ctxt, ret); |
| 7811 | return(NULL); |
| 7812 | } |
| 7813 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0); |
| 7814 | if (ret == NULL) |
| 7815 | return(NULL); |
| 7816 | } |
| 7817 | return(ret); |
| 7818 | } |
| 7819 | |
| 7820 | /** |
| 7821 | * xmlExpParse: |
| 7822 | * @ctxt: the expressions context |
| 7823 | * @expr: the 0 terminated string |
| 7824 | * |
| 7825 | * Minimal parser for regexps, it understand the following constructs |
| 7826 | * - string terminals |
| 7827 | * - choice operator | |
| 7828 | * - sequence operator , |
| 7829 | * - subexpressions (...) |
| 7830 | * - usual cardinality operators + * and ? |
| 7831 | * - finite sequences { min, max } |
| 7832 | * - infinite sequences { min, * } |
| 7833 | * There is minimal checkings made especially no checking on strings values |
| 7834 | * |
| 7835 | * Returns a new expression or NULL in case of failure |
| 7836 | */ |
| 7837 | xmlExpNodePtr |
| 7838 | xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) { |
| 7839 | xmlExpNodePtr ret; |
| 7840 | |
| 7841 | ctxt->expr = expr; |
| 7842 | ctxt->cur = expr; |
| 7843 | |
| 7844 | ret = xmlExpParseExpr(ctxt); |
| 7845 | SKIP_BLANKS |
| 7846 | if (*ctxt->cur != 0) { |
| 7847 | xmlExpFree(ctxt, ret); |
| 7848 | return(NULL); |
| 7849 | } |
| 7850 | return(ret); |
| 7851 | } |
| 7852 | |
| 7853 | static void |
| 7854 | xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) { |
| 7855 | xmlExpNodePtr c; |
| 7856 | |
| 7857 | if (expr == NULL) return; |
| 7858 | if (glob) xmlBufferWriteChar(buf, "("); |
| 7859 | switch (expr->type) { |
| 7860 | case XML_EXP_EMPTY: |
| 7861 | xmlBufferWriteChar(buf, "empty"); |
| 7862 | break; |
| 7863 | case XML_EXP_FORBID: |
| 7864 | xmlBufferWriteChar(buf, "forbidden"); |
| 7865 | break; |
| 7866 | case XML_EXP_ATOM: |
| 7867 | xmlBufferWriteCHAR(buf, expr->exp_str); |
| 7868 | break; |
| 7869 | case XML_EXP_SEQ: |
| 7870 | c = expr->exp_left; |
| 7871 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
| 7872 | xmlExpDumpInt(buf, c, 1); |
| 7873 | else |
| 7874 | xmlExpDumpInt(buf, c, 0); |
| 7875 | xmlBufferWriteChar(buf, " , "); |
| 7876 | c = expr->exp_right; |
| 7877 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
| 7878 | xmlExpDumpInt(buf, c, 1); |
| 7879 | else |
| 7880 | xmlExpDumpInt(buf, c, 0); |
| 7881 | break; |
| 7882 | case XML_EXP_OR: |
| 7883 | c = expr->exp_left; |
| 7884 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
| 7885 | xmlExpDumpInt(buf, c, 1); |
| 7886 | else |
| 7887 | xmlExpDumpInt(buf, c, 0); |
| 7888 | xmlBufferWriteChar(buf, " | "); |
| 7889 | c = expr->exp_right; |
| 7890 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
| 7891 | xmlExpDumpInt(buf, c, 1); |
| 7892 | else |
| 7893 | xmlExpDumpInt(buf, c, 0); |
| 7894 | break; |
| 7895 | case XML_EXP_COUNT: { |
| 7896 | char rep[40]; |
| 7897 | |
| 7898 | c = expr->exp_left; |
| 7899 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
| 7900 | xmlExpDumpInt(buf, c, 1); |
| 7901 | else |
| 7902 | xmlExpDumpInt(buf, c, 0); |
| 7903 | if ((expr->exp_min == 0) && (expr->exp_max == 1)) { |
| 7904 | rep[0] = '?'; |
| 7905 | rep[1] = 0; |
| 7906 | } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) { |
| 7907 | rep[0] = '*'; |
| 7908 | rep[1] = 0; |
| 7909 | } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) { |
| 7910 | rep[0] = '+'; |
| 7911 | rep[1] = 0; |
| 7912 | } else if (expr->exp_max == expr->exp_min) { |
| 7913 | snprintf(rep, 39, "{%d}", expr->exp_min); |
| 7914 | } else if (expr->exp_max < 0) { |
| 7915 | snprintf(rep, 39, "{%d,inf}", expr->exp_min); |
| 7916 | } else { |
| 7917 | snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max); |
| 7918 | } |
| 7919 | rep[39] = 0; |
| 7920 | xmlBufferWriteChar(buf, rep); |
| 7921 | break; |
| 7922 | } |
| 7923 | default: |
| 7924 | fprintf(stderr, "Error in tree\n"); |
| 7925 | } |
| 7926 | if (glob) |
| 7927 | xmlBufferWriteChar(buf, ")"); |
| 7928 | } |
| 7929 | /** |
| 7930 | * xmlExpDump: |
| 7931 | * @buf: a buffer to receive the output |
| 7932 | * @expr: the compiled expression |
| 7933 | * |
| 7934 | * Serialize the expression as compiled to the buffer |
| 7935 | */ |
| 7936 | void |
Daniel Veillard | 5eee767 | 2005-08-22 21:22:27 +0000 | [diff] [blame] | 7937 | xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) { |
| 7938 | if ((buf == NULL) || (expr == NULL)) |
Daniel Veillard | 465a000 | 2005-08-22 12:07:04 +0000 | [diff] [blame] | 7939 | return; |
Daniel Veillard | 5eee767 | 2005-08-22 21:22:27 +0000 | [diff] [blame] | 7940 | xmlExpDumpInt(buf, expr, 0); |
Daniel Veillard | 465a000 | 2005-08-22 12:07:04 +0000 | [diff] [blame] | 7941 | } |
| 7942 | |
| 7943 | /** |
| 7944 | * xmlExpMaxToken: |
| 7945 | * @expr: a compiled expression |
| 7946 | * |
| 7947 | * Indicate the maximum number of input a expression can accept |
| 7948 | * |
| 7949 | * Returns the maximum length or -1 in case of error |
| 7950 | */ |
| 7951 | int |
| 7952 | xmlExpMaxToken(xmlExpNodePtr expr) { |
| 7953 | if (expr == NULL) |
| 7954 | return(-1); |
| 7955 | return(expr->c_max); |
| 7956 | } |
| 7957 | |
| 7958 | /** |
| 7959 | * xmlExpCtxtNbNodes: |
| 7960 | * @ctxt: an expression context |
| 7961 | * |
| 7962 | * Debugging facility provides the number of allocated nodes at a that point |
| 7963 | * |
| 7964 | * Returns the number of nodes in use or -1 in case of error |
| 7965 | */ |
| 7966 | int |
| 7967 | xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) { |
| 7968 | if (ctxt == NULL) |
| 7969 | return(-1); |
| 7970 | return(ctxt->nb_nodes); |
| 7971 | } |
| 7972 | |
| 7973 | /** |
| 7974 | * xmlExpCtxtNbCons: |
| 7975 | * @ctxt: an expression context |
| 7976 | * |
| 7977 | * Debugging facility provides the number of allocated nodes over lifetime |
| 7978 | * |
| 7979 | * Returns the number of nodes ever allocated or -1 in case of error |
| 7980 | */ |
| 7981 | int |
| 7982 | xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) { |
| 7983 | if (ctxt == NULL) |
| 7984 | return(-1); |
| 7985 | return(ctxt->nb_cons); |
| 7986 | } |
| 7987 | |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7988 | #endif /* LIBXML_EXPR_ENABLED */ |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 7989 | #define bottom_xmlregexp |
| 7990 | #include "elfgcchack.h" |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 7991 | #endif /* LIBXML_REGEXP_ENABLED */ |