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 '|' |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 57 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 58 | /** |
| 59 | * TODO: |
| 60 | * |
| 61 | * macro to flag unimplemented blocks |
| 62 | */ |
| 63 | #define TODO \ |
| 64 | xmlGenericError(xmlGenericErrorContext, \ |
| 65 | "Unimplemented block at %s:%d\n", \ |
| 66 | __FILE__, __LINE__); |
| 67 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 68 | /************************************************************************ |
| 69 | * * |
| 70 | * Datatypes and structures * |
| 71 | * * |
| 72 | ************************************************************************/ |
| 73 | |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 74 | /* |
| 75 | * Note: the order of the enums below is significant, do not shuffle |
| 76 | */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 77 | typedef enum { |
| 78 | XML_REGEXP_EPSILON = 1, |
| 79 | XML_REGEXP_CHARVAL, |
| 80 | XML_REGEXP_RANGES, |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 81 | XML_REGEXP_SUBREG, /* used for () sub regexps */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 82 | XML_REGEXP_STRING, |
| 83 | XML_REGEXP_ANYCHAR, /* . */ |
| 84 | XML_REGEXP_ANYSPACE, /* \s */ |
| 85 | XML_REGEXP_NOTSPACE, /* \S */ |
| 86 | XML_REGEXP_INITNAME, /* \l */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 87 | XML_REGEXP_NOTINITNAME, /* \L */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 88 | XML_REGEXP_NAMECHAR, /* \c */ |
| 89 | XML_REGEXP_NOTNAMECHAR, /* \C */ |
| 90 | XML_REGEXP_DECIMAL, /* \d */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 91 | XML_REGEXP_NOTDECIMAL, /* \D */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 92 | XML_REGEXP_REALCHAR, /* \w */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 93 | XML_REGEXP_NOTREALCHAR, /* \W */ |
| 94 | XML_REGEXP_LETTER = 100, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 95 | XML_REGEXP_LETTER_UPPERCASE, |
| 96 | XML_REGEXP_LETTER_LOWERCASE, |
| 97 | XML_REGEXP_LETTER_TITLECASE, |
| 98 | XML_REGEXP_LETTER_MODIFIER, |
| 99 | XML_REGEXP_LETTER_OTHERS, |
| 100 | XML_REGEXP_MARK, |
| 101 | XML_REGEXP_MARK_NONSPACING, |
| 102 | XML_REGEXP_MARK_SPACECOMBINING, |
| 103 | XML_REGEXP_MARK_ENCLOSING, |
| 104 | XML_REGEXP_NUMBER, |
| 105 | XML_REGEXP_NUMBER_DECIMAL, |
| 106 | XML_REGEXP_NUMBER_LETTER, |
| 107 | XML_REGEXP_NUMBER_OTHERS, |
| 108 | XML_REGEXP_PUNCT, |
| 109 | XML_REGEXP_PUNCT_CONNECTOR, |
| 110 | XML_REGEXP_PUNCT_DASH, |
| 111 | XML_REGEXP_PUNCT_OPEN, |
| 112 | XML_REGEXP_PUNCT_CLOSE, |
| 113 | XML_REGEXP_PUNCT_INITQUOTE, |
| 114 | XML_REGEXP_PUNCT_FINQUOTE, |
| 115 | XML_REGEXP_PUNCT_OTHERS, |
| 116 | XML_REGEXP_SEPAR, |
| 117 | XML_REGEXP_SEPAR_SPACE, |
| 118 | XML_REGEXP_SEPAR_LINE, |
| 119 | XML_REGEXP_SEPAR_PARA, |
| 120 | XML_REGEXP_SYMBOL, |
| 121 | XML_REGEXP_SYMBOL_MATH, |
| 122 | XML_REGEXP_SYMBOL_CURRENCY, |
| 123 | XML_REGEXP_SYMBOL_MODIFIER, |
| 124 | XML_REGEXP_SYMBOL_OTHERS, |
| 125 | XML_REGEXP_OTHER, |
| 126 | XML_REGEXP_OTHER_CONTROL, |
| 127 | XML_REGEXP_OTHER_FORMAT, |
| 128 | XML_REGEXP_OTHER_PRIVATE, |
| 129 | XML_REGEXP_OTHER_NA, |
| 130 | XML_REGEXP_BLOCK_NAME |
| 131 | } xmlRegAtomType; |
| 132 | |
| 133 | typedef enum { |
| 134 | XML_REGEXP_QUANT_EPSILON = 1, |
| 135 | XML_REGEXP_QUANT_ONCE, |
| 136 | XML_REGEXP_QUANT_OPT, |
| 137 | XML_REGEXP_QUANT_MULT, |
| 138 | XML_REGEXP_QUANT_PLUS, |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 139 | XML_REGEXP_QUANT_ONCEONLY, |
| 140 | XML_REGEXP_QUANT_ALL, |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 141 | XML_REGEXP_QUANT_RANGE |
| 142 | } xmlRegQuantType; |
| 143 | |
| 144 | typedef enum { |
| 145 | XML_REGEXP_START_STATE = 1, |
| 146 | XML_REGEXP_FINAL_STATE, |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 147 | XML_REGEXP_TRANS_STATE, |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 148 | XML_REGEXP_SINK_STATE, |
| 149 | XML_REGEXP_UNREACH_STATE |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 150 | } xmlRegStateType; |
| 151 | |
| 152 | typedef enum { |
| 153 | XML_REGEXP_MARK_NORMAL = 0, |
| 154 | XML_REGEXP_MARK_START, |
| 155 | XML_REGEXP_MARK_VISITED |
| 156 | } xmlRegMarkedType; |
| 157 | |
| 158 | typedef struct _xmlRegRange xmlRegRange; |
| 159 | typedef xmlRegRange *xmlRegRangePtr; |
| 160 | |
| 161 | struct _xmlRegRange { |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 162 | int neg; /* 0 normal, 1 not, 2 exclude */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 163 | xmlRegAtomType type; |
| 164 | int start; |
| 165 | int end; |
| 166 | xmlChar *blockName; |
| 167 | }; |
| 168 | |
| 169 | typedef struct _xmlRegAtom xmlRegAtom; |
| 170 | typedef xmlRegAtom *xmlRegAtomPtr; |
| 171 | |
| 172 | typedef struct _xmlAutomataState xmlRegState; |
| 173 | typedef xmlRegState *xmlRegStatePtr; |
| 174 | |
| 175 | struct _xmlRegAtom { |
| 176 | int no; |
| 177 | xmlRegAtomType type; |
| 178 | xmlRegQuantType quant; |
| 179 | int min; |
| 180 | int max; |
| 181 | |
| 182 | void *valuep; |
Daniel Veillard | a646cfd | 2002-09-17 21:50:03 +0000 | [diff] [blame] | 183 | void *valuep2; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 184 | int neg; |
| 185 | int codepoint; |
| 186 | xmlRegStatePtr start; |
| 187 | xmlRegStatePtr stop; |
| 188 | int maxRanges; |
| 189 | int nbRanges; |
| 190 | xmlRegRangePtr *ranges; |
| 191 | void *data; |
| 192 | }; |
| 193 | |
| 194 | typedef struct _xmlRegCounter xmlRegCounter; |
| 195 | typedef xmlRegCounter *xmlRegCounterPtr; |
| 196 | |
| 197 | struct _xmlRegCounter { |
| 198 | int min; |
| 199 | int max; |
| 200 | }; |
| 201 | |
| 202 | typedef struct _xmlRegTrans xmlRegTrans; |
| 203 | typedef xmlRegTrans *xmlRegTransPtr; |
| 204 | |
| 205 | struct _xmlRegTrans { |
| 206 | xmlRegAtomPtr atom; |
| 207 | int to; |
| 208 | int counter; |
| 209 | int count; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 210 | int nd; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | struct _xmlAutomataState { |
| 214 | xmlRegStateType type; |
| 215 | xmlRegMarkedType mark; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 216 | xmlRegMarkedType reached; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 217 | int no; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 218 | int maxTrans; |
| 219 | int nbTrans; |
| 220 | xmlRegTrans *trans; |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 221 | /* knowing states ponting to us can speed things up */ |
| 222 | int maxTransTo; |
| 223 | int nbTransTo; |
| 224 | int *transTo; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | typedef struct _xmlAutomata xmlRegParserCtxt; |
| 228 | typedef xmlRegParserCtxt *xmlRegParserCtxtPtr; |
| 229 | |
| 230 | struct _xmlAutomata { |
| 231 | xmlChar *string; |
| 232 | xmlChar *cur; |
| 233 | |
| 234 | int error; |
| 235 | int neg; |
| 236 | |
| 237 | xmlRegStatePtr start; |
| 238 | xmlRegStatePtr end; |
| 239 | xmlRegStatePtr state; |
| 240 | |
| 241 | xmlRegAtomPtr atom; |
| 242 | |
| 243 | int maxAtoms; |
| 244 | int nbAtoms; |
| 245 | xmlRegAtomPtr *atoms; |
| 246 | |
| 247 | int maxStates; |
| 248 | int nbStates; |
| 249 | xmlRegStatePtr *states; |
| 250 | |
| 251 | int maxCounters; |
| 252 | int nbCounters; |
| 253 | xmlRegCounter *counters; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 254 | |
| 255 | int determinist; |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 256 | int negs; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 257 | }; |
| 258 | |
| 259 | struct _xmlRegexp { |
| 260 | xmlChar *string; |
| 261 | int nbStates; |
| 262 | xmlRegStatePtr *states; |
| 263 | int nbAtoms; |
| 264 | xmlRegAtomPtr *atoms; |
| 265 | int nbCounters; |
| 266 | xmlRegCounter *counters; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 267 | int determinist; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 268 | /* |
| 269 | * That's the compact form for determinists automatas |
| 270 | */ |
| 271 | int nbstates; |
| 272 | int *compact; |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 273 | void **transdata; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 274 | int nbstrings; |
| 275 | xmlChar **stringMap; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
| 278 | typedef struct _xmlRegExecRollback xmlRegExecRollback; |
| 279 | typedef xmlRegExecRollback *xmlRegExecRollbackPtr; |
| 280 | |
| 281 | struct _xmlRegExecRollback { |
| 282 | xmlRegStatePtr state;/* the current state */ |
| 283 | int index; /* the index in the input stack */ |
| 284 | int nextbranch; /* the next transition to explore in that state */ |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 285 | int *counts; /* save the automata state if it has some */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 286 | }; |
| 287 | |
| 288 | typedef struct _xmlRegInputToken xmlRegInputToken; |
| 289 | typedef xmlRegInputToken *xmlRegInputTokenPtr; |
| 290 | |
| 291 | struct _xmlRegInputToken { |
| 292 | xmlChar *value; |
| 293 | void *data; |
| 294 | }; |
| 295 | |
| 296 | struct _xmlRegExecCtxt { |
| 297 | int status; /* execution status != 0 indicate an error */ |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 298 | int determinist; /* did we find an indeterministic behaviour */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 299 | xmlRegexpPtr comp; /* the compiled regexp */ |
| 300 | xmlRegExecCallbacks callback; |
| 301 | void *data; |
| 302 | |
| 303 | xmlRegStatePtr state;/* the current state */ |
| 304 | int transno; /* the current transition on that state */ |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 305 | int transcount; /* the number of chars in char counted transitions */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 306 | |
| 307 | /* |
| 308 | * A stack of rollback states |
| 309 | */ |
| 310 | int maxRollbacks; |
| 311 | int nbRollbacks; |
| 312 | xmlRegExecRollback *rollbacks; |
| 313 | |
| 314 | /* |
| 315 | * The state of the automata if any |
| 316 | */ |
| 317 | int *counts; |
| 318 | |
| 319 | /* |
| 320 | * The input stack |
| 321 | */ |
| 322 | int inputStackMax; |
| 323 | int inputStackNr; |
| 324 | int index; |
| 325 | int *charStack; |
| 326 | const xmlChar *inputString; /* when operating on characters */ |
| 327 | xmlRegInputTokenPtr inputStack;/* when operating on strings */ |
| 328 | |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 329 | /* |
| 330 | * error handling |
| 331 | */ |
| 332 | int errStateNo; /* the error state number */ |
| 333 | xmlRegStatePtr errState; /* the error state */ |
| 334 | xmlChar *errString; /* the string raising the error */ |
| 335 | int *errCounts; /* counters at the error state */ |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 336 | int nbPush; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 337 | }; |
| 338 | |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 339 | #define REGEXP_ALL_COUNTER 0x123456 |
| 340 | #define REGEXP_ALL_LAX_COUNTER 0x123457 |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 341 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 342 | static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 343 | static void xmlRegFreeState(xmlRegStatePtr state); |
| 344 | static void xmlRegFreeAtom(xmlRegAtomPtr atom); |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 345 | static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 346 | static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint); |
| 347 | static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, |
| 348 | int neg, int start, int end, const xmlChar *blockName); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 349 | |
| 350 | /************************************************************************ |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 351 | * * |
| 352 | * Regexp memory error handler * |
| 353 | * * |
| 354 | ************************************************************************/ |
| 355 | /** |
| 356 | * xmlRegexpErrMemory: |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 357 | * @extra: extra information |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 358 | * |
| 359 | * Handle an out of memory condition |
| 360 | */ |
| 361 | static void |
| 362 | xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra) |
| 363 | { |
| 364 | const char *regexp = NULL; |
| 365 | if (ctxt != NULL) { |
| 366 | regexp = (const char *) ctxt->string; |
| 367 | ctxt->error = XML_ERR_NO_MEMORY; |
| 368 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 369 | __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP, |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 370 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra, |
| 371 | regexp, NULL, 0, 0, |
| 372 | "Memory allocation failed : %s\n", extra); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * xmlRegexpErrCompile: |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 377 | * @extra: extra information |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 378 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 379 | * Handle a compilation failure |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 380 | */ |
| 381 | static void |
| 382 | xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra) |
| 383 | { |
| 384 | const char *regexp = NULL; |
| 385 | int idx = 0; |
| 386 | |
| 387 | if (ctxt != NULL) { |
| 388 | regexp = (const char *) ctxt->string; |
| 389 | idx = ctxt->cur - ctxt->string; |
| 390 | ctxt->error = XML_REGEXP_COMPILE_ERROR; |
| 391 | } |
Daniel Veillard | 659e71e | 2003-10-10 14:10:40 +0000 | [diff] [blame] | 392 | __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP, |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 393 | XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra, |
| 394 | regexp, NULL, idx, 0, |
| 395 | "failed to compile: %s\n", extra); |
| 396 | } |
| 397 | |
| 398 | /************************************************************************ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 399 | * * |
| 400 | * Allocation/Deallocation * |
| 401 | * * |
| 402 | ************************************************************************/ |
| 403 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 404 | static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 405 | /** |
| 406 | * xmlRegEpxFromParse: |
| 407 | * @ctxt: the parser context used to build it |
| 408 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 409 | * 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] | 410 | * |
| 411 | * Returns the new regexp or NULL in case of error |
| 412 | */ |
| 413 | static xmlRegexpPtr |
| 414 | xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) { |
| 415 | xmlRegexpPtr ret; |
| 416 | |
| 417 | ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 418 | if (ret == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 419 | xmlRegexpErrMemory(ctxt, "compiling regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 420 | return(NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 421 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 422 | memset(ret, 0, sizeof(xmlRegexp)); |
| 423 | ret->string = ctxt->string; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 424 | ret->nbStates = ctxt->nbStates; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 425 | ret->states = ctxt->states; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 426 | ret->nbAtoms = ctxt->nbAtoms; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 427 | ret->atoms = ctxt->atoms; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 428 | ret->nbCounters = ctxt->nbCounters; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 429 | ret->counters = ctxt->counters; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 430 | ret->determinist = ctxt->determinist; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 431 | if (ret->determinist == -1) { |
| 432 | xmlRegexpIsDeterminist(ret); |
| 433 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 434 | |
| 435 | if ((ret->determinist != 0) && |
| 436 | (ret->nbCounters == 0) && |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 437 | (ctxt->negs == 0) && |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 438 | (ret->atoms != NULL) && |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 439 | (ret->atoms[0] != NULL) && |
| 440 | (ret->atoms[0]->type == XML_REGEXP_STRING)) { |
| 441 | int i, j, nbstates = 0, nbatoms = 0; |
| 442 | int *stateRemap; |
| 443 | int *stringRemap; |
| 444 | int *transitions; |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 445 | void **transdata; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 446 | xmlChar **stringMap; |
| 447 | xmlChar *value; |
| 448 | |
| 449 | /* |
| 450 | * Switch to a compact representation |
| 451 | * 1/ counting the effective number of states left |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 452 | * 2/ counting the unique number of atoms, and check that |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 453 | * they are all of the string type |
| 454 | * 3/ build a table state x atom for the transitions |
| 455 | */ |
| 456 | |
| 457 | stateRemap = xmlMalloc(ret->nbStates * sizeof(int)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 458 | if (stateRemap == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 459 | xmlRegexpErrMemory(ctxt, "compiling regexp"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 460 | xmlFree(ret); |
| 461 | return(NULL); |
| 462 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 463 | for (i = 0;i < ret->nbStates;i++) { |
| 464 | if (ret->states[i] != NULL) { |
| 465 | stateRemap[i] = nbstates; |
| 466 | nbstates++; |
| 467 | } else { |
| 468 | stateRemap[i] = -1; |
| 469 | } |
| 470 | } |
| 471 | #ifdef DEBUG_COMPACTION |
| 472 | printf("Final: %d states\n", nbstates); |
| 473 | #endif |
| 474 | stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 475 | if (stringMap == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 476 | xmlRegexpErrMemory(ctxt, "compiling regexp"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 477 | xmlFree(stateRemap); |
| 478 | xmlFree(ret); |
| 479 | return(NULL); |
| 480 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 481 | stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 482 | if (stringRemap == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 483 | xmlRegexpErrMemory(ctxt, "compiling regexp"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 484 | xmlFree(stringMap); |
| 485 | xmlFree(stateRemap); |
| 486 | xmlFree(ret); |
| 487 | return(NULL); |
| 488 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 489 | for (i = 0;i < ret->nbAtoms;i++) { |
| 490 | if ((ret->atoms[i]->type == XML_REGEXP_STRING) && |
| 491 | (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) { |
| 492 | value = ret->atoms[i]->valuep; |
| 493 | for (j = 0;j < nbatoms;j++) { |
| 494 | if (xmlStrEqual(stringMap[j], value)) { |
| 495 | stringRemap[i] = j; |
| 496 | break; |
| 497 | } |
| 498 | } |
| 499 | if (j >= nbatoms) { |
| 500 | stringRemap[i] = nbatoms; |
| 501 | stringMap[nbatoms] = xmlStrdup(value); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 502 | if (stringMap[nbatoms] == NULL) { |
| 503 | for (i = 0;i < nbatoms;i++) |
| 504 | xmlFree(stringMap[i]); |
| 505 | xmlFree(stringRemap); |
| 506 | xmlFree(stringMap); |
| 507 | xmlFree(stateRemap); |
| 508 | xmlFree(ret); |
| 509 | return(NULL); |
| 510 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 511 | nbatoms++; |
| 512 | } |
| 513 | } else { |
| 514 | xmlFree(stateRemap); |
| 515 | xmlFree(stringRemap); |
| 516 | for (i = 0;i < nbatoms;i++) |
| 517 | xmlFree(stringMap[i]); |
| 518 | xmlFree(stringMap); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 519 | xmlFree(ret); |
| 520 | return(NULL); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | #ifdef DEBUG_COMPACTION |
| 524 | printf("Final: %d atoms\n", nbatoms); |
| 525 | #endif |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 526 | transitions = (int *) xmlMalloc((nbstates + 1) * |
| 527 | (nbatoms + 1) * sizeof(int)); |
| 528 | if (transitions == NULL) { |
| 529 | xmlFree(stateRemap); |
| 530 | xmlFree(stringRemap); |
| 531 | xmlFree(stringMap); |
| 532 | xmlFree(ret); |
| 533 | return(NULL); |
| 534 | } |
| 535 | memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int)); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 536 | |
| 537 | /* |
| 538 | * Allocate the transition table. The first entry for each |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 539 | * state corresponds to the state type. |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 540 | */ |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 541 | transdata = NULL; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 542 | |
| 543 | for (i = 0;i < ret->nbStates;i++) { |
| 544 | int stateno, atomno, targetno, prev; |
| 545 | xmlRegStatePtr state; |
| 546 | xmlRegTransPtr trans; |
| 547 | |
| 548 | stateno = stateRemap[i]; |
| 549 | if (stateno == -1) |
| 550 | continue; |
| 551 | state = ret->states[i]; |
| 552 | |
| 553 | transitions[stateno * (nbatoms + 1)] = state->type; |
| 554 | |
| 555 | for (j = 0;j < state->nbTrans;j++) { |
| 556 | trans = &(state->trans[j]); |
| 557 | if ((trans->to == -1) || (trans->atom == NULL)) |
| 558 | continue; |
| 559 | atomno = stringRemap[trans->atom->no]; |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 560 | if ((trans->atom->data != NULL) && (transdata == NULL)) { |
| 561 | transdata = (void **) xmlMalloc(nbstates * nbatoms * |
| 562 | sizeof(void *)); |
| 563 | if (transdata != NULL) |
| 564 | memset(transdata, 0, |
| 565 | nbstates * nbatoms * sizeof(void *)); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 566 | else { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 567 | xmlRegexpErrMemory(ctxt, "compiling regexp"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 568 | break; |
| 569 | } |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 570 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 571 | targetno = stateRemap[trans->to]; |
| 572 | /* |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 573 | * if the same atom can generate transitions to 2 different |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 574 | * states then it means the automata is not determinist and |
| 575 | * the compact form can't be used ! |
| 576 | */ |
| 577 | prev = transitions[stateno * (nbatoms + 1) + atomno + 1]; |
| 578 | if (prev != 0) { |
| 579 | if (prev != targetno + 1) { |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 580 | ret->determinist = 0; |
| 581 | #ifdef DEBUG_COMPACTION |
| 582 | printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n", |
| 583 | i, j, trans->atom->no, trans->to, atomno, targetno); |
| 584 | printf(" previous to is %d\n", prev); |
| 585 | #endif |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 586 | if (transdata != NULL) |
| 587 | xmlFree(transdata); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 588 | xmlFree(transitions); |
| 589 | xmlFree(stateRemap); |
| 590 | xmlFree(stringRemap); |
| 591 | for (i = 0;i < nbatoms;i++) |
| 592 | xmlFree(stringMap[i]); |
| 593 | xmlFree(stringMap); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 594 | goto not_determ; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 595 | } |
| 596 | } else { |
| 597 | #if 0 |
| 598 | printf("State %d trans %d: atom %d to %d : %d to %d\n", |
| 599 | i, j, trans->atom->no, trans->to, atomno, targetno); |
| 600 | #endif |
| 601 | transitions[stateno * (nbatoms + 1) + atomno + 1] = |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 602 | targetno + 1; /* to avoid 0 */ |
| 603 | if (transdata != NULL) |
| 604 | transdata[stateno * nbatoms + atomno] = |
| 605 | trans->atom->data; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | } |
| 609 | ret->determinist = 1; |
| 610 | #ifdef DEBUG_COMPACTION |
| 611 | /* |
| 612 | * Debug |
| 613 | */ |
| 614 | for (i = 0;i < nbstates;i++) { |
| 615 | for (j = 0;j < nbatoms + 1;j++) { |
| 616 | printf("%02d ", transitions[i * (nbatoms + 1) + j]); |
| 617 | } |
| 618 | printf("\n"); |
| 619 | } |
| 620 | printf("\n"); |
| 621 | #endif |
| 622 | /* |
| 623 | * Cleanup of the old data |
| 624 | */ |
| 625 | if (ret->states != NULL) { |
| 626 | for (i = 0;i < ret->nbStates;i++) |
| 627 | xmlRegFreeState(ret->states[i]); |
| 628 | xmlFree(ret->states); |
| 629 | } |
| 630 | ret->states = NULL; |
| 631 | ret->nbStates = 0; |
| 632 | if (ret->atoms != NULL) { |
| 633 | for (i = 0;i < ret->nbAtoms;i++) |
| 634 | xmlRegFreeAtom(ret->atoms[i]); |
| 635 | xmlFree(ret->atoms); |
| 636 | } |
| 637 | ret->atoms = NULL; |
| 638 | ret->nbAtoms = 0; |
| 639 | |
| 640 | ret->compact = transitions; |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 641 | ret->transdata = transdata; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 642 | ret->stringMap = stringMap; |
| 643 | ret->nbstrings = nbatoms; |
| 644 | ret->nbstates = nbstates; |
| 645 | xmlFree(stateRemap); |
| 646 | xmlFree(stringRemap); |
| 647 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 648 | not_determ: |
| 649 | ctxt->string = NULL; |
| 650 | ctxt->nbStates = 0; |
| 651 | ctxt->states = NULL; |
| 652 | ctxt->nbAtoms = 0; |
| 653 | ctxt->atoms = NULL; |
| 654 | ctxt->nbCounters = 0; |
| 655 | ctxt->counters = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 656 | return(ret); |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * xmlRegNewParserCtxt: |
| 661 | * @string: the string to parse |
| 662 | * |
| 663 | * Allocate a new regexp parser context |
| 664 | * |
| 665 | * Returns the new context or NULL in case of error |
| 666 | */ |
| 667 | static xmlRegParserCtxtPtr |
| 668 | xmlRegNewParserCtxt(const xmlChar *string) { |
| 669 | xmlRegParserCtxtPtr ret; |
| 670 | |
| 671 | ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt)); |
| 672 | if (ret == NULL) |
| 673 | return(NULL); |
| 674 | memset(ret, 0, sizeof(xmlRegParserCtxt)); |
| 675 | if (string != NULL) |
| 676 | ret->string = xmlStrdup(string); |
| 677 | ret->cur = ret->string; |
| 678 | ret->neg = 0; |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 679 | ret->negs = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 680 | ret->error = 0; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 681 | ret->determinist = -1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 682 | return(ret); |
| 683 | } |
| 684 | |
| 685 | /** |
| 686 | * xmlRegNewRange: |
| 687 | * @ctxt: the regexp parser context |
| 688 | * @neg: is that negative |
| 689 | * @type: the type of range |
| 690 | * @start: the start codepoint |
| 691 | * @end: the end codepoint |
| 692 | * |
| 693 | * Allocate a new regexp range |
| 694 | * |
| 695 | * Returns the new range or NULL in case of error |
| 696 | */ |
| 697 | static xmlRegRangePtr |
| 698 | xmlRegNewRange(xmlRegParserCtxtPtr ctxt, |
| 699 | int neg, xmlRegAtomType type, int start, int end) { |
| 700 | xmlRegRangePtr ret; |
| 701 | |
| 702 | ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange)); |
| 703 | if (ret == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 704 | xmlRegexpErrMemory(ctxt, "allocating range"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 705 | return(NULL); |
| 706 | } |
| 707 | ret->neg = neg; |
| 708 | ret->type = type; |
| 709 | ret->start = start; |
| 710 | ret->end = end; |
| 711 | return(ret); |
| 712 | } |
| 713 | |
| 714 | /** |
| 715 | * xmlRegFreeRange: |
| 716 | * @range: the regexp range |
| 717 | * |
| 718 | * Free a regexp range |
| 719 | */ |
| 720 | static void |
| 721 | xmlRegFreeRange(xmlRegRangePtr range) { |
| 722 | if (range == NULL) |
| 723 | return; |
| 724 | |
| 725 | if (range->blockName != NULL) |
| 726 | xmlFree(range->blockName); |
| 727 | xmlFree(range); |
| 728 | } |
| 729 | |
| 730 | /** |
| 731 | * xmlRegNewAtom: |
| 732 | * @ctxt: the regexp parser context |
| 733 | * @type: the type of atom |
| 734 | * |
| 735 | * Allocate a new regexp range |
| 736 | * |
| 737 | * Returns the new atom or NULL in case of error |
| 738 | */ |
| 739 | static xmlRegAtomPtr |
| 740 | xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) { |
| 741 | xmlRegAtomPtr ret; |
| 742 | |
| 743 | ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom)); |
| 744 | if (ret == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 745 | xmlRegexpErrMemory(ctxt, "allocating atom"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 746 | return(NULL); |
| 747 | } |
| 748 | memset(ret, 0, sizeof(xmlRegAtom)); |
| 749 | ret->type = type; |
| 750 | ret->quant = XML_REGEXP_QUANT_ONCE; |
| 751 | ret->min = 0; |
| 752 | ret->max = 0; |
| 753 | return(ret); |
| 754 | } |
| 755 | |
| 756 | /** |
| 757 | * xmlRegFreeAtom: |
| 758 | * @atom: the regexp atom |
| 759 | * |
| 760 | * Free a regexp atom |
| 761 | */ |
| 762 | static void |
| 763 | xmlRegFreeAtom(xmlRegAtomPtr atom) { |
| 764 | int i; |
| 765 | |
| 766 | if (atom == NULL) |
| 767 | return; |
| 768 | |
| 769 | for (i = 0;i < atom->nbRanges;i++) |
| 770 | xmlRegFreeRange(atom->ranges[i]); |
| 771 | if (atom->ranges != NULL) |
| 772 | xmlFree(atom->ranges); |
Daniel Veillard | de0e498 | 2005-07-03 14:35:44 +0000 | [diff] [blame] | 773 | if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL)) |
| 774 | xmlFree(atom->valuep); |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 775 | if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL)) |
| 776 | xmlFree(atom->valuep2); |
Daniel Veillard | de0e498 | 2005-07-03 14:35:44 +0000 | [diff] [blame] | 777 | if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL)) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 778 | xmlFree(atom->valuep); |
| 779 | xmlFree(atom); |
| 780 | } |
| 781 | |
| 782 | static xmlRegStatePtr |
| 783 | xmlRegNewState(xmlRegParserCtxtPtr ctxt) { |
| 784 | xmlRegStatePtr ret; |
| 785 | |
| 786 | ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState)); |
| 787 | if (ret == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 788 | xmlRegexpErrMemory(ctxt, "allocating state"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 789 | return(NULL); |
| 790 | } |
| 791 | memset(ret, 0, sizeof(xmlRegState)); |
| 792 | ret->type = XML_REGEXP_TRANS_STATE; |
| 793 | ret->mark = XML_REGEXP_MARK_NORMAL; |
| 794 | return(ret); |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * xmlRegFreeState: |
| 799 | * @state: the regexp state |
| 800 | * |
| 801 | * Free a regexp state |
| 802 | */ |
| 803 | static void |
| 804 | xmlRegFreeState(xmlRegStatePtr state) { |
| 805 | if (state == NULL) |
| 806 | return; |
| 807 | |
| 808 | if (state->trans != NULL) |
| 809 | xmlFree(state->trans); |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 810 | if (state->transTo != NULL) |
| 811 | xmlFree(state->transTo); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 812 | xmlFree(state); |
| 813 | } |
| 814 | |
| 815 | /** |
| 816 | * xmlRegFreeParserCtxt: |
| 817 | * @ctxt: the regexp parser context |
| 818 | * |
| 819 | * Free a regexp parser context |
| 820 | */ |
| 821 | static void |
| 822 | xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) { |
| 823 | int i; |
| 824 | if (ctxt == NULL) |
| 825 | return; |
| 826 | |
| 827 | if (ctxt->string != NULL) |
| 828 | xmlFree(ctxt->string); |
| 829 | if (ctxt->states != NULL) { |
| 830 | for (i = 0;i < ctxt->nbStates;i++) |
| 831 | xmlRegFreeState(ctxt->states[i]); |
| 832 | xmlFree(ctxt->states); |
| 833 | } |
| 834 | if (ctxt->atoms != NULL) { |
| 835 | for (i = 0;i < ctxt->nbAtoms;i++) |
| 836 | xmlRegFreeAtom(ctxt->atoms[i]); |
| 837 | xmlFree(ctxt->atoms); |
| 838 | } |
| 839 | if (ctxt->counters != NULL) |
| 840 | xmlFree(ctxt->counters); |
| 841 | xmlFree(ctxt); |
| 842 | } |
| 843 | |
| 844 | /************************************************************************ |
| 845 | * * |
| 846 | * Display of Data structures * |
| 847 | * * |
| 848 | ************************************************************************/ |
| 849 | |
| 850 | static void |
| 851 | xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) { |
| 852 | switch (type) { |
| 853 | case XML_REGEXP_EPSILON: |
| 854 | fprintf(output, "epsilon "); break; |
| 855 | case XML_REGEXP_CHARVAL: |
| 856 | fprintf(output, "charval "); break; |
| 857 | case XML_REGEXP_RANGES: |
| 858 | fprintf(output, "ranges "); break; |
| 859 | case XML_REGEXP_SUBREG: |
| 860 | fprintf(output, "subexpr "); break; |
| 861 | case XML_REGEXP_STRING: |
| 862 | fprintf(output, "string "); break; |
| 863 | case XML_REGEXP_ANYCHAR: |
| 864 | fprintf(output, "anychar "); break; |
| 865 | case XML_REGEXP_ANYSPACE: |
| 866 | fprintf(output, "anyspace "); break; |
| 867 | case XML_REGEXP_NOTSPACE: |
| 868 | fprintf(output, "notspace "); break; |
| 869 | case XML_REGEXP_INITNAME: |
| 870 | fprintf(output, "initname "); break; |
| 871 | case XML_REGEXP_NOTINITNAME: |
| 872 | fprintf(output, "notinitname "); break; |
| 873 | case XML_REGEXP_NAMECHAR: |
| 874 | fprintf(output, "namechar "); break; |
| 875 | case XML_REGEXP_NOTNAMECHAR: |
| 876 | fprintf(output, "notnamechar "); break; |
| 877 | case XML_REGEXP_DECIMAL: |
| 878 | fprintf(output, "decimal "); break; |
| 879 | case XML_REGEXP_NOTDECIMAL: |
| 880 | fprintf(output, "notdecimal "); break; |
| 881 | case XML_REGEXP_REALCHAR: |
| 882 | fprintf(output, "realchar "); break; |
| 883 | case XML_REGEXP_NOTREALCHAR: |
| 884 | fprintf(output, "notrealchar "); break; |
| 885 | case XML_REGEXP_LETTER: |
| 886 | fprintf(output, "LETTER "); break; |
| 887 | case XML_REGEXP_LETTER_UPPERCASE: |
| 888 | fprintf(output, "LETTER_UPPERCASE "); break; |
| 889 | case XML_REGEXP_LETTER_LOWERCASE: |
| 890 | fprintf(output, "LETTER_LOWERCASE "); break; |
| 891 | case XML_REGEXP_LETTER_TITLECASE: |
| 892 | fprintf(output, "LETTER_TITLECASE "); break; |
| 893 | case XML_REGEXP_LETTER_MODIFIER: |
| 894 | fprintf(output, "LETTER_MODIFIER "); break; |
| 895 | case XML_REGEXP_LETTER_OTHERS: |
| 896 | fprintf(output, "LETTER_OTHERS "); break; |
| 897 | case XML_REGEXP_MARK: |
| 898 | fprintf(output, "MARK "); break; |
| 899 | case XML_REGEXP_MARK_NONSPACING: |
| 900 | fprintf(output, "MARK_NONSPACING "); break; |
| 901 | case XML_REGEXP_MARK_SPACECOMBINING: |
| 902 | fprintf(output, "MARK_SPACECOMBINING "); break; |
| 903 | case XML_REGEXP_MARK_ENCLOSING: |
| 904 | fprintf(output, "MARK_ENCLOSING "); break; |
| 905 | case XML_REGEXP_NUMBER: |
| 906 | fprintf(output, "NUMBER "); break; |
| 907 | case XML_REGEXP_NUMBER_DECIMAL: |
| 908 | fprintf(output, "NUMBER_DECIMAL "); break; |
| 909 | case XML_REGEXP_NUMBER_LETTER: |
| 910 | fprintf(output, "NUMBER_LETTER "); break; |
| 911 | case XML_REGEXP_NUMBER_OTHERS: |
| 912 | fprintf(output, "NUMBER_OTHERS "); break; |
| 913 | case XML_REGEXP_PUNCT: |
| 914 | fprintf(output, "PUNCT "); break; |
| 915 | case XML_REGEXP_PUNCT_CONNECTOR: |
| 916 | fprintf(output, "PUNCT_CONNECTOR "); break; |
| 917 | case XML_REGEXP_PUNCT_DASH: |
| 918 | fprintf(output, "PUNCT_DASH "); break; |
| 919 | case XML_REGEXP_PUNCT_OPEN: |
| 920 | fprintf(output, "PUNCT_OPEN "); break; |
| 921 | case XML_REGEXP_PUNCT_CLOSE: |
| 922 | fprintf(output, "PUNCT_CLOSE "); break; |
| 923 | case XML_REGEXP_PUNCT_INITQUOTE: |
| 924 | fprintf(output, "PUNCT_INITQUOTE "); break; |
| 925 | case XML_REGEXP_PUNCT_FINQUOTE: |
| 926 | fprintf(output, "PUNCT_FINQUOTE "); break; |
| 927 | case XML_REGEXP_PUNCT_OTHERS: |
| 928 | fprintf(output, "PUNCT_OTHERS "); break; |
| 929 | case XML_REGEXP_SEPAR: |
| 930 | fprintf(output, "SEPAR "); break; |
| 931 | case XML_REGEXP_SEPAR_SPACE: |
| 932 | fprintf(output, "SEPAR_SPACE "); break; |
| 933 | case XML_REGEXP_SEPAR_LINE: |
| 934 | fprintf(output, "SEPAR_LINE "); break; |
| 935 | case XML_REGEXP_SEPAR_PARA: |
| 936 | fprintf(output, "SEPAR_PARA "); break; |
| 937 | case XML_REGEXP_SYMBOL: |
| 938 | fprintf(output, "SYMBOL "); break; |
| 939 | case XML_REGEXP_SYMBOL_MATH: |
| 940 | fprintf(output, "SYMBOL_MATH "); break; |
| 941 | case XML_REGEXP_SYMBOL_CURRENCY: |
| 942 | fprintf(output, "SYMBOL_CURRENCY "); break; |
| 943 | case XML_REGEXP_SYMBOL_MODIFIER: |
| 944 | fprintf(output, "SYMBOL_MODIFIER "); break; |
| 945 | case XML_REGEXP_SYMBOL_OTHERS: |
| 946 | fprintf(output, "SYMBOL_OTHERS "); break; |
| 947 | case XML_REGEXP_OTHER: |
| 948 | fprintf(output, "OTHER "); break; |
| 949 | case XML_REGEXP_OTHER_CONTROL: |
| 950 | fprintf(output, "OTHER_CONTROL "); break; |
| 951 | case XML_REGEXP_OTHER_FORMAT: |
| 952 | fprintf(output, "OTHER_FORMAT "); break; |
| 953 | case XML_REGEXP_OTHER_PRIVATE: |
| 954 | fprintf(output, "OTHER_PRIVATE "); break; |
| 955 | case XML_REGEXP_OTHER_NA: |
| 956 | fprintf(output, "OTHER_NA "); break; |
| 957 | case XML_REGEXP_BLOCK_NAME: |
| 958 | fprintf(output, "BLOCK "); break; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | static void |
| 963 | xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) { |
| 964 | switch (type) { |
| 965 | case XML_REGEXP_QUANT_EPSILON: |
| 966 | fprintf(output, "epsilon "); break; |
| 967 | case XML_REGEXP_QUANT_ONCE: |
| 968 | fprintf(output, "once "); break; |
| 969 | case XML_REGEXP_QUANT_OPT: |
| 970 | fprintf(output, "? "); break; |
| 971 | case XML_REGEXP_QUANT_MULT: |
| 972 | fprintf(output, "* "); break; |
| 973 | case XML_REGEXP_QUANT_PLUS: |
| 974 | fprintf(output, "+ "); break; |
| 975 | case XML_REGEXP_QUANT_RANGE: |
| 976 | fprintf(output, "range "); break; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 977 | case XML_REGEXP_QUANT_ONCEONLY: |
| 978 | fprintf(output, "onceonly "); break; |
| 979 | case XML_REGEXP_QUANT_ALL: |
| 980 | fprintf(output, "all "); break; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 981 | } |
| 982 | } |
| 983 | static void |
| 984 | xmlRegPrintRange(FILE *output, xmlRegRangePtr range) { |
| 985 | fprintf(output, " range: "); |
| 986 | if (range->neg) |
| 987 | fprintf(output, "negative "); |
| 988 | xmlRegPrintAtomType(output, range->type); |
| 989 | fprintf(output, "%c - %c\n", range->start, range->end); |
| 990 | } |
| 991 | |
| 992 | static void |
| 993 | xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) { |
| 994 | fprintf(output, " atom: "); |
| 995 | if (atom == NULL) { |
| 996 | fprintf(output, "NULL\n"); |
| 997 | return; |
| 998 | } |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 999 | if (atom->neg) |
| 1000 | fprintf(output, "not "); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1001 | xmlRegPrintAtomType(output, atom->type); |
| 1002 | xmlRegPrintQuantType(output, atom->quant); |
| 1003 | if (atom->quant == XML_REGEXP_QUANT_RANGE) |
| 1004 | fprintf(output, "%d-%d ", atom->min, atom->max); |
| 1005 | if (atom->type == XML_REGEXP_STRING) |
| 1006 | fprintf(output, "'%s' ", (char *) atom->valuep); |
| 1007 | if (atom->type == XML_REGEXP_CHARVAL) |
| 1008 | fprintf(output, "char %c\n", atom->codepoint); |
| 1009 | else if (atom->type == XML_REGEXP_RANGES) { |
| 1010 | int i; |
| 1011 | fprintf(output, "%d entries\n", atom->nbRanges); |
| 1012 | for (i = 0; i < atom->nbRanges;i++) |
| 1013 | xmlRegPrintRange(output, atom->ranges[i]); |
| 1014 | } else if (atom->type == XML_REGEXP_SUBREG) { |
| 1015 | fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no); |
| 1016 | } else { |
| 1017 | fprintf(output, "\n"); |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | static void |
| 1022 | xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) { |
| 1023 | fprintf(output, " trans: "); |
| 1024 | if (trans == NULL) { |
| 1025 | fprintf(output, "NULL\n"); |
| 1026 | return; |
| 1027 | } |
| 1028 | if (trans->to < 0) { |
| 1029 | fprintf(output, "removed\n"); |
| 1030 | return; |
| 1031 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 1032 | if (trans->nd != 0) { |
| 1033 | if (trans->nd == 2) |
| 1034 | fprintf(output, "last not determinist, "); |
| 1035 | else |
| 1036 | fprintf(output, "not determinist, "); |
| 1037 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1038 | if (trans->counter >= 0) { |
| 1039 | fprintf(output, "counted %d, ", trans->counter); |
| 1040 | } |
Daniel Veillard | 8a001f6 | 2002-04-20 07:24:11 +0000 | [diff] [blame] | 1041 | if (trans->count == REGEXP_ALL_COUNTER) { |
| 1042 | fprintf(output, "all transition, "); |
| 1043 | } else if (trans->count >= 0) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1044 | fprintf(output, "count based %d, ", trans->count); |
| 1045 | } |
| 1046 | if (trans->atom == NULL) { |
| 1047 | fprintf(output, "epsilon to %d\n", trans->to); |
| 1048 | return; |
| 1049 | } |
| 1050 | if (trans->atom->type == XML_REGEXP_CHARVAL) |
| 1051 | fprintf(output, "char %c ", trans->atom->codepoint); |
| 1052 | fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to); |
| 1053 | } |
| 1054 | |
| 1055 | static void |
| 1056 | xmlRegPrintState(FILE *output, xmlRegStatePtr state) { |
| 1057 | int i; |
| 1058 | |
| 1059 | fprintf(output, " state: "); |
| 1060 | if (state == NULL) { |
| 1061 | fprintf(output, "NULL\n"); |
| 1062 | return; |
| 1063 | } |
| 1064 | if (state->type == XML_REGEXP_START_STATE) |
| 1065 | fprintf(output, "START "); |
| 1066 | if (state->type == XML_REGEXP_FINAL_STATE) |
| 1067 | fprintf(output, "FINAL "); |
| 1068 | |
| 1069 | fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans); |
| 1070 | for (i = 0;i < state->nbTrans; i++) { |
| 1071 | xmlRegPrintTrans(output, &(state->trans[i])); |
| 1072 | } |
| 1073 | } |
| 1074 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1075 | #ifdef DEBUG_REGEXP_GRAPH |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1076 | static void |
| 1077 | xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) { |
| 1078 | int i; |
| 1079 | |
| 1080 | fprintf(output, " ctxt: "); |
| 1081 | if (ctxt == NULL) { |
| 1082 | fprintf(output, "NULL\n"); |
| 1083 | return; |
| 1084 | } |
| 1085 | fprintf(output, "'%s' ", ctxt->string); |
| 1086 | if (ctxt->error) |
| 1087 | fprintf(output, "error "); |
| 1088 | if (ctxt->neg) |
| 1089 | fprintf(output, "neg "); |
| 1090 | fprintf(output, "\n"); |
| 1091 | fprintf(output, "%d atoms:\n", ctxt->nbAtoms); |
| 1092 | for (i = 0;i < ctxt->nbAtoms; i++) { |
| 1093 | fprintf(output, " %02d ", i); |
| 1094 | xmlRegPrintAtom(output, ctxt->atoms[i]); |
| 1095 | } |
| 1096 | if (ctxt->atom != NULL) { |
| 1097 | fprintf(output, "current atom:\n"); |
| 1098 | xmlRegPrintAtom(output, ctxt->atom); |
| 1099 | } |
| 1100 | fprintf(output, "%d states:", ctxt->nbStates); |
| 1101 | if (ctxt->start != NULL) |
| 1102 | fprintf(output, " start: %d", ctxt->start->no); |
| 1103 | if (ctxt->end != NULL) |
| 1104 | fprintf(output, " end: %d", ctxt->end->no); |
| 1105 | fprintf(output, "\n"); |
| 1106 | for (i = 0;i < ctxt->nbStates; i++) { |
| 1107 | xmlRegPrintState(output, ctxt->states[i]); |
| 1108 | } |
| 1109 | fprintf(output, "%d counters:\n", ctxt->nbCounters); |
| 1110 | for (i = 0;i < ctxt->nbCounters; i++) { |
| 1111 | fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min, |
| 1112 | ctxt->counters[i].max); |
| 1113 | } |
| 1114 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1115 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1116 | |
| 1117 | /************************************************************************ |
| 1118 | * * |
| 1119 | * Finite Automata structures manipulations * |
| 1120 | * * |
| 1121 | ************************************************************************/ |
| 1122 | |
| 1123 | static void |
| 1124 | xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom, |
| 1125 | int neg, xmlRegAtomType type, int start, int end, |
| 1126 | xmlChar *blockName) { |
| 1127 | xmlRegRangePtr range; |
| 1128 | |
| 1129 | if (atom == NULL) { |
| 1130 | ERROR("add range: atom is NULL"); |
| 1131 | return; |
| 1132 | } |
| 1133 | if (atom->type != XML_REGEXP_RANGES) { |
| 1134 | ERROR("add range: atom is not ranges"); |
| 1135 | return; |
| 1136 | } |
| 1137 | if (atom->maxRanges == 0) { |
| 1138 | atom->maxRanges = 4; |
| 1139 | atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges * |
| 1140 | sizeof(xmlRegRangePtr)); |
| 1141 | if (atom->ranges == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1142 | xmlRegexpErrMemory(ctxt, "adding ranges"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1143 | atom->maxRanges = 0; |
| 1144 | return; |
| 1145 | } |
| 1146 | } else if (atom->nbRanges >= atom->maxRanges) { |
| 1147 | xmlRegRangePtr *tmp; |
| 1148 | atom->maxRanges *= 2; |
| 1149 | tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges * |
| 1150 | sizeof(xmlRegRangePtr)); |
| 1151 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1152 | xmlRegexpErrMemory(ctxt, "adding ranges"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1153 | atom->maxRanges /= 2; |
| 1154 | return; |
| 1155 | } |
| 1156 | atom->ranges = tmp; |
| 1157 | } |
| 1158 | range = xmlRegNewRange(ctxt, neg, type, start, end); |
| 1159 | if (range == NULL) |
| 1160 | return; |
| 1161 | range->blockName = blockName; |
| 1162 | atom->ranges[atom->nbRanges++] = range; |
| 1163 | |
| 1164 | } |
| 1165 | |
| 1166 | static int |
| 1167 | xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) { |
| 1168 | if (ctxt->maxCounters == 0) { |
| 1169 | ctxt->maxCounters = 4; |
| 1170 | ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters * |
| 1171 | sizeof(xmlRegCounter)); |
| 1172 | if (ctxt->counters == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1173 | xmlRegexpErrMemory(ctxt, "allocating counter"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1174 | ctxt->maxCounters = 0; |
| 1175 | return(-1); |
| 1176 | } |
| 1177 | } else if (ctxt->nbCounters >= ctxt->maxCounters) { |
| 1178 | xmlRegCounter *tmp; |
| 1179 | ctxt->maxCounters *= 2; |
| 1180 | tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters * |
| 1181 | sizeof(xmlRegCounter)); |
| 1182 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1183 | xmlRegexpErrMemory(ctxt, "allocating counter"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1184 | ctxt->maxCounters /= 2; |
| 1185 | return(-1); |
| 1186 | } |
| 1187 | ctxt->counters = tmp; |
| 1188 | } |
| 1189 | ctxt->counters[ctxt->nbCounters].min = -1; |
| 1190 | ctxt->counters[ctxt->nbCounters].max = -1; |
| 1191 | return(ctxt->nbCounters++); |
| 1192 | } |
| 1193 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1194 | static int |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1195 | xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) { |
| 1196 | if (atom == NULL) { |
| 1197 | ERROR("atom push: atom is NULL"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1198 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1199 | } |
| 1200 | if (ctxt->maxAtoms == 0) { |
| 1201 | ctxt->maxAtoms = 4; |
| 1202 | ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms * |
| 1203 | sizeof(xmlRegAtomPtr)); |
| 1204 | if (ctxt->atoms == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1205 | xmlRegexpErrMemory(ctxt, "pushing atom"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1206 | ctxt->maxAtoms = 0; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1207 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1208 | } |
| 1209 | } else if (ctxt->nbAtoms >= ctxt->maxAtoms) { |
| 1210 | xmlRegAtomPtr *tmp; |
| 1211 | ctxt->maxAtoms *= 2; |
| 1212 | tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms * |
| 1213 | sizeof(xmlRegAtomPtr)); |
| 1214 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1215 | xmlRegexpErrMemory(ctxt, "allocating counter"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1216 | ctxt->maxAtoms /= 2; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1217 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1218 | } |
| 1219 | ctxt->atoms = tmp; |
| 1220 | } |
| 1221 | atom->no = ctxt->nbAtoms; |
| 1222 | ctxt->atoms[ctxt->nbAtoms++] = atom; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1223 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | static void |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1227 | xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target, |
| 1228 | int from) { |
| 1229 | if (target->maxTransTo == 0) { |
| 1230 | target->maxTransTo = 8; |
| 1231 | target->transTo = (int *) xmlMalloc(target->maxTransTo * |
| 1232 | sizeof(int)); |
| 1233 | if (target->transTo == NULL) { |
| 1234 | xmlRegexpErrMemory(ctxt, "adding transition"); |
| 1235 | target->maxTransTo = 0; |
| 1236 | return; |
| 1237 | } |
| 1238 | } else if (target->nbTransTo >= target->maxTransTo) { |
| 1239 | int *tmp; |
| 1240 | target->maxTransTo *= 2; |
| 1241 | tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo * |
| 1242 | sizeof(int)); |
| 1243 | if (tmp == NULL) { |
| 1244 | xmlRegexpErrMemory(ctxt, "adding transition"); |
| 1245 | target->maxTransTo /= 2; |
| 1246 | return; |
| 1247 | } |
| 1248 | target->transTo = tmp; |
| 1249 | } |
| 1250 | target->transTo[target->nbTransTo] = from; |
| 1251 | target->nbTransTo++; |
| 1252 | } |
| 1253 | |
| 1254 | static void |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1255 | xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state, |
| 1256 | xmlRegAtomPtr atom, xmlRegStatePtr target, |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1257 | int counter, int count) { |
William M. Brack | f9b5fa2 | 2004-05-10 07:52:15 +0000 | [diff] [blame] | 1258 | |
| 1259 | int nrtrans; |
| 1260 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1261 | if (state == NULL) { |
| 1262 | ERROR("add state: state is NULL"); |
| 1263 | return; |
| 1264 | } |
| 1265 | if (target == NULL) { |
| 1266 | ERROR("add state: target is NULL"); |
| 1267 | return; |
| 1268 | } |
William M. Brack | f9b5fa2 | 2004-05-10 07:52:15 +0000 | [diff] [blame] | 1269 | /* |
| 1270 | * Other routines follow the philosophy 'When in doubt, add a transition' |
| 1271 | * so we check here whether such a transition is already present and, if |
| 1272 | * so, silently ignore this request. |
| 1273 | */ |
| 1274 | |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1275 | for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) { |
| 1276 | xmlRegTransPtr trans = &(state->trans[nrtrans]); |
| 1277 | if ((trans->atom == atom) && |
| 1278 | (trans->to == target->no) && |
| 1279 | (trans->counter == counter) && |
| 1280 | (trans->count == count)) { |
William M. Brack | f9b5fa2 | 2004-05-10 07:52:15 +0000 | [diff] [blame] | 1281 | #ifdef DEBUG_REGEXP_GRAPH |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1282 | printf("Ignoring duplicate transition from %d to %d\n", |
| 1283 | state->no, target->no); |
William M. Brack | f9b5fa2 | 2004-05-10 07:52:15 +0000 | [diff] [blame] | 1284 | #endif |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1285 | return; |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1286 | } |
William M. Brack | f9b5fa2 | 2004-05-10 07:52:15 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1289 | if (state->maxTrans == 0) { |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1290 | state->maxTrans = 8; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1291 | state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans * |
| 1292 | sizeof(xmlRegTrans)); |
| 1293 | if (state->trans == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1294 | xmlRegexpErrMemory(ctxt, "adding transition"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1295 | state->maxTrans = 0; |
| 1296 | return; |
| 1297 | } |
| 1298 | } else if (state->nbTrans >= state->maxTrans) { |
| 1299 | xmlRegTrans *tmp; |
| 1300 | state->maxTrans *= 2; |
| 1301 | tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans * |
| 1302 | sizeof(xmlRegTrans)); |
| 1303 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1304 | xmlRegexpErrMemory(ctxt, "adding transition"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1305 | state->maxTrans /= 2; |
| 1306 | return; |
| 1307 | } |
| 1308 | state->trans = tmp; |
| 1309 | } |
| 1310 | #ifdef DEBUG_REGEXP_GRAPH |
| 1311 | printf("Add trans from %d to %d ", state->no, target->no); |
Daniel Veillard | 8a001f6 | 2002-04-20 07:24:11 +0000 | [diff] [blame] | 1312 | if (count == REGEXP_ALL_COUNTER) |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 1313 | printf("all transition\n"); |
Daniel Veillard | 4402ab4 | 2002-09-12 16:02:56 +0000 | [diff] [blame] | 1314 | else if (count >= 0) |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 1315 | printf("count based %d\n", count); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1316 | else if (counter >= 0) |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 1317 | printf("counted %d\n", counter); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1318 | else if (atom == NULL) |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 1319 | printf("epsilon transition\n"); |
| 1320 | else if (atom != NULL) |
| 1321 | xmlRegPrintAtom(stdout, atom); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1322 | #endif |
| 1323 | |
| 1324 | state->trans[state->nbTrans].atom = atom; |
| 1325 | state->trans[state->nbTrans].to = target->no; |
| 1326 | state->trans[state->nbTrans].counter = counter; |
| 1327 | state->trans[state->nbTrans].count = count; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 1328 | state->trans[state->nbTrans].nd = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1329 | state->nbTrans++; |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1330 | xmlRegStateAddTransTo(ctxt, target, state->no); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1333 | static int |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1334 | xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1335 | if (state == NULL) return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1336 | if (ctxt->maxStates == 0) { |
| 1337 | ctxt->maxStates = 4; |
| 1338 | ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates * |
| 1339 | sizeof(xmlRegStatePtr)); |
| 1340 | if (ctxt->states == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1341 | xmlRegexpErrMemory(ctxt, "adding state"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1342 | ctxt->maxStates = 0; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1343 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1344 | } |
| 1345 | } else if (ctxt->nbStates >= ctxt->maxStates) { |
| 1346 | xmlRegStatePtr *tmp; |
| 1347 | ctxt->maxStates *= 2; |
| 1348 | tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates * |
| 1349 | sizeof(xmlRegStatePtr)); |
| 1350 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 1351 | xmlRegexpErrMemory(ctxt, "adding state"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1352 | ctxt->maxStates /= 2; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1353 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1354 | } |
| 1355 | ctxt->states = tmp; |
| 1356 | } |
| 1357 | state->no = ctxt->nbStates; |
| 1358 | ctxt->states[ctxt->nbStates++] = state; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1359 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | /** |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1363 | * xmlFAGenerateAllTransition: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1364 | * @ctxt: a regexp parser context |
| 1365 | * @from: the from state |
| 1366 | * @to: the target state or NULL for building a new one |
| 1367 | * @lax: |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1368 | * |
| 1369 | */ |
| 1370 | static void |
| 1371 | xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt, |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1372 | xmlRegStatePtr from, xmlRegStatePtr to, |
| 1373 | int lax) { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1374 | if (to == NULL) { |
| 1375 | to = xmlRegNewState(ctxt); |
| 1376 | xmlRegStatePush(ctxt, to); |
| 1377 | ctxt->state = to; |
| 1378 | } |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1379 | if (lax) |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1380 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER); |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1381 | else |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1382 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1386 | * xmlFAGenerateEpsilonTransition: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1387 | * @ctxt: a regexp parser context |
| 1388 | * @from: the from state |
| 1389 | * @to: the target state or NULL for building a new one |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1390 | * |
| 1391 | */ |
| 1392 | static void |
| 1393 | xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt, |
| 1394 | xmlRegStatePtr from, xmlRegStatePtr to) { |
| 1395 | if (to == NULL) { |
| 1396 | to = xmlRegNewState(ctxt); |
| 1397 | xmlRegStatePush(ctxt, to); |
| 1398 | ctxt->state = to; |
| 1399 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1400 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | /** |
| 1404 | * xmlFAGenerateCountedEpsilonTransition: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1405 | * @ctxt: a regexp parser context |
| 1406 | * @from: the from state |
| 1407 | * @to: the target state or NULL for building a new one |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1408 | * counter: the counter for that transition |
| 1409 | * |
| 1410 | */ |
| 1411 | static void |
| 1412 | xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt, |
| 1413 | xmlRegStatePtr from, xmlRegStatePtr to, int counter) { |
| 1414 | if (to == NULL) { |
| 1415 | to = xmlRegNewState(ctxt); |
| 1416 | xmlRegStatePush(ctxt, to); |
| 1417 | ctxt->state = to; |
| 1418 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1419 | xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | /** |
| 1423 | * xmlFAGenerateCountedTransition: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1424 | * @ctxt: a regexp parser context |
| 1425 | * @from: the from state |
| 1426 | * @to: the target state or NULL for building a new one |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1427 | * counter: the counter for that transition |
| 1428 | * |
| 1429 | */ |
| 1430 | static void |
| 1431 | xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt, |
| 1432 | xmlRegStatePtr from, xmlRegStatePtr to, int counter) { |
| 1433 | if (to == NULL) { |
| 1434 | to = xmlRegNewState(ctxt); |
| 1435 | xmlRegStatePush(ctxt, to); |
| 1436 | ctxt->state = to; |
| 1437 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1438 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
| 1441 | /** |
| 1442 | * xmlFAGenerateTransitions: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1443 | * @ctxt: a regexp parser context |
| 1444 | * @from: the from state |
| 1445 | * @to: the target state or NULL for building a new one |
| 1446 | * @atom: the atom generating the transition |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1447 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1448 | * Returns 0 if success and -1 in case of error. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1449 | */ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1450 | static int |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1451 | xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from, |
| 1452 | xmlRegStatePtr to, xmlRegAtomPtr atom) { |
| 1453 | if (atom == NULL) { |
| 1454 | ERROR("genrate transition: atom == NULL"); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1455 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1456 | } |
| 1457 | if (atom->type == XML_REGEXP_SUBREG) { |
| 1458 | /* |
| 1459 | * this is a subexpression handling one should not need to |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1460 | * create a new node except for XML_REGEXP_QUANT_RANGE. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1461 | */ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1462 | if (xmlRegAtomPush(ctxt, atom) < 0) { |
| 1463 | return(-1); |
| 1464 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1465 | if ((to != NULL) && (atom->stop != to) && |
| 1466 | (atom->quant != XML_REGEXP_QUANT_RANGE)) { |
| 1467 | /* |
| 1468 | * Generate an epsilon transition to link to the target |
| 1469 | */ |
| 1470 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to); |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 1471 | #ifdef DV |
| 1472 | } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) && |
| 1473 | (atom->quant != XML_REGEXP_QUANT_ONCE)) { |
| 1474 | to = xmlRegNewState(ctxt); |
| 1475 | xmlRegStatePush(ctxt, to); |
| 1476 | ctxt->state = to; |
| 1477 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to); |
| 1478 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1479 | } |
| 1480 | switch (atom->quant) { |
| 1481 | case XML_REGEXP_QUANT_OPT: |
| 1482 | atom->quant = XML_REGEXP_QUANT_ONCE; |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 1483 | /* |
| 1484 | * transition done to the state after end of atom. |
| 1485 | * 1. set transition from atom start to new state |
| 1486 | * 2. set transition from atom end to this state. |
| 1487 | */ |
| 1488 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0); |
| 1489 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, ctxt->state); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1490 | break; |
| 1491 | case XML_REGEXP_QUANT_MULT: |
| 1492 | atom->quant = XML_REGEXP_QUANT_ONCE; |
| 1493 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop); |
| 1494 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start); |
| 1495 | break; |
| 1496 | case XML_REGEXP_QUANT_PLUS: |
| 1497 | atom->quant = XML_REGEXP_QUANT_ONCE; |
| 1498 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start); |
| 1499 | break; |
| 1500 | case XML_REGEXP_QUANT_RANGE: { |
| 1501 | int counter; |
| 1502 | xmlRegStatePtr newstate; |
| 1503 | |
| 1504 | /* |
| 1505 | * This one is nasty: |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1506 | * 1/ if range has minOccurs == 0, create a new state |
| 1507 | * and create epsilon transitions from atom->start |
| 1508 | * to atom->stop, as well as atom->start to the new |
| 1509 | * state |
| 1510 | * 2/ register a new counter |
| 1511 | * 3/ register an epsilon transition associated to |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1512 | * this counter going from atom->stop to atom->start |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1513 | * 4/ create a new state |
| 1514 | * 5/ generate a counted transition from atom->stop to |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1515 | * that state |
| 1516 | */ |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1517 | if (atom->min == 0) { |
| 1518 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, |
| 1519 | atom->stop); |
| 1520 | newstate = xmlRegNewState(ctxt); |
| 1521 | xmlRegStatePush(ctxt, newstate); |
| 1522 | ctxt->state = newstate; |
| 1523 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, |
| 1524 | newstate); |
| 1525 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1526 | counter = xmlRegGetCounter(ctxt); |
| 1527 | ctxt->counters[counter].min = atom->min - 1; |
| 1528 | ctxt->counters[counter].max = atom->max - 1; |
| 1529 | atom->min = 0; |
| 1530 | atom->max = 0; |
| 1531 | atom->quant = XML_REGEXP_QUANT_ONCE; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1532 | if (to != NULL) { |
| 1533 | newstate = to; |
| 1534 | } else { |
| 1535 | newstate = xmlRegNewState(ctxt); |
| 1536 | xmlRegStatePush(ctxt, newstate); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1537 | } |
Daniel Veillard | 9a00fd2 | 2005-11-09 08:56:26 +0000 | [diff] [blame] | 1538 | ctxt->state = newstate; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1539 | xmlFAGenerateCountedTransition(ctxt, atom->stop, |
| 1540 | newstate, counter); |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 1541 | |
| 1542 | /* |
| 1543 | * first check count and if OK jump forward; |
| 1544 | * if checking fail increment count and jump back |
| 1545 | */ |
| 1546 | xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop, |
| 1547 | atom->start, counter); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1548 | } |
| 1549 | default: |
| 1550 | break; |
| 1551 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1552 | return(0); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 1553 | } |
| 1554 | if ((atom->min == 0) && (atom->max == 0) && |
Daniel Veillard | 99c394d | 2005-07-14 12:58:49 +0000 | [diff] [blame] | 1555 | (atom->quant == XML_REGEXP_QUANT_RANGE)) { |
| 1556 | /* |
| 1557 | * we can discard the atom and generate an epsilon transition instead |
| 1558 | */ |
| 1559 | if (to == NULL) { |
| 1560 | to = xmlRegNewState(ctxt); |
| 1561 | if (to != NULL) |
| 1562 | xmlRegStatePush(ctxt, to); |
| 1563 | else { |
| 1564 | return(-1); |
| 1565 | } |
| 1566 | } |
| 1567 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
| 1568 | ctxt->state = to; |
| 1569 | xmlRegFreeAtom(atom); |
| 1570 | return(0); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 1571 | } |
| 1572 | if (to == NULL) { |
| 1573 | to = xmlRegNewState(ctxt); |
| 1574 | if (to != NULL) |
| 1575 | xmlRegStatePush(ctxt, to); |
| 1576 | else { |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1577 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1578 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1579 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 1580 | if (xmlRegAtomPush(ctxt, atom) < 0) { |
| 1581 | return(-1); |
| 1582 | } |
| 1583 | xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1); |
| 1584 | ctxt->state = to; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1585 | switch (atom->quant) { |
| 1586 | case XML_REGEXP_QUANT_OPT: |
| 1587 | atom->quant = XML_REGEXP_QUANT_ONCE; |
| 1588 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
| 1589 | break; |
| 1590 | case XML_REGEXP_QUANT_MULT: |
| 1591 | atom->quant = XML_REGEXP_QUANT_ONCE; |
| 1592 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1593 | xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1594 | break; |
| 1595 | case XML_REGEXP_QUANT_PLUS: |
| 1596 | atom->quant = XML_REGEXP_QUANT_ONCE; |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1597 | xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1598 | break; |
| 1599 | default: |
| 1600 | break; |
| 1601 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1602 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | /** |
| 1606 | * xmlFAReduceEpsilonTransitions: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1607 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1608 | * @fromnr: the from state |
| 1609 | * @tonr: the to state |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1610 | * @counter: should that transition be associated to a counted |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1611 | * |
| 1612 | */ |
| 1613 | static void |
| 1614 | xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr, |
| 1615 | int tonr, int counter) { |
| 1616 | int transnr; |
| 1617 | xmlRegStatePtr from; |
| 1618 | xmlRegStatePtr to; |
| 1619 | |
| 1620 | #ifdef DEBUG_REGEXP_GRAPH |
| 1621 | printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr); |
| 1622 | #endif |
| 1623 | from = ctxt->states[fromnr]; |
| 1624 | if (from == NULL) |
| 1625 | return; |
| 1626 | to = ctxt->states[tonr]; |
| 1627 | if (to == NULL) |
| 1628 | return; |
| 1629 | if ((to->mark == XML_REGEXP_MARK_START) || |
| 1630 | (to->mark == XML_REGEXP_MARK_VISITED)) |
| 1631 | return; |
| 1632 | |
| 1633 | to->mark = XML_REGEXP_MARK_VISITED; |
| 1634 | if (to->type == XML_REGEXP_FINAL_STATE) { |
| 1635 | #ifdef DEBUG_REGEXP_GRAPH |
| 1636 | printf("State %d is final, so %d becomes final\n", tonr, fromnr); |
| 1637 | #endif |
| 1638 | from->type = XML_REGEXP_FINAL_STATE; |
| 1639 | } |
| 1640 | for (transnr = 0;transnr < to->nbTrans;transnr++) { |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1641 | if (to->trans[transnr].to < 0) |
| 1642 | continue; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1643 | if (to->trans[transnr].atom == NULL) { |
| 1644 | /* |
| 1645 | * Don't remove counted transitions |
| 1646 | * Don't loop either |
| 1647 | */ |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1648 | if (to->trans[transnr].to != fromnr) { |
| 1649 | if (to->trans[transnr].count >= 0) { |
| 1650 | int newto = to->trans[transnr].to; |
| 1651 | |
| 1652 | xmlRegStateAddTrans(ctxt, from, NULL, |
| 1653 | ctxt->states[newto], |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1654 | -1, to->trans[transnr].count); |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1655 | } else { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1656 | #ifdef DEBUG_REGEXP_GRAPH |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1657 | printf("Found epsilon trans %d from %d to %d\n", |
| 1658 | transnr, tonr, to->trans[transnr].to); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1659 | #endif |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1660 | if (to->trans[transnr].counter >= 0) { |
| 1661 | xmlFAReduceEpsilonTransitions(ctxt, fromnr, |
| 1662 | to->trans[transnr].to, |
| 1663 | to->trans[transnr].counter); |
| 1664 | } else { |
| 1665 | xmlFAReduceEpsilonTransitions(ctxt, fromnr, |
| 1666 | to->trans[transnr].to, |
| 1667 | counter); |
| 1668 | } |
| 1669 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1670 | } |
| 1671 | } else { |
| 1672 | int newto = to->trans[transnr].to; |
| 1673 | |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1674 | if (to->trans[transnr].counter >= 0) { |
| 1675 | xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom, |
| 1676 | ctxt->states[newto], |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1677 | to->trans[transnr].counter, -1); |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1678 | } else { |
| 1679 | xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom, |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 1680 | ctxt->states[newto], counter, -1); |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1681 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1682 | } |
| 1683 | } |
| 1684 | to->mark = XML_REGEXP_MARK_NORMAL; |
| 1685 | } |
| 1686 | |
| 1687 | /** |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1688 | * xmlFAEliminateSimpleEpsilonTransitions: |
| 1689 | * @ctxt: a regexp parser context |
| 1690 | * |
| 1691 | * Eliminating general epsilon transitions can get costly in the general |
| 1692 | * algorithm due to the large amount of generated new transitions and |
| 1693 | * associated comparisons. However for simple epsilon transition used just |
| 1694 | * to separate building blocks when generating the automata this can be |
| 1695 | * reduced to state elimination: |
| 1696 | * - if there exists an epsilon from X to Y |
| 1697 | * - if there is no other transition from X |
| 1698 | * then X and Y are semantically equivalent and X can be eliminated |
| 1699 | * If X is the start state then make Y the start state, else replace the |
| 1700 | * target of all transitions to X by transitions to Y. |
| 1701 | */ |
| 1702 | static void |
| 1703 | xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) { |
| 1704 | int statenr, i, j, newto; |
| 1705 | xmlRegStatePtr state, tmp; |
| 1706 | |
| 1707 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1708 | state = ctxt->states[statenr]; |
| 1709 | if (state == NULL) |
| 1710 | continue; |
| 1711 | if (state->nbTrans != 1) |
| 1712 | continue; |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 1713 | if (state->type == XML_REGEXP_UNREACH_STATE) |
| 1714 | continue; |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1715 | /* is the only transition out a basic transition */ |
| 1716 | if ((state->trans[0].atom == NULL) && |
| 1717 | (state->trans[0].to >= 0) && |
| 1718 | (state->trans[0].to != statenr) && |
| 1719 | (state->trans[0].counter < 0) && |
| 1720 | (state->trans[0].count < 0)) { |
| 1721 | newto = state->trans[0].to; |
| 1722 | |
| 1723 | if (state->type == XML_REGEXP_START_STATE) { |
| 1724 | #ifdef DEBUG_REGEXP_GRAPH |
| 1725 | printf("Found simple epsilon trans from start %d to %d\n", |
| 1726 | statenr, newto); |
| 1727 | #endif |
| 1728 | } else { |
| 1729 | #ifdef DEBUG_REGEXP_GRAPH |
| 1730 | printf("Found simple epsilon trans from %d to %d\n", |
| 1731 | statenr, newto); |
| 1732 | #endif |
| 1733 | for (i = 0;i < state->nbTransTo;i++) { |
| 1734 | tmp = ctxt->states[state->transTo[i]]; |
| 1735 | for (j = 0;j < tmp->nbTrans;j++) { |
| 1736 | if (tmp->trans[j].to == statenr) { |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1737 | #ifdef DEBUG_REGEXP_GRAPH |
| 1738 | printf("Changed transition %d on %d to go to %d\n", |
| 1739 | j, tmp->no, newto); |
| 1740 | #endif |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 1741 | tmp->trans[j].to = -1; |
| 1742 | xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom, |
| 1743 | ctxt->states[newto], |
| 1744 | tmp->trans[j].counter, |
| 1745 | tmp->trans[j].count); |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1746 | } |
| 1747 | } |
| 1748 | } |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1749 | if (state->type == XML_REGEXP_FINAL_STATE) |
| 1750 | ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE; |
| 1751 | /* eliminate the transition completely */ |
| 1752 | state->nbTrans = 0; |
| 1753 | |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 1754 | state->type = XML_REGEXP_UNREACH_STATE; |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1755 | |
| 1756 | } |
| 1757 | |
| 1758 | } |
| 1759 | } |
| 1760 | } |
| 1761 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1762 | * xmlFAEliminateEpsilonTransitions: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1763 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1764 | * |
| 1765 | */ |
| 1766 | static void |
| 1767 | xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) { |
| 1768 | int statenr, transnr; |
| 1769 | xmlRegStatePtr state; |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1770 | int has_epsilon; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1771 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1772 | if (ctxt->states == NULL) return; |
| 1773 | |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 1774 | /* |
| 1775 | * Eliminate simple epsilon transition and the associated unreachable |
| 1776 | * states. |
| 1777 | */ |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1778 | xmlFAEliminateSimpleEpsilonTransitions(ctxt); |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 1779 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1780 | state = ctxt->states[statenr]; |
| 1781 | if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) { |
| 1782 | #ifdef DEBUG_REGEXP_GRAPH |
| 1783 | printf("Removed unreachable state %d\n", statenr); |
| 1784 | #endif |
| 1785 | xmlRegFreeState(state); |
| 1786 | ctxt->states[statenr] = NULL; |
| 1787 | } |
| 1788 | } |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1789 | |
| 1790 | has_epsilon = 0; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1791 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1792 | /* |
Daniel Veillard | fcd18ff | 2006-11-02 10:28:04 +0000 | [diff] [blame] | 1793 | * Build the completed transitions bypassing the epsilons |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1794 | * Use a marking algorithm to avoid loops |
Daniel Veillard | fcd18ff | 2006-11-02 10:28:04 +0000 | [diff] [blame] | 1795 | * Mark sink states too. |
| 1796 | * Process from the latests states backward to the start when |
| 1797 | * there is long cascading epsilon chains this minimize the |
| 1798 | * recursions and transition compares when adding the new ones |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1799 | */ |
Daniel Veillard | fcd18ff | 2006-11-02 10:28:04 +0000 | [diff] [blame] | 1800 | for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1801 | state = ctxt->states[statenr]; |
| 1802 | if (state == NULL) |
| 1803 | continue; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 1804 | if ((state->nbTrans == 0) && |
| 1805 | (state->type != XML_REGEXP_FINAL_STATE)) { |
| 1806 | state->type = XML_REGEXP_SINK_STATE; |
| 1807 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1808 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 1809 | if ((state->trans[transnr].atom == NULL) && |
| 1810 | (state->trans[transnr].to >= 0)) { |
| 1811 | if (state->trans[transnr].to == statenr) { |
| 1812 | state->trans[transnr].to = -1; |
| 1813 | #ifdef DEBUG_REGEXP_GRAPH |
| 1814 | printf("Removed loopback epsilon trans %d on %d\n", |
| 1815 | transnr, statenr); |
| 1816 | #endif |
| 1817 | } else if (state->trans[transnr].count < 0) { |
| 1818 | int newto = state->trans[transnr].to; |
| 1819 | |
| 1820 | #ifdef DEBUG_REGEXP_GRAPH |
| 1821 | printf("Found epsilon trans %d from %d to %d\n", |
| 1822 | transnr, statenr, newto); |
| 1823 | #endif |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1824 | has_epsilon = 1; |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 1825 | state->trans[transnr].to = -2; |
| 1826 | state->mark = XML_REGEXP_MARK_START; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1827 | xmlFAReduceEpsilonTransitions(ctxt, statenr, |
| 1828 | newto, state->trans[transnr].counter); |
| 1829 | state->mark = XML_REGEXP_MARK_NORMAL; |
| 1830 | #ifdef DEBUG_REGEXP_GRAPH |
| 1831 | } else { |
| 1832 | printf("Found counted transition %d on %d\n", |
| 1833 | transnr, statenr); |
| 1834 | #endif |
| 1835 | } |
| 1836 | } |
| 1837 | } |
| 1838 | } |
| 1839 | /* |
| 1840 | * Eliminate the epsilon transitions |
| 1841 | */ |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1842 | if (has_epsilon) { |
| 1843 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1844 | state = ctxt->states[statenr]; |
| 1845 | if (state == NULL) |
| 1846 | continue; |
| 1847 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 1848 | xmlRegTransPtr trans = &(state->trans[transnr]); |
| 1849 | if ((trans->atom == NULL) && |
| 1850 | (trans->count < 0) && |
| 1851 | (trans->to >= 0)) { |
| 1852 | trans->to = -1; |
| 1853 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1854 | } |
| 1855 | } |
| 1856 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1857 | |
| 1858 | /* |
| 1859 | * Use this pass to detect unreachable states too |
| 1860 | */ |
| 1861 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1862 | state = ctxt->states[statenr]; |
| 1863 | if (state != NULL) |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1864 | state->reached = XML_REGEXP_MARK_NORMAL; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1865 | } |
| 1866 | state = ctxt->states[0]; |
| 1867 | if (state != NULL) |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1868 | state->reached = XML_REGEXP_MARK_START; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1869 | while (state != NULL) { |
| 1870 | xmlRegStatePtr target = NULL; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1871 | state->reached = XML_REGEXP_MARK_VISITED; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1872 | /* |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1873 | * Mark all states reachable from the current reachable state |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1874 | */ |
| 1875 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 1876 | if ((state->trans[transnr].to >= 0) && |
| 1877 | ((state->trans[transnr].atom != NULL) || |
| 1878 | (state->trans[transnr].count >= 0))) { |
| 1879 | int newto = state->trans[transnr].to; |
| 1880 | |
| 1881 | if (ctxt->states[newto] == NULL) |
| 1882 | continue; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1883 | if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) { |
| 1884 | ctxt->states[newto]->reached = XML_REGEXP_MARK_START; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1885 | target = ctxt->states[newto]; |
| 1886 | } |
| 1887 | } |
| 1888 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 1889 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1890 | /* |
| 1891 | * find the next accessible state not explored |
| 1892 | */ |
| 1893 | if (target == NULL) { |
| 1894 | for (statenr = 1;statenr < ctxt->nbStates;statenr++) { |
| 1895 | state = ctxt->states[statenr]; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1896 | if ((state != NULL) && (state->reached == |
| 1897 | XML_REGEXP_MARK_START)) { |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1898 | target = state; |
| 1899 | break; |
| 1900 | } |
| 1901 | } |
| 1902 | } |
| 1903 | state = target; |
| 1904 | } |
| 1905 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1906 | state = ctxt->states[statenr]; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1907 | if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) { |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1908 | #ifdef DEBUG_REGEXP_GRAPH |
| 1909 | printf("Removed unreachable state %d\n", statenr); |
| 1910 | #endif |
| 1911 | xmlRegFreeState(state); |
| 1912 | ctxt->states[statenr] = NULL; |
| 1913 | } |
| 1914 | } |
| 1915 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 1918 | static int |
| 1919 | xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) { |
| 1920 | int ret = 0; |
| 1921 | |
| 1922 | if ((range1->type == XML_REGEXP_RANGES) || |
| 1923 | (range2->type == XML_REGEXP_RANGES) || |
| 1924 | (range2->type == XML_REGEXP_SUBREG) || |
| 1925 | (range1->type == XML_REGEXP_SUBREG) || |
| 1926 | (range1->type == XML_REGEXP_STRING) || |
| 1927 | (range2->type == XML_REGEXP_STRING)) |
| 1928 | return(-1); |
| 1929 | |
| 1930 | /* put them in order */ |
| 1931 | if (range1->type > range2->type) { |
| 1932 | xmlRegRangePtr tmp; |
| 1933 | |
| 1934 | tmp = range1; |
| 1935 | range1 = range2; |
| 1936 | range2 = tmp; |
| 1937 | } |
| 1938 | if ((range1->type == XML_REGEXP_ANYCHAR) || |
| 1939 | (range2->type == XML_REGEXP_ANYCHAR)) { |
| 1940 | ret = 1; |
| 1941 | } else if ((range1->type == XML_REGEXP_EPSILON) || |
| 1942 | (range2->type == XML_REGEXP_EPSILON)) { |
| 1943 | return(0); |
| 1944 | } else if (range1->type == range2->type) { |
| 1945 | if ((range1->type != XML_REGEXP_CHARVAL) || |
| 1946 | (range1->end < range2->start) || |
| 1947 | (range2->end < range1->start)) |
| 1948 | ret = 1; |
| 1949 | else |
| 1950 | ret = 0; |
| 1951 | } else if (range1->type == XML_REGEXP_CHARVAL) { |
| 1952 | int codepoint; |
| 1953 | int neg = 0; |
| 1954 | |
| 1955 | /* |
| 1956 | * just check all codepoints in the range for acceptance, |
| 1957 | * this is usually way cheaper since done only once at |
| 1958 | * compilation than testing over and over at runtime or |
| 1959 | * pushing too many states when evaluating. |
| 1960 | */ |
| 1961 | if (((range1->neg == 0) && (range2->neg != 0)) || |
| 1962 | ((range1->neg != 0) && (range2->neg == 0))) |
| 1963 | neg = 1; |
| 1964 | |
| 1965 | for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) { |
| 1966 | ret = xmlRegCheckCharacterRange(range2->type, codepoint, |
| 1967 | 0, range2->start, range2->end, |
| 1968 | range2->blockName); |
| 1969 | if (ret < 0) |
| 1970 | return(-1); |
| 1971 | if (((neg == 1) && (ret == 0)) || |
| 1972 | ((neg == 0) && (ret == 1))) |
| 1973 | return(1); |
| 1974 | } |
| 1975 | return(0); |
| 1976 | } else if ((range1->type == XML_REGEXP_BLOCK_NAME) || |
| 1977 | (range2->type == XML_REGEXP_BLOCK_NAME)) { |
| 1978 | if (range1->type == range2->type) { |
| 1979 | ret = xmlStrEqual(range1->blockName, range2->blockName); |
| 1980 | } else { |
| 1981 | /* |
| 1982 | * comparing a block range with anything else is way |
| 1983 | * too costly, and maintining the table is like too much |
| 1984 | * memory too, so let's force the automata to save state |
| 1985 | * here. |
| 1986 | */ |
| 1987 | return(1); |
| 1988 | } |
| 1989 | } else if ((range1->type < XML_REGEXP_LETTER) || |
| 1990 | (range2->type < XML_REGEXP_LETTER)) { |
| 1991 | if ((range1->type == XML_REGEXP_ANYSPACE) && |
| 1992 | (range2->type == XML_REGEXP_NOTSPACE)) |
| 1993 | ret = 0; |
| 1994 | else if ((range1->type == XML_REGEXP_INITNAME) && |
| 1995 | (range2->type == XML_REGEXP_NOTINITNAME)) |
| 1996 | ret = 0; |
| 1997 | else if ((range1->type == XML_REGEXP_NAMECHAR) && |
| 1998 | (range2->type == XML_REGEXP_NOTNAMECHAR)) |
| 1999 | ret = 0; |
| 2000 | else if ((range1->type == XML_REGEXP_DECIMAL) && |
| 2001 | (range2->type == XML_REGEXP_NOTDECIMAL)) |
| 2002 | ret = 0; |
| 2003 | else if ((range1->type == XML_REGEXP_REALCHAR) && |
| 2004 | (range2->type == XML_REGEXP_NOTREALCHAR)) |
| 2005 | ret = 0; |
| 2006 | else { |
| 2007 | /* same thing to limit complexity */ |
| 2008 | return(1); |
| 2009 | } |
| 2010 | } else { |
| 2011 | ret = 0; |
| 2012 | /* range1->type < range2->type here */ |
| 2013 | switch (range1->type) { |
| 2014 | case XML_REGEXP_LETTER: |
| 2015 | /* all disjoint except in the subgroups */ |
| 2016 | if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) || |
| 2017 | (range2->type == XML_REGEXP_LETTER_LOWERCASE) || |
| 2018 | (range2->type == XML_REGEXP_LETTER_TITLECASE) || |
| 2019 | (range2->type == XML_REGEXP_LETTER_MODIFIER) || |
| 2020 | (range2->type == XML_REGEXP_LETTER_OTHERS)) |
| 2021 | ret = 1; |
| 2022 | break; |
| 2023 | case XML_REGEXP_MARK: |
| 2024 | if ((range2->type == XML_REGEXP_MARK_NONSPACING) || |
| 2025 | (range2->type == XML_REGEXP_MARK_SPACECOMBINING) || |
| 2026 | (range2->type == XML_REGEXP_MARK_ENCLOSING)) |
| 2027 | ret = 1; |
| 2028 | break; |
| 2029 | case XML_REGEXP_NUMBER: |
| 2030 | if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) || |
| 2031 | (range2->type == XML_REGEXP_NUMBER_LETTER) || |
| 2032 | (range2->type == XML_REGEXP_NUMBER_OTHERS)) |
| 2033 | ret = 1; |
| 2034 | break; |
| 2035 | case XML_REGEXP_PUNCT: |
| 2036 | if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) || |
| 2037 | (range2->type == XML_REGEXP_PUNCT_DASH) || |
| 2038 | (range2->type == XML_REGEXP_PUNCT_OPEN) || |
| 2039 | (range2->type == XML_REGEXP_PUNCT_CLOSE) || |
| 2040 | (range2->type == XML_REGEXP_PUNCT_INITQUOTE) || |
| 2041 | (range2->type == XML_REGEXP_PUNCT_FINQUOTE) || |
| 2042 | (range2->type == XML_REGEXP_PUNCT_OTHERS)) |
| 2043 | ret = 1; |
| 2044 | break; |
| 2045 | case XML_REGEXP_SEPAR: |
| 2046 | if ((range2->type == XML_REGEXP_SEPAR_SPACE) || |
| 2047 | (range2->type == XML_REGEXP_SEPAR_LINE) || |
| 2048 | (range2->type == XML_REGEXP_SEPAR_PARA)) |
| 2049 | ret = 1; |
| 2050 | break; |
| 2051 | case XML_REGEXP_SYMBOL: |
| 2052 | if ((range2->type == XML_REGEXP_SYMBOL_MATH) || |
| 2053 | (range2->type == XML_REGEXP_SYMBOL_CURRENCY) || |
| 2054 | (range2->type == XML_REGEXP_SYMBOL_MODIFIER) || |
| 2055 | (range2->type == XML_REGEXP_SYMBOL_OTHERS)) |
| 2056 | ret = 1; |
| 2057 | break; |
| 2058 | case XML_REGEXP_OTHER: |
| 2059 | if ((range2->type == XML_REGEXP_OTHER_CONTROL) || |
| 2060 | (range2->type == XML_REGEXP_OTHER_FORMAT) || |
| 2061 | (range2->type == XML_REGEXP_OTHER_PRIVATE)) |
| 2062 | ret = 1; |
| 2063 | break; |
| 2064 | default: |
| 2065 | if ((range2->type >= XML_REGEXP_LETTER) && |
| 2066 | (range2->type < XML_REGEXP_BLOCK_NAME)) |
| 2067 | ret = 0; |
| 2068 | else { |
| 2069 | /* safety net ! */ |
| 2070 | return(1); |
| 2071 | } |
| 2072 | } |
| 2073 | } |
| 2074 | if (((range1->neg == 0) && (range2->neg != 0)) || |
| 2075 | ((range1->neg != 0) && (range2->neg == 0))) |
| 2076 | ret = !ret; |
| 2077 | return(1); |
| 2078 | } |
| 2079 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2080 | /** |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2081 | * xmlFACompareAtomTypes: |
| 2082 | * @type1: an atom type |
| 2083 | * @type2: an atom type |
| 2084 | * |
| 2085 | * Compares two atoms type to check whether they intersect in some ways, |
| 2086 | * this is used by xmlFACompareAtoms only |
| 2087 | * |
| 2088 | * Returns 1 if they may intersect and 0 otherwise |
| 2089 | */ |
| 2090 | static int |
| 2091 | xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) { |
| 2092 | if ((type1 == XML_REGEXP_EPSILON) || |
| 2093 | (type1 == XML_REGEXP_CHARVAL) || |
| 2094 | (type1 == XML_REGEXP_RANGES) || |
| 2095 | (type1 == XML_REGEXP_SUBREG) || |
| 2096 | (type1 == XML_REGEXP_STRING) || |
| 2097 | (type1 == XML_REGEXP_ANYCHAR)) |
| 2098 | return(1); |
| 2099 | if ((type2 == XML_REGEXP_EPSILON) || |
| 2100 | (type2 == XML_REGEXP_CHARVAL) || |
| 2101 | (type2 == XML_REGEXP_RANGES) || |
| 2102 | (type2 == XML_REGEXP_SUBREG) || |
| 2103 | (type2 == XML_REGEXP_STRING) || |
| 2104 | (type2 == XML_REGEXP_ANYCHAR)) |
| 2105 | return(1); |
| 2106 | |
| 2107 | if (type1 == type2) return(1); |
| 2108 | |
| 2109 | /* simplify subsequent compares by making sure type1 < type2 */ |
| 2110 | if (type1 > type2) { |
| 2111 | xmlRegAtomType tmp = type1; |
| 2112 | type1 = type2; |
| 2113 | type2 = tmp; |
| 2114 | } |
| 2115 | switch (type1) { |
| 2116 | case XML_REGEXP_ANYSPACE: /* \s */ |
| 2117 | /* can't be a letter, number, mark, pontuation, symbol */ |
| 2118 | if ((type2 == XML_REGEXP_NOTSPACE) || |
| 2119 | ((type2 >= XML_REGEXP_LETTER) && |
| 2120 | (type2 <= XML_REGEXP_LETTER_OTHERS)) || |
| 2121 | ((type2 >= XML_REGEXP_NUMBER) && |
| 2122 | (type2 <= XML_REGEXP_NUMBER_OTHERS)) || |
| 2123 | ((type2 >= XML_REGEXP_MARK) && |
| 2124 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
| 2125 | ((type2 >= XML_REGEXP_PUNCT) && |
| 2126 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
| 2127 | ((type2 >= XML_REGEXP_SYMBOL) && |
| 2128 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) |
| 2129 | ) return(0); |
| 2130 | break; |
| 2131 | case XML_REGEXP_NOTSPACE: /* \S */ |
| 2132 | break; |
| 2133 | case XML_REGEXP_INITNAME: /* \l */ |
| 2134 | /* can't be a number, mark, separator, pontuation, symbol or other */ |
| 2135 | if ((type2 == XML_REGEXP_NOTINITNAME) || |
| 2136 | ((type2 >= XML_REGEXP_NUMBER) && |
| 2137 | (type2 <= XML_REGEXP_NUMBER_OTHERS)) || |
| 2138 | ((type2 >= XML_REGEXP_MARK) && |
| 2139 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
| 2140 | ((type2 >= XML_REGEXP_SEPAR) && |
| 2141 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
| 2142 | ((type2 >= XML_REGEXP_PUNCT) && |
| 2143 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
| 2144 | ((type2 >= XML_REGEXP_SYMBOL) && |
| 2145 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
| 2146 | ((type2 >= XML_REGEXP_OTHER) && |
| 2147 | (type2 <= XML_REGEXP_OTHER_NA)) |
| 2148 | ) return(0); |
| 2149 | break; |
| 2150 | case XML_REGEXP_NOTINITNAME: /* \L */ |
| 2151 | break; |
| 2152 | case XML_REGEXP_NAMECHAR: /* \c */ |
| 2153 | /* can't be a mark, separator, pontuation, symbol or other */ |
| 2154 | if ((type2 == XML_REGEXP_NOTNAMECHAR) || |
| 2155 | ((type2 >= XML_REGEXP_MARK) && |
| 2156 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
| 2157 | ((type2 >= XML_REGEXP_PUNCT) && |
| 2158 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
| 2159 | ((type2 >= XML_REGEXP_SEPAR) && |
| 2160 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
| 2161 | ((type2 >= XML_REGEXP_SYMBOL) && |
| 2162 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
| 2163 | ((type2 >= XML_REGEXP_OTHER) && |
| 2164 | (type2 <= XML_REGEXP_OTHER_NA)) |
| 2165 | ) return(0); |
| 2166 | break; |
| 2167 | case XML_REGEXP_NOTNAMECHAR: /* \C */ |
| 2168 | break; |
| 2169 | case XML_REGEXP_DECIMAL: /* \d */ |
| 2170 | /* can't be a letter, mark, separator, pontuation, symbol or other */ |
| 2171 | if ((type2 == XML_REGEXP_NOTDECIMAL) || |
| 2172 | (type2 == XML_REGEXP_REALCHAR) || |
| 2173 | ((type2 >= XML_REGEXP_LETTER) && |
| 2174 | (type2 <= XML_REGEXP_LETTER_OTHERS)) || |
| 2175 | ((type2 >= XML_REGEXP_MARK) && |
| 2176 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
| 2177 | ((type2 >= XML_REGEXP_PUNCT) && |
| 2178 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
| 2179 | ((type2 >= XML_REGEXP_SEPAR) && |
| 2180 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
| 2181 | ((type2 >= XML_REGEXP_SYMBOL) && |
| 2182 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
| 2183 | ((type2 >= XML_REGEXP_OTHER) && |
| 2184 | (type2 <= XML_REGEXP_OTHER_NA)) |
| 2185 | )return(0); |
| 2186 | break; |
| 2187 | case XML_REGEXP_NOTDECIMAL: /* \D */ |
| 2188 | break; |
| 2189 | case XML_REGEXP_REALCHAR: /* \w */ |
| 2190 | /* can't be a mark, separator, pontuation, symbol or other */ |
| 2191 | if ((type2 == XML_REGEXP_NOTDECIMAL) || |
| 2192 | ((type2 >= XML_REGEXP_MARK) && |
| 2193 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
| 2194 | ((type2 >= XML_REGEXP_PUNCT) && |
| 2195 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
| 2196 | ((type2 >= XML_REGEXP_SEPAR) && |
| 2197 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
| 2198 | ((type2 >= XML_REGEXP_SYMBOL) && |
| 2199 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
| 2200 | ((type2 >= XML_REGEXP_OTHER) && |
| 2201 | (type2 <= XML_REGEXP_OTHER_NA)) |
| 2202 | )return(0); |
| 2203 | break; |
| 2204 | case XML_REGEXP_NOTREALCHAR: /* \W */ |
| 2205 | break; |
| 2206 | /* |
| 2207 | * at that point we know both type 1 and type2 are from |
| 2208 | * character categories are ordered and are different, |
| 2209 | * it becomes simple because this is a partition |
| 2210 | */ |
| 2211 | case XML_REGEXP_LETTER: |
| 2212 | if (type2 <= XML_REGEXP_LETTER_OTHERS) |
| 2213 | return(1); |
| 2214 | return(0); |
| 2215 | case XML_REGEXP_LETTER_UPPERCASE: |
| 2216 | case XML_REGEXP_LETTER_LOWERCASE: |
| 2217 | case XML_REGEXP_LETTER_TITLECASE: |
| 2218 | case XML_REGEXP_LETTER_MODIFIER: |
| 2219 | case XML_REGEXP_LETTER_OTHERS: |
| 2220 | return(0); |
| 2221 | case XML_REGEXP_MARK: |
| 2222 | if (type2 <= XML_REGEXP_MARK_ENCLOSING) |
| 2223 | return(1); |
| 2224 | return(0); |
| 2225 | case XML_REGEXP_MARK_NONSPACING: |
| 2226 | case XML_REGEXP_MARK_SPACECOMBINING: |
| 2227 | case XML_REGEXP_MARK_ENCLOSING: |
| 2228 | return(0); |
| 2229 | case XML_REGEXP_NUMBER: |
| 2230 | if (type2 <= XML_REGEXP_NUMBER_OTHERS) |
| 2231 | return(1); |
| 2232 | return(0); |
| 2233 | case XML_REGEXP_NUMBER_DECIMAL: |
| 2234 | case XML_REGEXP_NUMBER_LETTER: |
| 2235 | case XML_REGEXP_NUMBER_OTHERS: |
| 2236 | return(0); |
| 2237 | case XML_REGEXP_PUNCT: |
| 2238 | if (type2 <= XML_REGEXP_PUNCT_OTHERS) |
| 2239 | return(1); |
| 2240 | return(0); |
| 2241 | case XML_REGEXP_PUNCT_CONNECTOR: |
| 2242 | case XML_REGEXP_PUNCT_DASH: |
| 2243 | case XML_REGEXP_PUNCT_OPEN: |
| 2244 | case XML_REGEXP_PUNCT_CLOSE: |
| 2245 | case XML_REGEXP_PUNCT_INITQUOTE: |
| 2246 | case XML_REGEXP_PUNCT_FINQUOTE: |
| 2247 | case XML_REGEXP_PUNCT_OTHERS: |
| 2248 | return(0); |
| 2249 | case XML_REGEXP_SEPAR: |
| 2250 | if (type2 <= XML_REGEXP_SEPAR_PARA) |
| 2251 | return(1); |
| 2252 | return(0); |
| 2253 | case XML_REGEXP_SEPAR_SPACE: |
| 2254 | case XML_REGEXP_SEPAR_LINE: |
| 2255 | case XML_REGEXP_SEPAR_PARA: |
| 2256 | return(0); |
| 2257 | case XML_REGEXP_SYMBOL: |
| 2258 | if (type2 <= XML_REGEXP_SYMBOL_OTHERS) |
| 2259 | return(1); |
| 2260 | return(0); |
| 2261 | case XML_REGEXP_SYMBOL_MATH: |
| 2262 | case XML_REGEXP_SYMBOL_CURRENCY: |
| 2263 | case XML_REGEXP_SYMBOL_MODIFIER: |
| 2264 | case XML_REGEXP_SYMBOL_OTHERS: |
| 2265 | return(0); |
| 2266 | case XML_REGEXP_OTHER: |
| 2267 | if (type2 <= XML_REGEXP_OTHER_NA) |
| 2268 | return(1); |
| 2269 | return(0); |
| 2270 | case XML_REGEXP_OTHER_CONTROL: |
| 2271 | case XML_REGEXP_OTHER_FORMAT: |
| 2272 | case XML_REGEXP_OTHER_PRIVATE: |
| 2273 | case XML_REGEXP_OTHER_NA: |
| 2274 | return(0); |
| 2275 | default: |
| 2276 | break; |
| 2277 | } |
| 2278 | return(1); |
| 2279 | } |
| 2280 | |
| 2281 | /** |
| 2282 | * xmlFAEqualAtoms: |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2283 | * @atom1: an atom |
| 2284 | * @atom2: an atom |
| 2285 | * |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2286 | * Compares two atoms to check whether they are the same exactly |
| 2287 | * this is used to remove equivalent transitions |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2288 | * |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2289 | * Returns 1 if same and 0 otherwise |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2290 | */ |
| 2291 | static int |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2292 | xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) { |
| 2293 | int ret = 0; |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 2294 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2295 | if (atom1 == atom2) |
| 2296 | return(1); |
| 2297 | if ((atom1 == NULL) || (atom2 == NULL)) |
| 2298 | return(0); |
| 2299 | |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2300 | if (atom1->type != atom2->type) |
| 2301 | return(0); |
| 2302 | switch (atom1->type) { |
| 2303 | case XML_REGEXP_EPSILON: |
| 2304 | ret = 0; |
| 2305 | break; |
| 2306 | case XML_REGEXP_STRING: |
| 2307 | ret = xmlStrEqual((xmlChar *)atom1->valuep, |
| 2308 | (xmlChar *)atom2->valuep); |
| 2309 | break; |
| 2310 | case XML_REGEXP_CHARVAL: |
| 2311 | ret = (atom1->codepoint == atom2->codepoint); |
| 2312 | break; |
| 2313 | case XML_REGEXP_RANGES: |
| 2314 | /* too hard to do in the general case */ |
| 2315 | ret = 0; |
| 2316 | default: |
| 2317 | break; |
| 2318 | } |
| 2319 | return(ret); |
| 2320 | } |
| 2321 | |
| 2322 | /** |
| 2323 | * xmlFACompareAtoms: |
| 2324 | * @atom1: an atom |
| 2325 | * @atom2: an atom |
| 2326 | * |
| 2327 | * Compares two atoms to check whether they intersect in some ways, |
| 2328 | * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only |
| 2329 | * |
| 2330 | * Returns 1 if yes and 0 otherwise |
| 2331 | */ |
| 2332 | static int |
| 2333 | xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) { |
| 2334 | int ret = 1; |
| 2335 | |
| 2336 | if (atom1 == atom2) |
| 2337 | return(1); |
| 2338 | if ((atom1 == NULL) || (atom2 == NULL)) |
| 2339 | return(0); |
| 2340 | |
| 2341 | if ((atom1->type == XML_REGEXP_ANYCHAR) || |
| 2342 | (atom2->type == XML_REGEXP_ANYCHAR)) |
| 2343 | return(1); |
| 2344 | |
| 2345 | if (atom1->type > atom2->type) { |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2346 | xmlRegAtomPtr tmp; |
| 2347 | tmp = atom1; |
| 2348 | atom1 = atom2; |
| 2349 | atom2 = tmp; |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2350 | } |
| 2351 | if (atom1->type != atom2->type) { |
| 2352 | ret = xmlFACompareAtomTypes(atom1->type, atom2->type); |
| 2353 | /* if they can't intersect at the type level break now */ |
| 2354 | if (ret == 0) |
| 2355 | return(0); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2356 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2357 | switch (atom1->type) { |
| 2358 | case XML_REGEXP_STRING: |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 2359 | ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep, |
| 2360 | (xmlChar *)atom2->valuep); |
| 2361 | break; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2362 | case XML_REGEXP_EPSILON: |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2363 | goto not_determinist; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2364 | case XML_REGEXP_CHARVAL: |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2365 | if (atom2->type == XML_REGEXP_CHARVAL) { |
| 2366 | ret = (atom1->codepoint == atom2->codepoint); |
| 2367 | } else { |
| 2368 | ret = xmlRegCheckCharacter(atom2, atom1->codepoint); |
| 2369 | if (ret < 0) |
| 2370 | ret = 1; |
| 2371 | } |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 2372 | break; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2373 | case XML_REGEXP_RANGES: |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2374 | if (atom2->type == XML_REGEXP_RANGES) { |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2375 | int i, j, res; |
| 2376 | xmlRegRangePtr r1, r2; |
| 2377 | |
| 2378 | /* |
| 2379 | * need to check that none of the ranges eventually matches |
| 2380 | */ |
| 2381 | for (i = 0;i < atom1->nbRanges;i++) { |
| 2382 | for (j = 0;j < atom2->nbRanges;j++) { |
| 2383 | r1 = atom1->ranges[i]; |
| 2384 | r2 = atom2->ranges[j]; |
| 2385 | res = xmlFACompareRanges(r1, r2); |
| 2386 | if (res == 1) { |
| 2387 | ret = 1; |
| 2388 | goto done; |
| 2389 | } |
| 2390 | } |
| 2391 | } |
| 2392 | ret = 0; |
| 2393 | } |
| 2394 | break; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2395 | default: |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2396 | goto not_determinist; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2397 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2398 | done: |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 2399 | if (atom1->neg != atom2->neg) { |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 2400 | ret = !ret; |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 2401 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2402 | if (ret == 0) |
| 2403 | return(0); |
| 2404 | not_determinist: |
| 2405 | return(1); |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | /** |
| 2409 | * xmlFARecurseDeterminism: |
| 2410 | * @ctxt: a regexp parser context |
| 2411 | * |
| 2412 | * Check whether the associated regexp is determinist, |
| 2413 | * should be called after xmlFAEliminateEpsilonTransitions() |
| 2414 | * |
| 2415 | */ |
| 2416 | static int |
| 2417 | xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state, |
| 2418 | int to, xmlRegAtomPtr atom) { |
| 2419 | int ret = 1; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2420 | int res; |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 2421 | int transnr, nbTrans; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2422 | xmlRegTransPtr t1; |
| 2423 | |
| 2424 | if (state == NULL) |
| 2425 | return(ret); |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 2426 | /* |
| 2427 | * don't recurse on transitions potentially added in the course of |
| 2428 | * the elimination. |
| 2429 | */ |
| 2430 | nbTrans = state->nbTrans; |
| 2431 | for (transnr = 0;transnr < nbTrans;transnr++) { |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2432 | t1 = &(state->trans[transnr]); |
| 2433 | /* |
| 2434 | * check transitions conflicting with the one looked at |
| 2435 | */ |
| 2436 | if (t1->atom == NULL) { |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 2437 | if (t1->to < 0) |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2438 | continue; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2439 | res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to], |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2440 | to, atom); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2441 | if (res == 0) { |
| 2442 | ret = 0; |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 2443 | /* t1->nd = 1; */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2444 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2445 | continue; |
| 2446 | } |
| 2447 | if (t1->to != to) |
| 2448 | continue; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2449 | if (xmlFACompareAtoms(t1->atom, atom)) { |
| 2450 | ret = 0; |
| 2451 | /* mark the transition as non-deterministic */ |
| 2452 | t1->nd = 1; |
| 2453 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2454 | } |
| 2455 | return(ret); |
| 2456 | } |
| 2457 | |
| 2458 | /** |
| 2459 | * xmlFAComputesDeterminism: |
| 2460 | * @ctxt: a regexp parser context |
| 2461 | * |
| 2462 | * Check whether the associated regexp is determinist, |
| 2463 | * should be called after xmlFAEliminateEpsilonTransitions() |
| 2464 | * |
| 2465 | */ |
| 2466 | static int |
| 2467 | xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) { |
| 2468 | int statenr, transnr; |
| 2469 | xmlRegStatePtr state; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2470 | xmlRegTransPtr t1, t2, last; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2471 | int i; |
| 2472 | int ret = 1; |
| 2473 | |
Daniel Veillard | 4402ab4 | 2002-09-12 16:02:56 +0000 | [diff] [blame] | 2474 | #ifdef DEBUG_REGEXP_GRAPH |
| 2475 | printf("xmlFAComputesDeterminism\n"); |
| 2476 | xmlRegPrintCtxt(stdout, ctxt); |
| 2477 | #endif |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2478 | if (ctxt->determinist != -1) |
| 2479 | return(ctxt->determinist); |
| 2480 | |
| 2481 | /* |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2482 | * First cleanup the automata removing cancelled transitions |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2483 | */ |
| 2484 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 2485 | state = ctxt->states[statenr]; |
| 2486 | if (state == NULL) |
| 2487 | continue; |
Daniel Veillard | 4f82c8a | 2005-08-09 21:40:08 +0000 | [diff] [blame] | 2488 | if (state->nbTrans < 2) |
| 2489 | continue; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2490 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 2491 | t1 = &(state->trans[transnr]); |
| 2492 | /* |
| 2493 | * Determinism checks in case of counted or all transitions |
| 2494 | * will have to be handled separately |
| 2495 | */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2496 | if (t1->atom == NULL) { |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 2497 | /* t1->nd = 1; */ |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2498 | continue; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2499 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2500 | if (t1->to == -1) /* eliminated */ |
| 2501 | continue; |
| 2502 | for (i = 0;i < transnr;i++) { |
| 2503 | t2 = &(state->trans[i]); |
| 2504 | if (t2->to == -1) /* eliminated */ |
| 2505 | continue; |
| 2506 | if (t2->atom != NULL) { |
| 2507 | if (t1->to == t2->to) { |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2508 | if (xmlFAEqualAtoms(t1->atom, t2->atom)) |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 2509 | t2->to = -1; /* eliminated */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2510 | } |
| 2511 | } |
| 2512 | } |
| 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | /* |
| 2517 | * Check for all states that there aren't 2 transitions |
| 2518 | * with the same atom and a different target. |
| 2519 | */ |
| 2520 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 2521 | state = ctxt->states[statenr]; |
| 2522 | if (state == NULL) |
| 2523 | continue; |
| 2524 | if (state->nbTrans < 2) |
| 2525 | continue; |
| 2526 | last = NULL; |
| 2527 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 2528 | t1 = &(state->trans[transnr]); |
| 2529 | /* |
| 2530 | * Determinism checks in case of counted or all transitions |
| 2531 | * will have to be handled separately |
| 2532 | */ |
| 2533 | if (t1->atom == NULL) { |
| 2534 | continue; |
| 2535 | } |
| 2536 | if (t1->to == -1) /* eliminated */ |
| 2537 | continue; |
| 2538 | for (i = 0;i < transnr;i++) { |
| 2539 | t2 = &(state->trans[i]); |
| 2540 | if (t2->to == -1) /* eliminated */ |
| 2541 | continue; |
| 2542 | if (t2->atom != NULL) { |
| 2543 | /* not determinist ! */ |
| 2544 | if (xmlFACompareAtoms(t1->atom, t2->atom)) { |
| 2545 | ret = 0; |
| 2546 | /* mark the transitions as non-deterministic ones */ |
| 2547 | t1->nd = 1; |
| 2548 | t2->nd = 1; |
| 2549 | last = t1; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2550 | } |
| 2551 | } else if (t1->to != -1) { |
| 2552 | /* |
| 2553 | * do the closure in case of remaining specific |
| 2554 | * epsilon transitions like choices or all |
| 2555 | */ |
| 2556 | ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to], |
| 2557 | t2->to, t2->atom); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2558 | /* don't shortcut the computation so all non deterministic |
| 2559 | transition get marked down |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2560 | if (ret == 0) |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 2561 | return(0); |
| 2562 | */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2563 | if (ret == 0) { |
| 2564 | t1->nd = 1; |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 2565 | /* t2->nd = 1; */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2566 | last = t1; |
| 2567 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2568 | } |
| 2569 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2570 | /* don't shortcut the computation so all non deterministic |
| 2571 | transition get marked down |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2572 | if (ret == 0) |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2573 | break; */ |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2574 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2575 | |
| 2576 | /* |
| 2577 | * mark specifically the last non-deterministic transition |
| 2578 | * from a state since there is no need to set-up rollback |
| 2579 | * from it |
| 2580 | */ |
| 2581 | if (last != NULL) { |
| 2582 | last->nd = 2; |
| 2583 | } |
| 2584 | |
| 2585 | /* don't shortcut the computation so all non deterministic |
| 2586 | transition get marked down |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2587 | if (ret == 0) |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2588 | break; */ |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2589 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2590 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2591 | ctxt->determinist = ret; |
| 2592 | return(ret); |
| 2593 | } |
| 2594 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2595 | /************************************************************************ |
| 2596 | * * |
| 2597 | * Routines to check input against transition atoms * |
| 2598 | * * |
| 2599 | ************************************************************************/ |
| 2600 | |
| 2601 | static int |
| 2602 | xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg, |
| 2603 | int start, int end, const xmlChar *blockName) { |
| 2604 | int ret = 0; |
| 2605 | |
| 2606 | switch (type) { |
| 2607 | case XML_REGEXP_STRING: |
| 2608 | case XML_REGEXP_SUBREG: |
| 2609 | case XML_REGEXP_RANGES: |
| 2610 | case XML_REGEXP_EPSILON: |
| 2611 | return(-1); |
| 2612 | case XML_REGEXP_ANYCHAR: |
| 2613 | ret = ((codepoint != '\n') && (codepoint != '\r')); |
| 2614 | break; |
| 2615 | case XML_REGEXP_CHARVAL: |
| 2616 | ret = ((codepoint >= start) && (codepoint <= end)); |
| 2617 | break; |
| 2618 | case XML_REGEXP_NOTSPACE: |
| 2619 | neg = !neg; |
| 2620 | case XML_REGEXP_ANYSPACE: |
| 2621 | ret = ((codepoint == '\n') || (codepoint == '\r') || |
| 2622 | (codepoint == '\t') || (codepoint == ' ')); |
| 2623 | break; |
| 2624 | case XML_REGEXP_NOTINITNAME: |
| 2625 | neg = !neg; |
| 2626 | case XML_REGEXP_INITNAME: |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2627 | ret = (IS_LETTER(codepoint) || |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2628 | (codepoint == '_') || (codepoint == ':')); |
| 2629 | break; |
| 2630 | case XML_REGEXP_NOTNAMECHAR: |
| 2631 | neg = !neg; |
| 2632 | case XML_REGEXP_NAMECHAR: |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2633 | ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) || |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2634 | (codepoint == '.') || (codepoint == '-') || |
| 2635 | (codepoint == '_') || (codepoint == ':') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2636 | IS_COMBINING(codepoint) || IS_EXTENDER(codepoint)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2637 | break; |
| 2638 | case XML_REGEXP_NOTDECIMAL: |
| 2639 | neg = !neg; |
| 2640 | case XML_REGEXP_DECIMAL: |
| 2641 | ret = xmlUCSIsCatNd(codepoint); |
| 2642 | break; |
| 2643 | case XML_REGEXP_REALCHAR: |
| 2644 | neg = !neg; |
| 2645 | case XML_REGEXP_NOTREALCHAR: |
| 2646 | ret = xmlUCSIsCatP(codepoint); |
| 2647 | if (ret == 0) |
| 2648 | ret = xmlUCSIsCatZ(codepoint); |
| 2649 | if (ret == 0) |
| 2650 | ret = xmlUCSIsCatC(codepoint); |
| 2651 | break; |
| 2652 | case XML_REGEXP_LETTER: |
| 2653 | ret = xmlUCSIsCatL(codepoint); |
| 2654 | break; |
| 2655 | case XML_REGEXP_LETTER_UPPERCASE: |
| 2656 | ret = xmlUCSIsCatLu(codepoint); |
| 2657 | break; |
| 2658 | case XML_REGEXP_LETTER_LOWERCASE: |
| 2659 | ret = xmlUCSIsCatLl(codepoint); |
| 2660 | break; |
| 2661 | case XML_REGEXP_LETTER_TITLECASE: |
| 2662 | ret = xmlUCSIsCatLt(codepoint); |
| 2663 | break; |
| 2664 | case XML_REGEXP_LETTER_MODIFIER: |
| 2665 | ret = xmlUCSIsCatLm(codepoint); |
| 2666 | break; |
| 2667 | case XML_REGEXP_LETTER_OTHERS: |
| 2668 | ret = xmlUCSIsCatLo(codepoint); |
| 2669 | break; |
| 2670 | case XML_REGEXP_MARK: |
| 2671 | ret = xmlUCSIsCatM(codepoint); |
| 2672 | break; |
| 2673 | case XML_REGEXP_MARK_NONSPACING: |
| 2674 | ret = xmlUCSIsCatMn(codepoint); |
| 2675 | break; |
| 2676 | case XML_REGEXP_MARK_SPACECOMBINING: |
| 2677 | ret = xmlUCSIsCatMc(codepoint); |
| 2678 | break; |
| 2679 | case XML_REGEXP_MARK_ENCLOSING: |
| 2680 | ret = xmlUCSIsCatMe(codepoint); |
| 2681 | break; |
| 2682 | case XML_REGEXP_NUMBER: |
| 2683 | ret = xmlUCSIsCatN(codepoint); |
| 2684 | break; |
| 2685 | case XML_REGEXP_NUMBER_DECIMAL: |
| 2686 | ret = xmlUCSIsCatNd(codepoint); |
| 2687 | break; |
| 2688 | case XML_REGEXP_NUMBER_LETTER: |
| 2689 | ret = xmlUCSIsCatNl(codepoint); |
| 2690 | break; |
| 2691 | case XML_REGEXP_NUMBER_OTHERS: |
| 2692 | ret = xmlUCSIsCatNo(codepoint); |
| 2693 | break; |
| 2694 | case XML_REGEXP_PUNCT: |
| 2695 | ret = xmlUCSIsCatP(codepoint); |
| 2696 | break; |
| 2697 | case XML_REGEXP_PUNCT_CONNECTOR: |
| 2698 | ret = xmlUCSIsCatPc(codepoint); |
| 2699 | break; |
| 2700 | case XML_REGEXP_PUNCT_DASH: |
| 2701 | ret = xmlUCSIsCatPd(codepoint); |
| 2702 | break; |
| 2703 | case XML_REGEXP_PUNCT_OPEN: |
| 2704 | ret = xmlUCSIsCatPs(codepoint); |
| 2705 | break; |
| 2706 | case XML_REGEXP_PUNCT_CLOSE: |
| 2707 | ret = xmlUCSIsCatPe(codepoint); |
| 2708 | break; |
| 2709 | case XML_REGEXP_PUNCT_INITQUOTE: |
| 2710 | ret = xmlUCSIsCatPi(codepoint); |
| 2711 | break; |
| 2712 | case XML_REGEXP_PUNCT_FINQUOTE: |
| 2713 | ret = xmlUCSIsCatPf(codepoint); |
| 2714 | break; |
| 2715 | case XML_REGEXP_PUNCT_OTHERS: |
| 2716 | ret = xmlUCSIsCatPo(codepoint); |
| 2717 | break; |
| 2718 | case XML_REGEXP_SEPAR: |
| 2719 | ret = xmlUCSIsCatZ(codepoint); |
| 2720 | break; |
| 2721 | case XML_REGEXP_SEPAR_SPACE: |
| 2722 | ret = xmlUCSIsCatZs(codepoint); |
| 2723 | break; |
| 2724 | case XML_REGEXP_SEPAR_LINE: |
| 2725 | ret = xmlUCSIsCatZl(codepoint); |
| 2726 | break; |
| 2727 | case XML_REGEXP_SEPAR_PARA: |
| 2728 | ret = xmlUCSIsCatZp(codepoint); |
| 2729 | break; |
| 2730 | case XML_REGEXP_SYMBOL: |
| 2731 | ret = xmlUCSIsCatS(codepoint); |
| 2732 | break; |
| 2733 | case XML_REGEXP_SYMBOL_MATH: |
| 2734 | ret = xmlUCSIsCatSm(codepoint); |
| 2735 | break; |
| 2736 | case XML_REGEXP_SYMBOL_CURRENCY: |
| 2737 | ret = xmlUCSIsCatSc(codepoint); |
| 2738 | break; |
| 2739 | case XML_REGEXP_SYMBOL_MODIFIER: |
| 2740 | ret = xmlUCSIsCatSk(codepoint); |
| 2741 | break; |
| 2742 | case XML_REGEXP_SYMBOL_OTHERS: |
| 2743 | ret = xmlUCSIsCatSo(codepoint); |
| 2744 | break; |
| 2745 | case XML_REGEXP_OTHER: |
| 2746 | ret = xmlUCSIsCatC(codepoint); |
| 2747 | break; |
| 2748 | case XML_REGEXP_OTHER_CONTROL: |
| 2749 | ret = xmlUCSIsCatCc(codepoint); |
| 2750 | break; |
| 2751 | case XML_REGEXP_OTHER_FORMAT: |
| 2752 | ret = xmlUCSIsCatCf(codepoint); |
| 2753 | break; |
| 2754 | case XML_REGEXP_OTHER_PRIVATE: |
| 2755 | ret = xmlUCSIsCatCo(codepoint); |
| 2756 | break; |
| 2757 | case XML_REGEXP_OTHER_NA: |
| 2758 | /* ret = xmlUCSIsCatCn(codepoint); */ |
| 2759 | /* Seems it doesn't exist anymore in recent Unicode releases */ |
| 2760 | ret = 0; |
| 2761 | break; |
| 2762 | case XML_REGEXP_BLOCK_NAME: |
| 2763 | ret = xmlUCSIsBlock(codepoint, (const char *) blockName); |
| 2764 | break; |
| 2765 | } |
| 2766 | if (neg) |
| 2767 | return(!ret); |
| 2768 | return(ret); |
| 2769 | } |
| 2770 | |
| 2771 | static int |
| 2772 | xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) { |
| 2773 | int i, ret = 0; |
| 2774 | xmlRegRangePtr range; |
| 2775 | |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2776 | if ((atom == NULL) || (!IS_CHAR(codepoint))) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2777 | return(-1); |
| 2778 | |
| 2779 | switch (atom->type) { |
| 2780 | case XML_REGEXP_SUBREG: |
| 2781 | case XML_REGEXP_EPSILON: |
| 2782 | return(-1); |
| 2783 | case XML_REGEXP_CHARVAL: |
| 2784 | return(codepoint == atom->codepoint); |
| 2785 | case XML_REGEXP_RANGES: { |
| 2786 | int accept = 0; |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 2787 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2788 | for (i = 0;i < atom->nbRanges;i++) { |
| 2789 | range = atom->ranges[i]; |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 2790 | if (range->neg == 2) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2791 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
| 2792 | 0, range->start, range->end, |
| 2793 | range->blockName); |
| 2794 | if (ret != 0) |
| 2795 | return(0); /* excluded char */ |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 2796 | } else if (range->neg) { |
| 2797 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
| 2798 | 0, range->start, range->end, |
| 2799 | range->blockName); |
| 2800 | if (ret == 0) |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 2801 | accept = 1; |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 2802 | else |
| 2803 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2804 | } else { |
| 2805 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
| 2806 | 0, range->start, range->end, |
| 2807 | range->blockName); |
| 2808 | if (ret != 0) |
| 2809 | accept = 1; /* might still be excluded */ |
| 2810 | } |
| 2811 | } |
| 2812 | return(accept); |
| 2813 | } |
| 2814 | case XML_REGEXP_STRING: |
| 2815 | printf("TODO: XML_REGEXP_STRING\n"); |
| 2816 | return(-1); |
| 2817 | case XML_REGEXP_ANYCHAR: |
| 2818 | case XML_REGEXP_ANYSPACE: |
| 2819 | case XML_REGEXP_NOTSPACE: |
| 2820 | case XML_REGEXP_INITNAME: |
| 2821 | case XML_REGEXP_NOTINITNAME: |
| 2822 | case XML_REGEXP_NAMECHAR: |
| 2823 | case XML_REGEXP_NOTNAMECHAR: |
| 2824 | case XML_REGEXP_DECIMAL: |
| 2825 | case XML_REGEXP_NOTDECIMAL: |
| 2826 | case XML_REGEXP_REALCHAR: |
| 2827 | case XML_REGEXP_NOTREALCHAR: |
| 2828 | case XML_REGEXP_LETTER: |
| 2829 | case XML_REGEXP_LETTER_UPPERCASE: |
| 2830 | case XML_REGEXP_LETTER_LOWERCASE: |
| 2831 | case XML_REGEXP_LETTER_TITLECASE: |
| 2832 | case XML_REGEXP_LETTER_MODIFIER: |
| 2833 | case XML_REGEXP_LETTER_OTHERS: |
| 2834 | case XML_REGEXP_MARK: |
| 2835 | case XML_REGEXP_MARK_NONSPACING: |
| 2836 | case XML_REGEXP_MARK_SPACECOMBINING: |
| 2837 | case XML_REGEXP_MARK_ENCLOSING: |
| 2838 | case XML_REGEXP_NUMBER: |
| 2839 | case XML_REGEXP_NUMBER_DECIMAL: |
| 2840 | case XML_REGEXP_NUMBER_LETTER: |
| 2841 | case XML_REGEXP_NUMBER_OTHERS: |
| 2842 | case XML_REGEXP_PUNCT: |
| 2843 | case XML_REGEXP_PUNCT_CONNECTOR: |
| 2844 | case XML_REGEXP_PUNCT_DASH: |
| 2845 | case XML_REGEXP_PUNCT_OPEN: |
| 2846 | case XML_REGEXP_PUNCT_CLOSE: |
| 2847 | case XML_REGEXP_PUNCT_INITQUOTE: |
| 2848 | case XML_REGEXP_PUNCT_FINQUOTE: |
| 2849 | case XML_REGEXP_PUNCT_OTHERS: |
| 2850 | case XML_REGEXP_SEPAR: |
| 2851 | case XML_REGEXP_SEPAR_SPACE: |
| 2852 | case XML_REGEXP_SEPAR_LINE: |
| 2853 | case XML_REGEXP_SEPAR_PARA: |
| 2854 | case XML_REGEXP_SYMBOL: |
| 2855 | case XML_REGEXP_SYMBOL_MATH: |
| 2856 | case XML_REGEXP_SYMBOL_CURRENCY: |
| 2857 | case XML_REGEXP_SYMBOL_MODIFIER: |
| 2858 | case XML_REGEXP_SYMBOL_OTHERS: |
| 2859 | case XML_REGEXP_OTHER: |
| 2860 | case XML_REGEXP_OTHER_CONTROL: |
| 2861 | case XML_REGEXP_OTHER_FORMAT: |
| 2862 | case XML_REGEXP_OTHER_PRIVATE: |
| 2863 | case XML_REGEXP_OTHER_NA: |
| 2864 | case XML_REGEXP_BLOCK_NAME: |
| 2865 | ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0, |
| 2866 | (const xmlChar *)atom->valuep); |
| 2867 | if (atom->neg) |
| 2868 | ret = !ret; |
| 2869 | break; |
| 2870 | } |
| 2871 | return(ret); |
| 2872 | } |
| 2873 | |
| 2874 | /************************************************************************ |
| 2875 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 2876 | * Saving and restoring state of an execution context * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2877 | * * |
| 2878 | ************************************************************************/ |
| 2879 | |
| 2880 | #ifdef DEBUG_REGEXP_EXEC |
| 2881 | static void |
| 2882 | xmlFARegDebugExec(xmlRegExecCtxtPtr exec) { |
| 2883 | printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index); |
| 2884 | if (exec->inputStack != NULL) { |
| 2885 | int i; |
| 2886 | printf(": "); |
| 2887 | for (i = 0;(i < 3) && (i < exec->inputStackNr);i++) |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame] | 2888 | printf("%s ", (const char *) |
| 2889 | exec->inputStack[exec->inputStackNr - (i + 1)].value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2890 | } else { |
| 2891 | printf(": %s", &(exec->inputString[exec->index])); |
| 2892 | } |
| 2893 | printf("\n"); |
| 2894 | } |
| 2895 | #endif |
| 2896 | |
| 2897 | static void |
| 2898 | xmlFARegExecSave(xmlRegExecCtxtPtr exec) { |
| 2899 | #ifdef DEBUG_REGEXP_EXEC |
| 2900 | printf("saving "); |
| 2901 | exec->transno++; |
| 2902 | xmlFARegDebugExec(exec); |
| 2903 | exec->transno--; |
| 2904 | #endif |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 2905 | #ifdef MAX_PUSH |
| 2906 | if (exec->nbPush > MAX_PUSH) { |
| 2907 | return; |
| 2908 | } |
| 2909 | exec->nbPush++; |
| 2910 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2911 | |
| 2912 | if (exec->maxRollbacks == 0) { |
| 2913 | exec->maxRollbacks = 4; |
| 2914 | exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks * |
| 2915 | sizeof(xmlRegExecRollback)); |
| 2916 | if (exec->rollbacks == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2917 | xmlRegexpErrMemory(NULL, "saving regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2918 | exec->maxRollbacks = 0; |
| 2919 | return; |
| 2920 | } |
| 2921 | memset(exec->rollbacks, 0, |
| 2922 | exec->maxRollbacks * sizeof(xmlRegExecRollback)); |
| 2923 | } else if (exec->nbRollbacks >= exec->maxRollbacks) { |
| 2924 | xmlRegExecRollback *tmp; |
| 2925 | int len = exec->maxRollbacks; |
| 2926 | |
| 2927 | exec->maxRollbacks *= 2; |
| 2928 | tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks, |
| 2929 | exec->maxRollbacks * sizeof(xmlRegExecRollback)); |
| 2930 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2931 | xmlRegexpErrMemory(NULL, "saving regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2932 | exec->maxRollbacks /= 2; |
| 2933 | return; |
| 2934 | } |
| 2935 | exec->rollbacks = tmp; |
| 2936 | tmp = &exec->rollbacks[len]; |
| 2937 | memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback)); |
| 2938 | } |
| 2939 | exec->rollbacks[exec->nbRollbacks].state = exec->state; |
| 2940 | exec->rollbacks[exec->nbRollbacks].index = exec->index; |
| 2941 | exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1; |
| 2942 | if (exec->comp->nbCounters > 0) { |
| 2943 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
| 2944 | exec->rollbacks[exec->nbRollbacks].counts = (int *) |
| 2945 | xmlMalloc(exec->comp->nbCounters * sizeof(int)); |
| 2946 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2947 | xmlRegexpErrMemory(NULL, "saving regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2948 | exec->status = -5; |
| 2949 | return; |
| 2950 | } |
| 2951 | } |
| 2952 | memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts, |
| 2953 | exec->comp->nbCounters * sizeof(int)); |
| 2954 | } |
| 2955 | exec->nbRollbacks++; |
| 2956 | } |
| 2957 | |
| 2958 | static void |
| 2959 | xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) { |
| 2960 | if (exec->nbRollbacks <= 0) { |
| 2961 | exec->status = -1; |
| 2962 | #ifdef DEBUG_REGEXP_EXEC |
| 2963 | printf("rollback failed on empty stack\n"); |
| 2964 | #endif |
| 2965 | return; |
| 2966 | } |
| 2967 | exec->nbRollbacks--; |
| 2968 | exec->state = exec->rollbacks[exec->nbRollbacks].state; |
| 2969 | exec->index = exec->rollbacks[exec->nbRollbacks].index; |
| 2970 | exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch; |
| 2971 | if (exec->comp->nbCounters > 0) { |
| 2972 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
| 2973 | fprintf(stderr, "exec save: allocation failed"); |
| 2974 | exec->status = -6; |
| 2975 | return; |
| 2976 | } |
| 2977 | memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts, |
| 2978 | exec->comp->nbCounters * sizeof(int)); |
| 2979 | } |
| 2980 | |
| 2981 | #ifdef DEBUG_REGEXP_EXEC |
| 2982 | printf("restored "); |
| 2983 | xmlFARegDebugExec(exec); |
| 2984 | #endif |
| 2985 | } |
| 2986 | |
| 2987 | /************************************************************************ |
| 2988 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 2989 | * Verifier, running an input against a compiled regexp * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2990 | * * |
| 2991 | ************************************************************************/ |
| 2992 | |
| 2993 | static int |
| 2994 | xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) { |
| 2995 | xmlRegExecCtxt execval; |
| 2996 | xmlRegExecCtxtPtr exec = &execval; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2997 | int ret, codepoint = 0, len, deter; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2998 | |
| 2999 | exec->inputString = content; |
| 3000 | exec->index = 0; |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 3001 | exec->nbPush = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3002 | exec->determinist = 1; |
| 3003 | exec->maxRollbacks = 0; |
| 3004 | exec->nbRollbacks = 0; |
| 3005 | exec->rollbacks = NULL; |
| 3006 | exec->status = 0; |
| 3007 | exec->comp = comp; |
| 3008 | exec->state = comp->states[0]; |
| 3009 | exec->transno = 0; |
| 3010 | exec->transcount = 0; |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 3011 | exec->inputStack = NULL; |
| 3012 | exec->inputStackMax = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3013 | if (comp->nbCounters > 0) { |
| 3014 | exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)); |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3015 | if (exec->counts == NULL) { |
| 3016 | xmlRegexpErrMemory(NULL, "running regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3017 | return(-1); |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3018 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3019 | memset(exec->counts, 0, comp->nbCounters * sizeof(int)); |
| 3020 | } else |
| 3021 | exec->counts = NULL; |
| 3022 | while ((exec->status == 0) && |
| 3023 | ((exec->inputString[exec->index] != 0) || |
| 3024 | (exec->state->type != XML_REGEXP_FINAL_STATE))) { |
| 3025 | xmlRegTransPtr trans; |
| 3026 | xmlRegAtomPtr atom; |
| 3027 | |
| 3028 | /* |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3029 | * If end of input on non-terminal state, rollback, however we may |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3030 | * still have epsilon like transition for counted transitions |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3031 | * on counters, in that case don't break too early. Additionally, |
| 3032 | * if we are working on a range like "AB{0,2}", where B is not present, |
| 3033 | * we don't want to break. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3034 | */ |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3035 | len = 1; |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3036 | if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) { |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 3037 | /* |
| 3038 | * if there is a transition, we must check if |
| 3039 | * atom allows minOccurs of 0 |
| 3040 | */ |
| 3041 | if (exec->transno < exec->state->nbTrans) { |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3042 | trans = &exec->state->trans[exec->transno]; |
| 3043 | if (trans->to >=0) { |
| 3044 | atom = trans->atom; |
| 3045 | if (!((atom->min == 0) && (atom->max > 0))) |
| 3046 | goto rollback; |
| 3047 | } |
| 3048 | } else |
| 3049 | goto rollback; |
| 3050 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3051 | |
| 3052 | exec->transcount = 0; |
| 3053 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
| 3054 | trans = &exec->state->trans[exec->transno]; |
| 3055 | if (trans->to < 0) |
| 3056 | continue; |
| 3057 | atom = trans->atom; |
| 3058 | ret = 0; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 3059 | deter = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3060 | if (trans->count >= 0) { |
| 3061 | int count; |
| 3062 | xmlRegCounterPtr counter; |
| 3063 | |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3064 | if (exec->counts == NULL) { |
| 3065 | exec->status = -1; |
| 3066 | goto error; |
| 3067 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3068 | /* |
| 3069 | * A counted transition. |
| 3070 | */ |
| 3071 | |
| 3072 | count = exec->counts[trans->count]; |
| 3073 | counter = &exec->comp->counters[trans->count]; |
| 3074 | #ifdef DEBUG_REGEXP_EXEC |
| 3075 | printf("testing count %d: val %d, min %d, max %d\n", |
| 3076 | trans->count, count, counter->min, counter->max); |
| 3077 | #endif |
| 3078 | ret = ((count >= counter->min) && (count <= counter->max)); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 3079 | if ((ret) && (counter->min != counter->max)) |
| 3080 | deter = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3081 | } else if (atom == NULL) { |
| 3082 | fprintf(stderr, "epsilon transition left at runtime\n"); |
| 3083 | exec->status = -2; |
| 3084 | break; |
| 3085 | } else if (exec->inputString[exec->index] != 0) { |
| 3086 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len); |
| 3087 | ret = xmlRegCheckCharacter(atom, codepoint); |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3088 | if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3089 | xmlRegStatePtr to = comp->states[trans->to]; |
| 3090 | |
| 3091 | /* |
| 3092 | * this is a multiple input sequence |
Daniel Veillard | fc6eca0 | 2005-11-01 15:24:02 +0000 | [diff] [blame] | 3093 | * If there is a counter associated increment it now. |
| 3094 | * before potentially saving and rollback |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3095 | */ |
Daniel Veillard | fc6eca0 | 2005-11-01 15:24:02 +0000 | [diff] [blame] | 3096 | if (trans->counter >= 0) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3097 | if (exec->counts == NULL) { |
| 3098 | exec->status = -1; |
| 3099 | goto error; |
| 3100 | } |
Daniel Veillard | fc6eca0 | 2005-11-01 15:24:02 +0000 | [diff] [blame] | 3101 | #ifdef DEBUG_REGEXP_EXEC |
| 3102 | printf("Increasing count %d\n", trans->counter); |
| 3103 | #endif |
| 3104 | exec->counts[trans->counter]++; |
| 3105 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3106 | if (exec->state->nbTrans > exec->transno + 1) { |
| 3107 | xmlFARegExecSave(exec); |
| 3108 | } |
| 3109 | exec->transcount = 1; |
| 3110 | do { |
| 3111 | /* |
| 3112 | * Try to progress as much as possible on the input |
| 3113 | */ |
| 3114 | if (exec->transcount == atom->max) { |
| 3115 | break; |
| 3116 | } |
| 3117 | exec->index += len; |
| 3118 | /* |
| 3119 | * End of input: stop here |
| 3120 | */ |
| 3121 | if (exec->inputString[exec->index] == 0) { |
| 3122 | exec->index -= len; |
| 3123 | break; |
| 3124 | } |
| 3125 | if (exec->transcount >= atom->min) { |
| 3126 | int transno = exec->transno; |
| 3127 | xmlRegStatePtr state = exec->state; |
| 3128 | |
| 3129 | /* |
| 3130 | * The transition is acceptable save it |
| 3131 | */ |
| 3132 | exec->transno = -1; /* trick */ |
| 3133 | exec->state = to; |
| 3134 | xmlFARegExecSave(exec); |
| 3135 | exec->transno = transno; |
| 3136 | exec->state = state; |
| 3137 | } |
| 3138 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), |
| 3139 | len); |
| 3140 | ret = xmlRegCheckCharacter(atom, codepoint); |
| 3141 | exec->transcount++; |
| 3142 | } while (ret == 1); |
| 3143 | if (exec->transcount < atom->min) |
| 3144 | ret = 0; |
| 3145 | |
| 3146 | /* |
| 3147 | * If the last check failed but one transition was found |
| 3148 | * possible, rollback |
| 3149 | */ |
| 3150 | if (ret < 0) |
| 3151 | ret = 0; |
| 3152 | if (ret == 0) { |
| 3153 | goto rollback; |
| 3154 | } |
Daniel Veillard | fc6eca0 | 2005-11-01 15:24:02 +0000 | [diff] [blame] | 3155 | if (trans->counter >= 0) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3156 | if (exec->counts == NULL) { |
| 3157 | exec->status = -1; |
| 3158 | goto error; |
| 3159 | } |
Daniel Veillard | fc6eca0 | 2005-11-01 15:24:02 +0000 | [diff] [blame] | 3160 | #ifdef DEBUG_REGEXP_EXEC |
| 3161 | printf("Decreasing count %d\n", trans->counter); |
| 3162 | #endif |
| 3163 | exec->counts[trans->counter]--; |
| 3164 | } |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3165 | } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) { |
| 3166 | /* |
| 3167 | * we don't match on the codepoint, but minOccurs of 0 |
| 3168 | * says that's ok. Setting len to 0 inhibits stepping |
| 3169 | * over the codepoint. |
| 3170 | */ |
| 3171 | exec->transcount = 1; |
| 3172 | len = 0; |
| 3173 | ret = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3174 | } |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3175 | } else if ((atom->min == 0) && (atom->max > 0)) { |
| 3176 | /* another spot to match when minOccurs is 0 */ |
| 3177 | exec->transcount = 1; |
| 3178 | len = 0; |
| 3179 | ret = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3180 | } |
| 3181 | if (ret == 1) { |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 3182 | if ((trans->nd == 1) || |
| 3183 | ((trans->count >= 0) && (deter == 0) && |
| 3184 | (exec->state->nbTrans > exec->transno + 1))) { |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 3185 | #ifdef DEBUG_REGEXP_EXEC |
| 3186 | if (trans->nd == 1) |
| 3187 | printf("Saving on nd transition atom %d for %c at %d\n", |
| 3188 | trans->atom->no, codepoint, exec->index); |
| 3189 | else |
| 3190 | printf("Saving on counted transition count %d for %c at %d\n", |
| 3191 | trans->count, codepoint, exec->index); |
| 3192 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3193 | xmlFARegExecSave(exec); |
| 3194 | } |
| 3195 | if (trans->counter >= 0) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3196 | if (exec->counts == NULL) { |
| 3197 | exec->status = -1; |
| 3198 | goto error; |
| 3199 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3200 | #ifdef DEBUG_REGEXP_EXEC |
| 3201 | printf("Increasing count %d\n", trans->counter); |
| 3202 | #endif |
| 3203 | exec->counts[trans->counter]++; |
| 3204 | } |
Daniel Veillard | 1075228 | 2005-08-08 13:05:13 +0000 | [diff] [blame] | 3205 | if ((trans->count >= 0) && |
| 3206 | (trans->count < REGEXP_ALL_COUNTER)) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3207 | if (exec->counts == NULL) { |
| 3208 | exec->status = -1; |
| 3209 | goto error; |
| 3210 | } |
Daniel Veillard | 1075228 | 2005-08-08 13:05:13 +0000 | [diff] [blame] | 3211 | #ifdef DEBUG_REGEXP_EXEC |
| 3212 | printf("resetting count %d on transition\n", |
| 3213 | trans->count); |
| 3214 | #endif |
| 3215 | exec->counts[trans->count] = 0; |
| 3216 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3217 | #ifdef DEBUG_REGEXP_EXEC |
| 3218 | printf("entering state %d\n", trans->to); |
| 3219 | #endif |
| 3220 | exec->state = comp->states[trans->to]; |
| 3221 | exec->transno = 0; |
| 3222 | if (trans->atom != NULL) { |
| 3223 | exec->index += len; |
| 3224 | } |
| 3225 | goto progress; |
| 3226 | } else if (ret < 0) { |
| 3227 | exec->status = -4; |
| 3228 | break; |
| 3229 | } |
| 3230 | } |
| 3231 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
| 3232 | rollback: |
| 3233 | /* |
| 3234 | * Failed to find a way out |
| 3235 | */ |
| 3236 | exec->determinist = 0; |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 3237 | #ifdef DEBUG_REGEXP_EXEC |
| 3238 | printf("rollback from state %d on %d:%c\n", exec->state->no, |
| 3239 | codepoint,codepoint); |
| 3240 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3241 | xmlFARegExecRollBack(exec); |
| 3242 | } |
| 3243 | progress: |
| 3244 | continue; |
| 3245 | } |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3246 | error: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3247 | if (exec->rollbacks != NULL) { |
| 3248 | if (exec->counts != NULL) { |
| 3249 | int i; |
| 3250 | |
| 3251 | for (i = 0;i < exec->maxRollbacks;i++) |
| 3252 | if (exec->rollbacks[i].counts != NULL) |
| 3253 | xmlFree(exec->rollbacks[i].counts); |
| 3254 | } |
| 3255 | xmlFree(exec->rollbacks); |
| 3256 | } |
| 3257 | if (exec->counts != NULL) |
| 3258 | xmlFree(exec->counts); |
| 3259 | if (exec->status == 0) |
| 3260 | return(1); |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 3261 | if (exec->status == -1) { |
| 3262 | if (exec->nbPush > MAX_PUSH) |
| 3263 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3264 | return(0); |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 3265 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3266 | return(exec->status); |
| 3267 | } |
| 3268 | |
| 3269 | /************************************************************************ |
| 3270 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 3271 | * Progressive interface to the verifier one atom at a time * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3272 | * * |
| 3273 | ************************************************************************/ |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3274 | #ifdef DEBUG_ERR |
| 3275 | static void testerr(xmlRegExecCtxtPtr exec); |
| 3276 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3277 | |
| 3278 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3279 | * xmlRegNewExecCtxt: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3280 | * @comp: a precompiled regular expression |
| 3281 | * @callback: a callback function used for handling progresses in the |
| 3282 | * automata matching phase |
| 3283 | * @data: the context data associated to the callback in this context |
| 3284 | * |
| 3285 | * Build a context used for progressive evaluation of a regexp. |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3286 | * |
| 3287 | * Returns the new context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3288 | */ |
| 3289 | xmlRegExecCtxtPtr |
| 3290 | xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) { |
| 3291 | xmlRegExecCtxtPtr exec; |
| 3292 | |
| 3293 | if (comp == NULL) |
| 3294 | return(NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 3295 | if ((comp->compact == NULL) && (comp->states == NULL)) |
| 3296 | return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3297 | exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt)); |
| 3298 | if (exec == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3299 | xmlRegexpErrMemory(NULL, "creating execution context"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3300 | return(NULL); |
| 3301 | } |
| 3302 | memset(exec, 0, sizeof(xmlRegExecCtxt)); |
| 3303 | exec->inputString = NULL; |
| 3304 | exec->index = 0; |
| 3305 | exec->determinist = 1; |
| 3306 | exec->maxRollbacks = 0; |
| 3307 | exec->nbRollbacks = 0; |
| 3308 | exec->rollbacks = NULL; |
| 3309 | exec->status = 0; |
| 3310 | exec->comp = comp; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3311 | if (comp->compact == NULL) |
| 3312 | exec->state = comp->states[0]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3313 | exec->transno = 0; |
| 3314 | exec->transcount = 0; |
| 3315 | exec->callback = callback; |
| 3316 | exec->data = data; |
| 3317 | if (comp->nbCounters > 0) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3318 | /* |
| 3319 | * For error handling, exec->counts is allocated twice the size |
| 3320 | * the second half is used to store the data in case of rollback |
| 3321 | */ |
| 3322 | exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int) |
| 3323 | * 2); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3324 | if (exec->counts == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3325 | xmlRegexpErrMemory(NULL, "creating execution context"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3326 | xmlFree(exec); |
| 3327 | return(NULL); |
| 3328 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3329 | memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2); |
| 3330 | exec->errCounts = &exec->counts[comp->nbCounters]; |
| 3331 | } else { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3332 | exec->counts = NULL; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3333 | exec->errCounts = NULL; |
| 3334 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3335 | exec->inputStackMax = 0; |
| 3336 | exec->inputStackNr = 0; |
| 3337 | exec->inputStack = NULL; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3338 | exec->errStateNo = -1; |
| 3339 | exec->errString = NULL; |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 3340 | exec->nbPush = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3341 | return(exec); |
| 3342 | } |
| 3343 | |
| 3344 | /** |
| 3345 | * xmlRegFreeExecCtxt: |
| 3346 | * @exec: a regular expression evaulation context |
| 3347 | * |
| 3348 | * Free the structures associated to a regular expression evaulation context. |
| 3349 | */ |
| 3350 | void |
| 3351 | xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) { |
| 3352 | if (exec == NULL) |
| 3353 | return; |
| 3354 | |
| 3355 | if (exec->rollbacks != NULL) { |
| 3356 | if (exec->counts != NULL) { |
| 3357 | int i; |
| 3358 | |
| 3359 | for (i = 0;i < exec->maxRollbacks;i++) |
| 3360 | if (exec->rollbacks[i].counts != NULL) |
| 3361 | xmlFree(exec->rollbacks[i].counts); |
| 3362 | } |
| 3363 | xmlFree(exec->rollbacks); |
| 3364 | } |
| 3365 | if (exec->counts != NULL) |
| 3366 | xmlFree(exec->counts); |
| 3367 | if (exec->inputStack != NULL) { |
| 3368 | int i; |
| 3369 | |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3370 | for (i = 0;i < exec->inputStackNr;i++) { |
| 3371 | if (exec->inputStack[i].value != NULL) |
| 3372 | xmlFree(exec->inputStack[i].value); |
| 3373 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3374 | xmlFree(exec->inputStack); |
| 3375 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3376 | if (exec->errString != NULL) |
| 3377 | xmlFree(exec->errString); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3378 | xmlFree(exec); |
| 3379 | } |
| 3380 | |
| 3381 | static void |
| 3382 | xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value, |
| 3383 | void *data) { |
| 3384 | #ifdef DEBUG_PUSH |
| 3385 | printf("saving value: %d:%s\n", exec->inputStackNr, value); |
| 3386 | #endif |
| 3387 | if (exec->inputStackMax == 0) { |
| 3388 | exec->inputStackMax = 4; |
| 3389 | exec->inputStack = (xmlRegInputTokenPtr) |
| 3390 | xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken)); |
| 3391 | if (exec->inputStack == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3392 | xmlRegexpErrMemory(NULL, "pushing input string"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3393 | exec->inputStackMax = 0; |
| 3394 | return; |
| 3395 | } |
| 3396 | } else if (exec->inputStackNr + 1 >= exec->inputStackMax) { |
| 3397 | xmlRegInputTokenPtr tmp; |
| 3398 | |
| 3399 | exec->inputStackMax *= 2; |
| 3400 | tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack, |
| 3401 | exec->inputStackMax * sizeof(xmlRegInputToken)); |
| 3402 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3403 | xmlRegexpErrMemory(NULL, "pushing input string"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3404 | exec->inputStackMax /= 2; |
| 3405 | return; |
| 3406 | } |
| 3407 | exec->inputStack = tmp; |
| 3408 | } |
| 3409 | exec->inputStack[exec->inputStackNr].value = xmlStrdup(value); |
| 3410 | exec->inputStack[exec->inputStackNr].data = data; |
| 3411 | exec->inputStackNr++; |
| 3412 | exec->inputStack[exec->inputStackNr].value = NULL; |
| 3413 | exec->inputStack[exec->inputStackNr].data = NULL; |
| 3414 | } |
| 3415 | |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3416 | /** |
| 3417 | * xmlRegStrEqualWildcard: |
| 3418 | * @expStr: the string to be evaluated |
| 3419 | * @valStr: the validation string |
| 3420 | * |
| 3421 | * Checks if both strings are equal or have the same content. "*" |
| 3422 | * can be used as a wildcard in @valStr; "|" is used as a seperator of |
| 3423 | * substrings in both @expStr and @valStr. |
| 3424 | * |
| 3425 | * Returns 1 if the comparison is satisfied and the number of substrings |
| 3426 | * is equal, 0 otherwise. |
| 3427 | */ |
| 3428 | |
| 3429 | static int |
| 3430 | xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) { |
| 3431 | if (expStr == valStr) return(1); |
| 3432 | if (expStr == NULL) return(0); |
| 3433 | if (valStr == NULL) return(0); |
| 3434 | do { |
| 3435 | /* |
| 3436 | * Eval if we have a wildcard for the current item. |
| 3437 | */ |
| 3438 | if (*expStr != *valStr) { |
Daniel Veillard | 4f82c8a | 2005-08-09 21:40:08 +0000 | [diff] [blame] | 3439 | /* if one of them starts with a wildcard make valStr be it */ |
| 3440 | if (*valStr == '*') { |
| 3441 | const xmlChar *tmp; |
| 3442 | |
| 3443 | tmp = valStr; |
| 3444 | valStr = expStr; |
| 3445 | expStr = tmp; |
| 3446 | } |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3447 | if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) { |
| 3448 | do { |
| 3449 | if (*valStr == XML_REG_STRING_SEPARATOR) |
| 3450 | break; |
Kasimier T. Buchcik | c0e833f | 2005-04-19 15:02:20 +0000 | [diff] [blame] | 3451 | valStr++; |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3452 | } while (*valStr != 0); |
| 3453 | continue; |
| 3454 | } else |
| 3455 | return(0); |
| 3456 | } |
Kasimier T. Buchcik | c0e833f | 2005-04-19 15:02:20 +0000 | [diff] [blame] | 3457 | expStr++; |
| 3458 | valStr++; |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3459 | } while (*valStr != 0); |
| 3460 | if (*expStr != 0) |
| 3461 | return (0); |
| 3462 | else |
| 3463 | return (1); |
| 3464 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3465 | |
| 3466 | /** |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3467 | * xmlRegCompactPushString: |
| 3468 | * @exec: a regexp execution context |
| 3469 | * @comp: the precompiled exec with a compact table |
| 3470 | * @value: a string token input |
| 3471 | * @data: data associated to the token to reuse in callbacks |
| 3472 | * |
| 3473 | * Push one input token in the execution context |
| 3474 | * |
| 3475 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
| 3476 | * a negative value in case of error. |
| 3477 | */ |
| 3478 | static int |
| 3479 | xmlRegCompactPushString(xmlRegExecCtxtPtr exec, |
| 3480 | xmlRegexpPtr comp, |
| 3481 | const xmlChar *value, |
| 3482 | void *data) { |
| 3483 | int state = exec->index; |
| 3484 | int i, target; |
| 3485 | |
| 3486 | if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL)) |
| 3487 | return(-1); |
| 3488 | |
| 3489 | if (value == NULL) { |
| 3490 | /* |
| 3491 | * are we at a final state ? |
| 3492 | */ |
| 3493 | if (comp->compact[state * (comp->nbstrings + 1)] == |
| 3494 | XML_REGEXP_FINAL_STATE) |
| 3495 | return(1); |
| 3496 | return(0); |
| 3497 | } |
| 3498 | |
| 3499 | #ifdef DEBUG_PUSH |
| 3500 | printf("value pushed: %s\n", value); |
| 3501 | #endif |
| 3502 | |
| 3503 | /* |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 3504 | * Examine all outside transitions from current state |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3505 | */ |
| 3506 | for (i = 0;i < comp->nbstrings;i++) { |
| 3507 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
| 3508 | if ((target > 0) && (target <= comp->nbstates)) { |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3509 | target--; /* to avoid 0 */ |
| 3510 | if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) { |
| 3511 | exec->index = target; |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 3512 | if ((exec->callback != NULL) && (comp->transdata != NULL)) { |
| 3513 | exec->callback(exec->data, value, |
| 3514 | comp->transdata[state * comp->nbstrings + i], data); |
| 3515 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3516 | #ifdef DEBUG_PUSH |
| 3517 | printf("entering state %d\n", target); |
| 3518 | #endif |
| 3519 | if (comp->compact[target * (comp->nbstrings + 1)] == |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3520 | XML_REGEXP_SINK_STATE) |
| 3521 | goto error; |
| 3522 | |
| 3523 | if (comp->compact[target * (comp->nbstrings + 1)] == |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3524 | XML_REGEXP_FINAL_STATE) |
| 3525 | return(1); |
| 3526 | return(0); |
| 3527 | } |
| 3528 | } |
| 3529 | } |
| 3530 | /* |
| 3531 | * Failed to find an exit transition out from current state for the |
| 3532 | * current token |
| 3533 | */ |
| 3534 | #ifdef DEBUG_PUSH |
| 3535 | printf("failed to find a transition for %s on state %d\n", value, state); |
| 3536 | #endif |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3537 | error: |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3538 | if (exec->errString != NULL) |
| 3539 | xmlFree(exec->errString); |
| 3540 | exec->errString = xmlStrdup(value); |
| 3541 | exec->errStateNo = state; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3542 | exec->status = -1; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3543 | #ifdef DEBUG_ERR |
| 3544 | testerr(exec); |
| 3545 | #endif |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3546 | return(-1); |
| 3547 | } |
| 3548 | |
| 3549 | /** |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3550 | * xmlRegExecPushStringInternal: |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 3551 | * @exec: a regexp execution context or NULL to indicate the end |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3552 | * @value: a string token input |
| 3553 | * @data: data associated to the token to reuse in callbacks |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3554 | * @compound: value was assembled from 2 strings |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3555 | * |
| 3556 | * Push one input token in the execution context |
| 3557 | * |
| 3558 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
| 3559 | * a negative value in case of error. |
| 3560 | */ |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3561 | static int |
| 3562 | xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value, |
| 3563 | void *data, int compound) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3564 | xmlRegTransPtr trans; |
| 3565 | xmlRegAtomPtr atom; |
| 3566 | int ret; |
| 3567 | int final = 0; |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3568 | int progress = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3569 | |
| 3570 | if (exec == NULL) |
| 3571 | return(-1); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3572 | if (exec->comp == NULL) |
| 3573 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3574 | if (exec->status != 0) |
| 3575 | return(exec->status); |
| 3576 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3577 | if (exec->comp->compact != NULL) |
| 3578 | return(xmlRegCompactPushString(exec, exec->comp, value, data)); |
| 3579 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3580 | if (value == NULL) { |
| 3581 | if (exec->state->type == XML_REGEXP_FINAL_STATE) |
| 3582 | return(1); |
| 3583 | final = 1; |
| 3584 | } |
| 3585 | |
| 3586 | #ifdef DEBUG_PUSH |
| 3587 | printf("value pushed: %s\n", value); |
| 3588 | #endif |
| 3589 | /* |
| 3590 | * If we have an active rollback stack push the new value there |
| 3591 | * and get back to where we were left |
| 3592 | */ |
| 3593 | if ((value != NULL) && (exec->inputStackNr > 0)) { |
| 3594 | xmlFARegExecSaveInputString(exec, value, data); |
| 3595 | value = exec->inputStack[exec->index].value; |
| 3596 | data = exec->inputStack[exec->index].data; |
| 3597 | #ifdef DEBUG_PUSH |
| 3598 | printf("value loaded: %s\n", value); |
| 3599 | #endif |
| 3600 | } |
| 3601 | |
| 3602 | while ((exec->status == 0) && |
| 3603 | ((value != NULL) || |
| 3604 | ((final == 1) && |
| 3605 | (exec->state->type != XML_REGEXP_FINAL_STATE)))) { |
| 3606 | |
| 3607 | /* |
| 3608 | * End of input on non-terminal state, rollback, however we may |
| 3609 | * still have epsilon like transition for counted transitions |
| 3610 | * on counters, in that case don't break too early. |
| 3611 | */ |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 3612 | if ((value == NULL) && (exec->counts == NULL)) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3613 | goto rollback; |
| 3614 | |
| 3615 | exec->transcount = 0; |
| 3616 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
| 3617 | trans = &exec->state->trans[exec->transno]; |
| 3618 | if (trans->to < 0) |
| 3619 | continue; |
| 3620 | atom = trans->atom; |
| 3621 | ret = 0; |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 3622 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
| 3623 | int i; |
| 3624 | int count; |
| 3625 | xmlRegTransPtr t; |
| 3626 | xmlRegCounterPtr counter; |
| 3627 | |
| 3628 | ret = 0; |
| 3629 | |
| 3630 | #ifdef DEBUG_PUSH |
| 3631 | printf("testing all lax %d\n", trans->count); |
| 3632 | #endif |
| 3633 | /* |
| 3634 | * Check all counted transitions from the current state |
| 3635 | */ |
| 3636 | if ((value == NULL) && (final)) { |
| 3637 | ret = 1; |
| 3638 | } else if (value != NULL) { |
| 3639 | for (i = 0;i < exec->state->nbTrans;i++) { |
| 3640 | t = &exec->state->trans[i]; |
| 3641 | if ((t->counter < 0) || (t == trans)) |
| 3642 | continue; |
| 3643 | counter = &exec->comp->counters[t->counter]; |
| 3644 | count = exec->counts[t->counter]; |
| 3645 | if ((count < counter->max) && |
| 3646 | (t->atom != NULL) && |
| 3647 | (xmlStrEqual(value, t->atom->valuep))) { |
| 3648 | ret = 0; |
| 3649 | break; |
| 3650 | } |
| 3651 | if ((count >= counter->min) && |
| 3652 | (count < counter->max) && |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3653 | (t->atom != NULL) && |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 3654 | (xmlStrEqual(value, t->atom->valuep))) { |
| 3655 | ret = 1; |
| 3656 | break; |
| 3657 | } |
| 3658 | } |
| 3659 | } |
| 3660 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
Daniel Veillard | 8a001f6 | 2002-04-20 07:24:11 +0000 | [diff] [blame] | 3661 | int i; |
| 3662 | int count; |
| 3663 | xmlRegTransPtr t; |
| 3664 | xmlRegCounterPtr counter; |
| 3665 | |
| 3666 | ret = 1; |
| 3667 | |
| 3668 | #ifdef DEBUG_PUSH |
| 3669 | printf("testing all %d\n", trans->count); |
| 3670 | #endif |
| 3671 | /* |
| 3672 | * Check all counted transitions from the current state |
| 3673 | */ |
| 3674 | for (i = 0;i < exec->state->nbTrans;i++) { |
| 3675 | t = &exec->state->trans[i]; |
| 3676 | if ((t->counter < 0) || (t == trans)) |
| 3677 | continue; |
| 3678 | counter = &exec->comp->counters[t->counter]; |
| 3679 | count = exec->counts[t->counter]; |
| 3680 | if ((count < counter->min) || (count > counter->max)) { |
| 3681 | ret = 0; |
| 3682 | break; |
| 3683 | } |
| 3684 | } |
| 3685 | } else if (trans->count >= 0) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3686 | int count; |
| 3687 | xmlRegCounterPtr counter; |
| 3688 | |
| 3689 | /* |
| 3690 | * A counted transition. |
| 3691 | */ |
| 3692 | |
| 3693 | count = exec->counts[trans->count]; |
| 3694 | counter = &exec->comp->counters[trans->count]; |
| 3695 | #ifdef DEBUG_PUSH |
| 3696 | printf("testing count %d: val %d, min %d, max %d\n", |
| 3697 | trans->count, count, counter->min, counter->max); |
| 3698 | #endif |
| 3699 | ret = ((count >= counter->min) && (count <= counter->max)); |
| 3700 | } else if (atom == NULL) { |
| 3701 | fprintf(stderr, "epsilon transition left at runtime\n"); |
| 3702 | exec->status = -2; |
| 3703 | break; |
| 3704 | } else if (value != NULL) { |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3705 | ret = xmlRegStrEqualWildcard(atom->valuep, value); |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3706 | if (atom->neg) { |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 3707 | ret = !ret; |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3708 | if (!compound) |
| 3709 | ret = 0; |
| 3710 | } |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 3711 | if ((ret == 1) && (trans->counter >= 0)) { |
| 3712 | xmlRegCounterPtr counter; |
| 3713 | int count; |
| 3714 | |
| 3715 | count = exec->counts[trans->counter]; |
| 3716 | counter = &exec->comp->counters[trans->counter]; |
| 3717 | if (count >= counter->max) |
| 3718 | ret = 0; |
| 3719 | } |
| 3720 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3721 | if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) { |
| 3722 | xmlRegStatePtr to = exec->comp->states[trans->to]; |
| 3723 | |
| 3724 | /* |
| 3725 | * this is a multiple input sequence |
| 3726 | */ |
| 3727 | if (exec->state->nbTrans > exec->transno + 1) { |
| 3728 | if (exec->inputStackNr <= 0) { |
| 3729 | xmlFARegExecSaveInputString(exec, value, data); |
| 3730 | } |
| 3731 | xmlFARegExecSave(exec); |
| 3732 | } |
| 3733 | exec->transcount = 1; |
| 3734 | do { |
| 3735 | /* |
| 3736 | * Try to progress as much as possible on the input |
| 3737 | */ |
| 3738 | if (exec->transcount == atom->max) { |
| 3739 | break; |
| 3740 | } |
| 3741 | exec->index++; |
| 3742 | value = exec->inputStack[exec->index].value; |
| 3743 | data = exec->inputStack[exec->index].data; |
| 3744 | #ifdef DEBUG_PUSH |
| 3745 | printf("value loaded: %s\n", value); |
| 3746 | #endif |
| 3747 | |
| 3748 | /* |
| 3749 | * End of input: stop here |
| 3750 | */ |
| 3751 | if (value == NULL) { |
| 3752 | exec->index --; |
| 3753 | break; |
| 3754 | } |
| 3755 | if (exec->transcount >= atom->min) { |
| 3756 | int transno = exec->transno; |
| 3757 | xmlRegStatePtr state = exec->state; |
| 3758 | |
| 3759 | /* |
| 3760 | * The transition is acceptable save it |
| 3761 | */ |
| 3762 | exec->transno = -1; /* trick */ |
| 3763 | exec->state = to; |
| 3764 | if (exec->inputStackNr <= 0) { |
| 3765 | xmlFARegExecSaveInputString(exec, value, data); |
| 3766 | } |
| 3767 | xmlFARegExecSave(exec); |
| 3768 | exec->transno = transno; |
| 3769 | exec->state = state; |
| 3770 | } |
| 3771 | ret = xmlStrEqual(value, atom->valuep); |
| 3772 | exec->transcount++; |
| 3773 | } while (ret == 1); |
| 3774 | if (exec->transcount < atom->min) |
| 3775 | ret = 0; |
| 3776 | |
| 3777 | /* |
| 3778 | * If the last check failed but one transition was found |
| 3779 | * possible, rollback |
| 3780 | */ |
| 3781 | if (ret < 0) |
| 3782 | ret = 0; |
| 3783 | if (ret == 0) { |
| 3784 | goto rollback; |
| 3785 | } |
| 3786 | } |
| 3787 | } |
| 3788 | if (ret == 1) { |
William M. Brack | 9887395 | 2003-12-26 06:03:14 +0000 | [diff] [blame] | 3789 | if ((exec->callback != NULL) && (atom != NULL) && |
| 3790 | (data != NULL)) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3791 | exec->callback(exec->data, atom->valuep, |
| 3792 | atom->data, data); |
| 3793 | } |
| 3794 | if (exec->state->nbTrans > exec->transno + 1) { |
| 3795 | if (exec->inputStackNr <= 0) { |
| 3796 | xmlFARegExecSaveInputString(exec, value, data); |
| 3797 | } |
| 3798 | xmlFARegExecSave(exec); |
| 3799 | } |
| 3800 | if (trans->counter >= 0) { |
| 3801 | #ifdef DEBUG_PUSH |
| 3802 | printf("Increasing count %d\n", trans->counter); |
| 3803 | #endif |
| 3804 | exec->counts[trans->counter]++; |
| 3805 | } |
Daniel Veillard | 1075228 | 2005-08-08 13:05:13 +0000 | [diff] [blame] | 3806 | if ((trans->count >= 0) && |
| 3807 | (trans->count < REGEXP_ALL_COUNTER)) { |
| 3808 | #ifdef DEBUG_REGEXP_EXEC |
| 3809 | printf("resetting count %d on transition\n", |
| 3810 | trans->count); |
| 3811 | #endif |
| 3812 | exec->counts[trans->count] = 0; |
| 3813 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3814 | #ifdef DEBUG_PUSH |
| 3815 | printf("entering state %d\n", trans->to); |
| 3816 | #endif |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3817 | if ((exec->comp->states[trans->to] != NULL) && |
| 3818 | (exec->comp->states[trans->to]->type == |
| 3819 | XML_REGEXP_SINK_STATE)) { |
| 3820 | /* |
| 3821 | * entering a sink state, save the current state as error |
| 3822 | * state. |
| 3823 | */ |
| 3824 | if (exec->errString != NULL) |
| 3825 | xmlFree(exec->errString); |
| 3826 | exec->errString = xmlStrdup(value); |
| 3827 | exec->errState = exec->state; |
| 3828 | memcpy(exec->errCounts, exec->counts, |
| 3829 | exec->comp->nbCounters * sizeof(int)); |
| 3830 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3831 | exec->state = exec->comp->states[trans->to]; |
| 3832 | exec->transno = 0; |
| 3833 | if (trans->atom != NULL) { |
| 3834 | if (exec->inputStack != NULL) { |
| 3835 | exec->index++; |
| 3836 | if (exec->index < exec->inputStackNr) { |
| 3837 | value = exec->inputStack[exec->index].value; |
| 3838 | data = exec->inputStack[exec->index].data; |
| 3839 | #ifdef DEBUG_PUSH |
| 3840 | printf("value loaded: %s\n", value); |
| 3841 | #endif |
| 3842 | } else { |
| 3843 | value = NULL; |
| 3844 | data = NULL; |
| 3845 | #ifdef DEBUG_PUSH |
| 3846 | printf("end of input\n"); |
| 3847 | #endif |
| 3848 | } |
| 3849 | } else { |
| 3850 | value = NULL; |
| 3851 | data = NULL; |
| 3852 | #ifdef DEBUG_PUSH |
| 3853 | printf("end of input\n"); |
| 3854 | #endif |
| 3855 | } |
| 3856 | } |
| 3857 | goto progress; |
| 3858 | } else if (ret < 0) { |
| 3859 | exec->status = -4; |
| 3860 | break; |
| 3861 | } |
| 3862 | } |
| 3863 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
| 3864 | rollback: |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3865 | /* |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3866 | * if we didn't yet rollback on the current input |
| 3867 | * store the current state as the error state. |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3868 | */ |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3869 | if ((progress) && (exec->state != NULL) && |
| 3870 | (exec->state->type != XML_REGEXP_SINK_STATE)) { |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3871 | progress = 0; |
| 3872 | if (exec->errString != NULL) |
| 3873 | xmlFree(exec->errString); |
| 3874 | exec->errString = xmlStrdup(value); |
| 3875 | exec->errState = exec->state; |
| 3876 | memcpy(exec->errCounts, exec->counts, |
| 3877 | exec->comp->nbCounters * sizeof(int)); |
| 3878 | } |
| 3879 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3880 | /* |
| 3881 | * Failed to find a way out |
| 3882 | */ |
| 3883 | exec->determinist = 0; |
| 3884 | xmlFARegExecRollBack(exec); |
| 3885 | if (exec->status == 0) { |
| 3886 | value = exec->inputStack[exec->index].value; |
| 3887 | data = exec->inputStack[exec->index].data; |
| 3888 | #ifdef DEBUG_PUSH |
| 3889 | printf("value loaded: %s\n", value); |
| 3890 | #endif |
| 3891 | } |
| 3892 | } |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3893 | continue; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3894 | progress: |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3895 | progress = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3896 | continue; |
| 3897 | } |
| 3898 | if (exec->status == 0) { |
| 3899 | return(exec->state->type == XML_REGEXP_FINAL_STATE); |
| 3900 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3901 | #ifdef DEBUG_ERR |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3902 | if (exec->status < 0) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3903 | testerr(exec); |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3904 | } |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3905 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3906 | return(exec->status); |
| 3907 | } |
| 3908 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3909 | /** |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3910 | * xmlRegExecPushString: |
| 3911 | * @exec: a regexp execution context or NULL to indicate the end |
| 3912 | * @value: a string token input |
| 3913 | * @data: data associated to the token to reuse in callbacks |
| 3914 | * |
| 3915 | * Push one input token in the execution context |
| 3916 | * |
| 3917 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
| 3918 | * a negative value in case of error. |
| 3919 | */ |
| 3920 | int |
| 3921 | xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value, |
| 3922 | void *data) { |
| 3923 | return(xmlRegExecPushStringInternal(exec, value, data, 0)); |
| 3924 | } |
| 3925 | |
| 3926 | /** |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3927 | * xmlRegExecPushString2: |
| 3928 | * @exec: a regexp execution context or NULL to indicate the end |
| 3929 | * @value: the first string token input |
| 3930 | * @value2: the second string token input |
| 3931 | * @data: data associated to the token to reuse in callbacks |
| 3932 | * |
| 3933 | * Push one input token in the execution context |
| 3934 | * |
| 3935 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
| 3936 | * a negative value in case of error. |
| 3937 | */ |
| 3938 | int |
| 3939 | xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value, |
| 3940 | const xmlChar *value2, void *data) { |
| 3941 | xmlChar buf[150]; |
| 3942 | int lenn, lenp, ret; |
| 3943 | xmlChar *str; |
| 3944 | |
| 3945 | if (exec == NULL) |
| 3946 | return(-1); |
| 3947 | if (exec->comp == NULL) |
| 3948 | return(-1); |
| 3949 | if (exec->status != 0) |
| 3950 | return(exec->status); |
| 3951 | |
| 3952 | if (value2 == NULL) |
| 3953 | return(xmlRegExecPushString(exec, value, data)); |
| 3954 | |
| 3955 | lenn = strlen((char *) value2); |
| 3956 | lenp = strlen((char *) value); |
| 3957 | |
| 3958 | if (150 < lenn + lenp + 2) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3959 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3960 | if (str == NULL) { |
| 3961 | exec->status = -1; |
| 3962 | return(-1); |
| 3963 | } |
| 3964 | } else { |
| 3965 | str = buf; |
| 3966 | } |
| 3967 | memcpy(&str[0], value, lenp); |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3968 | str[lenp] = XML_REG_STRING_SEPARATOR; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3969 | memcpy(&str[lenp + 1], value2, lenn); |
| 3970 | str[lenn + lenp + 1] = 0; |
| 3971 | |
| 3972 | if (exec->comp->compact != NULL) |
| 3973 | ret = xmlRegCompactPushString(exec, exec->comp, str, data); |
| 3974 | else |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3975 | ret = xmlRegExecPushStringInternal(exec, str, data, 1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3976 | |
| 3977 | if (str != buf) |
Daniel Veillard | 0b1ff14 | 2005-12-28 21:13:33 +0000 | [diff] [blame] | 3978 | xmlFree(str); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3979 | return(ret); |
| 3980 | } |
| 3981 | |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3982 | /** |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 3983 | * xmlRegExecGetValues: |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3984 | * @exec: a regexp execution context |
| 3985 | * @err: error extraction or normal one |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3986 | * @nbval: pointer to the number of accepted values IN/OUT |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3987 | * @nbneg: return number of negative transitions |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3988 | * @values: pointer to the array of acceptable values |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3989 | * @terminal: return value if this was a terminal state |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3990 | * |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3991 | * Extract informations from the regexp execution, internal routine to |
| 3992 | * implement xmlRegExecNextValues() and xmlRegExecErrInfo() |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3993 | * |
| 3994 | * Returns: 0 in case of success or -1 in case of error. |
| 3995 | */ |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3996 | static int |
| 3997 | xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err, |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3998 | int *nbval, int *nbneg, |
| 3999 | xmlChar **values, int *terminal) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4000 | int maxval; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4001 | int nb = 0; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4002 | |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4003 | if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) || |
| 4004 | (values == NULL) || (*nbval <= 0)) |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4005 | return(-1); |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4006 | |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4007 | maxval = *nbval; |
| 4008 | *nbval = 0; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4009 | *nbneg = 0; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4010 | if ((exec->comp != NULL) && (exec->comp->compact != NULL)) { |
| 4011 | xmlRegexpPtr comp; |
| 4012 | int target, i, state; |
| 4013 | |
| 4014 | comp = exec->comp; |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4015 | |
| 4016 | if (err) { |
| 4017 | if (exec->errStateNo == -1) return(-1); |
| 4018 | state = exec->errStateNo; |
| 4019 | } else { |
| 4020 | state = exec->index; |
| 4021 | } |
| 4022 | if (terminal != NULL) { |
| 4023 | if (comp->compact[state * (comp->nbstrings + 1)] == |
| 4024 | XML_REGEXP_FINAL_STATE) |
| 4025 | *terminal = 1; |
| 4026 | else |
| 4027 | *terminal = 0; |
| 4028 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4029 | for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4030 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4031 | if ((target > 0) && (target <= comp->nbstates) && |
| 4032 | (comp->compact[(target - 1) * (comp->nbstrings + 1)] != |
| 4033 | XML_REGEXP_SINK_STATE)) { |
| 4034 | values[nb++] = comp->stringMap[i]; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4035 | (*nbval)++; |
| 4036 | } |
| 4037 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4038 | for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) { |
| 4039 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
| 4040 | if ((target > 0) && (target <= comp->nbstates) && |
| 4041 | (comp->compact[(target - 1) * (comp->nbstrings + 1)] == |
| 4042 | XML_REGEXP_SINK_STATE)) { |
| 4043 | values[nb++] = comp->stringMap[i]; |
| 4044 | (*nbneg)++; |
| 4045 | } |
| 4046 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4047 | } else { |
| 4048 | int transno; |
| 4049 | xmlRegTransPtr trans; |
| 4050 | xmlRegAtomPtr atom; |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4051 | xmlRegStatePtr state; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4052 | |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4053 | if (terminal != NULL) { |
| 4054 | if (exec->state->type == XML_REGEXP_FINAL_STATE) |
| 4055 | *terminal = 1; |
| 4056 | else |
| 4057 | *terminal = 0; |
| 4058 | } |
| 4059 | |
| 4060 | if (err) { |
| 4061 | if (exec->errState == NULL) return(-1); |
| 4062 | state = exec->errState; |
| 4063 | } else { |
| 4064 | if (exec->state == NULL) return(-1); |
| 4065 | state = exec->state; |
| 4066 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4067 | for (transno = 0; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4068 | (transno < state->nbTrans) && (nb < maxval); |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4069 | transno++) { |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4070 | trans = &state->trans[transno]; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4071 | if (trans->to < 0) |
| 4072 | continue; |
| 4073 | atom = trans->atom; |
| 4074 | if ((atom == NULL) || (atom->valuep == NULL)) |
| 4075 | continue; |
| 4076 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4077 | /* this should not be reached but ... */ |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4078 | TODO; |
| 4079 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4080 | /* this should not be reached but ... */ |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4081 | TODO; |
| 4082 | } else if (trans->counter >= 0) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 4083 | xmlRegCounterPtr counter = NULL; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4084 | int count; |
| 4085 | |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4086 | if (err) |
| 4087 | count = exec->errCounts[trans->counter]; |
| 4088 | else |
| 4089 | count = exec->counts[trans->counter]; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 4090 | if (exec->comp != NULL) |
| 4091 | counter = &exec->comp->counters[trans->counter]; |
| 4092 | if ((counter == NULL) || (count < counter->max)) { |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 4093 | if (atom->neg) |
| 4094 | values[nb++] = (xmlChar *) atom->valuep2; |
| 4095 | else |
| 4096 | values[nb++] = (xmlChar *) atom->valuep; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4097 | (*nbval)++; |
| 4098 | } |
| 4099 | } else { |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4100 | if ((exec->comp->states[trans->to] != NULL) && |
| 4101 | (exec->comp->states[trans->to]->type != |
| 4102 | XML_REGEXP_SINK_STATE)) { |
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 | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4107 | (*nbval)++; |
| 4108 | } |
| 4109 | } |
| 4110 | } |
| 4111 | for (transno = 0; |
| 4112 | (transno < state->nbTrans) && (nb < maxval); |
| 4113 | transno++) { |
| 4114 | trans = &state->trans[transno]; |
| 4115 | if (trans->to < 0) |
| 4116 | continue; |
| 4117 | atom = trans->atom; |
| 4118 | if ((atom == NULL) || (atom->valuep == NULL)) |
| 4119 | continue; |
| 4120 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
| 4121 | continue; |
| 4122 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
| 4123 | continue; |
| 4124 | } else if (trans->counter >= 0) { |
| 4125 | continue; |
| 4126 | } else { |
| 4127 | if ((exec->comp->states[trans->to] != NULL) && |
| 4128 | (exec->comp->states[trans->to]->type == |
| 4129 | XML_REGEXP_SINK_STATE)) { |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 4130 | if (atom->neg) |
| 4131 | values[nb++] = (xmlChar *) atom->valuep2; |
| 4132 | else |
| 4133 | values[nb++] = (xmlChar *) atom->valuep; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4134 | (*nbneg)++; |
| 4135 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4136 | } |
| 4137 | } |
| 4138 | } |
| 4139 | return(0); |
| 4140 | } |
| 4141 | |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4142 | /** |
| 4143 | * xmlRegExecNextValues: |
| 4144 | * @exec: a regexp execution context |
| 4145 | * @nbval: pointer to the number of accepted values IN/OUT |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4146 | * @nbneg: return number of negative transitions |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4147 | * @values: pointer to the array of acceptable values |
| 4148 | * @terminal: return value if this was a terminal state |
| 4149 | * |
| 4150 | * Extract informations from the regexp execution, |
| 4151 | * the parameter @values must point to an array of @nbval string pointers |
| 4152 | * on return nbval will contain the number of possible strings in that |
| 4153 | * state and the @values array will be updated with them. The string values |
| 4154 | * returned will be freed with the @exec context and don't need to be |
| 4155 | * deallocated. |
| 4156 | * |
| 4157 | * Returns: 0 in case of success or -1 in case of error. |
| 4158 | */ |
| 4159 | int |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4160 | xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg, |
| 4161 | xmlChar **values, int *terminal) { |
| 4162 | return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal)); |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
| 4165 | /** |
| 4166 | * xmlRegExecErrInfo: |
| 4167 | * @exec: a regexp execution context generating an error |
| 4168 | * @string: return value for the error string |
| 4169 | * @nbval: pointer to the number of accepted values IN/OUT |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4170 | * @nbneg: return number of negative transitions |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4171 | * @values: pointer to the array of acceptable values |
| 4172 | * @terminal: return value if this was a terminal state |
| 4173 | * |
| 4174 | * Extract error informations from the regexp execution, the parameter |
| 4175 | * @string will be updated with the value pushed and not accepted, |
| 4176 | * the parameter @values must point to an array of @nbval string pointers |
| 4177 | * on return nbval will contain the number of possible strings in that |
| 4178 | * state and the @values array will be updated with them. The string values |
| 4179 | * returned will be freed with the @exec context and don't need to be |
| 4180 | * deallocated. |
| 4181 | * |
| 4182 | * Returns: 0 in case of success or -1 in case of error. |
| 4183 | */ |
| 4184 | int |
| 4185 | xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string, |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4186 | int *nbval, int *nbneg, xmlChar **values, int *terminal) { |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4187 | if (exec == NULL) |
| 4188 | return(-1); |
| 4189 | if (string != NULL) { |
| 4190 | if (exec->status != 0) |
| 4191 | *string = exec->errString; |
| 4192 | else |
| 4193 | *string = NULL; |
| 4194 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4195 | return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal)); |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4196 | } |
| 4197 | |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4198 | #ifdef DEBUG_ERR |
| 4199 | static void testerr(xmlRegExecCtxtPtr exec) { |
| 4200 | const xmlChar *string; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4201 | xmlChar *values[5]; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4202 | int nb = 5; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4203 | int nbneg; |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4204 | int terminal; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4205 | xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal); |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4206 | } |
| 4207 | #endif |
| 4208 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4209 | #if 0 |
| 4210 | static int |
| 4211 | xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) { |
| 4212 | xmlRegTransPtr trans; |
| 4213 | xmlRegAtomPtr atom; |
| 4214 | int ret; |
| 4215 | int codepoint, len; |
| 4216 | |
| 4217 | if (exec == NULL) |
| 4218 | return(-1); |
| 4219 | if (exec->status != 0) |
| 4220 | return(exec->status); |
| 4221 | |
| 4222 | while ((exec->status == 0) && |
| 4223 | ((exec->inputString[exec->index] != 0) || |
| 4224 | (exec->state->type != XML_REGEXP_FINAL_STATE))) { |
| 4225 | |
| 4226 | /* |
| 4227 | * End of input on non-terminal state, rollback, however we may |
| 4228 | * still have epsilon like transition for counted transitions |
| 4229 | * on counters, in that case don't break too early. |
| 4230 | */ |
| 4231 | if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) |
| 4232 | goto rollback; |
| 4233 | |
| 4234 | exec->transcount = 0; |
| 4235 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
| 4236 | trans = &exec->state->trans[exec->transno]; |
| 4237 | if (trans->to < 0) |
| 4238 | continue; |
| 4239 | atom = trans->atom; |
| 4240 | ret = 0; |
| 4241 | if (trans->count >= 0) { |
| 4242 | int count; |
| 4243 | xmlRegCounterPtr counter; |
| 4244 | |
| 4245 | /* |
| 4246 | * A counted transition. |
| 4247 | */ |
| 4248 | |
| 4249 | count = exec->counts[trans->count]; |
| 4250 | counter = &exec->comp->counters[trans->count]; |
| 4251 | #ifdef DEBUG_REGEXP_EXEC |
| 4252 | printf("testing count %d: val %d, min %d, max %d\n", |
| 4253 | trans->count, count, counter->min, counter->max); |
| 4254 | #endif |
| 4255 | ret = ((count >= counter->min) && (count <= counter->max)); |
| 4256 | } else if (atom == NULL) { |
| 4257 | fprintf(stderr, "epsilon transition left at runtime\n"); |
| 4258 | exec->status = -2; |
| 4259 | break; |
| 4260 | } else if (exec->inputString[exec->index] != 0) { |
| 4261 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len); |
| 4262 | ret = xmlRegCheckCharacter(atom, codepoint); |
| 4263 | if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) { |
| 4264 | xmlRegStatePtr to = exec->comp->states[trans->to]; |
| 4265 | |
| 4266 | /* |
| 4267 | * this is a multiple input sequence |
| 4268 | */ |
| 4269 | if (exec->state->nbTrans > exec->transno + 1) { |
| 4270 | xmlFARegExecSave(exec); |
| 4271 | } |
| 4272 | exec->transcount = 1; |
| 4273 | do { |
| 4274 | /* |
| 4275 | * Try to progress as much as possible on the input |
| 4276 | */ |
| 4277 | if (exec->transcount == atom->max) { |
| 4278 | break; |
| 4279 | } |
| 4280 | exec->index += len; |
| 4281 | /* |
| 4282 | * End of input: stop here |
| 4283 | */ |
| 4284 | if (exec->inputString[exec->index] == 0) { |
| 4285 | exec->index -= len; |
| 4286 | break; |
| 4287 | } |
| 4288 | if (exec->transcount >= atom->min) { |
| 4289 | int transno = exec->transno; |
| 4290 | xmlRegStatePtr state = exec->state; |
| 4291 | |
| 4292 | /* |
| 4293 | * The transition is acceptable save it |
| 4294 | */ |
| 4295 | exec->transno = -1; /* trick */ |
| 4296 | exec->state = to; |
| 4297 | xmlFARegExecSave(exec); |
| 4298 | exec->transno = transno; |
| 4299 | exec->state = state; |
| 4300 | } |
| 4301 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), |
| 4302 | len); |
| 4303 | ret = xmlRegCheckCharacter(atom, codepoint); |
| 4304 | exec->transcount++; |
| 4305 | } while (ret == 1); |
| 4306 | if (exec->transcount < atom->min) |
| 4307 | ret = 0; |
| 4308 | |
| 4309 | /* |
| 4310 | * If the last check failed but one transition was found |
| 4311 | * possible, rollback |
| 4312 | */ |
| 4313 | if (ret < 0) |
| 4314 | ret = 0; |
| 4315 | if (ret == 0) { |
| 4316 | goto rollback; |
| 4317 | } |
| 4318 | } |
| 4319 | } |
| 4320 | if (ret == 1) { |
| 4321 | if (exec->state->nbTrans > exec->transno + 1) { |
| 4322 | xmlFARegExecSave(exec); |
| 4323 | } |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 4324 | /* |
| 4325 | * restart count for expressions like this ((abc){2})* |
| 4326 | */ |
| 4327 | if (trans->count >= 0) { |
| 4328 | #ifdef DEBUG_REGEXP_EXEC |
| 4329 | printf("Reset count %d\n", trans->count); |
| 4330 | #endif |
| 4331 | exec->counts[trans->count] = 0; |
| 4332 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4333 | if (trans->counter >= 0) { |
| 4334 | #ifdef DEBUG_REGEXP_EXEC |
| 4335 | printf("Increasing count %d\n", trans->counter); |
| 4336 | #endif |
| 4337 | exec->counts[trans->counter]++; |
| 4338 | } |
| 4339 | #ifdef DEBUG_REGEXP_EXEC |
| 4340 | printf("entering state %d\n", trans->to); |
| 4341 | #endif |
| 4342 | exec->state = exec->comp->states[trans->to]; |
| 4343 | exec->transno = 0; |
| 4344 | if (trans->atom != NULL) { |
| 4345 | exec->index += len; |
| 4346 | } |
| 4347 | goto progress; |
| 4348 | } else if (ret < 0) { |
| 4349 | exec->status = -4; |
| 4350 | break; |
| 4351 | } |
| 4352 | } |
| 4353 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
| 4354 | rollback: |
| 4355 | /* |
| 4356 | * Failed to find a way out |
| 4357 | */ |
| 4358 | exec->determinist = 0; |
| 4359 | xmlFARegExecRollBack(exec); |
| 4360 | } |
| 4361 | progress: |
| 4362 | continue; |
| 4363 | } |
| 4364 | } |
| 4365 | #endif |
| 4366 | /************************************************************************ |
| 4367 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 4368 | * Parser for the Schemas Datatype Regular Expressions * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4369 | * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs * |
| 4370 | * * |
| 4371 | ************************************************************************/ |
| 4372 | |
| 4373 | /** |
| 4374 | * xmlFAIsChar: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4375 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4376 | * |
| 4377 | * [10] Char ::= [^.\?*+()|#x5B#x5D] |
| 4378 | */ |
| 4379 | static int |
| 4380 | xmlFAIsChar(xmlRegParserCtxtPtr ctxt) { |
| 4381 | int cur; |
| 4382 | int len; |
| 4383 | |
| 4384 | cur = CUR_SCHAR(ctxt->cur, len); |
| 4385 | if ((cur == '.') || (cur == '\\') || (cur == '?') || |
| 4386 | (cur == '*') || (cur == '+') || (cur == '(') || |
| 4387 | (cur == ')') || (cur == '|') || (cur == 0x5B) || |
| 4388 | (cur == 0x5D) || (cur == 0)) |
| 4389 | return(-1); |
| 4390 | return(cur); |
| 4391 | } |
| 4392 | |
| 4393 | /** |
| 4394 | * xmlFAParseCharProp: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4395 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4396 | * |
| 4397 | * [27] charProp ::= IsCategory | IsBlock |
| 4398 | * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation | |
| 4399 | * Separators | Symbols | Others |
| 4400 | * [29] Letters ::= 'L' [ultmo]? |
| 4401 | * [30] Marks ::= 'M' [nce]? |
| 4402 | * [31] Numbers ::= 'N' [dlo]? |
| 4403 | * [32] Punctuation ::= 'P' [cdseifo]? |
| 4404 | * [33] Separators ::= 'Z' [slp]? |
| 4405 | * [34] Symbols ::= 'S' [mcko]? |
| 4406 | * [35] Others ::= 'C' [cfon]? |
| 4407 | * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+ |
| 4408 | */ |
| 4409 | static void |
| 4410 | xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) { |
| 4411 | int cur; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 4412 | xmlRegAtomType type = (xmlRegAtomType) 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4413 | xmlChar *blockName = NULL; |
| 4414 | |
| 4415 | cur = CUR; |
| 4416 | if (cur == 'L') { |
| 4417 | NEXT; |
| 4418 | cur = CUR; |
| 4419 | if (cur == 'u') { |
| 4420 | NEXT; |
| 4421 | type = XML_REGEXP_LETTER_UPPERCASE; |
| 4422 | } else if (cur == 'l') { |
| 4423 | NEXT; |
| 4424 | type = XML_REGEXP_LETTER_LOWERCASE; |
| 4425 | } else if (cur == 't') { |
| 4426 | NEXT; |
| 4427 | type = XML_REGEXP_LETTER_TITLECASE; |
| 4428 | } else if (cur == 'm') { |
| 4429 | NEXT; |
| 4430 | type = XML_REGEXP_LETTER_MODIFIER; |
| 4431 | } else if (cur == 'o') { |
| 4432 | NEXT; |
| 4433 | type = XML_REGEXP_LETTER_OTHERS; |
| 4434 | } else { |
| 4435 | type = XML_REGEXP_LETTER; |
| 4436 | } |
| 4437 | } else if (cur == 'M') { |
| 4438 | NEXT; |
| 4439 | cur = CUR; |
| 4440 | if (cur == 'n') { |
| 4441 | NEXT; |
| 4442 | /* nonspacing */ |
| 4443 | type = XML_REGEXP_MARK_NONSPACING; |
| 4444 | } else if (cur == 'c') { |
| 4445 | NEXT; |
| 4446 | /* spacing combining */ |
| 4447 | type = XML_REGEXP_MARK_SPACECOMBINING; |
| 4448 | } else if (cur == 'e') { |
| 4449 | NEXT; |
| 4450 | /* enclosing */ |
| 4451 | type = XML_REGEXP_MARK_ENCLOSING; |
| 4452 | } else { |
| 4453 | /* all marks */ |
| 4454 | type = XML_REGEXP_MARK; |
| 4455 | } |
| 4456 | } else if (cur == 'N') { |
| 4457 | NEXT; |
| 4458 | cur = CUR; |
| 4459 | if (cur == 'd') { |
| 4460 | NEXT; |
| 4461 | /* digital */ |
| 4462 | type = XML_REGEXP_NUMBER_DECIMAL; |
| 4463 | } else if (cur == 'l') { |
| 4464 | NEXT; |
| 4465 | /* letter */ |
| 4466 | type = XML_REGEXP_NUMBER_LETTER; |
| 4467 | } else if (cur == 'o') { |
| 4468 | NEXT; |
| 4469 | /* other */ |
| 4470 | type = XML_REGEXP_NUMBER_OTHERS; |
| 4471 | } else { |
| 4472 | /* all numbers */ |
| 4473 | type = XML_REGEXP_NUMBER; |
| 4474 | } |
| 4475 | } else if (cur == 'P') { |
| 4476 | NEXT; |
| 4477 | cur = CUR; |
| 4478 | if (cur == 'c') { |
| 4479 | NEXT; |
| 4480 | /* connector */ |
| 4481 | type = XML_REGEXP_PUNCT_CONNECTOR; |
| 4482 | } else if (cur == 'd') { |
| 4483 | NEXT; |
| 4484 | /* dash */ |
| 4485 | type = XML_REGEXP_PUNCT_DASH; |
| 4486 | } else if (cur == 's') { |
| 4487 | NEXT; |
| 4488 | /* open */ |
| 4489 | type = XML_REGEXP_PUNCT_OPEN; |
| 4490 | } else if (cur == 'e') { |
| 4491 | NEXT; |
| 4492 | /* close */ |
| 4493 | type = XML_REGEXP_PUNCT_CLOSE; |
| 4494 | } else if (cur == 'i') { |
| 4495 | NEXT; |
| 4496 | /* initial quote */ |
| 4497 | type = XML_REGEXP_PUNCT_INITQUOTE; |
| 4498 | } else if (cur == 'f') { |
| 4499 | NEXT; |
| 4500 | /* final quote */ |
| 4501 | type = XML_REGEXP_PUNCT_FINQUOTE; |
| 4502 | } else if (cur == 'o') { |
| 4503 | NEXT; |
| 4504 | /* other */ |
| 4505 | type = XML_REGEXP_PUNCT_OTHERS; |
| 4506 | } else { |
| 4507 | /* all punctuation */ |
| 4508 | type = XML_REGEXP_PUNCT; |
| 4509 | } |
| 4510 | } else if (cur == 'Z') { |
| 4511 | NEXT; |
| 4512 | cur = CUR; |
| 4513 | if (cur == 's') { |
| 4514 | NEXT; |
| 4515 | /* space */ |
| 4516 | type = XML_REGEXP_SEPAR_SPACE; |
| 4517 | } else if (cur == 'l') { |
| 4518 | NEXT; |
| 4519 | /* line */ |
| 4520 | type = XML_REGEXP_SEPAR_LINE; |
| 4521 | } else if (cur == 'p') { |
| 4522 | NEXT; |
| 4523 | /* paragraph */ |
| 4524 | type = XML_REGEXP_SEPAR_PARA; |
| 4525 | } else { |
| 4526 | /* all separators */ |
| 4527 | type = XML_REGEXP_SEPAR; |
| 4528 | } |
| 4529 | } else if (cur == 'S') { |
| 4530 | NEXT; |
| 4531 | cur = CUR; |
| 4532 | if (cur == 'm') { |
| 4533 | NEXT; |
| 4534 | type = XML_REGEXP_SYMBOL_MATH; |
| 4535 | /* math */ |
| 4536 | } else if (cur == 'c') { |
| 4537 | NEXT; |
| 4538 | type = XML_REGEXP_SYMBOL_CURRENCY; |
| 4539 | /* currency */ |
| 4540 | } else if (cur == 'k') { |
| 4541 | NEXT; |
| 4542 | type = XML_REGEXP_SYMBOL_MODIFIER; |
| 4543 | /* modifiers */ |
| 4544 | } else if (cur == 'o') { |
| 4545 | NEXT; |
| 4546 | type = XML_REGEXP_SYMBOL_OTHERS; |
| 4547 | /* other */ |
| 4548 | } else { |
| 4549 | /* all symbols */ |
| 4550 | type = XML_REGEXP_SYMBOL; |
| 4551 | } |
| 4552 | } else if (cur == 'C') { |
| 4553 | NEXT; |
| 4554 | cur = CUR; |
| 4555 | if (cur == 'c') { |
| 4556 | NEXT; |
| 4557 | /* control */ |
| 4558 | type = XML_REGEXP_OTHER_CONTROL; |
| 4559 | } else if (cur == 'f') { |
| 4560 | NEXT; |
| 4561 | /* format */ |
| 4562 | type = XML_REGEXP_OTHER_FORMAT; |
| 4563 | } else if (cur == 'o') { |
| 4564 | NEXT; |
| 4565 | /* private use */ |
| 4566 | type = XML_REGEXP_OTHER_PRIVATE; |
| 4567 | } else if (cur == 'n') { |
| 4568 | NEXT; |
| 4569 | /* not assigned */ |
| 4570 | type = XML_REGEXP_OTHER_NA; |
| 4571 | } else { |
| 4572 | /* all others */ |
| 4573 | type = XML_REGEXP_OTHER; |
| 4574 | } |
| 4575 | } else if (cur == 'I') { |
| 4576 | const xmlChar *start; |
| 4577 | NEXT; |
| 4578 | cur = CUR; |
| 4579 | if (cur != 's') { |
| 4580 | ERROR("IsXXXX expected"); |
| 4581 | return; |
| 4582 | } |
| 4583 | NEXT; |
| 4584 | start = ctxt->cur; |
| 4585 | cur = CUR; |
| 4586 | if (((cur >= 'a') && (cur <= 'z')) || |
| 4587 | ((cur >= 'A') && (cur <= 'Z')) || |
| 4588 | ((cur >= '0') && (cur <= '9')) || |
| 4589 | (cur == 0x2D)) { |
| 4590 | NEXT; |
| 4591 | cur = CUR; |
| 4592 | while (((cur >= 'a') && (cur <= 'z')) || |
| 4593 | ((cur >= 'A') && (cur <= 'Z')) || |
| 4594 | ((cur >= '0') && (cur <= '9')) || |
| 4595 | (cur == 0x2D)) { |
| 4596 | NEXT; |
| 4597 | cur = CUR; |
| 4598 | } |
| 4599 | } |
| 4600 | type = XML_REGEXP_BLOCK_NAME; |
| 4601 | blockName = xmlStrndup(start, ctxt->cur - start); |
| 4602 | } else { |
| 4603 | ERROR("Unknown char property"); |
| 4604 | return; |
| 4605 | } |
| 4606 | if (ctxt->atom == NULL) { |
| 4607 | ctxt->atom = xmlRegNewAtom(ctxt, type); |
| 4608 | if (ctxt->atom != NULL) |
| 4609 | ctxt->atom->valuep = blockName; |
| 4610 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 4611 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4612 | type, 0, 0, blockName); |
| 4613 | } |
| 4614 | } |
| 4615 | |
| 4616 | /** |
| 4617 | * xmlFAParseCharClassEsc: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4618 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4619 | * |
| 4620 | * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc ) |
| 4621 | * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E] |
| 4622 | * [25] catEsc ::= '\p{' charProp '}' |
| 4623 | * [26] complEsc ::= '\P{' charProp '}' |
| 4624 | * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW]) |
| 4625 | */ |
| 4626 | static void |
| 4627 | xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) { |
| 4628 | int cur; |
| 4629 | |
| 4630 | if (CUR == '.') { |
| 4631 | if (ctxt->atom == NULL) { |
| 4632 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR); |
| 4633 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 4634 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4635 | XML_REGEXP_ANYCHAR, 0, 0, NULL); |
| 4636 | } |
| 4637 | NEXT; |
| 4638 | return; |
| 4639 | } |
| 4640 | if (CUR != '\\') { |
| 4641 | ERROR("Escaped sequence: expecting \\"); |
| 4642 | return; |
| 4643 | } |
| 4644 | NEXT; |
| 4645 | cur = CUR; |
| 4646 | if (cur == 'p') { |
| 4647 | NEXT; |
| 4648 | if (CUR != '{') { |
| 4649 | ERROR("Expecting '{'"); |
| 4650 | return; |
| 4651 | } |
| 4652 | NEXT; |
| 4653 | xmlFAParseCharProp(ctxt); |
| 4654 | if (CUR != '}') { |
| 4655 | ERROR("Expecting '}'"); |
| 4656 | return; |
| 4657 | } |
| 4658 | NEXT; |
| 4659 | } else if (cur == 'P') { |
| 4660 | NEXT; |
| 4661 | if (CUR != '{') { |
| 4662 | ERROR("Expecting '{'"); |
| 4663 | return; |
| 4664 | } |
| 4665 | NEXT; |
| 4666 | xmlFAParseCharProp(ctxt); |
| 4667 | ctxt->atom->neg = 1; |
| 4668 | if (CUR != '}') { |
| 4669 | ERROR("Expecting '}'"); |
| 4670 | return; |
| 4671 | } |
| 4672 | NEXT; |
| 4673 | } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') || |
| 4674 | (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') || |
| 4675 | (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') || |
| 4676 | (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) || |
| 4677 | (cur == 0x5E)) { |
| 4678 | if (ctxt->atom == NULL) { |
| 4679 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL); |
Daniel Veillard | 99c394d | 2005-07-14 12:58:49 +0000 | [diff] [blame] | 4680 | if (ctxt->atom != NULL) { |
| 4681 | switch (cur) { |
| 4682 | case 'n': |
| 4683 | ctxt->atom->codepoint = '\n'; |
| 4684 | break; |
| 4685 | case 'r': |
| 4686 | ctxt->atom->codepoint = '\r'; |
| 4687 | break; |
| 4688 | case 't': |
| 4689 | ctxt->atom->codepoint = '\t'; |
| 4690 | break; |
| 4691 | default: |
| 4692 | ctxt->atom->codepoint = cur; |
| 4693 | } |
| 4694 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4695 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 4696 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4697 | XML_REGEXP_CHARVAL, cur, cur, NULL); |
| 4698 | } |
| 4699 | NEXT; |
| 4700 | } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') || |
| 4701 | (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') || |
| 4702 | (cur == 'w') || (cur == 'W')) { |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 4703 | xmlRegAtomType type = XML_REGEXP_ANYSPACE; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4704 | |
| 4705 | switch (cur) { |
| 4706 | case 's': |
| 4707 | type = XML_REGEXP_ANYSPACE; |
| 4708 | break; |
| 4709 | case 'S': |
| 4710 | type = XML_REGEXP_NOTSPACE; |
| 4711 | break; |
| 4712 | case 'i': |
| 4713 | type = XML_REGEXP_INITNAME; |
| 4714 | break; |
| 4715 | case 'I': |
| 4716 | type = XML_REGEXP_NOTINITNAME; |
| 4717 | break; |
| 4718 | case 'c': |
| 4719 | type = XML_REGEXP_NAMECHAR; |
| 4720 | break; |
| 4721 | case 'C': |
| 4722 | type = XML_REGEXP_NOTNAMECHAR; |
| 4723 | break; |
| 4724 | case 'd': |
| 4725 | type = XML_REGEXP_DECIMAL; |
| 4726 | break; |
| 4727 | case 'D': |
| 4728 | type = XML_REGEXP_NOTDECIMAL; |
| 4729 | break; |
| 4730 | case 'w': |
| 4731 | type = XML_REGEXP_REALCHAR; |
| 4732 | break; |
| 4733 | case 'W': |
| 4734 | type = XML_REGEXP_NOTREALCHAR; |
| 4735 | break; |
| 4736 | } |
| 4737 | NEXT; |
| 4738 | if (ctxt->atom == NULL) { |
| 4739 | ctxt->atom = xmlRegNewAtom(ctxt, type); |
| 4740 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 4741 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4742 | type, 0, 0, NULL); |
| 4743 | } |
| 4744 | } |
| 4745 | } |
| 4746 | |
| 4747 | /** |
| 4748 | * xmlFAParseCharRef: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4749 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4750 | * |
| 4751 | * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' ) |
| 4752 | */ |
| 4753 | static int |
| 4754 | xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) { |
| 4755 | int ret = 0, cur; |
| 4756 | |
| 4757 | if ((CUR != '&') || (NXT(1) != '#')) |
| 4758 | return(-1); |
| 4759 | NEXT; |
| 4760 | NEXT; |
| 4761 | cur = CUR; |
| 4762 | if (cur == 'x') { |
| 4763 | NEXT; |
| 4764 | cur = CUR; |
| 4765 | if (((cur >= '0') && (cur <= '9')) || |
| 4766 | ((cur >= 'a') && (cur <= 'f')) || |
| 4767 | ((cur >= 'A') && (cur <= 'F'))) { |
| 4768 | while (((cur >= '0') && (cur <= '9')) || |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 4769 | ((cur >= 'a') && (cur <= 'f')) || |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4770 | ((cur >= 'A') && (cur <= 'F'))) { |
| 4771 | if ((cur >= '0') && (cur <= '9')) |
| 4772 | ret = ret * 16 + cur - '0'; |
| 4773 | else if ((cur >= 'a') && (cur <= 'f')) |
| 4774 | ret = ret * 16 + 10 + (cur - 'a'); |
| 4775 | else |
| 4776 | ret = ret * 16 + 10 + (cur - 'A'); |
| 4777 | NEXT; |
| 4778 | cur = CUR; |
| 4779 | } |
| 4780 | } else { |
| 4781 | ERROR("Char ref: expecting [0-9A-F]"); |
| 4782 | return(-1); |
| 4783 | } |
| 4784 | } else { |
| 4785 | if ((cur >= '0') && (cur <= '9')) { |
| 4786 | while ((cur >= '0') && (cur <= '9')) { |
| 4787 | ret = ret * 10 + cur - '0'; |
| 4788 | NEXT; |
| 4789 | cur = CUR; |
| 4790 | } |
| 4791 | } else { |
| 4792 | ERROR("Char ref: expecting [0-9]"); |
| 4793 | return(-1); |
| 4794 | } |
| 4795 | } |
| 4796 | if (cur != ';') { |
| 4797 | ERROR("Char ref: expecting ';'"); |
| 4798 | return(-1); |
| 4799 | } else { |
| 4800 | NEXT; |
| 4801 | } |
| 4802 | return(ret); |
| 4803 | } |
| 4804 | |
| 4805 | /** |
| 4806 | * xmlFAParseCharRange: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4807 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4808 | * |
| 4809 | * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash |
| 4810 | * [18] seRange ::= charOrEsc '-' charOrEsc |
| 4811 | * [20] charOrEsc ::= XmlChar | SingleCharEsc |
| 4812 | * [21] XmlChar ::= [^\#x2D#x5B#x5D] |
| 4813 | * [22] XmlCharIncDash ::= [^\#x5B#x5D] |
| 4814 | */ |
| 4815 | static void |
| 4816 | xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) { |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4817 | int cur, len; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4818 | int start = -1; |
| 4819 | int end = -1; |
| 4820 | |
Daniel Veillard | 777737e | 2006-10-17 21:23:17 +0000 | [diff] [blame] | 4821 | if (CUR == '\0') { |
| 4822 | ERROR("Expecting ']'"); |
| 4823 | return; |
| 4824 | } |
| 4825 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4826 | if ((CUR == '&') && (NXT(1) == '#')) { |
| 4827 | end = start = xmlFAParseCharRef(ctxt); |
| 4828 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4829 | XML_REGEXP_CHARVAL, start, end, NULL); |
| 4830 | return; |
| 4831 | } |
| 4832 | cur = CUR; |
| 4833 | if (cur == '\\') { |
| 4834 | NEXT; |
| 4835 | cur = CUR; |
| 4836 | switch (cur) { |
| 4837 | case 'n': start = 0xA; break; |
| 4838 | case 'r': start = 0xD; break; |
| 4839 | case 't': start = 0x9; break; |
| 4840 | case '\\': case '|': case '.': case '-': case '^': case '?': |
| 4841 | case '*': case '+': case '{': case '}': case '(': case ')': |
| 4842 | case '[': case ']': |
| 4843 | start = cur; break; |
| 4844 | default: |
| 4845 | ERROR("Invalid escape value"); |
| 4846 | return; |
| 4847 | } |
| 4848 | end = start; |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4849 | len = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4850 | } else if ((cur != 0x5B) && (cur != 0x5D)) { |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4851 | end = start = CUR_SCHAR(ctxt->cur, len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4852 | } else { |
| 4853 | ERROR("Expecting a char range"); |
| 4854 | return; |
| 4855 | } |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4856 | NEXTL(len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4857 | if (start == '-') { |
| 4858 | return; |
| 4859 | } |
| 4860 | cur = CUR; |
William M. Brack | 10f1ef4 | 2004-03-20 14:51:25 +0000 | [diff] [blame] | 4861 | if ((cur != '-') || (NXT(1) == ']')) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4862 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4863 | XML_REGEXP_CHARVAL, start, end, NULL); |
| 4864 | return; |
| 4865 | } |
| 4866 | NEXT; |
| 4867 | cur = CUR; |
| 4868 | if (cur == '\\') { |
| 4869 | NEXT; |
| 4870 | cur = CUR; |
| 4871 | switch (cur) { |
| 4872 | case 'n': end = 0xA; break; |
| 4873 | case 'r': end = 0xD; break; |
| 4874 | case 't': end = 0x9; break; |
| 4875 | case '\\': case '|': case '.': case '-': case '^': case '?': |
| 4876 | case '*': case '+': case '{': case '}': case '(': case ')': |
| 4877 | case '[': case ']': |
| 4878 | end = cur; break; |
| 4879 | default: |
| 4880 | ERROR("Invalid escape value"); |
| 4881 | return; |
| 4882 | } |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4883 | len = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4884 | } else if ((cur != 0x5B) && (cur != 0x5D)) { |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4885 | end = CUR_SCHAR(ctxt->cur, len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4886 | } else { |
| 4887 | ERROR("Expecting the end of a char range"); |
| 4888 | return; |
| 4889 | } |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4890 | NEXTL(len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4891 | /* TODO check that the values are acceptable character ranges for XML */ |
| 4892 | if (end < start) { |
| 4893 | ERROR("End of range is before start of range"); |
| 4894 | } else { |
| 4895 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4896 | XML_REGEXP_CHARVAL, start, end, NULL); |
| 4897 | } |
| 4898 | return; |
| 4899 | } |
| 4900 | |
| 4901 | /** |
| 4902 | * xmlFAParsePosCharGroup: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4903 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4904 | * |
| 4905 | * [14] posCharGroup ::= ( charRange | charClassEsc )+ |
| 4906 | */ |
| 4907 | static void |
| 4908 | xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) { |
| 4909 | do { |
| 4910 | if ((CUR == '\\') || (CUR == '.')) { |
| 4911 | xmlFAParseCharClassEsc(ctxt); |
| 4912 | } else { |
| 4913 | xmlFAParseCharRange(ctxt); |
| 4914 | } |
| 4915 | } while ((CUR != ']') && (CUR != '^') && (CUR != '-') && |
Daniel Veillard | 777737e | 2006-10-17 21:23:17 +0000 | [diff] [blame] | 4916 | (CUR != 0) && (ctxt->error == 0)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4917 | } |
| 4918 | |
| 4919 | /** |
| 4920 | * xmlFAParseCharGroup: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4921 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4922 | * |
| 4923 | * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub |
| 4924 | * [15] negCharGroup ::= '^' posCharGroup |
| 4925 | * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr |
| 4926 | * [12] charClassExpr ::= '[' charGroup ']' |
| 4927 | */ |
| 4928 | static void |
| 4929 | xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) { |
| 4930 | int n = ctxt->neg; |
| 4931 | while ((CUR != ']') && (ctxt->error == 0)) { |
| 4932 | if (CUR == '^') { |
| 4933 | int neg = ctxt->neg; |
| 4934 | |
| 4935 | NEXT; |
| 4936 | ctxt->neg = !ctxt->neg; |
| 4937 | xmlFAParsePosCharGroup(ctxt); |
| 4938 | ctxt->neg = neg; |
William M. Brack | 10f1ef4 | 2004-03-20 14:51:25 +0000 | [diff] [blame] | 4939 | } else if ((CUR == '-') && (NXT(1) == '[')) { |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 4940 | int neg = ctxt->neg; |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 4941 | ctxt->neg = 2; |
William M. Brack | 10f1ef4 | 2004-03-20 14:51:25 +0000 | [diff] [blame] | 4942 | NEXT; /* eat the '-' */ |
| 4943 | NEXT; /* eat the '[' */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4944 | xmlFAParseCharGroup(ctxt); |
| 4945 | if (CUR == ']') { |
| 4946 | NEXT; |
| 4947 | } else { |
| 4948 | ERROR("charClassExpr: ']' expected"); |
| 4949 | break; |
| 4950 | } |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 4951 | ctxt->neg = neg; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4952 | break; |
| 4953 | } else if (CUR != ']') { |
| 4954 | xmlFAParsePosCharGroup(ctxt); |
| 4955 | } |
| 4956 | } |
| 4957 | ctxt->neg = n; |
| 4958 | } |
| 4959 | |
| 4960 | /** |
| 4961 | * xmlFAParseCharClass: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4962 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4963 | * |
| 4964 | * [11] charClass ::= charClassEsc | charClassExpr |
| 4965 | * [12] charClassExpr ::= '[' charGroup ']' |
| 4966 | */ |
| 4967 | static void |
| 4968 | xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) { |
| 4969 | if (CUR == '[') { |
| 4970 | NEXT; |
| 4971 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES); |
| 4972 | if (ctxt->atom == NULL) |
| 4973 | return; |
| 4974 | xmlFAParseCharGroup(ctxt); |
| 4975 | if (CUR == ']') { |
| 4976 | NEXT; |
| 4977 | } else { |
| 4978 | ERROR("xmlFAParseCharClass: ']' expected"); |
| 4979 | } |
| 4980 | } else { |
| 4981 | xmlFAParseCharClassEsc(ctxt); |
| 4982 | } |
| 4983 | } |
| 4984 | |
| 4985 | /** |
| 4986 | * xmlFAParseQuantExact: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4987 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4988 | * |
| 4989 | * [8] QuantExact ::= [0-9]+ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4990 | * |
| 4991 | * Returns 0 if success or -1 in case of error |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4992 | */ |
| 4993 | static int |
| 4994 | xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) { |
| 4995 | int ret = 0; |
| 4996 | int ok = 0; |
| 4997 | |
| 4998 | while ((CUR >= '0') && (CUR <= '9')) { |
| 4999 | ret = ret * 10 + (CUR - '0'); |
| 5000 | ok = 1; |
| 5001 | NEXT; |
| 5002 | } |
| 5003 | if (ok != 1) { |
| 5004 | return(-1); |
| 5005 | } |
| 5006 | return(ret); |
| 5007 | } |
| 5008 | |
| 5009 | /** |
| 5010 | * xmlFAParseQuantifier: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5011 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5012 | * |
| 5013 | * [4] quantifier ::= [?*+] | ( '{' quantity '}' ) |
| 5014 | * [5] quantity ::= quantRange | quantMin | QuantExact |
| 5015 | * [6] quantRange ::= QuantExact ',' QuantExact |
| 5016 | * [7] quantMin ::= QuantExact ',' |
| 5017 | * [8] QuantExact ::= [0-9]+ |
| 5018 | */ |
| 5019 | static int |
| 5020 | xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) { |
| 5021 | int cur; |
| 5022 | |
| 5023 | cur = CUR; |
| 5024 | if ((cur == '?') || (cur == '*') || (cur == '+')) { |
| 5025 | if (ctxt->atom != NULL) { |
| 5026 | if (cur == '?') |
| 5027 | ctxt->atom->quant = XML_REGEXP_QUANT_OPT; |
| 5028 | else if (cur == '*') |
| 5029 | ctxt->atom->quant = XML_REGEXP_QUANT_MULT; |
| 5030 | else if (cur == '+') |
| 5031 | ctxt->atom->quant = XML_REGEXP_QUANT_PLUS; |
| 5032 | } |
| 5033 | NEXT; |
| 5034 | return(1); |
| 5035 | } |
| 5036 | if (cur == '{') { |
| 5037 | int min = 0, max = 0; |
| 5038 | |
| 5039 | NEXT; |
| 5040 | cur = xmlFAParseQuantExact(ctxt); |
| 5041 | if (cur >= 0) |
| 5042 | min = cur; |
| 5043 | if (CUR == ',') { |
| 5044 | NEXT; |
Daniel Veillard | ebe48c6 | 2003-12-03 12:12:27 +0000 | [diff] [blame] | 5045 | if (CUR == '}') |
| 5046 | max = INT_MAX; |
| 5047 | else { |
| 5048 | cur = xmlFAParseQuantExact(ctxt); |
| 5049 | if (cur >= 0) |
| 5050 | max = cur; |
| 5051 | else { |
| 5052 | ERROR("Improper quantifier"); |
| 5053 | } |
| 5054 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5055 | } |
| 5056 | if (CUR == '}') { |
| 5057 | NEXT; |
| 5058 | } else { |
| 5059 | ERROR("Unterminated quantifier"); |
| 5060 | } |
| 5061 | if (max == 0) |
| 5062 | max = min; |
| 5063 | if (ctxt->atom != NULL) { |
| 5064 | ctxt->atom->quant = XML_REGEXP_QUANT_RANGE; |
| 5065 | ctxt->atom->min = min; |
| 5066 | ctxt->atom->max = max; |
| 5067 | } |
| 5068 | return(1); |
| 5069 | } |
| 5070 | return(0); |
| 5071 | } |
| 5072 | |
| 5073 | /** |
| 5074 | * xmlFAParseAtom: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5075 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5076 | * |
| 5077 | * [9] atom ::= Char | charClass | ( '(' regExp ')' ) |
| 5078 | */ |
| 5079 | static int |
| 5080 | xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) { |
| 5081 | int codepoint, len; |
| 5082 | |
| 5083 | codepoint = xmlFAIsChar(ctxt); |
| 5084 | if (codepoint > 0) { |
| 5085 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL); |
| 5086 | if (ctxt->atom == NULL) |
| 5087 | return(-1); |
| 5088 | codepoint = CUR_SCHAR(ctxt->cur, len); |
| 5089 | ctxt->atom->codepoint = codepoint; |
| 5090 | NEXTL(len); |
| 5091 | return(1); |
| 5092 | } else if (CUR == '|') { |
| 5093 | return(0); |
| 5094 | } else if (CUR == 0) { |
| 5095 | return(0); |
| 5096 | } else if (CUR == ')') { |
| 5097 | return(0); |
| 5098 | } else if (CUR == '(') { |
| 5099 | xmlRegStatePtr start, oldend; |
| 5100 | |
| 5101 | NEXT; |
| 5102 | xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL); |
| 5103 | start = ctxt->state; |
| 5104 | oldend = ctxt->end; |
| 5105 | ctxt->end = NULL; |
| 5106 | ctxt->atom = NULL; |
| 5107 | xmlFAParseRegExp(ctxt, 0); |
| 5108 | if (CUR == ')') { |
| 5109 | NEXT; |
| 5110 | } else { |
| 5111 | ERROR("xmlFAParseAtom: expecting ')'"); |
| 5112 | } |
| 5113 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG); |
| 5114 | if (ctxt->atom == NULL) |
| 5115 | return(-1); |
| 5116 | ctxt->atom->start = start; |
| 5117 | ctxt->atom->stop = ctxt->state; |
| 5118 | ctxt->end = oldend; |
| 5119 | return(1); |
| 5120 | } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) { |
| 5121 | xmlFAParseCharClass(ctxt); |
| 5122 | return(1); |
| 5123 | } |
| 5124 | return(0); |
| 5125 | } |
| 5126 | |
| 5127 | /** |
| 5128 | * xmlFAParsePiece: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5129 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5130 | * |
| 5131 | * [3] piece ::= atom quantifier? |
| 5132 | */ |
| 5133 | static int |
| 5134 | xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) { |
| 5135 | int ret; |
| 5136 | |
| 5137 | ctxt->atom = NULL; |
| 5138 | ret = xmlFAParseAtom(ctxt); |
| 5139 | if (ret == 0) |
| 5140 | return(0); |
| 5141 | if (ctxt->atom == NULL) { |
| 5142 | ERROR("internal: no atom generated"); |
| 5143 | } |
| 5144 | xmlFAParseQuantifier(ctxt); |
| 5145 | return(1); |
| 5146 | } |
| 5147 | |
| 5148 | /** |
| 5149 | * xmlFAParseBranch: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5150 | * @ctxt: a regexp parser context |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5151 | * @to: optional target to the end of the branch |
| 5152 | * |
| 5153 | * @to is used to optimize by removing duplicate path in automata |
| 5154 | * in expressions like (a|b)(c|d) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5155 | * |
| 5156 | * [2] branch ::= piece* |
| 5157 | */ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5158 | static int |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5159 | xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5160 | xmlRegStatePtr previous; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5161 | int ret; |
| 5162 | |
| 5163 | previous = ctxt->state; |
| 5164 | ret = xmlFAParsePiece(ctxt); |
| 5165 | if (ret != 0) { |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5166 | if (xmlFAGenerateTransitions(ctxt, previous, |
| 5167 | (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0) |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5168 | return(-1); |
| 5169 | previous = ctxt->state; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5170 | ctxt->atom = NULL; |
| 5171 | } |
| 5172 | while ((ret != 0) && (ctxt->error == 0)) { |
| 5173 | ret = xmlFAParsePiece(ctxt); |
| 5174 | if (ret != 0) { |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5175 | if (xmlFAGenerateTransitions(ctxt, previous, |
| 5176 | (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5177 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5178 | previous = ctxt->state; |
| 5179 | ctxt->atom = NULL; |
| 5180 | } |
| 5181 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5182 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5183 | } |
| 5184 | |
| 5185 | /** |
| 5186 | * xmlFAParseRegExp: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5187 | * @ctxt: a regexp parser context |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5188 | * @top: is this the top-level expression ? |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5189 | * |
| 5190 | * [1] regExp ::= branch ( '|' branch )* |
| 5191 | */ |
| 5192 | static void |
| 5193 | xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) { |
Daniel Veillard | c7e3cc4 | 2004-09-28 12:33:52 +0000 | [diff] [blame] | 5194 | xmlRegStatePtr start, end; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5195 | |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5196 | /* if not top start should have been generated by an epsilon trans */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5197 | start = ctxt->state; |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5198 | ctxt->end = NULL; |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5199 | xmlFAParseBranch(ctxt, NULL); |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5200 | if (top) { |
| 5201 | #ifdef DEBUG_REGEXP_GRAPH |
| 5202 | printf("State %d is final\n", ctxt->state->no); |
| 5203 | #endif |
| 5204 | ctxt->state->type = XML_REGEXP_FINAL_STATE; |
| 5205 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5206 | if (CUR != '|') { |
| 5207 | ctxt->end = ctxt->state; |
| 5208 | return; |
| 5209 | } |
| 5210 | end = ctxt->state; |
| 5211 | while ((CUR == '|') && (ctxt->error == 0)) { |
| 5212 | NEXT; |
| 5213 | ctxt->state = start; |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5214 | ctxt->end = NULL; |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5215 | xmlFAParseBranch(ctxt, end); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5216 | } |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5217 | if (!top) { |
| 5218 | ctxt->state = end; |
| 5219 | ctxt->end = end; |
| 5220 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5221 | } |
| 5222 | |
| 5223 | /************************************************************************ |
| 5224 | * * |
| 5225 | * The basic API * |
| 5226 | * * |
| 5227 | ************************************************************************/ |
| 5228 | |
| 5229 | /** |
| 5230 | * xmlRegexpPrint: |
| 5231 | * @output: the file for the output debug |
| 5232 | * @regexp: the compiled regexp |
| 5233 | * |
| 5234 | * Print the content of the compiled regular expression |
| 5235 | */ |
| 5236 | void |
| 5237 | xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) { |
| 5238 | int i; |
| 5239 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5240 | if (output == NULL) |
| 5241 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5242 | fprintf(output, " regexp: "); |
| 5243 | if (regexp == NULL) { |
| 5244 | fprintf(output, "NULL\n"); |
| 5245 | return; |
| 5246 | } |
| 5247 | fprintf(output, "'%s' ", regexp->string); |
| 5248 | fprintf(output, "\n"); |
| 5249 | fprintf(output, "%d atoms:\n", regexp->nbAtoms); |
| 5250 | for (i = 0;i < regexp->nbAtoms; i++) { |
| 5251 | fprintf(output, " %02d ", i); |
| 5252 | xmlRegPrintAtom(output, regexp->atoms[i]); |
| 5253 | } |
| 5254 | fprintf(output, "%d states:", regexp->nbStates); |
| 5255 | fprintf(output, "\n"); |
| 5256 | for (i = 0;i < regexp->nbStates; i++) { |
| 5257 | xmlRegPrintState(output, regexp->states[i]); |
| 5258 | } |
| 5259 | fprintf(output, "%d counters:\n", regexp->nbCounters); |
| 5260 | for (i = 0;i < regexp->nbCounters; i++) { |
| 5261 | fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min, |
| 5262 | regexp->counters[i].max); |
| 5263 | } |
| 5264 | } |
| 5265 | |
| 5266 | /** |
| 5267 | * xmlRegexpCompile: |
| 5268 | * @regexp: a regular expression string |
| 5269 | * |
| 5270 | * Parses a regular expression conforming to XML Schemas Part 2 Datatype |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5271 | * Appendix F and builds an automata suitable for testing strings against |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5272 | * that regular expression |
| 5273 | * |
| 5274 | * Returns the compiled expression or NULL in case of error |
| 5275 | */ |
| 5276 | xmlRegexpPtr |
| 5277 | xmlRegexpCompile(const xmlChar *regexp) { |
| 5278 | xmlRegexpPtr ret; |
| 5279 | xmlRegParserCtxtPtr ctxt; |
| 5280 | |
| 5281 | ctxt = xmlRegNewParserCtxt(regexp); |
| 5282 | if (ctxt == NULL) |
| 5283 | return(NULL); |
| 5284 | |
| 5285 | /* initialize the parser */ |
| 5286 | ctxt->end = NULL; |
| 5287 | ctxt->start = ctxt->state = xmlRegNewState(ctxt); |
| 5288 | xmlRegStatePush(ctxt, ctxt->start); |
| 5289 | |
| 5290 | /* parse the expression building an automata */ |
| 5291 | xmlFAParseRegExp(ctxt, 1); |
| 5292 | if (CUR != 0) { |
| 5293 | ERROR("xmlFAParseRegExp: extra characters"); |
| 5294 | } |
| 5295 | ctxt->end = ctxt->state; |
| 5296 | ctxt->start->type = XML_REGEXP_START_STATE; |
| 5297 | ctxt->end->type = XML_REGEXP_FINAL_STATE; |
| 5298 | |
| 5299 | /* remove the Epsilon except for counted transitions */ |
| 5300 | xmlFAEliminateEpsilonTransitions(ctxt); |
| 5301 | |
| 5302 | |
| 5303 | if (ctxt->error != 0) { |
| 5304 | xmlRegFreeParserCtxt(ctxt); |
| 5305 | return(NULL); |
| 5306 | } |
| 5307 | ret = xmlRegEpxFromParse(ctxt); |
| 5308 | xmlRegFreeParserCtxt(ctxt); |
| 5309 | return(ret); |
| 5310 | } |
| 5311 | |
| 5312 | /** |
| 5313 | * xmlRegexpExec: |
| 5314 | * @comp: the compiled regular expression |
| 5315 | * @content: the value to check against the regular expression |
| 5316 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5317 | * Check if the regular expression generates the value |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5318 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5319 | * 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] | 5320 | */ |
| 5321 | int |
| 5322 | xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) { |
| 5323 | if ((comp == NULL) || (content == NULL)) |
| 5324 | return(-1); |
| 5325 | return(xmlFARegExec(comp, content)); |
| 5326 | } |
| 5327 | |
| 5328 | /** |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5329 | * xmlRegexpIsDeterminist: |
| 5330 | * @comp: the compiled regular expression |
| 5331 | * |
| 5332 | * Check if the regular expression is determinist |
| 5333 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5334 | * 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] | 5335 | */ |
| 5336 | int |
| 5337 | xmlRegexpIsDeterminist(xmlRegexpPtr comp) { |
| 5338 | xmlAutomataPtr am; |
| 5339 | int ret; |
| 5340 | |
| 5341 | if (comp == NULL) |
| 5342 | return(-1); |
| 5343 | if (comp->determinist != -1) |
| 5344 | return(comp->determinist); |
| 5345 | |
| 5346 | am = xmlNewAutomata(); |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5347 | if (am->states != NULL) { |
| 5348 | int i; |
| 5349 | |
| 5350 | for (i = 0;i < am->nbStates;i++) |
| 5351 | xmlRegFreeState(am->states[i]); |
| 5352 | xmlFree(am->states); |
| 5353 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5354 | am->nbAtoms = comp->nbAtoms; |
| 5355 | am->atoms = comp->atoms; |
| 5356 | am->nbStates = comp->nbStates; |
| 5357 | am->states = comp->states; |
| 5358 | am->determinist = -1; |
| 5359 | ret = xmlFAComputesDeterminism(am); |
| 5360 | am->atoms = NULL; |
| 5361 | am->states = NULL; |
| 5362 | xmlFreeAutomata(am); |
| 5363 | return(ret); |
| 5364 | } |
| 5365 | |
| 5366 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5367 | * xmlRegFreeRegexp: |
| 5368 | * @regexp: the regexp |
| 5369 | * |
| 5370 | * Free a regexp |
| 5371 | */ |
| 5372 | void |
| 5373 | xmlRegFreeRegexp(xmlRegexpPtr regexp) { |
| 5374 | int i; |
| 5375 | if (regexp == NULL) |
| 5376 | return; |
| 5377 | |
| 5378 | if (regexp->string != NULL) |
| 5379 | xmlFree(regexp->string); |
| 5380 | if (regexp->states != NULL) { |
| 5381 | for (i = 0;i < regexp->nbStates;i++) |
| 5382 | xmlRegFreeState(regexp->states[i]); |
| 5383 | xmlFree(regexp->states); |
| 5384 | } |
| 5385 | if (regexp->atoms != NULL) { |
| 5386 | for (i = 0;i < regexp->nbAtoms;i++) |
| 5387 | xmlRegFreeAtom(regexp->atoms[i]); |
| 5388 | xmlFree(regexp->atoms); |
| 5389 | } |
| 5390 | if (regexp->counters != NULL) |
| 5391 | xmlFree(regexp->counters); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5392 | if (regexp->compact != NULL) |
| 5393 | xmlFree(regexp->compact); |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 5394 | if (regexp->transdata != NULL) |
| 5395 | xmlFree(regexp->transdata); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5396 | if (regexp->stringMap != NULL) { |
| 5397 | for (i = 0; i < regexp->nbstrings;i++) |
| 5398 | xmlFree(regexp->stringMap[i]); |
| 5399 | xmlFree(regexp->stringMap); |
| 5400 | } |
| 5401 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5402 | xmlFree(regexp); |
| 5403 | } |
| 5404 | |
| 5405 | #ifdef LIBXML_AUTOMATA_ENABLED |
| 5406 | /************************************************************************ |
| 5407 | * * |
| 5408 | * The Automata interface * |
| 5409 | * * |
| 5410 | ************************************************************************/ |
| 5411 | |
| 5412 | /** |
| 5413 | * xmlNewAutomata: |
| 5414 | * |
| 5415 | * Create a new automata |
| 5416 | * |
| 5417 | * Returns the new object or NULL in case of failure |
| 5418 | */ |
| 5419 | xmlAutomataPtr |
| 5420 | xmlNewAutomata(void) { |
| 5421 | xmlAutomataPtr ctxt; |
| 5422 | |
| 5423 | ctxt = xmlRegNewParserCtxt(NULL); |
| 5424 | if (ctxt == NULL) |
| 5425 | return(NULL); |
| 5426 | |
| 5427 | /* initialize the parser */ |
| 5428 | ctxt->end = NULL; |
| 5429 | ctxt->start = ctxt->state = xmlRegNewState(ctxt); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5430 | if (ctxt->start == NULL) { |
| 5431 | xmlFreeAutomata(ctxt); |
| 5432 | return(NULL); |
| 5433 | } |
Daniel Veillard | d027147 | 2006-01-02 10:22:02 +0000 | [diff] [blame] | 5434 | ctxt->start->type = XML_REGEXP_START_STATE; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5435 | if (xmlRegStatePush(ctxt, ctxt->start) < 0) { |
| 5436 | xmlRegFreeState(ctxt->start); |
| 5437 | xmlFreeAutomata(ctxt); |
| 5438 | return(NULL); |
| 5439 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5440 | |
| 5441 | return(ctxt); |
| 5442 | } |
| 5443 | |
| 5444 | /** |
| 5445 | * xmlFreeAutomata: |
| 5446 | * @am: an automata |
| 5447 | * |
| 5448 | * Free an automata |
| 5449 | */ |
| 5450 | void |
| 5451 | xmlFreeAutomata(xmlAutomataPtr am) { |
| 5452 | if (am == NULL) |
| 5453 | return; |
| 5454 | xmlRegFreeParserCtxt(am); |
| 5455 | } |
| 5456 | |
| 5457 | /** |
| 5458 | * xmlAutomataGetInitState: |
| 5459 | * @am: an automata |
| 5460 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5461 | * Initial state lookup |
| 5462 | * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5463 | * Returns the initial state of the automata |
| 5464 | */ |
| 5465 | xmlAutomataStatePtr |
| 5466 | xmlAutomataGetInitState(xmlAutomataPtr am) { |
| 5467 | if (am == NULL) |
| 5468 | return(NULL); |
| 5469 | return(am->start); |
| 5470 | } |
| 5471 | |
| 5472 | /** |
| 5473 | * xmlAutomataSetFinalState: |
| 5474 | * @am: an automata |
| 5475 | * @state: a state in this automata |
| 5476 | * |
| 5477 | * Makes that state a final state |
| 5478 | * |
| 5479 | * Returns 0 or -1 in case of error |
| 5480 | */ |
| 5481 | int |
| 5482 | xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) { |
| 5483 | if ((am == NULL) || (state == NULL)) |
| 5484 | return(-1); |
| 5485 | state->type = XML_REGEXP_FINAL_STATE; |
| 5486 | return(0); |
| 5487 | } |
| 5488 | |
| 5489 | /** |
| 5490 | * xmlAutomataNewTransition: |
| 5491 | * @am: an automata |
| 5492 | * @from: the starting point of the transition |
| 5493 | * @to: the target point of the transition or NULL |
| 5494 | * @token: the input string associated to that transition |
| 5495 | * @data: data passed to the callback function if the transition is activated |
| 5496 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5497 | * 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] | 5498 | * and then adds a transition from the @from state to the target state |
| 5499 | * activated by the value of @token |
| 5500 | * |
| 5501 | * Returns the target state or NULL in case of error |
| 5502 | */ |
| 5503 | xmlAutomataStatePtr |
| 5504 | xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5505 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5506 | void *data) { |
| 5507 | xmlRegAtomPtr atom; |
| 5508 | |
| 5509 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5510 | return(NULL); |
| 5511 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5512 | if (atom == NULL) |
| 5513 | return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5514 | atom->data = data; |
| 5515 | if (atom == NULL) |
| 5516 | return(NULL); |
| 5517 | atom->valuep = xmlStrdup(token); |
| 5518 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5519 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
| 5520 | xmlRegFreeAtom(atom); |
| 5521 | return(NULL); |
| 5522 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5523 | if (to == NULL) |
| 5524 | return(am->state); |
| 5525 | return(to); |
| 5526 | } |
| 5527 | |
| 5528 | /** |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5529 | * xmlAutomataNewTransition2: |
| 5530 | * @am: an automata |
| 5531 | * @from: the starting point of the transition |
| 5532 | * @to: the target point of the transition or NULL |
| 5533 | * @token: the first input string associated to that transition |
| 5534 | * @token2: the second input string associated to that transition |
| 5535 | * @data: data passed to the callback function if the transition is activated |
| 5536 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5537 | * 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] | 5538 | * and then adds a transition from the @from state to the target state |
| 5539 | * activated by the value of @token |
| 5540 | * |
| 5541 | * Returns the target state or NULL in case of error |
| 5542 | */ |
| 5543 | xmlAutomataStatePtr |
| 5544 | xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5545 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5546 | const xmlChar *token2, void *data) { |
| 5547 | xmlRegAtomPtr atom; |
| 5548 | |
| 5549 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5550 | return(NULL); |
| 5551 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5552 | if (atom == NULL) |
| 5553 | return(NULL); |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5554 | atom->data = data; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5555 | if ((token2 == NULL) || (*token2 == 0)) { |
| 5556 | atom->valuep = xmlStrdup(token); |
| 5557 | } else { |
| 5558 | int lenn, lenp; |
| 5559 | xmlChar *str; |
| 5560 | |
| 5561 | lenn = strlen((char *) token2); |
| 5562 | lenp = strlen((char *) token); |
| 5563 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 5564 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5565 | if (str == NULL) { |
| 5566 | xmlRegFreeAtom(atom); |
| 5567 | return(NULL); |
| 5568 | } |
| 5569 | memcpy(&str[0], token, lenp); |
| 5570 | str[lenp] = '|'; |
| 5571 | memcpy(&str[lenp + 1], token2, lenn); |
| 5572 | str[lenn + lenp + 1] = 0; |
| 5573 | |
| 5574 | atom->valuep = str; |
| 5575 | } |
| 5576 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5577 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
| 5578 | xmlRegFreeAtom(atom); |
| 5579 | return(NULL); |
| 5580 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5581 | if (to == NULL) |
| 5582 | return(am->state); |
| 5583 | return(to); |
| 5584 | } |
| 5585 | |
| 5586 | /** |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 5587 | * xmlAutomataNewNegTrans: |
| 5588 | * @am: an automata |
| 5589 | * @from: the starting point of the transition |
| 5590 | * @to: the target point of the transition or NULL |
| 5591 | * @token: the first input string associated to that transition |
| 5592 | * @token2: the second input string associated to that transition |
| 5593 | * @data: data passed to the callback function if the transition is activated |
| 5594 | * |
| 5595 | * If @to is NULL, this creates first a new target state in the automata |
| 5596 | * and then adds a transition from the @from state to the target state |
| 5597 | * activated by any value except (@token,@token2) |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 5598 | * Note that if @token2 is not NULL, then (X, NULL) won't match to follow |
| 5599 | # the semantic of XSD ##other |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 5600 | * |
| 5601 | * Returns the target state or NULL in case of error |
| 5602 | */ |
| 5603 | xmlAutomataStatePtr |
| 5604 | xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5605 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5606 | const xmlChar *token2, void *data) { |
| 5607 | xmlRegAtomPtr atom; |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 5608 | xmlChar err_msg[200]; |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 5609 | |
| 5610 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5611 | return(NULL); |
| 5612 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5613 | if (atom == NULL) |
| 5614 | return(NULL); |
| 5615 | atom->data = data; |
| 5616 | atom->neg = 1; |
| 5617 | if ((token2 == NULL) || (*token2 == 0)) { |
| 5618 | atom->valuep = xmlStrdup(token); |
| 5619 | } else { |
| 5620 | int lenn, lenp; |
| 5621 | xmlChar *str; |
| 5622 | |
| 5623 | lenn = strlen((char *) token2); |
| 5624 | lenp = strlen((char *) token); |
| 5625 | |
| 5626 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
| 5627 | if (str == NULL) { |
| 5628 | xmlRegFreeAtom(atom); |
| 5629 | return(NULL); |
| 5630 | } |
| 5631 | memcpy(&str[0], token, lenp); |
| 5632 | str[lenp] = '|'; |
| 5633 | memcpy(&str[lenp + 1], token2, lenn); |
| 5634 | str[lenn + lenp + 1] = 0; |
| 5635 | |
| 5636 | atom->valuep = str; |
| 5637 | } |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 5638 | snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep); |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 5639 | err_msg[199] = 0; |
| 5640 | atom->valuep2 = xmlStrdup(err_msg); |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 5641 | |
| 5642 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
| 5643 | xmlRegFreeAtom(atom); |
| 5644 | return(NULL); |
| 5645 | } |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 5646 | am->negs++; |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 5647 | if (to == NULL) |
| 5648 | return(am->state); |
| 5649 | return(to); |
| 5650 | } |
| 5651 | |
| 5652 | /** |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 5653 | * xmlAutomataNewCountTrans2: |
| 5654 | * @am: an automata |
| 5655 | * @from: the starting point of the transition |
| 5656 | * @to: the target point of the transition or NULL |
| 5657 | * @token: the input string associated to that transition |
| 5658 | * @token2: the second input string associated to that transition |
| 5659 | * @min: the minimum successive occurences of token |
| 5660 | * @max: the maximum successive occurences of token |
| 5661 | * @data: data associated to the transition |
| 5662 | * |
| 5663 | * If @to is NULL, this creates first a new target state in the automata |
| 5664 | * and then adds a transition from the @from state to the target state |
| 5665 | * activated by a succession of input of value @token and @token2 and |
| 5666 | * whose number is between @min and @max |
| 5667 | * |
| 5668 | * Returns the target state or NULL in case of error |
| 5669 | */ |
| 5670 | xmlAutomataStatePtr |
| 5671 | xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5672 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5673 | const xmlChar *token2, |
| 5674 | int min, int max, void *data) { |
| 5675 | xmlRegAtomPtr atom; |
| 5676 | int counter; |
| 5677 | |
| 5678 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5679 | return(NULL); |
| 5680 | if (min < 0) |
| 5681 | return(NULL); |
| 5682 | if ((max < min) || (max < 1)) |
| 5683 | return(NULL); |
| 5684 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5685 | if (atom == NULL) |
| 5686 | return(NULL); |
| 5687 | if ((token2 == NULL) || (*token2 == 0)) { |
| 5688 | atom->valuep = xmlStrdup(token); |
| 5689 | } else { |
| 5690 | int lenn, lenp; |
| 5691 | xmlChar *str; |
| 5692 | |
| 5693 | lenn = strlen((char *) token2); |
| 5694 | lenp = strlen((char *) token); |
| 5695 | |
| 5696 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
| 5697 | if (str == NULL) { |
| 5698 | xmlRegFreeAtom(atom); |
| 5699 | return(NULL); |
| 5700 | } |
| 5701 | memcpy(&str[0], token, lenp); |
| 5702 | str[lenp] = '|'; |
| 5703 | memcpy(&str[lenp + 1], token2, lenn); |
| 5704 | str[lenn + lenp + 1] = 0; |
| 5705 | |
| 5706 | atom->valuep = str; |
| 5707 | } |
| 5708 | atom->data = data; |
| 5709 | if (min == 0) |
| 5710 | atom->min = 1; |
| 5711 | else |
| 5712 | atom->min = min; |
| 5713 | atom->max = max; |
| 5714 | |
| 5715 | /* |
| 5716 | * associate a counter to the transition. |
| 5717 | */ |
| 5718 | counter = xmlRegGetCounter(am); |
| 5719 | am->counters[counter].min = min; |
| 5720 | am->counters[counter].max = max; |
| 5721 | |
| 5722 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 5723 | if (to == NULL) { |
| 5724 | to = xmlRegNewState(am); |
| 5725 | xmlRegStatePush(am, to); |
| 5726 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 5727 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 5728 | xmlRegAtomPush(am, atom); |
| 5729 | am->state = to; |
| 5730 | |
| 5731 | if (to == NULL) |
| 5732 | to = am->state; |
| 5733 | if (to == NULL) |
| 5734 | return(NULL); |
| 5735 | if (min == 0) |
| 5736 | xmlFAGenerateEpsilonTransition(am, from, to); |
| 5737 | return(to); |
| 5738 | } |
| 5739 | |
| 5740 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5741 | * xmlAutomataNewCountTrans: |
| 5742 | * @am: an automata |
| 5743 | * @from: the starting point of the transition |
| 5744 | * @to: the target point of the transition or NULL |
| 5745 | * @token: the input string associated to that transition |
| 5746 | * @min: the minimum successive occurences of token |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5747 | * @max: the maximum successive occurences of token |
| 5748 | * @data: data associated to the transition |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5749 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5750 | * 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] | 5751 | * and then adds a transition from the @from state to the target state |
| 5752 | * activated by a succession of input of value @token and whose number |
| 5753 | * is between @min and @max |
| 5754 | * |
| 5755 | * Returns the target state or NULL in case of error |
| 5756 | */ |
| 5757 | xmlAutomataStatePtr |
| 5758 | xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5759 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5760 | int min, int max, void *data) { |
| 5761 | xmlRegAtomPtr atom; |
Daniel Veillard | 0ddb21c | 2004-02-12 12:43:49 +0000 | [diff] [blame] | 5762 | int counter; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5763 | |
| 5764 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5765 | return(NULL); |
| 5766 | if (min < 0) |
| 5767 | return(NULL); |
| 5768 | if ((max < min) || (max < 1)) |
| 5769 | return(NULL); |
| 5770 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5771 | if (atom == NULL) |
| 5772 | return(NULL); |
| 5773 | atom->valuep = xmlStrdup(token); |
| 5774 | atom->data = data; |
| 5775 | if (min == 0) |
| 5776 | atom->min = 1; |
| 5777 | else |
| 5778 | atom->min = min; |
| 5779 | atom->max = max; |
| 5780 | |
Daniel Veillard | 0ddb21c | 2004-02-12 12:43:49 +0000 | [diff] [blame] | 5781 | /* |
| 5782 | * associate a counter to the transition. |
| 5783 | */ |
| 5784 | counter = xmlRegGetCounter(am); |
| 5785 | am->counters[counter].min = min; |
| 5786 | am->counters[counter].max = max; |
| 5787 | |
| 5788 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 5789 | if (to == NULL) { |
| 5790 | to = xmlRegNewState(am); |
| 5791 | xmlRegStatePush(am, to); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5792 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 5793 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
Daniel Veillard | 0ddb21c | 2004-02-12 12:43:49 +0000 | [diff] [blame] | 5794 | xmlRegAtomPush(am, atom); |
| 5795 | am->state = to; |
| 5796 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5797 | if (to == NULL) |
| 5798 | to = am->state; |
| 5799 | if (to == NULL) |
| 5800 | return(NULL); |
| 5801 | if (min == 0) |
| 5802 | xmlFAGenerateEpsilonTransition(am, from, to); |
| 5803 | return(to); |
| 5804 | } |
| 5805 | |
| 5806 | /** |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 5807 | * xmlAutomataNewOnceTrans2: |
| 5808 | * @am: an automata |
| 5809 | * @from: the starting point of the transition |
| 5810 | * @to: the target point of the transition or NULL |
| 5811 | * @token: the input string associated to that transition |
| 5812 | * @token2: the second input string associated to that transition |
| 5813 | * @min: the minimum successive occurences of token |
| 5814 | * @max: the maximum successive occurences of token |
| 5815 | * @data: data associated to the transition |
| 5816 | * |
| 5817 | * If @to is NULL, this creates first a new target state in the automata |
| 5818 | * and then adds a transition from the @from state to the target state |
| 5819 | * activated by a succession of input of value @token and @token2 and whose |
| 5820 | * number is between @min and @max, moreover that transition can only be |
| 5821 | * crossed once. |
| 5822 | * |
| 5823 | * Returns the target state or NULL in case of error |
| 5824 | */ |
| 5825 | xmlAutomataStatePtr |
| 5826 | xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5827 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5828 | const xmlChar *token2, |
| 5829 | int min, int max, void *data) { |
| 5830 | xmlRegAtomPtr atom; |
| 5831 | int counter; |
| 5832 | |
| 5833 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5834 | return(NULL); |
| 5835 | if (min < 1) |
| 5836 | return(NULL); |
| 5837 | if ((max < min) || (max < 1)) |
| 5838 | return(NULL); |
| 5839 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5840 | if (atom == NULL) |
| 5841 | return(NULL); |
| 5842 | if ((token2 == NULL) || (*token2 == 0)) { |
| 5843 | atom->valuep = xmlStrdup(token); |
| 5844 | } else { |
| 5845 | int lenn, lenp; |
| 5846 | xmlChar *str; |
| 5847 | |
| 5848 | lenn = strlen((char *) token2); |
| 5849 | lenp = strlen((char *) token); |
| 5850 | |
| 5851 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
| 5852 | if (str == NULL) { |
| 5853 | xmlRegFreeAtom(atom); |
| 5854 | return(NULL); |
| 5855 | } |
| 5856 | memcpy(&str[0], token, lenp); |
| 5857 | str[lenp] = '|'; |
| 5858 | memcpy(&str[lenp + 1], token2, lenn); |
| 5859 | str[lenn + lenp + 1] = 0; |
| 5860 | |
| 5861 | atom->valuep = str; |
| 5862 | } |
| 5863 | atom->data = data; |
| 5864 | atom->quant = XML_REGEXP_QUANT_ONCEONLY; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5865 | atom->min = min; |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 5866 | atom->max = max; |
| 5867 | /* |
| 5868 | * associate a counter to the transition. |
| 5869 | */ |
| 5870 | counter = xmlRegGetCounter(am); |
| 5871 | am->counters[counter].min = 1; |
| 5872 | am->counters[counter].max = 1; |
| 5873 | |
| 5874 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 5875 | if (to == NULL) { |
| 5876 | to = xmlRegNewState(am); |
| 5877 | xmlRegStatePush(am, to); |
| 5878 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 5879 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 5880 | xmlRegAtomPush(am, atom); |
| 5881 | am->state = to; |
| 5882 | return(to); |
| 5883 | } |
| 5884 | |
| 5885 | |
| 5886 | |
| 5887 | /** |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5888 | * xmlAutomataNewOnceTrans: |
| 5889 | * @am: an automata |
| 5890 | * @from: the starting point of the transition |
| 5891 | * @to: the target point of the transition or NULL |
| 5892 | * @token: the input string associated to that transition |
| 5893 | * @min: the minimum successive occurences of token |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5894 | * @max: the maximum successive occurences of token |
| 5895 | * @data: data associated to the transition |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5896 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5897 | * 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] | 5898 | * and then adds a transition from the @from state to the target state |
| 5899 | * 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] | 5900 | * is between @min and @max, moreover that transition can only be crossed |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5901 | * once. |
| 5902 | * |
| 5903 | * Returns the target state or NULL in case of error |
| 5904 | */ |
| 5905 | xmlAutomataStatePtr |
| 5906 | xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5907 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5908 | int min, int max, void *data) { |
| 5909 | xmlRegAtomPtr atom; |
| 5910 | int counter; |
| 5911 | |
| 5912 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5913 | return(NULL); |
| 5914 | if (min < 1) |
| 5915 | return(NULL); |
| 5916 | if ((max < min) || (max < 1)) |
| 5917 | return(NULL); |
| 5918 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5919 | if (atom == NULL) |
| 5920 | return(NULL); |
| 5921 | atom->valuep = xmlStrdup(token); |
| 5922 | atom->data = data; |
| 5923 | atom->quant = XML_REGEXP_QUANT_ONCEONLY; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5924 | atom->min = min; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5925 | atom->max = max; |
| 5926 | /* |
| 5927 | * associate a counter to the transition. |
| 5928 | */ |
| 5929 | counter = xmlRegGetCounter(am); |
| 5930 | am->counters[counter].min = 1; |
| 5931 | am->counters[counter].max = 1; |
| 5932 | |
| 5933 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 5934 | if (to == NULL) { |
| 5935 | to = xmlRegNewState(am); |
| 5936 | xmlRegStatePush(am, to); |
| 5937 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 5938 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5939 | xmlRegAtomPush(am, atom); |
| 5940 | am->state = to; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5941 | return(to); |
| 5942 | } |
| 5943 | |
| 5944 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5945 | * xmlAutomataNewState: |
| 5946 | * @am: an automata |
| 5947 | * |
| 5948 | * Create a new disconnected state in the automata |
| 5949 | * |
| 5950 | * Returns the new state or NULL in case of error |
| 5951 | */ |
| 5952 | xmlAutomataStatePtr |
| 5953 | xmlAutomataNewState(xmlAutomataPtr am) { |
| 5954 | xmlAutomataStatePtr to; |
| 5955 | |
| 5956 | if (am == NULL) |
| 5957 | return(NULL); |
| 5958 | to = xmlRegNewState(am); |
| 5959 | xmlRegStatePush(am, to); |
| 5960 | return(to); |
| 5961 | } |
| 5962 | |
| 5963 | /** |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5964 | * xmlAutomataNewEpsilon: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5965 | * @am: an automata |
| 5966 | * @from: the starting point of the transition |
| 5967 | * @to: the target point of the transition or NULL |
| 5968 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5969 | * If @to is NULL, this creates first a new target state in the automata |
| 5970 | * and then adds an epsilon transition from the @from state to the |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5971 | * target state |
| 5972 | * |
| 5973 | * Returns the target state or NULL in case of error |
| 5974 | */ |
| 5975 | xmlAutomataStatePtr |
| 5976 | xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5977 | xmlAutomataStatePtr to) { |
| 5978 | if ((am == NULL) || (from == NULL)) |
| 5979 | return(NULL); |
| 5980 | xmlFAGenerateEpsilonTransition(am, from, to); |
| 5981 | if (to == NULL) |
| 5982 | return(am->state); |
| 5983 | return(to); |
| 5984 | } |
| 5985 | |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 5986 | /** |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5987 | * xmlAutomataNewAllTrans: |
| 5988 | * @am: an automata |
| 5989 | * @from: the starting point of the transition |
| 5990 | * @to: the target point of the transition or NULL |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5991 | * @lax: allow to transition if not all all transitions have been activated |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5992 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5993 | * 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] | 5994 | * and then adds a an ALL transition from the @from state to the |
| 5995 | * target state. That transition is an epsilon transition allowed only when |
| 5996 | * all transitions from the @from node have been activated. |
| 5997 | * |
| 5998 | * Returns the target state or NULL in case of error |
| 5999 | */ |
| 6000 | xmlAutomataStatePtr |
| 6001 | xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 6002 | xmlAutomataStatePtr to, int lax) { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 6003 | if ((am == NULL) || (from == NULL)) |
| 6004 | return(NULL); |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 6005 | xmlFAGenerateAllTransition(am, from, to, lax); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 6006 | if (to == NULL) |
| 6007 | return(am->state); |
| 6008 | return(to); |
| 6009 | } |
| 6010 | |
| 6011 | /** |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 6012 | * xmlAutomataNewCounter: |
| 6013 | * @am: an automata |
| 6014 | * @min: the minimal value on the counter |
| 6015 | * @max: the maximal value on the counter |
| 6016 | * |
| 6017 | * Create a new counter |
| 6018 | * |
| 6019 | * Returns the counter number or -1 in case of error |
| 6020 | */ |
| 6021 | int |
| 6022 | xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) { |
| 6023 | int ret; |
| 6024 | |
| 6025 | if (am == NULL) |
| 6026 | return(-1); |
| 6027 | |
| 6028 | ret = xmlRegGetCounter(am); |
| 6029 | if (ret < 0) |
| 6030 | return(-1); |
| 6031 | am->counters[ret].min = min; |
| 6032 | am->counters[ret].max = max; |
| 6033 | return(ret); |
| 6034 | } |
| 6035 | |
| 6036 | /** |
| 6037 | * xmlAutomataNewCountedTrans: |
| 6038 | * @am: an automata |
| 6039 | * @from: the starting point of the transition |
| 6040 | * @to: the target point of the transition or NULL |
| 6041 | * @counter: the counter associated to that transition |
| 6042 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 6043 | * 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] | 6044 | * and then adds an epsilon transition from the @from state to the target state |
| 6045 | * which will increment the counter provided |
| 6046 | * |
| 6047 | * Returns the target state or NULL in case of error |
| 6048 | */ |
| 6049 | xmlAutomataStatePtr |
| 6050 | xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 6051 | xmlAutomataStatePtr to, int counter) { |
| 6052 | if ((am == NULL) || (from == NULL) || (counter < 0)) |
| 6053 | return(NULL); |
| 6054 | xmlFAGenerateCountedEpsilonTransition(am, from, to, counter); |
| 6055 | if (to == NULL) |
| 6056 | return(am->state); |
| 6057 | return(to); |
| 6058 | } |
| 6059 | |
| 6060 | /** |
| 6061 | * xmlAutomataNewCounterTrans: |
| 6062 | * @am: an automata |
| 6063 | * @from: the starting point of the transition |
| 6064 | * @to: the target point of the transition or NULL |
| 6065 | * @counter: the counter associated to that transition |
| 6066 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 6067 | * 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] | 6068 | * and then adds an epsilon transition from the @from state to the target state |
| 6069 | * which will be allowed only if the counter is within the right range. |
| 6070 | * |
| 6071 | * Returns the target state or NULL in case of error |
| 6072 | */ |
| 6073 | xmlAutomataStatePtr |
| 6074 | xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 6075 | xmlAutomataStatePtr to, int counter) { |
| 6076 | if ((am == NULL) || (from == NULL) || (counter < 0)) |
| 6077 | return(NULL); |
| 6078 | xmlFAGenerateCountedTransition(am, from, to, counter); |
| 6079 | if (to == NULL) |
| 6080 | return(am->state); |
| 6081 | return(to); |
| 6082 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6083 | |
| 6084 | /** |
| 6085 | * xmlAutomataCompile: |
| 6086 | * @am: an automata |
| 6087 | * |
| 6088 | * Compile the automata into a Reg Exp ready for being executed. |
| 6089 | * The automata should be free after this point. |
| 6090 | * |
| 6091 | * Returns the compiled regexp or NULL in case of error |
| 6092 | */ |
| 6093 | xmlRegexpPtr |
| 6094 | xmlAutomataCompile(xmlAutomataPtr am) { |
| 6095 | xmlRegexpPtr ret; |
| 6096 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6097 | if ((am == NULL) || (am->error != 0)) return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6098 | xmlFAEliminateEpsilonTransitions(am); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 6099 | /* xmlFAComputesDeterminism(am); */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6100 | ret = xmlRegEpxFromParse(am); |
| 6101 | |
| 6102 | return(ret); |
| 6103 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 6104 | |
| 6105 | /** |
| 6106 | * xmlAutomataIsDeterminist: |
| 6107 | * @am: an automata |
| 6108 | * |
| 6109 | * Checks if an automata is determinist. |
| 6110 | * |
| 6111 | * Returns 1 if true, 0 if not, and -1 in case of error |
| 6112 | */ |
| 6113 | int |
| 6114 | xmlAutomataIsDeterminist(xmlAutomataPtr am) { |
| 6115 | int ret; |
| 6116 | |
| 6117 | if (am == NULL) |
| 6118 | return(-1); |
| 6119 | |
| 6120 | ret = xmlFAComputesDeterminism(am); |
| 6121 | return(ret); |
| 6122 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6123 | #endif /* LIBXML_AUTOMATA_ENABLED */ |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6124 | |
| 6125 | #ifdef LIBXML_EXPR_ENABLED |
| 6126 | /************************************************************************ |
| 6127 | * * |
| 6128 | * Formal Expression handling code * |
| 6129 | * * |
| 6130 | ************************************************************************/ |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6131 | /************************************************************************ |
| 6132 | * * |
| 6133 | * Expression handling context * |
| 6134 | * * |
| 6135 | ************************************************************************/ |
| 6136 | |
| 6137 | struct _xmlExpCtxt { |
| 6138 | xmlDictPtr dict; |
| 6139 | xmlExpNodePtr *table; |
| 6140 | int size; |
| 6141 | int nbElems; |
| 6142 | int nb_nodes; |
| 6143 | const char *expr; |
| 6144 | const char *cur; |
| 6145 | int nb_cons; |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6146 | int tabSize; |
| 6147 | }; |
| 6148 | |
| 6149 | /** |
| 6150 | * xmlExpNewCtxt: |
| 6151 | * @maxNodes: the maximum number of nodes |
| 6152 | * @dict: optional dictionnary to use internally |
| 6153 | * |
| 6154 | * Creates a new context for manipulating expressions |
| 6155 | * |
| 6156 | * Returns the context or NULL in case of error |
| 6157 | */ |
| 6158 | xmlExpCtxtPtr |
| 6159 | xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) { |
| 6160 | xmlExpCtxtPtr ret; |
| 6161 | int size = 256; |
| 6162 | |
| 6163 | if (maxNodes <= 4096) |
| 6164 | maxNodes = 4096; |
| 6165 | |
| 6166 | ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt)); |
| 6167 | if (ret == NULL) |
| 6168 | return(NULL); |
| 6169 | memset(ret, 0, sizeof(xmlExpCtxt)); |
| 6170 | ret->size = size; |
| 6171 | ret->nbElems = 0; |
| 6172 | ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr)); |
| 6173 | if (ret->table == NULL) { |
| 6174 | xmlFree(ret); |
| 6175 | return(NULL); |
| 6176 | } |
| 6177 | memset(ret->table, 0, size * sizeof(xmlExpNodePtr)); |
| 6178 | if (dict == NULL) { |
| 6179 | ret->dict = xmlDictCreate(); |
| 6180 | if (ret->dict == NULL) { |
| 6181 | xmlFree(ret->table); |
| 6182 | xmlFree(ret); |
| 6183 | return(NULL); |
| 6184 | } |
| 6185 | } else { |
| 6186 | ret->dict = dict; |
| 6187 | xmlDictReference(ret->dict); |
| 6188 | } |
| 6189 | return(ret); |
| 6190 | } |
| 6191 | |
| 6192 | /** |
| 6193 | * xmlExpFreeCtxt: |
| 6194 | * @ctxt: an expression context |
| 6195 | * |
| 6196 | * Free an expression context |
| 6197 | */ |
| 6198 | void |
| 6199 | xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) { |
| 6200 | if (ctxt == NULL) |
| 6201 | return; |
| 6202 | xmlDictFree(ctxt->dict); |
| 6203 | if (ctxt->table != NULL) |
| 6204 | xmlFree(ctxt->table); |
| 6205 | xmlFree(ctxt); |
| 6206 | } |
| 6207 | |
| 6208 | /************************************************************************ |
| 6209 | * * |
| 6210 | * Structure associated to an expression node * |
| 6211 | * * |
| 6212 | ************************************************************************/ |
Daniel Veillard | 465a000 | 2005-08-22 12:07:04 +0000 | [diff] [blame] | 6213 | #define MAX_NODES 10000 |
| 6214 | |
| 6215 | /* #define DEBUG_DERIV */ |
| 6216 | |
| 6217 | /* |
| 6218 | * TODO: |
| 6219 | * - Wildcards |
| 6220 | * - public API for creation |
| 6221 | * |
| 6222 | * Started |
| 6223 | * - regression testing |
| 6224 | * |
| 6225 | * Done |
| 6226 | * - split into module and test tool |
| 6227 | * - memleaks |
| 6228 | */ |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6229 | |
| 6230 | typedef enum { |
| 6231 | XML_EXP_NILABLE = (1 << 0) |
| 6232 | } xmlExpNodeInfo; |
| 6233 | |
| 6234 | #define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE) |
| 6235 | |
| 6236 | struct _xmlExpNode { |
| 6237 | unsigned char type;/* xmlExpNodeType */ |
| 6238 | unsigned char info;/* OR of xmlExpNodeInfo */ |
| 6239 | unsigned short key; /* the hash key */ |
| 6240 | unsigned int ref; /* The number of references */ |
| 6241 | int c_max; /* the maximum length it can consume */ |
| 6242 | xmlExpNodePtr exp_left; |
| 6243 | xmlExpNodePtr next;/* the next node in the hash table or free list */ |
| 6244 | union { |
| 6245 | struct { |
| 6246 | int f_min; |
| 6247 | int f_max; |
| 6248 | } count; |
| 6249 | struct { |
| 6250 | xmlExpNodePtr f_right; |
| 6251 | } children; |
| 6252 | const xmlChar *f_str; |
| 6253 | } field; |
| 6254 | }; |
| 6255 | |
| 6256 | #define exp_min field.count.f_min |
| 6257 | #define exp_max field.count.f_max |
| 6258 | /* #define exp_left field.children.f_left */ |
| 6259 | #define exp_right field.children.f_right |
| 6260 | #define exp_str field.f_str |
| 6261 | |
| 6262 | static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type); |
| 6263 | static xmlExpNode forbiddenExpNode = { |
| 6264 | XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}} |
| 6265 | }; |
| 6266 | xmlExpNodePtr forbiddenExp = &forbiddenExpNode; |
| 6267 | static xmlExpNode emptyExpNode = { |
| 6268 | XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}} |
| 6269 | }; |
| 6270 | xmlExpNodePtr emptyExp = &emptyExpNode; |
| 6271 | |
| 6272 | /************************************************************************ |
| 6273 | * * |
| 6274 | * The custom hash table for unicity and canonicalization * |
| 6275 | * of sub-expressions pointers * |
| 6276 | * * |
| 6277 | ************************************************************************/ |
| 6278 | /* |
| 6279 | * xmlExpHashNameComputeKey: |
| 6280 | * Calculate the hash key for a token |
| 6281 | */ |
| 6282 | static unsigned short |
| 6283 | xmlExpHashNameComputeKey(const xmlChar *name) { |
| 6284 | unsigned short value = 0L; |
| 6285 | char ch; |
| 6286 | |
| 6287 | if (name != NULL) { |
| 6288 | value += 30 * (*name); |
| 6289 | while ((ch = *name++) != 0) { |
| 6290 | value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch); |
| 6291 | } |
| 6292 | } |
| 6293 | return (value); |
| 6294 | } |
| 6295 | |
| 6296 | /* |
| 6297 | * xmlExpHashComputeKey: |
| 6298 | * Calculate the hash key for a compound expression |
| 6299 | */ |
| 6300 | static unsigned short |
| 6301 | xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left, |
| 6302 | xmlExpNodePtr right) { |
| 6303 | unsigned long value; |
| 6304 | unsigned short ret; |
| 6305 | |
| 6306 | switch (type) { |
| 6307 | case XML_EXP_SEQ: |
| 6308 | value = left->key; |
| 6309 | value += right->key; |
| 6310 | value *= 3; |
| 6311 | ret = (unsigned short) value; |
| 6312 | break; |
| 6313 | case XML_EXP_OR: |
| 6314 | value = left->key; |
| 6315 | value += right->key; |
| 6316 | value *= 7; |
| 6317 | ret = (unsigned short) value; |
| 6318 | break; |
| 6319 | case XML_EXP_COUNT: |
| 6320 | value = left->key; |
| 6321 | value += right->key; |
| 6322 | ret = (unsigned short) value; |
| 6323 | break; |
| 6324 | default: |
| 6325 | ret = 0; |
| 6326 | } |
| 6327 | return(ret); |
| 6328 | } |
| 6329 | |
| 6330 | |
| 6331 | static xmlExpNodePtr |
| 6332 | xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) { |
| 6333 | xmlExpNodePtr ret; |
| 6334 | |
| 6335 | if (ctxt->nb_nodes >= MAX_NODES) |
| 6336 | return(NULL); |
| 6337 | ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode)); |
| 6338 | if (ret == NULL) |
| 6339 | return(NULL); |
| 6340 | memset(ret, 0, sizeof(xmlExpNode)); |
| 6341 | ret->type = type; |
| 6342 | ret->next = NULL; |
| 6343 | ctxt->nb_nodes++; |
| 6344 | ctxt->nb_cons++; |
| 6345 | return(ret); |
| 6346 | } |
| 6347 | |
| 6348 | /** |
| 6349 | * xmlExpHashGetEntry: |
| 6350 | * @table: the hash table |
| 6351 | * |
| 6352 | * Get the unique entry from the hash table. The entry is created if |
| 6353 | * needed. @left and @right are consumed, i.e. their ref count will |
| 6354 | * be decremented by the operation. |
| 6355 | * |
| 6356 | * Returns the pointer or NULL in case of error |
| 6357 | */ |
| 6358 | static xmlExpNodePtr |
| 6359 | xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type, |
| 6360 | xmlExpNodePtr left, xmlExpNodePtr right, |
| 6361 | const xmlChar *name, int min, int max) { |
| 6362 | unsigned short kbase, key; |
| 6363 | xmlExpNodePtr entry; |
| 6364 | xmlExpNodePtr insert; |
| 6365 | |
| 6366 | if (ctxt == NULL) |
| 6367 | return(NULL); |
| 6368 | |
| 6369 | /* |
| 6370 | * Check for duplicate and insertion location. |
| 6371 | */ |
| 6372 | if (type == XML_EXP_ATOM) { |
| 6373 | kbase = xmlExpHashNameComputeKey(name); |
| 6374 | } else if (type == XML_EXP_COUNT) { |
| 6375 | /* COUNT reduction rule 1 */ |
| 6376 | /* a{1} -> a */ |
| 6377 | if (min == max) { |
| 6378 | if (min == 1) { |
| 6379 | return(left); |
| 6380 | } |
| 6381 | if (min == 0) { |
| 6382 | xmlExpFree(ctxt, left); |
| 6383 | return(emptyExp); |
| 6384 | } |
| 6385 | } |
| 6386 | if (min < 0) { |
| 6387 | xmlExpFree(ctxt, left); |
| 6388 | return(forbiddenExp); |
| 6389 | } |
| 6390 | if (max == -1) |
| 6391 | kbase = min + 79; |
| 6392 | else |
| 6393 | kbase = max - min; |
| 6394 | kbase += left->key; |
| 6395 | } else if (type == XML_EXP_OR) { |
| 6396 | /* Forbid reduction rules */ |
| 6397 | if (left->type == XML_EXP_FORBID) { |
| 6398 | xmlExpFree(ctxt, left); |
| 6399 | return(right); |
| 6400 | } |
| 6401 | if (right->type == XML_EXP_FORBID) { |
| 6402 | xmlExpFree(ctxt, right); |
| 6403 | return(left); |
| 6404 | } |
| 6405 | |
| 6406 | /* OR reduction rule 1 */ |
| 6407 | /* a | a reduced to a */ |
| 6408 | if (left == right) { |
| 6409 | left->ref--; |
| 6410 | return(left); |
| 6411 | } |
| 6412 | /* OR canonicalization rule 1 */ |
| 6413 | /* linearize (a | b) | c into a | (b | c) */ |
| 6414 | if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) { |
| 6415 | xmlExpNodePtr tmp = left; |
| 6416 | left = right; |
| 6417 | right = tmp; |
| 6418 | } |
| 6419 | /* OR reduction rule 2 */ |
| 6420 | /* a | (a | b) and b | (a | b) are reduced to a | b */ |
| 6421 | if (right->type == XML_EXP_OR) { |
| 6422 | if ((left == right->exp_left) || |
| 6423 | (left == right->exp_right)) { |
| 6424 | xmlExpFree(ctxt, left); |
| 6425 | return(right); |
| 6426 | } |
| 6427 | } |
| 6428 | /* OR canonicalization rule 2 */ |
| 6429 | /* linearize (a | b) | c into a | (b | c) */ |
| 6430 | if (left->type == XML_EXP_OR) { |
| 6431 | xmlExpNodePtr tmp; |
| 6432 | |
| 6433 | /* OR canonicalization rule 2 */ |
| 6434 | if ((left->exp_right->type != XML_EXP_OR) && |
| 6435 | (left->exp_right->key < left->exp_left->key)) { |
| 6436 | tmp = left->exp_right; |
| 6437 | left->exp_right = left->exp_left; |
| 6438 | left->exp_left = tmp; |
| 6439 | } |
| 6440 | left->exp_right->ref++; |
| 6441 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right, |
| 6442 | NULL, 0, 0); |
| 6443 | left->exp_left->ref++; |
| 6444 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp, |
| 6445 | NULL, 0, 0); |
| 6446 | |
| 6447 | xmlExpFree(ctxt, left); |
| 6448 | return(tmp); |
| 6449 | } |
| 6450 | if (right->type == XML_EXP_OR) { |
| 6451 | /* Ordering in the tree */ |
| 6452 | /* C | (A | B) -> A | (B | C) */ |
| 6453 | if (left->key > right->exp_right->key) { |
| 6454 | xmlExpNodePtr tmp; |
| 6455 | right->exp_right->ref++; |
| 6456 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right, |
| 6457 | left, NULL, 0, 0); |
| 6458 | right->exp_left->ref++; |
| 6459 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left, |
| 6460 | tmp, NULL, 0, 0); |
| 6461 | xmlExpFree(ctxt, right); |
| 6462 | return(tmp); |
| 6463 | } |
| 6464 | /* Ordering in the tree */ |
| 6465 | /* B | (A | C) -> A | (B | C) */ |
| 6466 | if (left->key > right->exp_left->key) { |
| 6467 | xmlExpNodePtr tmp; |
| 6468 | right->exp_right->ref++; |
| 6469 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, |
| 6470 | right->exp_right, NULL, 0, 0); |
| 6471 | right->exp_left->ref++; |
| 6472 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left, |
| 6473 | tmp, NULL, 0, 0); |
| 6474 | xmlExpFree(ctxt, right); |
| 6475 | return(tmp); |
| 6476 | } |
| 6477 | } |
| 6478 | /* we know both types are != XML_EXP_OR here */ |
| 6479 | else if (left->key > right->key) { |
| 6480 | xmlExpNodePtr tmp = left; |
| 6481 | left = right; |
| 6482 | right = tmp; |
| 6483 | } |
| 6484 | kbase = xmlExpHashComputeKey(type, left, right); |
| 6485 | } else if (type == XML_EXP_SEQ) { |
| 6486 | /* Forbid reduction rules */ |
| 6487 | if (left->type == XML_EXP_FORBID) { |
| 6488 | xmlExpFree(ctxt, right); |
| 6489 | return(left); |
| 6490 | } |
| 6491 | if (right->type == XML_EXP_FORBID) { |
| 6492 | xmlExpFree(ctxt, left); |
| 6493 | return(right); |
| 6494 | } |
| 6495 | /* Empty reduction rules */ |
| 6496 | if (right->type == XML_EXP_EMPTY) { |
| 6497 | return(left); |
| 6498 | } |
| 6499 | if (left->type == XML_EXP_EMPTY) { |
| 6500 | return(right); |
| 6501 | } |
| 6502 | kbase = xmlExpHashComputeKey(type, left, right); |
| 6503 | } else |
| 6504 | return(NULL); |
| 6505 | |
| 6506 | key = kbase % ctxt->size; |
| 6507 | if (ctxt->table[key] != NULL) { |
| 6508 | for (insert = ctxt->table[key]; insert != NULL; |
| 6509 | insert = insert->next) { |
| 6510 | if ((insert->key == kbase) && |
| 6511 | (insert->type == type)) { |
| 6512 | if (type == XML_EXP_ATOM) { |
| 6513 | if (name == insert->exp_str) { |
| 6514 | insert->ref++; |
| 6515 | return(insert); |
| 6516 | } |
| 6517 | } else if (type == XML_EXP_COUNT) { |
| 6518 | if ((insert->exp_min == min) && (insert->exp_max == max) && |
| 6519 | (insert->exp_left == left)) { |
| 6520 | insert->ref++; |
| 6521 | left->ref--; |
| 6522 | return(insert); |
| 6523 | } |
| 6524 | } else if ((insert->exp_left == left) && |
| 6525 | (insert->exp_right == right)) { |
| 6526 | insert->ref++; |
| 6527 | left->ref--; |
| 6528 | right->ref--; |
| 6529 | return(insert); |
| 6530 | } |
| 6531 | } |
| 6532 | } |
| 6533 | } |
| 6534 | |
| 6535 | entry = xmlExpNewNode(ctxt, type); |
| 6536 | if (entry == NULL) |
| 6537 | return(NULL); |
| 6538 | entry->key = kbase; |
| 6539 | if (type == XML_EXP_ATOM) { |
| 6540 | entry->exp_str = name; |
| 6541 | entry->c_max = 1; |
| 6542 | } else if (type == XML_EXP_COUNT) { |
| 6543 | entry->exp_min = min; |
| 6544 | entry->exp_max = max; |
| 6545 | entry->exp_left = left; |
| 6546 | if ((min == 0) || (IS_NILLABLE(left))) |
| 6547 | entry->info |= XML_EXP_NILABLE; |
| 6548 | if (max < 0) |
| 6549 | entry->c_max = -1; |
| 6550 | else |
| 6551 | entry->c_max = max * entry->exp_left->c_max; |
| 6552 | } else { |
| 6553 | entry->exp_left = left; |
| 6554 | entry->exp_right = right; |
| 6555 | if (type == XML_EXP_OR) { |
| 6556 | if ((IS_NILLABLE(left)) || (IS_NILLABLE(right))) |
| 6557 | entry->info |= XML_EXP_NILABLE; |
| 6558 | if ((entry->exp_left->c_max == -1) || |
| 6559 | (entry->exp_right->c_max == -1)) |
| 6560 | entry->c_max = -1; |
| 6561 | else if (entry->exp_left->c_max > entry->exp_right->c_max) |
| 6562 | entry->c_max = entry->exp_left->c_max; |
| 6563 | else |
| 6564 | entry->c_max = entry->exp_right->c_max; |
| 6565 | } else { |
| 6566 | if ((IS_NILLABLE(left)) && (IS_NILLABLE(right))) |
| 6567 | entry->info |= XML_EXP_NILABLE; |
| 6568 | if ((entry->exp_left->c_max == -1) || |
| 6569 | (entry->exp_right->c_max == -1)) |
| 6570 | entry->c_max = -1; |
| 6571 | else |
| 6572 | entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max; |
| 6573 | } |
| 6574 | } |
| 6575 | entry->ref = 1; |
| 6576 | if (ctxt->table[key] != NULL) |
| 6577 | entry->next = ctxt->table[key]; |
| 6578 | |
| 6579 | ctxt->table[key] = entry; |
| 6580 | ctxt->nbElems++; |
| 6581 | |
| 6582 | return(entry); |
| 6583 | } |
| 6584 | |
| 6585 | /** |
| 6586 | * xmlExpFree: |
| 6587 | * @ctxt: the expression context |
| 6588 | * @exp: the expression |
| 6589 | * |
| 6590 | * Dereference the expression |
| 6591 | */ |
| 6592 | void |
| 6593 | xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) { |
| 6594 | if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp)) |
| 6595 | return; |
| 6596 | exp->ref--; |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6597 | if (exp->ref == 0) { |
| 6598 | unsigned short key; |
| 6599 | |
| 6600 | /* Unlink it first from the hash table */ |
| 6601 | key = exp->key % ctxt->size; |
| 6602 | if (ctxt->table[key] == exp) { |
| 6603 | ctxt->table[key] = exp->next; |
| 6604 | } else { |
| 6605 | xmlExpNodePtr tmp; |
| 6606 | |
| 6607 | tmp = ctxt->table[key]; |
| 6608 | while (tmp != NULL) { |
| 6609 | if (tmp->next == exp) { |
| 6610 | tmp->next = exp->next; |
| 6611 | break; |
| 6612 | } |
| 6613 | tmp = tmp->next; |
| 6614 | } |
| 6615 | } |
| 6616 | |
| 6617 | if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) { |
| 6618 | xmlExpFree(ctxt, exp->exp_left); |
| 6619 | xmlExpFree(ctxt, exp->exp_right); |
| 6620 | } else if (exp->type == XML_EXP_COUNT) { |
| 6621 | xmlExpFree(ctxt, exp->exp_left); |
| 6622 | } |
| 6623 | xmlFree(exp); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6624 | ctxt->nb_nodes--; |
| 6625 | } |
| 6626 | } |
| 6627 | |
| 6628 | /** |
| 6629 | * xmlExpRef: |
| 6630 | * @exp: the expression |
| 6631 | * |
| 6632 | * Increase the reference count of the expression |
| 6633 | */ |
| 6634 | void |
| 6635 | xmlExpRef(xmlExpNodePtr exp) { |
| 6636 | if (exp != NULL) |
| 6637 | exp->ref++; |
| 6638 | } |
| 6639 | |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 6640 | /** |
| 6641 | * xmlExpNewAtom: |
| 6642 | * @ctxt: the expression context |
| 6643 | * @name: the atom name |
| 6644 | * @len: the atom name lenght in byte (or -1); |
| 6645 | * |
| 6646 | * Get the atom associated to this name from that context |
| 6647 | * |
| 6648 | * Returns the node or NULL in case of error |
| 6649 | */ |
| 6650 | xmlExpNodePtr |
| 6651 | xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) { |
| 6652 | if ((ctxt == NULL) || (name == NULL)) |
| 6653 | return(NULL); |
| 6654 | name = xmlDictLookup(ctxt->dict, name, len); |
| 6655 | if (name == NULL) |
| 6656 | return(NULL); |
| 6657 | return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0)); |
| 6658 | } |
| 6659 | |
| 6660 | /** |
| 6661 | * xmlExpNewOr: |
| 6662 | * @ctxt: the expression context |
| 6663 | * @left: left expression |
| 6664 | * @right: right expression |
| 6665 | * |
| 6666 | * Get the atom associated to the choice @left | @right |
| 6667 | * Note that @left and @right are consumed in the operation, to keep |
| 6668 | * an handle on them use xmlExpRef() and use xmlExpFree() to release them, |
| 6669 | * this is true even in case of failure (unless ctxt == NULL). |
| 6670 | * |
| 6671 | * Returns the node or NULL in case of error |
| 6672 | */ |
| 6673 | xmlExpNodePtr |
| 6674 | xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 6675 | if (ctxt == NULL) |
| 6676 | return(NULL); |
| 6677 | if ((left == NULL) || (right == NULL)) { |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 6678 | xmlExpFree(ctxt, left); |
| 6679 | xmlExpFree(ctxt, right); |
| 6680 | return(NULL); |
| 6681 | } |
| 6682 | return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0)); |
| 6683 | } |
| 6684 | |
| 6685 | /** |
| 6686 | * xmlExpNewSeq: |
| 6687 | * @ctxt: the expression context |
| 6688 | * @left: left expression |
| 6689 | * @right: right expression |
| 6690 | * |
| 6691 | * Get the atom associated to the sequence @left , @right |
| 6692 | * Note that @left and @right are consumed in the operation, to keep |
| 6693 | * an handle on them use xmlExpRef() and use xmlExpFree() to release them, |
| 6694 | * this is true even in case of failure (unless ctxt == NULL). |
| 6695 | * |
| 6696 | * Returns the node or NULL in case of error |
| 6697 | */ |
| 6698 | xmlExpNodePtr |
| 6699 | xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 6700 | if (ctxt == NULL) |
| 6701 | return(NULL); |
| 6702 | if ((left == NULL) || (right == NULL)) { |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 6703 | xmlExpFree(ctxt, left); |
| 6704 | xmlExpFree(ctxt, right); |
| 6705 | return(NULL); |
| 6706 | } |
| 6707 | return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0)); |
| 6708 | } |
| 6709 | |
| 6710 | /** |
| 6711 | * xmlExpNewRange: |
| 6712 | * @ctxt: the expression context |
| 6713 | * @subset: the expression to be repeated |
| 6714 | * @min: the lower bound for the repetition |
| 6715 | * @max: the upper bound for the repetition, -1 means infinite |
| 6716 | * |
| 6717 | * Get the atom associated to the range (@subset){@min, @max} |
| 6718 | * Note that @subset is consumed in the operation, to keep |
| 6719 | * an handle on it use xmlExpRef() and use xmlExpFree() to release it, |
| 6720 | * this is true even in case of failure (unless ctxt == NULL). |
| 6721 | * |
| 6722 | * Returns the node or NULL in case of error |
| 6723 | */ |
| 6724 | xmlExpNodePtr |
| 6725 | xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 6726 | if (ctxt == NULL) |
| 6727 | return(NULL); |
| 6728 | if ((subset == NULL) || (min < 0) || (max < -1) || |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 6729 | ((max >= 0) && (min > max))) { |
| 6730 | xmlExpFree(ctxt, subset); |
| 6731 | return(NULL); |
| 6732 | } |
| 6733 | return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset, |
| 6734 | NULL, NULL, min, max)); |
| 6735 | } |
| 6736 | |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6737 | /************************************************************************ |
| 6738 | * * |
| 6739 | * Public API for operations on expressions * |
| 6740 | * * |
| 6741 | ************************************************************************/ |
| 6742 | |
| 6743 | static int |
| 6744 | xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
| 6745 | const xmlChar**list, int len, int nb) { |
| 6746 | int tmp, tmp2; |
| 6747 | tail: |
| 6748 | switch (exp->type) { |
| 6749 | case XML_EXP_EMPTY: |
| 6750 | return(0); |
| 6751 | case XML_EXP_ATOM: |
| 6752 | for (tmp = 0;tmp < nb;tmp++) |
| 6753 | if (list[tmp] == exp->exp_str) |
| 6754 | return(0); |
| 6755 | if (nb >= len) |
| 6756 | return(-2); |
| 6757 | list[nb++] = exp->exp_str; |
| 6758 | return(1); |
| 6759 | case XML_EXP_COUNT: |
| 6760 | exp = exp->exp_left; |
| 6761 | goto tail; |
| 6762 | case XML_EXP_SEQ: |
| 6763 | case XML_EXP_OR: |
| 6764 | tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb); |
| 6765 | if (tmp < 0) |
| 6766 | return(tmp); |
| 6767 | tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len, |
| 6768 | nb + tmp); |
| 6769 | if (tmp2 < 0) |
| 6770 | return(tmp2); |
| 6771 | return(tmp + tmp2); |
| 6772 | } |
| 6773 | return(-1); |
| 6774 | } |
| 6775 | |
| 6776 | /** |
| 6777 | * xmlExpGetLanguage: |
| 6778 | * @ctxt: the expression context |
| 6779 | * @exp: the expression |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6780 | * @langList: where to store the tokens |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6781 | * @len: the allocated lenght of @list |
| 6782 | * |
| 6783 | * Find all the strings used in @exp and store them in @list |
| 6784 | * |
| 6785 | * Returns the number of unique strings found, -1 in case of errors and |
| 6786 | * -2 if there is more than @len strings |
| 6787 | */ |
| 6788 | int |
| 6789 | xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6790 | const xmlChar**langList, int len) { |
| 6791 | if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0)) |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6792 | return(-1); |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6793 | return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0)); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6794 | } |
| 6795 | |
| 6796 | static int |
| 6797 | xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
| 6798 | const xmlChar**list, int len, int nb) { |
| 6799 | int tmp, tmp2; |
| 6800 | tail: |
| 6801 | switch (exp->type) { |
| 6802 | case XML_EXP_FORBID: |
| 6803 | return(0); |
| 6804 | case XML_EXP_EMPTY: |
| 6805 | return(0); |
| 6806 | case XML_EXP_ATOM: |
| 6807 | for (tmp = 0;tmp < nb;tmp++) |
| 6808 | if (list[tmp] == exp->exp_str) |
| 6809 | return(0); |
| 6810 | if (nb >= len) |
| 6811 | return(-2); |
| 6812 | list[nb++] = exp->exp_str; |
| 6813 | return(1); |
| 6814 | case XML_EXP_COUNT: |
| 6815 | exp = exp->exp_left; |
| 6816 | goto tail; |
| 6817 | case XML_EXP_SEQ: |
| 6818 | tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb); |
| 6819 | if (tmp < 0) |
| 6820 | return(tmp); |
| 6821 | if (IS_NILLABLE(exp->exp_left)) { |
| 6822 | tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len, |
| 6823 | nb + tmp); |
| 6824 | if (tmp2 < 0) |
| 6825 | return(tmp2); |
| 6826 | tmp += tmp2; |
| 6827 | } |
| 6828 | return(tmp); |
| 6829 | case XML_EXP_OR: |
| 6830 | tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb); |
| 6831 | if (tmp < 0) |
| 6832 | return(tmp); |
| 6833 | tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len, |
| 6834 | nb + tmp); |
| 6835 | if (tmp2 < 0) |
| 6836 | return(tmp2); |
| 6837 | return(tmp + tmp2); |
| 6838 | } |
| 6839 | return(-1); |
| 6840 | } |
| 6841 | |
| 6842 | /** |
| 6843 | * xmlExpGetStart: |
| 6844 | * @ctxt: the expression context |
| 6845 | * @exp: the expression |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6846 | * @tokList: where to store the tokens |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6847 | * @len: the allocated lenght of @list |
| 6848 | * |
| 6849 | * Find all the strings that appears at the start of the languages |
| 6850 | * accepted by @exp and store them in @list. E.g. for (a, b) | c |
| 6851 | * it will return the list [a, c] |
| 6852 | * |
| 6853 | * Returns the number of unique strings found, -1 in case of errors and |
| 6854 | * -2 if there is more than @len strings |
| 6855 | */ |
| 6856 | int |
| 6857 | xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6858 | const xmlChar**tokList, int len) { |
| 6859 | if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0)) |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6860 | return(-1); |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6861 | return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0)); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6862 | } |
| 6863 | |
| 6864 | /** |
| 6865 | * xmlExpIsNillable: |
| 6866 | * @exp: the expression |
| 6867 | * |
| 6868 | * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce |
| 6869 | * |
| 6870 | * Returns 1 if nillable, 0 if not and -1 in case of error |
| 6871 | */ |
| 6872 | int |
| 6873 | xmlExpIsNillable(xmlExpNodePtr exp) { |
| 6874 | if (exp == NULL) |
| 6875 | return(-1); |
| 6876 | return(IS_NILLABLE(exp) != 0); |
| 6877 | } |
| 6878 | |
| 6879 | static xmlExpNodePtr |
| 6880 | xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str) |
| 6881 | { |
| 6882 | xmlExpNodePtr ret; |
| 6883 | |
| 6884 | switch (exp->type) { |
| 6885 | case XML_EXP_EMPTY: |
| 6886 | return(forbiddenExp); |
| 6887 | case XML_EXP_FORBID: |
| 6888 | return(forbiddenExp); |
| 6889 | case XML_EXP_ATOM: |
| 6890 | if (exp->exp_str == str) { |
| 6891 | #ifdef DEBUG_DERIV |
| 6892 | printf("deriv atom: equal => Empty\n"); |
| 6893 | #endif |
| 6894 | ret = emptyExp; |
| 6895 | } else { |
| 6896 | #ifdef DEBUG_DERIV |
| 6897 | printf("deriv atom: mismatch => forbid\n"); |
| 6898 | #endif |
| 6899 | /* TODO wildcards here */ |
| 6900 | ret = forbiddenExp; |
| 6901 | } |
| 6902 | return(ret); |
| 6903 | case XML_EXP_OR: { |
| 6904 | xmlExpNodePtr tmp; |
| 6905 | |
| 6906 | #ifdef DEBUG_DERIV |
| 6907 | printf("deriv or: => or(derivs)\n"); |
| 6908 | #endif |
| 6909 | tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str); |
| 6910 | if (tmp == NULL) { |
| 6911 | return(NULL); |
| 6912 | } |
| 6913 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str); |
| 6914 | if (ret == NULL) { |
| 6915 | xmlExpFree(ctxt, tmp); |
| 6916 | return(NULL); |
| 6917 | } |
| 6918 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, |
| 6919 | NULL, 0, 0); |
| 6920 | return(ret); |
| 6921 | } |
| 6922 | case XML_EXP_SEQ: |
| 6923 | #ifdef DEBUG_DERIV |
| 6924 | printf("deriv seq: starting with left\n"); |
| 6925 | #endif |
| 6926 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str); |
| 6927 | if (ret == NULL) { |
| 6928 | return(NULL); |
| 6929 | } else if (ret == forbiddenExp) { |
| 6930 | if (IS_NILLABLE(exp->exp_left)) { |
| 6931 | #ifdef DEBUG_DERIV |
| 6932 | printf("deriv seq: left failed but nillable\n"); |
| 6933 | #endif |
| 6934 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str); |
| 6935 | } |
| 6936 | } else { |
| 6937 | #ifdef DEBUG_DERIV |
| 6938 | printf("deriv seq: left match => sequence\n"); |
| 6939 | #endif |
| 6940 | exp->exp_right->ref++; |
| 6941 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right, |
| 6942 | NULL, 0, 0); |
| 6943 | } |
| 6944 | return(ret); |
| 6945 | case XML_EXP_COUNT: { |
| 6946 | int min, max; |
| 6947 | xmlExpNodePtr tmp; |
| 6948 | |
| 6949 | if (exp->exp_max == 0) |
| 6950 | return(forbiddenExp); |
| 6951 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str); |
| 6952 | if (ret == NULL) |
| 6953 | return(NULL); |
| 6954 | if (ret == forbiddenExp) { |
| 6955 | #ifdef DEBUG_DERIV |
| 6956 | printf("deriv count: pattern mismatch => forbid\n"); |
| 6957 | #endif |
| 6958 | return(ret); |
| 6959 | } |
| 6960 | if (exp->exp_max == 1) |
| 6961 | return(ret); |
| 6962 | if (exp->exp_max < 0) /* unbounded */ |
| 6963 | max = -1; |
| 6964 | else |
| 6965 | max = exp->exp_max - 1; |
| 6966 | if (exp->exp_min > 0) |
| 6967 | min = exp->exp_min - 1; |
| 6968 | else |
| 6969 | min = 0; |
| 6970 | exp->exp_left->ref++; |
| 6971 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL, |
| 6972 | NULL, min, max); |
| 6973 | if (ret == emptyExp) { |
| 6974 | #ifdef DEBUG_DERIV |
| 6975 | printf("deriv count: match to empty => new count\n"); |
| 6976 | #endif |
| 6977 | return(tmp); |
| 6978 | } |
| 6979 | #ifdef DEBUG_DERIV |
| 6980 | printf("deriv count: match => sequence with new count\n"); |
| 6981 | #endif |
| 6982 | return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp, |
| 6983 | NULL, 0, 0)); |
| 6984 | } |
| 6985 | } |
| 6986 | return(NULL); |
| 6987 | } |
| 6988 | |
| 6989 | /** |
| 6990 | * xmlExpStringDerive: |
| 6991 | * @ctxt: the expression context |
| 6992 | * @exp: the expression |
| 6993 | * @str: the string |
| 6994 | * @len: the string len in bytes if available |
| 6995 | * |
| 6996 | * Do one step of Brzozowski derivation of the expression @exp with |
| 6997 | * respect to the input string |
| 6998 | * |
| 6999 | * Returns the resulting expression or NULL in case of internal error |
| 7000 | */ |
| 7001 | xmlExpNodePtr |
| 7002 | xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
| 7003 | const xmlChar *str, int len) { |
| 7004 | const xmlChar *input; |
| 7005 | |
| 7006 | if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) { |
| 7007 | return(NULL); |
| 7008 | } |
| 7009 | /* |
| 7010 | * check the string is in the dictionnary, if yes use an interned |
| 7011 | * copy, otherwise we know it's not an acceptable input |
| 7012 | */ |
| 7013 | input = xmlDictExists(ctxt->dict, str, len); |
| 7014 | if (input == NULL) { |
| 7015 | return(forbiddenExp); |
| 7016 | } |
| 7017 | return(xmlExpStringDeriveInt(ctxt, exp, input)); |
| 7018 | } |
| 7019 | |
| 7020 | static int |
| 7021 | xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) { |
| 7022 | int ret = 1; |
| 7023 | |
| 7024 | if (sub->c_max == -1) { |
| 7025 | if (exp->c_max != -1) |
| 7026 | ret = 0; |
| 7027 | } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) { |
| 7028 | ret = 0; |
| 7029 | } |
| 7030 | #if 0 |
| 7031 | if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp))) |
| 7032 | ret = 0; |
| 7033 | #endif |
| 7034 | return(ret); |
| 7035 | } |
| 7036 | |
| 7037 | static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
| 7038 | xmlExpNodePtr sub); |
| 7039 | /** |
| 7040 | * xmlExpDivide: |
| 7041 | * @ctxt: the expressions context |
| 7042 | * @exp: the englobing expression |
| 7043 | * @sub: the subexpression |
| 7044 | * @mult: the multiple expression |
| 7045 | * @remain: the remain from the derivation of the multiple |
| 7046 | * |
| 7047 | * Check if exp is a multiple of sub, i.e. if there is a finite number n |
| 7048 | * so that sub{n} subsume exp |
| 7049 | * |
| 7050 | * Returns the multiple value if successful, 0 if it is not a multiple |
| 7051 | * and -1 in case of internel error. |
| 7052 | */ |
| 7053 | |
| 7054 | static int |
| 7055 | xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub, |
| 7056 | xmlExpNodePtr *mult, xmlExpNodePtr *remain) { |
| 7057 | int i; |
| 7058 | xmlExpNodePtr tmp, tmp2; |
| 7059 | |
| 7060 | if (mult != NULL) *mult = NULL; |
| 7061 | if (remain != NULL) *remain = NULL; |
| 7062 | if (exp->c_max == -1) return(0); |
| 7063 | if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0); |
| 7064 | |
| 7065 | for (i = 1;i <= exp->c_max;i++) { |
| 7066 | sub->ref++; |
| 7067 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, |
| 7068 | sub, NULL, NULL, i, i); |
| 7069 | if (tmp == NULL) { |
| 7070 | return(-1); |
| 7071 | } |
| 7072 | if (!xmlExpCheckCard(tmp, exp)) { |
| 7073 | xmlExpFree(ctxt, tmp); |
| 7074 | continue; |
| 7075 | } |
| 7076 | tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp); |
| 7077 | if (tmp2 == NULL) { |
| 7078 | xmlExpFree(ctxt, tmp); |
| 7079 | return(-1); |
| 7080 | } |
| 7081 | if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) { |
| 7082 | if (remain != NULL) |
| 7083 | *remain = tmp2; |
| 7084 | else |
| 7085 | xmlExpFree(ctxt, tmp2); |
| 7086 | if (mult != NULL) |
| 7087 | *mult = tmp; |
| 7088 | else |
| 7089 | xmlExpFree(ctxt, tmp); |
| 7090 | #ifdef DEBUG_DERIV |
| 7091 | printf("Divide succeeded %d\n", i); |
| 7092 | #endif |
| 7093 | return(i); |
| 7094 | } |
| 7095 | xmlExpFree(ctxt, tmp); |
| 7096 | xmlExpFree(ctxt, tmp2); |
| 7097 | } |
| 7098 | #ifdef DEBUG_DERIV |
| 7099 | printf("Divide failed\n"); |
| 7100 | #endif |
| 7101 | return(0); |
| 7102 | } |
| 7103 | |
| 7104 | /** |
| 7105 | * xmlExpExpDeriveInt: |
| 7106 | * @ctxt: the expressions context |
| 7107 | * @exp: the englobing expression |
| 7108 | * @sub: the subexpression |
| 7109 | * |
| 7110 | * Try to do a step of Brzozowski derivation but at a higher level |
| 7111 | * the input being a subexpression. |
| 7112 | * |
| 7113 | * Returns the resulting expression or NULL in case of internal error |
| 7114 | */ |
| 7115 | static xmlExpNodePtr |
| 7116 | xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) { |
| 7117 | xmlExpNodePtr ret, tmp, tmp2, tmp3; |
| 7118 | const xmlChar **tab; |
| 7119 | int len, i; |
| 7120 | |
| 7121 | /* |
| 7122 | * In case of equality and if the expression can only consume a finite |
| 7123 | * amount, then the derivation is empty |
| 7124 | */ |
| 7125 | if ((exp == sub) && (exp->c_max >= 0)) { |
| 7126 | #ifdef DEBUG_DERIV |
| 7127 | printf("Equal(exp, sub) and finite -> Empty\n"); |
| 7128 | #endif |
| 7129 | return(emptyExp); |
| 7130 | } |
| 7131 | /* |
| 7132 | * decompose sub sequence first |
| 7133 | */ |
| 7134 | if (sub->type == XML_EXP_EMPTY) { |
| 7135 | #ifdef DEBUG_DERIV |
| 7136 | printf("Empty(sub) -> Empty\n"); |
| 7137 | #endif |
| 7138 | exp->ref++; |
| 7139 | return(exp); |
| 7140 | } |
| 7141 | if (sub->type == XML_EXP_SEQ) { |
| 7142 | #ifdef DEBUG_DERIV |
| 7143 | printf("Seq(sub) -> decompose\n"); |
| 7144 | #endif |
| 7145 | tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left); |
| 7146 | if (tmp == NULL) |
| 7147 | return(NULL); |
| 7148 | if (tmp == forbiddenExp) |
| 7149 | return(tmp); |
| 7150 | ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right); |
| 7151 | xmlExpFree(ctxt, tmp); |
| 7152 | return(ret); |
| 7153 | } |
| 7154 | if (sub->type == XML_EXP_OR) { |
| 7155 | #ifdef DEBUG_DERIV |
| 7156 | printf("Or(sub) -> decompose\n"); |
| 7157 | #endif |
| 7158 | tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left); |
| 7159 | if (tmp == forbiddenExp) |
| 7160 | return(tmp); |
| 7161 | if (tmp == NULL) |
| 7162 | return(NULL); |
| 7163 | ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right); |
| 7164 | if ((ret == NULL) || (ret == forbiddenExp)) { |
| 7165 | xmlExpFree(ctxt, tmp); |
| 7166 | return(ret); |
| 7167 | } |
| 7168 | return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0)); |
| 7169 | } |
| 7170 | if (!xmlExpCheckCard(exp, sub)) { |
| 7171 | #ifdef DEBUG_DERIV |
| 7172 | printf("CheckCard(exp, sub) failed -> Forbid\n"); |
| 7173 | #endif |
| 7174 | return(forbiddenExp); |
| 7175 | } |
| 7176 | switch (exp->type) { |
| 7177 | case XML_EXP_EMPTY: |
| 7178 | if (sub == emptyExp) |
| 7179 | return(emptyExp); |
| 7180 | #ifdef DEBUG_DERIV |
| 7181 | printf("Empty(exp) -> Forbid\n"); |
| 7182 | #endif |
| 7183 | return(forbiddenExp); |
| 7184 | case XML_EXP_FORBID: |
| 7185 | #ifdef DEBUG_DERIV |
| 7186 | printf("Forbid(exp) -> Forbid\n"); |
| 7187 | #endif |
| 7188 | return(forbiddenExp); |
| 7189 | case XML_EXP_ATOM: |
| 7190 | if (sub->type == XML_EXP_ATOM) { |
| 7191 | /* TODO: handle wildcards */ |
| 7192 | if (exp->exp_str == sub->exp_str) { |
| 7193 | #ifdef DEBUG_DERIV |
| 7194 | printf("Atom match -> Empty\n"); |
| 7195 | #endif |
| 7196 | return(emptyExp); |
| 7197 | } |
| 7198 | #ifdef DEBUG_DERIV |
| 7199 | printf("Atom mismatch -> Forbid\n"); |
| 7200 | #endif |
| 7201 | return(forbiddenExp); |
| 7202 | } |
| 7203 | if ((sub->type == XML_EXP_COUNT) && |
| 7204 | (sub->exp_max == 1) && |
| 7205 | (sub->exp_left->type == XML_EXP_ATOM)) { |
| 7206 | /* TODO: handle wildcards */ |
| 7207 | if (exp->exp_str == sub->exp_left->exp_str) { |
| 7208 | #ifdef DEBUG_DERIV |
| 7209 | printf("Atom match -> Empty\n"); |
| 7210 | #endif |
| 7211 | return(emptyExp); |
| 7212 | } |
| 7213 | #ifdef DEBUG_DERIV |
| 7214 | printf("Atom mismatch -> Forbid\n"); |
| 7215 | #endif |
| 7216 | return(forbiddenExp); |
| 7217 | } |
| 7218 | #ifdef DEBUG_DERIV |
| 7219 | printf("Compex exp vs Atom -> Forbid\n"); |
| 7220 | #endif |
| 7221 | return(forbiddenExp); |
| 7222 | case XML_EXP_SEQ: |
| 7223 | /* try to get the sequence consumed only if possible */ |
| 7224 | if (xmlExpCheckCard(exp->exp_left, sub)) { |
| 7225 | /* See if the sequence can be consumed directly */ |
| 7226 | #ifdef DEBUG_DERIV |
| 7227 | printf("Seq trying left only\n"); |
| 7228 | #endif |
| 7229 | ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub); |
| 7230 | if ((ret != forbiddenExp) && (ret != NULL)) { |
| 7231 | #ifdef DEBUG_DERIV |
| 7232 | printf("Seq trying left only worked\n"); |
| 7233 | #endif |
| 7234 | /* |
| 7235 | * TODO: assumption here that we are determinist |
| 7236 | * i.e. we won't get to a nillable exp left |
| 7237 | * subset which could be matched by the right |
| 7238 | * part too. |
| 7239 | * e.g.: (a | b)+,(a | c) and 'a+,a' |
| 7240 | */ |
| 7241 | exp->exp_right->ref++; |
| 7242 | return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, |
| 7243 | exp->exp_right, NULL, 0, 0)); |
| 7244 | } |
| 7245 | #ifdef DEBUG_DERIV |
| 7246 | } else { |
| 7247 | printf("Seq: left too short\n"); |
| 7248 | #endif |
| 7249 | } |
| 7250 | /* Try instead to decompose */ |
| 7251 | if (sub->type == XML_EXP_COUNT) { |
| 7252 | int min, max; |
| 7253 | |
| 7254 | #ifdef DEBUG_DERIV |
| 7255 | printf("Seq: sub is a count\n"); |
| 7256 | #endif |
| 7257 | ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left); |
| 7258 | if (ret == NULL) |
| 7259 | return(NULL); |
| 7260 | if (ret != forbiddenExp) { |
| 7261 | #ifdef DEBUG_DERIV |
| 7262 | printf("Seq , Count match on left\n"); |
| 7263 | #endif |
| 7264 | if (sub->exp_max < 0) |
| 7265 | max = -1; |
| 7266 | else |
| 7267 | max = sub->exp_max -1; |
| 7268 | if (sub->exp_min > 0) |
| 7269 | min = sub->exp_min -1; |
| 7270 | else |
| 7271 | min = 0; |
| 7272 | exp->exp_right->ref++; |
| 7273 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, |
| 7274 | exp->exp_right, NULL, 0, 0); |
| 7275 | if (tmp == NULL) |
| 7276 | return(NULL); |
| 7277 | |
| 7278 | sub->exp_left->ref++; |
| 7279 | tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, |
| 7280 | sub->exp_left, NULL, NULL, min, max); |
| 7281 | if (tmp2 == NULL) { |
| 7282 | xmlExpFree(ctxt, tmp); |
| 7283 | return(NULL); |
| 7284 | } |
| 7285 | ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2); |
| 7286 | xmlExpFree(ctxt, tmp); |
| 7287 | xmlExpFree(ctxt, tmp2); |
| 7288 | return(ret); |
| 7289 | } |
| 7290 | } |
| 7291 | /* we made no progress on structured operations */ |
| 7292 | break; |
| 7293 | case XML_EXP_OR: |
| 7294 | #ifdef DEBUG_DERIV |
| 7295 | printf("Or , trying both side\n"); |
| 7296 | #endif |
| 7297 | ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub); |
| 7298 | if (ret == NULL) |
| 7299 | return(NULL); |
| 7300 | tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub); |
| 7301 | if (tmp == NULL) { |
| 7302 | xmlExpFree(ctxt, ret); |
| 7303 | return(NULL); |
| 7304 | } |
| 7305 | return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0)); |
| 7306 | case XML_EXP_COUNT: { |
| 7307 | int min, max; |
| 7308 | |
| 7309 | if (sub->type == XML_EXP_COUNT) { |
| 7310 | /* |
| 7311 | * Try to see if the loop is completely subsumed |
| 7312 | */ |
| 7313 | tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left); |
| 7314 | if (tmp == NULL) |
| 7315 | return(NULL); |
| 7316 | if (tmp == forbiddenExp) { |
| 7317 | int mult; |
| 7318 | |
| 7319 | #ifdef DEBUG_DERIV |
| 7320 | printf("Count, Count inner don't subsume\n"); |
| 7321 | #endif |
| 7322 | mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left, |
| 7323 | NULL, &tmp); |
| 7324 | if (mult <= 0) { |
| 7325 | #ifdef DEBUG_DERIV |
| 7326 | printf("Count, Count not multiple => forbidden\n"); |
| 7327 | #endif |
| 7328 | return(forbiddenExp); |
| 7329 | } |
| 7330 | if (sub->exp_max == -1) { |
| 7331 | max = -1; |
| 7332 | if (exp->exp_max == -1) { |
| 7333 | if (exp->exp_min <= sub->exp_min * mult) |
| 7334 | min = 0; |
| 7335 | else |
| 7336 | min = exp->exp_min - sub->exp_min * mult; |
| 7337 | } else { |
| 7338 | #ifdef DEBUG_DERIV |
| 7339 | printf("Count, Count finite can't subsume infinite\n"); |
| 7340 | #endif |
| 7341 | xmlExpFree(ctxt, tmp); |
| 7342 | return(forbiddenExp); |
| 7343 | } |
| 7344 | } else { |
| 7345 | if (exp->exp_max == -1) { |
| 7346 | #ifdef DEBUG_DERIV |
| 7347 | printf("Infinite loop consume mult finite loop\n"); |
| 7348 | #endif |
| 7349 | if (exp->exp_min > sub->exp_min * mult) { |
| 7350 | max = -1; |
| 7351 | min = exp->exp_min - sub->exp_min * mult; |
| 7352 | } else { |
| 7353 | max = -1; |
| 7354 | min = 0; |
| 7355 | } |
| 7356 | } else { |
| 7357 | if (exp->exp_max < sub->exp_max * mult) { |
| 7358 | #ifdef DEBUG_DERIV |
| 7359 | printf("loops max mult mismatch => forbidden\n"); |
| 7360 | #endif |
| 7361 | xmlExpFree(ctxt, tmp); |
| 7362 | return(forbiddenExp); |
| 7363 | } |
| 7364 | if (sub->exp_max * mult > exp->exp_min) |
| 7365 | min = 0; |
| 7366 | else |
| 7367 | min = exp->exp_min - sub->exp_max * mult; |
| 7368 | max = exp->exp_max - sub->exp_max * mult; |
| 7369 | } |
| 7370 | } |
| 7371 | } else if (!IS_NILLABLE(tmp)) { |
| 7372 | /* |
| 7373 | * TODO: loop here to try to grow if working on finite |
| 7374 | * blocks. |
| 7375 | */ |
| 7376 | #ifdef DEBUG_DERIV |
| 7377 | printf("Count, Count remain not nillable => forbidden\n"); |
| 7378 | #endif |
| 7379 | xmlExpFree(ctxt, tmp); |
| 7380 | return(forbiddenExp); |
| 7381 | } else if (sub->exp_max == -1) { |
| 7382 | if (exp->exp_max == -1) { |
| 7383 | if (exp->exp_min <= sub->exp_min) { |
| 7384 | #ifdef DEBUG_DERIV |
| 7385 | printf("Infinite loops Okay => COUNT(0,Inf)\n"); |
| 7386 | #endif |
| 7387 | max = -1; |
| 7388 | min = 0; |
| 7389 | } else { |
| 7390 | #ifdef DEBUG_DERIV |
| 7391 | printf("Infinite loops min => Count(X,Inf)\n"); |
| 7392 | #endif |
| 7393 | max = -1; |
| 7394 | min = exp->exp_min - sub->exp_min; |
| 7395 | } |
| 7396 | } else if (exp->exp_min > sub->exp_min) { |
| 7397 | #ifdef DEBUG_DERIV |
| 7398 | printf("loops min mismatch 1 => forbidden ???\n"); |
| 7399 | #endif |
| 7400 | xmlExpFree(ctxt, tmp); |
| 7401 | return(forbiddenExp); |
| 7402 | } else { |
| 7403 | max = -1; |
| 7404 | min = 0; |
| 7405 | } |
| 7406 | } else { |
| 7407 | if (exp->exp_max == -1) { |
| 7408 | #ifdef DEBUG_DERIV |
| 7409 | printf("Infinite loop consume finite loop\n"); |
| 7410 | #endif |
| 7411 | if (exp->exp_min > sub->exp_min) { |
| 7412 | max = -1; |
| 7413 | min = exp->exp_min - sub->exp_min; |
| 7414 | } else { |
| 7415 | max = -1; |
| 7416 | min = 0; |
| 7417 | } |
| 7418 | } else { |
| 7419 | if (exp->exp_max < sub->exp_max) { |
| 7420 | #ifdef DEBUG_DERIV |
| 7421 | printf("loops max mismatch => forbidden\n"); |
| 7422 | #endif |
| 7423 | xmlExpFree(ctxt, tmp); |
| 7424 | return(forbiddenExp); |
| 7425 | } |
| 7426 | if (sub->exp_max > exp->exp_min) |
| 7427 | min = 0; |
| 7428 | else |
| 7429 | min = exp->exp_min - sub->exp_max; |
| 7430 | max = exp->exp_max - sub->exp_max; |
| 7431 | } |
| 7432 | } |
| 7433 | #ifdef DEBUG_DERIV |
| 7434 | printf("loops match => SEQ(COUNT())\n"); |
| 7435 | #endif |
| 7436 | exp->exp_left->ref++; |
| 7437 | tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, |
| 7438 | NULL, NULL, min, max); |
| 7439 | if (tmp2 == NULL) { |
| 7440 | return(NULL); |
| 7441 | } |
| 7442 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2, |
| 7443 | NULL, 0, 0); |
| 7444 | return(ret); |
| 7445 | } |
| 7446 | tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub); |
| 7447 | if (tmp == NULL) |
| 7448 | return(NULL); |
| 7449 | if (tmp == forbiddenExp) { |
| 7450 | #ifdef DEBUG_DERIV |
| 7451 | printf("loop mismatch => forbidden\n"); |
| 7452 | #endif |
| 7453 | return(forbiddenExp); |
| 7454 | } |
| 7455 | if (exp->exp_min > 0) |
| 7456 | min = exp->exp_min - 1; |
| 7457 | else |
| 7458 | min = 0; |
| 7459 | if (exp->exp_max < 0) |
| 7460 | max = -1; |
| 7461 | else |
| 7462 | max = exp->exp_max - 1; |
| 7463 | |
| 7464 | #ifdef DEBUG_DERIV |
| 7465 | printf("loop match => SEQ(COUNT())\n"); |
| 7466 | #endif |
| 7467 | exp->exp_left->ref++; |
| 7468 | tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, |
| 7469 | NULL, NULL, min, max); |
| 7470 | if (tmp2 == NULL) |
| 7471 | return(NULL); |
| 7472 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2, |
| 7473 | NULL, 0, 0); |
| 7474 | return(ret); |
| 7475 | } |
| 7476 | } |
| 7477 | |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 7478 | #ifdef DEBUG_DERIV |
| 7479 | printf("Fallback to derivative\n"); |
| 7480 | #endif |
| 7481 | if (IS_NILLABLE(sub)) { |
| 7482 | if (!(IS_NILLABLE(exp))) |
| 7483 | return(forbiddenExp); |
| 7484 | else |
| 7485 | ret = emptyExp; |
| 7486 | } else |
| 7487 | ret = NULL; |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7488 | /* |
| 7489 | * here the structured derivation made no progress so |
| 7490 | * we use the default token based derivation to force one more step |
| 7491 | */ |
| 7492 | if (ctxt->tabSize == 0) |
| 7493 | ctxt->tabSize = 40; |
| 7494 | |
| 7495 | tab = (const xmlChar **) xmlMalloc(ctxt->tabSize * |
| 7496 | sizeof(const xmlChar *)); |
| 7497 | if (tab == NULL) { |
| 7498 | return(NULL); |
| 7499 | } |
| 7500 | |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7501 | /* |
| 7502 | * collect all the strings accepted by the subexpression on input |
| 7503 | */ |
| 7504 | len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0); |
| 7505 | while (len < 0) { |
| 7506 | const xmlChar **temp; |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7507 | temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 * |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7508 | sizeof(const xmlChar *)); |
| 7509 | if (temp == NULL) { |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7510 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7511 | return(NULL); |
| 7512 | } |
| 7513 | tab = temp; |
| 7514 | ctxt->tabSize *= 2; |
| 7515 | len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0); |
| 7516 | } |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7517 | for (i = 0;i < len;i++) { |
| 7518 | tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]); |
| 7519 | if ((tmp == NULL) || (tmp == forbiddenExp)) { |
| 7520 | xmlExpFree(ctxt, ret); |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7521 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7522 | return(tmp); |
| 7523 | } |
| 7524 | tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]); |
| 7525 | if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) { |
| 7526 | xmlExpFree(ctxt, tmp); |
| 7527 | xmlExpFree(ctxt, ret); |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7528 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7529 | return(tmp); |
| 7530 | } |
| 7531 | tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2); |
| 7532 | xmlExpFree(ctxt, tmp); |
| 7533 | xmlExpFree(ctxt, tmp2); |
| 7534 | |
| 7535 | if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) { |
| 7536 | xmlExpFree(ctxt, ret); |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7537 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7538 | return(tmp3); |
| 7539 | } |
| 7540 | |
| 7541 | if (ret == NULL) |
| 7542 | ret = tmp3; |
| 7543 | else { |
| 7544 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0); |
| 7545 | if (ret == NULL) { |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7546 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7547 | return(NULL); |
| 7548 | } |
| 7549 | } |
| 7550 | } |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7551 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7552 | return(ret); |
| 7553 | } |
| 7554 | |
| 7555 | /** |
Daniel Veillard | 0090bd5 | 2005-08-22 14:43:43 +0000 | [diff] [blame] | 7556 | * xmlExpExpDerive: |
| 7557 | * @ctxt: the expressions context |
| 7558 | * @exp: the englobing expression |
| 7559 | * @sub: the subexpression |
| 7560 | * |
| 7561 | * Evaluates the expression resulting from @exp consuming a sub expression @sub |
| 7562 | * Based on algebraic derivation and sometimes direct Brzozowski derivation |
| 7563 | * it usually tatkes less than linear time and can handle expressions generating |
| 7564 | * infinite languages. |
| 7565 | * |
| 7566 | * Returns the resulting expression or NULL in case of internal error, the |
| 7567 | * result must be freed |
| 7568 | */ |
| 7569 | xmlExpNodePtr |
| 7570 | xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) { |
| 7571 | if ((exp == NULL) || (ctxt == NULL) || (sub == NULL)) |
| 7572 | return(NULL); |
| 7573 | |
| 7574 | /* |
| 7575 | * O(1) speedups |
| 7576 | */ |
| 7577 | if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) { |
| 7578 | #ifdef DEBUG_DERIV |
| 7579 | printf("Sub nillable and not exp : can't subsume\n"); |
| 7580 | #endif |
| 7581 | return(forbiddenExp); |
| 7582 | } |
| 7583 | if (xmlExpCheckCard(exp, sub) == 0) { |
| 7584 | #ifdef DEBUG_DERIV |
| 7585 | printf("sub generate longuer sequances than exp : can't subsume\n"); |
| 7586 | #endif |
| 7587 | return(forbiddenExp); |
| 7588 | } |
| 7589 | return(xmlExpExpDeriveInt(ctxt, exp, sub)); |
| 7590 | } |
| 7591 | |
| 7592 | /** |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7593 | * xmlExpSubsume: |
| 7594 | * @ctxt: the expressions context |
| 7595 | * @exp: the englobing expression |
| 7596 | * @sub: the subexpression |
| 7597 | * |
| 7598 | * Check whether @exp accepts all the languages accexpted by @sub |
| 7599 | * the input being a subexpression. |
| 7600 | * |
| 7601 | * Returns 1 if true 0 if false and -1 in case of failure. |
| 7602 | */ |
| 7603 | int |
| 7604 | xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) { |
| 7605 | xmlExpNodePtr tmp; |
| 7606 | |
| 7607 | if ((exp == NULL) || (ctxt == NULL) || (sub == NULL)) |
| 7608 | return(-1); |
| 7609 | |
| 7610 | /* |
| 7611 | * TODO: speedup by checking the language of sub is a subset of the |
| 7612 | * language of exp |
| 7613 | */ |
| 7614 | /* |
| 7615 | * O(1) speedups |
| 7616 | */ |
| 7617 | if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) { |
| 7618 | #ifdef DEBUG_DERIV |
| 7619 | printf("Sub nillable and not exp : can't subsume\n"); |
| 7620 | #endif |
| 7621 | return(0); |
| 7622 | } |
| 7623 | if (xmlExpCheckCard(exp, sub) == 0) { |
| 7624 | #ifdef DEBUG_DERIV |
| 7625 | printf("sub generate longuer sequances than exp : can't subsume\n"); |
| 7626 | #endif |
| 7627 | return(0); |
| 7628 | } |
| 7629 | tmp = xmlExpExpDeriveInt(ctxt, exp, sub); |
| 7630 | #ifdef DEBUG_DERIV |
| 7631 | printf("Result derivation :\n"); |
| 7632 | PRINT_EXP(tmp); |
| 7633 | #endif |
| 7634 | if (tmp == NULL) |
| 7635 | return(-1); |
| 7636 | if (tmp == forbiddenExp) |
| 7637 | return(0); |
| 7638 | if (tmp == emptyExp) |
| 7639 | return(1); |
| 7640 | if ((tmp != NULL) && (IS_NILLABLE(tmp))) { |
| 7641 | xmlExpFree(ctxt, tmp); |
| 7642 | return(1); |
| 7643 | } |
| 7644 | xmlExpFree(ctxt, tmp); |
| 7645 | return(0); |
| 7646 | } |
Daniel Veillard | 465a000 | 2005-08-22 12:07:04 +0000 | [diff] [blame] | 7647 | |
| 7648 | /************************************************************************ |
| 7649 | * * |
| 7650 | * Parsing expression * |
| 7651 | * * |
| 7652 | ************************************************************************/ |
| 7653 | |
| 7654 | static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt); |
| 7655 | |
| 7656 | #undef CUR |
| 7657 | #define CUR (*ctxt->cur) |
| 7658 | #undef NEXT |
| 7659 | #define NEXT ctxt->cur++; |
| 7660 | #undef IS_BLANK |
| 7661 | #define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t')) |
| 7662 | #define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++; |
| 7663 | |
| 7664 | static int |
| 7665 | xmlExpParseNumber(xmlExpCtxtPtr ctxt) { |
| 7666 | int ret = 0; |
| 7667 | |
| 7668 | SKIP_BLANKS |
| 7669 | if (CUR == '*') { |
| 7670 | NEXT |
| 7671 | return(-1); |
| 7672 | } |
| 7673 | if ((CUR < '0') || (CUR > '9')) |
| 7674 | return(-1); |
| 7675 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7676 | ret = ret * 10 + (CUR - '0'); |
| 7677 | NEXT |
| 7678 | } |
| 7679 | return(ret); |
| 7680 | } |
| 7681 | |
| 7682 | static xmlExpNodePtr |
| 7683 | xmlExpParseOr(xmlExpCtxtPtr ctxt) { |
| 7684 | const char *base; |
| 7685 | xmlExpNodePtr ret; |
| 7686 | const xmlChar *val; |
| 7687 | |
| 7688 | SKIP_BLANKS |
| 7689 | base = ctxt->cur; |
| 7690 | if (*ctxt->cur == '(') { |
| 7691 | NEXT |
| 7692 | ret = xmlExpParseExpr(ctxt); |
| 7693 | SKIP_BLANKS |
| 7694 | if (*ctxt->cur != ')') { |
| 7695 | fprintf(stderr, "unbalanced '(' : %s\n", base); |
| 7696 | xmlExpFree(ctxt, ret); |
| 7697 | return(NULL); |
| 7698 | } |
| 7699 | NEXT; |
| 7700 | SKIP_BLANKS |
| 7701 | goto parse_quantifier; |
| 7702 | } |
| 7703 | while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') && |
| 7704 | (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') && |
| 7705 | (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}')) |
| 7706 | NEXT; |
| 7707 | val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base); |
| 7708 | if (val == NULL) |
| 7709 | return(NULL); |
| 7710 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0); |
| 7711 | if (ret == NULL) |
| 7712 | return(NULL); |
| 7713 | SKIP_BLANKS |
| 7714 | parse_quantifier: |
| 7715 | if (CUR == '{') { |
| 7716 | int min, max; |
| 7717 | |
| 7718 | NEXT |
| 7719 | min = xmlExpParseNumber(ctxt); |
| 7720 | if (min < 0) { |
| 7721 | xmlExpFree(ctxt, ret); |
| 7722 | return(NULL); |
| 7723 | } |
| 7724 | SKIP_BLANKS |
| 7725 | if (CUR == ',') { |
| 7726 | NEXT |
| 7727 | max = xmlExpParseNumber(ctxt); |
| 7728 | SKIP_BLANKS |
| 7729 | } else |
| 7730 | max = min; |
| 7731 | if (CUR != '}') { |
| 7732 | xmlExpFree(ctxt, ret); |
| 7733 | return(NULL); |
| 7734 | } |
| 7735 | NEXT |
| 7736 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
| 7737 | min, max); |
| 7738 | SKIP_BLANKS |
| 7739 | } else if (CUR == '?') { |
| 7740 | NEXT |
| 7741 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
| 7742 | 0, 1); |
| 7743 | SKIP_BLANKS |
| 7744 | } else if (CUR == '+') { |
| 7745 | NEXT |
| 7746 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
| 7747 | 1, -1); |
| 7748 | SKIP_BLANKS |
| 7749 | } else if (CUR == '*') { |
| 7750 | NEXT |
| 7751 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
| 7752 | 0, -1); |
| 7753 | SKIP_BLANKS |
| 7754 | } |
| 7755 | return(ret); |
| 7756 | } |
| 7757 | |
| 7758 | |
| 7759 | static xmlExpNodePtr |
| 7760 | xmlExpParseSeq(xmlExpCtxtPtr ctxt) { |
| 7761 | xmlExpNodePtr ret, right; |
| 7762 | |
| 7763 | ret = xmlExpParseOr(ctxt); |
| 7764 | SKIP_BLANKS |
| 7765 | while (CUR == '|') { |
| 7766 | NEXT |
| 7767 | right = xmlExpParseOr(ctxt); |
| 7768 | if (right == NULL) { |
| 7769 | xmlExpFree(ctxt, ret); |
| 7770 | return(NULL); |
| 7771 | } |
| 7772 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0); |
| 7773 | if (ret == NULL) |
| 7774 | return(NULL); |
| 7775 | } |
| 7776 | return(ret); |
| 7777 | } |
| 7778 | |
| 7779 | static xmlExpNodePtr |
| 7780 | xmlExpParseExpr(xmlExpCtxtPtr ctxt) { |
| 7781 | xmlExpNodePtr ret, right; |
| 7782 | |
| 7783 | ret = xmlExpParseSeq(ctxt); |
| 7784 | SKIP_BLANKS |
| 7785 | while (CUR == ',') { |
| 7786 | NEXT |
| 7787 | right = xmlExpParseSeq(ctxt); |
| 7788 | if (right == NULL) { |
| 7789 | xmlExpFree(ctxt, ret); |
| 7790 | return(NULL); |
| 7791 | } |
| 7792 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0); |
| 7793 | if (ret == NULL) |
| 7794 | return(NULL); |
| 7795 | } |
| 7796 | return(ret); |
| 7797 | } |
| 7798 | |
| 7799 | /** |
| 7800 | * xmlExpParse: |
| 7801 | * @ctxt: the expressions context |
| 7802 | * @expr: the 0 terminated string |
| 7803 | * |
| 7804 | * Minimal parser for regexps, it understand the following constructs |
| 7805 | * - string terminals |
| 7806 | * - choice operator | |
| 7807 | * - sequence operator , |
| 7808 | * - subexpressions (...) |
| 7809 | * - usual cardinality operators + * and ? |
| 7810 | * - finite sequences { min, max } |
| 7811 | * - infinite sequences { min, * } |
| 7812 | * There is minimal checkings made especially no checking on strings values |
| 7813 | * |
| 7814 | * Returns a new expression or NULL in case of failure |
| 7815 | */ |
| 7816 | xmlExpNodePtr |
| 7817 | xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) { |
| 7818 | xmlExpNodePtr ret; |
| 7819 | |
| 7820 | ctxt->expr = expr; |
| 7821 | ctxt->cur = expr; |
| 7822 | |
| 7823 | ret = xmlExpParseExpr(ctxt); |
| 7824 | SKIP_BLANKS |
| 7825 | if (*ctxt->cur != 0) { |
| 7826 | xmlExpFree(ctxt, ret); |
| 7827 | return(NULL); |
| 7828 | } |
| 7829 | return(ret); |
| 7830 | } |
| 7831 | |
| 7832 | static void |
| 7833 | xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) { |
| 7834 | xmlExpNodePtr c; |
| 7835 | |
| 7836 | if (expr == NULL) return; |
| 7837 | if (glob) xmlBufferWriteChar(buf, "("); |
| 7838 | switch (expr->type) { |
| 7839 | case XML_EXP_EMPTY: |
| 7840 | xmlBufferWriteChar(buf, "empty"); |
| 7841 | break; |
| 7842 | case XML_EXP_FORBID: |
| 7843 | xmlBufferWriteChar(buf, "forbidden"); |
| 7844 | break; |
| 7845 | case XML_EXP_ATOM: |
| 7846 | xmlBufferWriteCHAR(buf, expr->exp_str); |
| 7847 | break; |
| 7848 | case XML_EXP_SEQ: |
| 7849 | c = expr->exp_left; |
| 7850 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
| 7851 | xmlExpDumpInt(buf, c, 1); |
| 7852 | else |
| 7853 | xmlExpDumpInt(buf, c, 0); |
| 7854 | xmlBufferWriteChar(buf, " , "); |
| 7855 | c = expr->exp_right; |
| 7856 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
| 7857 | xmlExpDumpInt(buf, c, 1); |
| 7858 | else |
| 7859 | xmlExpDumpInt(buf, c, 0); |
| 7860 | break; |
| 7861 | case XML_EXP_OR: |
| 7862 | c = expr->exp_left; |
| 7863 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
| 7864 | xmlExpDumpInt(buf, c, 1); |
| 7865 | else |
| 7866 | xmlExpDumpInt(buf, c, 0); |
| 7867 | xmlBufferWriteChar(buf, " | "); |
| 7868 | c = expr->exp_right; |
| 7869 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
| 7870 | xmlExpDumpInt(buf, c, 1); |
| 7871 | else |
| 7872 | xmlExpDumpInt(buf, c, 0); |
| 7873 | break; |
| 7874 | case XML_EXP_COUNT: { |
| 7875 | char rep[40]; |
| 7876 | |
| 7877 | c = expr->exp_left; |
| 7878 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
| 7879 | xmlExpDumpInt(buf, c, 1); |
| 7880 | else |
| 7881 | xmlExpDumpInt(buf, c, 0); |
| 7882 | if ((expr->exp_min == 0) && (expr->exp_max == 1)) { |
| 7883 | rep[0] = '?'; |
| 7884 | rep[1] = 0; |
| 7885 | } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) { |
| 7886 | rep[0] = '*'; |
| 7887 | rep[1] = 0; |
| 7888 | } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) { |
| 7889 | rep[0] = '+'; |
| 7890 | rep[1] = 0; |
| 7891 | } else if (expr->exp_max == expr->exp_min) { |
| 7892 | snprintf(rep, 39, "{%d}", expr->exp_min); |
| 7893 | } else if (expr->exp_max < 0) { |
| 7894 | snprintf(rep, 39, "{%d,inf}", expr->exp_min); |
| 7895 | } else { |
| 7896 | snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max); |
| 7897 | } |
| 7898 | rep[39] = 0; |
| 7899 | xmlBufferWriteChar(buf, rep); |
| 7900 | break; |
| 7901 | } |
| 7902 | default: |
| 7903 | fprintf(stderr, "Error in tree\n"); |
| 7904 | } |
| 7905 | if (glob) |
| 7906 | xmlBufferWriteChar(buf, ")"); |
| 7907 | } |
| 7908 | /** |
| 7909 | * xmlExpDump: |
| 7910 | * @buf: a buffer to receive the output |
| 7911 | * @expr: the compiled expression |
| 7912 | * |
| 7913 | * Serialize the expression as compiled to the buffer |
| 7914 | */ |
| 7915 | void |
Daniel Veillard | 5eee767 | 2005-08-22 21:22:27 +0000 | [diff] [blame] | 7916 | xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) { |
| 7917 | if ((buf == NULL) || (expr == NULL)) |
Daniel Veillard | 465a000 | 2005-08-22 12:07:04 +0000 | [diff] [blame] | 7918 | return; |
Daniel Veillard | 5eee767 | 2005-08-22 21:22:27 +0000 | [diff] [blame] | 7919 | xmlExpDumpInt(buf, expr, 0); |
Daniel Veillard | 465a000 | 2005-08-22 12:07:04 +0000 | [diff] [blame] | 7920 | } |
| 7921 | |
| 7922 | /** |
| 7923 | * xmlExpMaxToken: |
| 7924 | * @expr: a compiled expression |
| 7925 | * |
| 7926 | * Indicate the maximum number of input a expression can accept |
| 7927 | * |
| 7928 | * Returns the maximum length or -1 in case of error |
| 7929 | */ |
| 7930 | int |
| 7931 | xmlExpMaxToken(xmlExpNodePtr expr) { |
| 7932 | if (expr == NULL) |
| 7933 | return(-1); |
| 7934 | return(expr->c_max); |
| 7935 | } |
| 7936 | |
| 7937 | /** |
| 7938 | * xmlExpCtxtNbNodes: |
| 7939 | * @ctxt: an expression context |
| 7940 | * |
| 7941 | * Debugging facility provides the number of allocated nodes at a that point |
| 7942 | * |
| 7943 | * Returns the number of nodes in use or -1 in case of error |
| 7944 | */ |
| 7945 | int |
| 7946 | xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) { |
| 7947 | if (ctxt == NULL) |
| 7948 | return(-1); |
| 7949 | return(ctxt->nb_nodes); |
| 7950 | } |
| 7951 | |
| 7952 | /** |
| 7953 | * xmlExpCtxtNbCons: |
| 7954 | * @ctxt: an expression context |
| 7955 | * |
| 7956 | * Debugging facility provides the number of allocated nodes over lifetime |
| 7957 | * |
| 7958 | * Returns the number of nodes ever allocated or -1 in case of error |
| 7959 | */ |
| 7960 | int |
| 7961 | xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) { |
| 7962 | if (ctxt == NULL) |
| 7963 | return(-1); |
| 7964 | return(ctxt->nb_cons); |
| 7965 | } |
| 7966 | |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7967 | #endif /* LIBXML_EXPR_ENABLED */ |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 7968 | #define bottom_xmlregexp |
| 7969 | #include "elfgcchack.h" |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 7970 | #endif /* LIBXML_REGEXP_ENABLED */ |