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 | #if 0 |
| 1742 | tmp->trans[j].to = newto; |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1743 | xmlRegStateAddTransTo(ctxt, ctxt->states[newto], |
| 1744 | tmp->no); |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame^] | 1745 | #endif |
| 1746 | tmp->trans[j].to = -1; |
| 1747 | xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom, |
| 1748 | ctxt->states[newto], |
| 1749 | tmp->trans[j].counter, |
| 1750 | tmp->trans[j].count); |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1751 | } |
| 1752 | } |
| 1753 | } |
| 1754 | #if 0 |
| 1755 | for (i = 0;i < ctxt->nbStates;i++) { |
| 1756 | tmp = ctxt->states[i]; |
| 1757 | for (j = 0;j < tmp->nbTrans;j++) { |
| 1758 | if (tmp->trans[j].to == statenr) { |
| 1759 | tmp->trans[j].to = newto; |
| 1760 | #ifdef DEBUG_REGEXP_GRAPH |
| 1761 | printf("Changed transition %d on %d to go to %d\n", |
| 1762 | j, tmp->no, newto); |
| 1763 | #endif |
| 1764 | } |
| 1765 | } |
| 1766 | } |
| 1767 | #endif |
| 1768 | if (state->type == XML_REGEXP_FINAL_STATE) |
| 1769 | ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE; |
| 1770 | /* eliminate the transition completely */ |
| 1771 | state->nbTrans = 0; |
| 1772 | |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame^] | 1773 | state->type = XML_REGEXP_UNREACH_STATE; |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1774 | |
| 1775 | } |
| 1776 | |
| 1777 | } |
| 1778 | } |
| 1779 | } |
| 1780 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1781 | * xmlFAEliminateEpsilonTransitions: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1782 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1783 | * |
| 1784 | */ |
| 1785 | static void |
| 1786 | xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) { |
| 1787 | int statenr, transnr; |
| 1788 | xmlRegStatePtr state; |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1789 | int has_epsilon; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1790 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1791 | if (ctxt->states == NULL) return; |
| 1792 | |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame^] | 1793 | /* |
| 1794 | * Eliminate simple epsilon transition and the associated unreachable |
| 1795 | * states. |
| 1796 | */ |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1797 | xmlFAEliminateSimpleEpsilonTransitions(ctxt); |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame^] | 1798 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1799 | state = ctxt->states[statenr]; |
| 1800 | if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) { |
| 1801 | #ifdef DEBUG_REGEXP_GRAPH |
| 1802 | printf("Removed unreachable state %d\n", statenr); |
| 1803 | #endif |
| 1804 | xmlRegFreeState(state); |
| 1805 | ctxt->states[statenr] = NULL; |
| 1806 | } |
| 1807 | } |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1808 | |
| 1809 | has_epsilon = 0; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1810 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1811 | /* |
| 1812 | * build the completed transitions bypassing the epsilons |
| 1813 | * Use a marking algorithm to avoid loops |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 1814 | * mark sink states too. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1815 | */ |
| 1816 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1817 | state = ctxt->states[statenr]; |
| 1818 | if (state == NULL) |
| 1819 | continue; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 1820 | if ((state->nbTrans == 0) && |
| 1821 | (state->type != XML_REGEXP_FINAL_STATE)) { |
| 1822 | state->type = XML_REGEXP_SINK_STATE; |
| 1823 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1824 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 1825 | if ((state->trans[transnr].atom == NULL) && |
| 1826 | (state->trans[transnr].to >= 0)) { |
| 1827 | if (state->trans[transnr].to == statenr) { |
| 1828 | state->trans[transnr].to = -1; |
| 1829 | #ifdef DEBUG_REGEXP_GRAPH |
| 1830 | printf("Removed loopback epsilon trans %d on %d\n", |
| 1831 | transnr, statenr); |
| 1832 | #endif |
| 1833 | } else if (state->trans[transnr].count < 0) { |
| 1834 | int newto = state->trans[transnr].to; |
| 1835 | |
| 1836 | #ifdef DEBUG_REGEXP_GRAPH |
| 1837 | printf("Found epsilon trans %d from %d to %d\n", |
| 1838 | transnr, statenr, newto); |
| 1839 | #endif |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1840 | has_epsilon = 1; |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame^] | 1841 | state->trans[transnr].to = -2; |
| 1842 | state->mark = XML_REGEXP_MARK_START; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1843 | xmlFAReduceEpsilonTransitions(ctxt, statenr, |
| 1844 | newto, state->trans[transnr].counter); |
| 1845 | state->mark = XML_REGEXP_MARK_NORMAL; |
| 1846 | #ifdef DEBUG_REGEXP_GRAPH |
| 1847 | } else { |
| 1848 | printf("Found counted transition %d on %d\n", |
| 1849 | transnr, statenr); |
| 1850 | #endif |
| 1851 | } |
| 1852 | } |
| 1853 | } |
| 1854 | } |
| 1855 | /* |
| 1856 | * Eliminate the epsilon transitions |
| 1857 | */ |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 1858 | if (has_epsilon) { |
| 1859 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1860 | state = ctxt->states[statenr]; |
| 1861 | if (state == NULL) |
| 1862 | continue; |
| 1863 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 1864 | xmlRegTransPtr trans = &(state->trans[transnr]); |
| 1865 | if ((trans->atom == NULL) && |
| 1866 | (trans->count < 0) && |
| 1867 | (trans->to >= 0)) { |
| 1868 | trans->to = -1; |
| 1869 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1870 | } |
| 1871 | } |
| 1872 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1873 | |
| 1874 | /* |
| 1875 | * Use this pass to detect unreachable states too |
| 1876 | */ |
| 1877 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1878 | state = ctxt->states[statenr]; |
| 1879 | if (state != NULL) |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1880 | state->reached = XML_REGEXP_MARK_NORMAL; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1881 | } |
| 1882 | state = ctxt->states[0]; |
| 1883 | if (state != NULL) |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1884 | state->reached = XML_REGEXP_MARK_START; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1885 | while (state != NULL) { |
| 1886 | xmlRegStatePtr target = NULL; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1887 | state->reached = XML_REGEXP_MARK_VISITED; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1888 | /* |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1889 | * Mark all states reachable from the current reachable state |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1890 | */ |
| 1891 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 1892 | if ((state->trans[transnr].to >= 0) && |
| 1893 | ((state->trans[transnr].atom != NULL) || |
| 1894 | (state->trans[transnr].count >= 0))) { |
| 1895 | int newto = state->trans[transnr].to; |
| 1896 | |
| 1897 | if (ctxt->states[newto] == NULL) |
| 1898 | continue; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1899 | if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) { |
| 1900 | ctxt->states[newto]->reached = XML_REGEXP_MARK_START; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1901 | target = ctxt->states[newto]; |
| 1902 | } |
| 1903 | } |
| 1904 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 1905 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1906 | /* |
| 1907 | * find the next accessible state not explored |
| 1908 | */ |
| 1909 | if (target == NULL) { |
| 1910 | for (statenr = 1;statenr < ctxt->nbStates;statenr++) { |
| 1911 | state = ctxt->states[statenr]; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1912 | if ((state != NULL) && (state->reached == |
| 1913 | XML_REGEXP_MARK_START)) { |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1914 | target = state; |
| 1915 | break; |
| 1916 | } |
| 1917 | } |
| 1918 | } |
| 1919 | state = target; |
| 1920 | } |
| 1921 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1922 | state = ctxt->states[statenr]; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1923 | if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) { |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1924 | #ifdef DEBUG_REGEXP_GRAPH |
| 1925 | printf("Removed unreachable state %d\n", statenr); |
| 1926 | #endif |
| 1927 | xmlRegFreeState(state); |
| 1928 | ctxt->states[statenr] = NULL; |
| 1929 | } |
| 1930 | } |
| 1931 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 1934 | static int |
| 1935 | xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) { |
| 1936 | int ret = 0; |
| 1937 | |
| 1938 | if ((range1->type == XML_REGEXP_RANGES) || |
| 1939 | (range2->type == XML_REGEXP_RANGES) || |
| 1940 | (range2->type == XML_REGEXP_SUBREG) || |
| 1941 | (range1->type == XML_REGEXP_SUBREG) || |
| 1942 | (range1->type == XML_REGEXP_STRING) || |
| 1943 | (range2->type == XML_REGEXP_STRING)) |
| 1944 | return(-1); |
| 1945 | |
| 1946 | /* put them in order */ |
| 1947 | if (range1->type > range2->type) { |
| 1948 | xmlRegRangePtr tmp; |
| 1949 | |
| 1950 | tmp = range1; |
| 1951 | range1 = range2; |
| 1952 | range2 = tmp; |
| 1953 | } |
| 1954 | if ((range1->type == XML_REGEXP_ANYCHAR) || |
| 1955 | (range2->type == XML_REGEXP_ANYCHAR)) { |
| 1956 | ret = 1; |
| 1957 | } else if ((range1->type == XML_REGEXP_EPSILON) || |
| 1958 | (range2->type == XML_REGEXP_EPSILON)) { |
| 1959 | return(0); |
| 1960 | } else if (range1->type == range2->type) { |
| 1961 | if ((range1->type != XML_REGEXP_CHARVAL) || |
| 1962 | (range1->end < range2->start) || |
| 1963 | (range2->end < range1->start)) |
| 1964 | ret = 1; |
| 1965 | else |
| 1966 | ret = 0; |
| 1967 | } else if (range1->type == XML_REGEXP_CHARVAL) { |
| 1968 | int codepoint; |
| 1969 | int neg = 0; |
| 1970 | |
| 1971 | /* |
| 1972 | * just check all codepoints in the range for acceptance, |
| 1973 | * this is usually way cheaper since done only once at |
| 1974 | * compilation than testing over and over at runtime or |
| 1975 | * pushing too many states when evaluating. |
| 1976 | */ |
| 1977 | if (((range1->neg == 0) && (range2->neg != 0)) || |
| 1978 | ((range1->neg != 0) && (range2->neg == 0))) |
| 1979 | neg = 1; |
| 1980 | |
| 1981 | for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) { |
| 1982 | ret = xmlRegCheckCharacterRange(range2->type, codepoint, |
| 1983 | 0, range2->start, range2->end, |
| 1984 | range2->blockName); |
| 1985 | if (ret < 0) |
| 1986 | return(-1); |
| 1987 | if (((neg == 1) && (ret == 0)) || |
| 1988 | ((neg == 0) && (ret == 1))) |
| 1989 | return(1); |
| 1990 | } |
| 1991 | return(0); |
| 1992 | } else if ((range1->type == XML_REGEXP_BLOCK_NAME) || |
| 1993 | (range2->type == XML_REGEXP_BLOCK_NAME)) { |
| 1994 | if (range1->type == range2->type) { |
| 1995 | ret = xmlStrEqual(range1->blockName, range2->blockName); |
| 1996 | } else { |
| 1997 | /* |
| 1998 | * comparing a block range with anything else is way |
| 1999 | * too costly, and maintining the table is like too much |
| 2000 | * memory too, so let's force the automata to save state |
| 2001 | * here. |
| 2002 | */ |
| 2003 | return(1); |
| 2004 | } |
| 2005 | } else if ((range1->type < XML_REGEXP_LETTER) || |
| 2006 | (range2->type < XML_REGEXP_LETTER)) { |
| 2007 | if ((range1->type == XML_REGEXP_ANYSPACE) && |
| 2008 | (range2->type == XML_REGEXP_NOTSPACE)) |
| 2009 | ret = 0; |
| 2010 | else if ((range1->type == XML_REGEXP_INITNAME) && |
| 2011 | (range2->type == XML_REGEXP_NOTINITNAME)) |
| 2012 | ret = 0; |
| 2013 | else if ((range1->type == XML_REGEXP_NAMECHAR) && |
| 2014 | (range2->type == XML_REGEXP_NOTNAMECHAR)) |
| 2015 | ret = 0; |
| 2016 | else if ((range1->type == XML_REGEXP_DECIMAL) && |
| 2017 | (range2->type == XML_REGEXP_NOTDECIMAL)) |
| 2018 | ret = 0; |
| 2019 | else if ((range1->type == XML_REGEXP_REALCHAR) && |
| 2020 | (range2->type == XML_REGEXP_NOTREALCHAR)) |
| 2021 | ret = 0; |
| 2022 | else { |
| 2023 | /* same thing to limit complexity */ |
| 2024 | return(1); |
| 2025 | } |
| 2026 | } else { |
| 2027 | ret = 0; |
| 2028 | /* range1->type < range2->type here */ |
| 2029 | switch (range1->type) { |
| 2030 | case XML_REGEXP_LETTER: |
| 2031 | /* all disjoint except in the subgroups */ |
| 2032 | if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) || |
| 2033 | (range2->type == XML_REGEXP_LETTER_LOWERCASE) || |
| 2034 | (range2->type == XML_REGEXP_LETTER_TITLECASE) || |
| 2035 | (range2->type == XML_REGEXP_LETTER_MODIFIER) || |
| 2036 | (range2->type == XML_REGEXP_LETTER_OTHERS)) |
| 2037 | ret = 1; |
| 2038 | break; |
| 2039 | case XML_REGEXP_MARK: |
| 2040 | if ((range2->type == XML_REGEXP_MARK_NONSPACING) || |
| 2041 | (range2->type == XML_REGEXP_MARK_SPACECOMBINING) || |
| 2042 | (range2->type == XML_REGEXP_MARK_ENCLOSING)) |
| 2043 | ret = 1; |
| 2044 | break; |
| 2045 | case XML_REGEXP_NUMBER: |
| 2046 | if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) || |
| 2047 | (range2->type == XML_REGEXP_NUMBER_LETTER) || |
| 2048 | (range2->type == XML_REGEXP_NUMBER_OTHERS)) |
| 2049 | ret = 1; |
| 2050 | break; |
| 2051 | case XML_REGEXP_PUNCT: |
| 2052 | if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) || |
| 2053 | (range2->type == XML_REGEXP_PUNCT_DASH) || |
| 2054 | (range2->type == XML_REGEXP_PUNCT_OPEN) || |
| 2055 | (range2->type == XML_REGEXP_PUNCT_CLOSE) || |
| 2056 | (range2->type == XML_REGEXP_PUNCT_INITQUOTE) || |
| 2057 | (range2->type == XML_REGEXP_PUNCT_FINQUOTE) || |
| 2058 | (range2->type == XML_REGEXP_PUNCT_OTHERS)) |
| 2059 | ret = 1; |
| 2060 | break; |
| 2061 | case XML_REGEXP_SEPAR: |
| 2062 | if ((range2->type == XML_REGEXP_SEPAR_SPACE) || |
| 2063 | (range2->type == XML_REGEXP_SEPAR_LINE) || |
| 2064 | (range2->type == XML_REGEXP_SEPAR_PARA)) |
| 2065 | ret = 1; |
| 2066 | break; |
| 2067 | case XML_REGEXP_SYMBOL: |
| 2068 | if ((range2->type == XML_REGEXP_SYMBOL_MATH) || |
| 2069 | (range2->type == XML_REGEXP_SYMBOL_CURRENCY) || |
| 2070 | (range2->type == XML_REGEXP_SYMBOL_MODIFIER) || |
| 2071 | (range2->type == XML_REGEXP_SYMBOL_OTHERS)) |
| 2072 | ret = 1; |
| 2073 | break; |
| 2074 | case XML_REGEXP_OTHER: |
| 2075 | if ((range2->type == XML_REGEXP_OTHER_CONTROL) || |
| 2076 | (range2->type == XML_REGEXP_OTHER_FORMAT) || |
| 2077 | (range2->type == XML_REGEXP_OTHER_PRIVATE)) |
| 2078 | ret = 1; |
| 2079 | break; |
| 2080 | default: |
| 2081 | if ((range2->type >= XML_REGEXP_LETTER) && |
| 2082 | (range2->type < XML_REGEXP_BLOCK_NAME)) |
| 2083 | ret = 0; |
| 2084 | else { |
| 2085 | /* safety net ! */ |
| 2086 | return(1); |
| 2087 | } |
| 2088 | } |
| 2089 | } |
| 2090 | if (((range1->neg == 0) && (range2->neg != 0)) || |
| 2091 | ((range1->neg != 0) && (range2->neg == 0))) |
| 2092 | ret = !ret; |
| 2093 | return(1); |
| 2094 | } |
| 2095 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2096 | /** |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2097 | * xmlFACompareAtomTypes: |
| 2098 | * @type1: an atom type |
| 2099 | * @type2: an atom type |
| 2100 | * |
| 2101 | * Compares two atoms type to check whether they intersect in some ways, |
| 2102 | * this is used by xmlFACompareAtoms only |
| 2103 | * |
| 2104 | * Returns 1 if they may intersect and 0 otherwise |
| 2105 | */ |
| 2106 | static int |
| 2107 | xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) { |
| 2108 | if ((type1 == XML_REGEXP_EPSILON) || |
| 2109 | (type1 == XML_REGEXP_CHARVAL) || |
| 2110 | (type1 == XML_REGEXP_RANGES) || |
| 2111 | (type1 == XML_REGEXP_SUBREG) || |
| 2112 | (type1 == XML_REGEXP_STRING) || |
| 2113 | (type1 == XML_REGEXP_ANYCHAR)) |
| 2114 | return(1); |
| 2115 | if ((type2 == XML_REGEXP_EPSILON) || |
| 2116 | (type2 == XML_REGEXP_CHARVAL) || |
| 2117 | (type2 == XML_REGEXP_RANGES) || |
| 2118 | (type2 == XML_REGEXP_SUBREG) || |
| 2119 | (type2 == XML_REGEXP_STRING) || |
| 2120 | (type2 == XML_REGEXP_ANYCHAR)) |
| 2121 | return(1); |
| 2122 | |
| 2123 | if (type1 == type2) return(1); |
| 2124 | |
| 2125 | /* simplify subsequent compares by making sure type1 < type2 */ |
| 2126 | if (type1 > type2) { |
| 2127 | xmlRegAtomType tmp = type1; |
| 2128 | type1 = type2; |
| 2129 | type2 = tmp; |
| 2130 | } |
| 2131 | switch (type1) { |
| 2132 | case XML_REGEXP_ANYSPACE: /* \s */ |
| 2133 | /* can't be a letter, number, mark, pontuation, symbol */ |
| 2134 | if ((type2 == XML_REGEXP_NOTSPACE) || |
| 2135 | ((type2 >= XML_REGEXP_LETTER) && |
| 2136 | (type2 <= XML_REGEXP_LETTER_OTHERS)) || |
| 2137 | ((type2 >= XML_REGEXP_NUMBER) && |
| 2138 | (type2 <= XML_REGEXP_NUMBER_OTHERS)) || |
| 2139 | ((type2 >= XML_REGEXP_MARK) && |
| 2140 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
| 2141 | ((type2 >= XML_REGEXP_PUNCT) && |
| 2142 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
| 2143 | ((type2 >= XML_REGEXP_SYMBOL) && |
| 2144 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) |
| 2145 | ) return(0); |
| 2146 | break; |
| 2147 | case XML_REGEXP_NOTSPACE: /* \S */ |
| 2148 | break; |
| 2149 | case XML_REGEXP_INITNAME: /* \l */ |
| 2150 | /* can't be a number, mark, separator, pontuation, symbol or other */ |
| 2151 | if ((type2 == XML_REGEXP_NOTINITNAME) || |
| 2152 | ((type2 >= XML_REGEXP_NUMBER) && |
| 2153 | (type2 <= XML_REGEXP_NUMBER_OTHERS)) || |
| 2154 | ((type2 >= XML_REGEXP_MARK) && |
| 2155 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
| 2156 | ((type2 >= XML_REGEXP_SEPAR) && |
| 2157 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
| 2158 | ((type2 >= XML_REGEXP_PUNCT) && |
| 2159 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
| 2160 | ((type2 >= XML_REGEXP_SYMBOL) && |
| 2161 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
| 2162 | ((type2 >= XML_REGEXP_OTHER) && |
| 2163 | (type2 <= XML_REGEXP_OTHER_NA)) |
| 2164 | ) return(0); |
| 2165 | break; |
| 2166 | case XML_REGEXP_NOTINITNAME: /* \L */ |
| 2167 | break; |
| 2168 | case XML_REGEXP_NAMECHAR: /* \c */ |
| 2169 | /* can't be a mark, separator, pontuation, symbol or other */ |
| 2170 | if ((type2 == XML_REGEXP_NOTNAMECHAR) || |
| 2171 | ((type2 >= XML_REGEXP_MARK) && |
| 2172 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
| 2173 | ((type2 >= XML_REGEXP_PUNCT) && |
| 2174 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
| 2175 | ((type2 >= XML_REGEXP_SEPAR) && |
| 2176 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
| 2177 | ((type2 >= XML_REGEXP_SYMBOL) && |
| 2178 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
| 2179 | ((type2 >= XML_REGEXP_OTHER) && |
| 2180 | (type2 <= XML_REGEXP_OTHER_NA)) |
| 2181 | ) return(0); |
| 2182 | break; |
| 2183 | case XML_REGEXP_NOTNAMECHAR: /* \C */ |
| 2184 | break; |
| 2185 | case XML_REGEXP_DECIMAL: /* \d */ |
| 2186 | /* can't be a letter, mark, separator, pontuation, symbol or other */ |
| 2187 | if ((type2 == XML_REGEXP_NOTDECIMAL) || |
| 2188 | (type2 == XML_REGEXP_REALCHAR) || |
| 2189 | ((type2 >= XML_REGEXP_LETTER) && |
| 2190 | (type2 <= XML_REGEXP_LETTER_OTHERS)) || |
| 2191 | ((type2 >= XML_REGEXP_MARK) && |
| 2192 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
| 2193 | ((type2 >= XML_REGEXP_PUNCT) && |
| 2194 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
| 2195 | ((type2 >= XML_REGEXP_SEPAR) && |
| 2196 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
| 2197 | ((type2 >= XML_REGEXP_SYMBOL) && |
| 2198 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
| 2199 | ((type2 >= XML_REGEXP_OTHER) && |
| 2200 | (type2 <= XML_REGEXP_OTHER_NA)) |
| 2201 | )return(0); |
| 2202 | break; |
| 2203 | case XML_REGEXP_NOTDECIMAL: /* \D */ |
| 2204 | break; |
| 2205 | case XML_REGEXP_REALCHAR: /* \w */ |
| 2206 | /* can't be a mark, separator, pontuation, symbol or other */ |
| 2207 | if ((type2 == XML_REGEXP_NOTDECIMAL) || |
| 2208 | ((type2 >= XML_REGEXP_MARK) && |
| 2209 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) || |
| 2210 | ((type2 >= XML_REGEXP_PUNCT) && |
| 2211 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) || |
| 2212 | ((type2 >= XML_REGEXP_SEPAR) && |
| 2213 | (type2 <= XML_REGEXP_SEPAR_PARA)) || |
| 2214 | ((type2 >= XML_REGEXP_SYMBOL) && |
| 2215 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) || |
| 2216 | ((type2 >= XML_REGEXP_OTHER) && |
| 2217 | (type2 <= XML_REGEXP_OTHER_NA)) |
| 2218 | )return(0); |
| 2219 | break; |
| 2220 | case XML_REGEXP_NOTREALCHAR: /* \W */ |
| 2221 | break; |
| 2222 | /* |
| 2223 | * at that point we know both type 1 and type2 are from |
| 2224 | * character categories are ordered and are different, |
| 2225 | * it becomes simple because this is a partition |
| 2226 | */ |
| 2227 | case XML_REGEXP_LETTER: |
| 2228 | if (type2 <= XML_REGEXP_LETTER_OTHERS) |
| 2229 | return(1); |
| 2230 | return(0); |
| 2231 | case XML_REGEXP_LETTER_UPPERCASE: |
| 2232 | case XML_REGEXP_LETTER_LOWERCASE: |
| 2233 | case XML_REGEXP_LETTER_TITLECASE: |
| 2234 | case XML_REGEXP_LETTER_MODIFIER: |
| 2235 | case XML_REGEXP_LETTER_OTHERS: |
| 2236 | return(0); |
| 2237 | case XML_REGEXP_MARK: |
| 2238 | if (type2 <= XML_REGEXP_MARK_ENCLOSING) |
| 2239 | return(1); |
| 2240 | return(0); |
| 2241 | case XML_REGEXP_MARK_NONSPACING: |
| 2242 | case XML_REGEXP_MARK_SPACECOMBINING: |
| 2243 | case XML_REGEXP_MARK_ENCLOSING: |
| 2244 | return(0); |
| 2245 | case XML_REGEXP_NUMBER: |
| 2246 | if (type2 <= XML_REGEXP_NUMBER_OTHERS) |
| 2247 | return(1); |
| 2248 | return(0); |
| 2249 | case XML_REGEXP_NUMBER_DECIMAL: |
| 2250 | case XML_REGEXP_NUMBER_LETTER: |
| 2251 | case XML_REGEXP_NUMBER_OTHERS: |
| 2252 | return(0); |
| 2253 | case XML_REGEXP_PUNCT: |
| 2254 | if (type2 <= XML_REGEXP_PUNCT_OTHERS) |
| 2255 | return(1); |
| 2256 | return(0); |
| 2257 | case XML_REGEXP_PUNCT_CONNECTOR: |
| 2258 | case XML_REGEXP_PUNCT_DASH: |
| 2259 | case XML_REGEXP_PUNCT_OPEN: |
| 2260 | case XML_REGEXP_PUNCT_CLOSE: |
| 2261 | case XML_REGEXP_PUNCT_INITQUOTE: |
| 2262 | case XML_REGEXP_PUNCT_FINQUOTE: |
| 2263 | case XML_REGEXP_PUNCT_OTHERS: |
| 2264 | return(0); |
| 2265 | case XML_REGEXP_SEPAR: |
| 2266 | if (type2 <= XML_REGEXP_SEPAR_PARA) |
| 2267 | return(1); |
| 2268 | return(0); |
| 2269 | case XML_REGEXP_SEPAR_SPACE: |
| 2270 | case XML_REGEXP_SEPAR_LINE: |
| 2271 | case XML_REGEXP_SEPAR_PARA: |
| 2272 | return(0); |
| 2273 | case XML_REGEXP_SYMBOL: |
| 2274 | if (type2 <= XML_REGEXP_SYMBOL_OTHERS) |
| 2275 | return(1); |
| 2276 | return(0); |
| 2277 | case XML_REGEXP_SYMBOL_MATH: |
| 2278 | case XML_REGEXP_SYMBOL_CURRENCY: |
| 2279 | case XML_REGEXP_SYMBOL_MODIFIER: |
| 2280 | case XML_REGEXP_SYMBOL_OTHERS: |
| 2281 | return(0); |
| 2282 | case XML_REGEXP_OTHER: |
| 2283 | if (type2 <= XML_REGEXP_OTHER_NA) |
| 2284 | return(1); |
| 2285 | return(0); |
| 2286 | case XML_REGEXP_OTHER_CONTROL: |
| 2287 | case XML_REGEXP_OTHER_FORMAT: |
| 2288 | case XML_REGEXP_OTHER_PRIVATE: |
| 2289 | case XML_REGEXP_OTHER_NA: |
| 2290 | return(0); |
| 2291 | default: |
| 2292 | break; |
| 2293 | } |
| 2294 | return(1); |
| 2295 | } |
| 2296 | |
| 2297 | /** |
| 2298 | * xmlFAEqualAtoms: |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2299 | * @atom1: an atom |
| 2300 | * @atom2: an atom |
| 2301 | * |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2302 | * Compares two atoms to check whether they are the same exactly |
| 2303 | * this is used to remove equivalent transitions |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2304 | * |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2305 | * Returns 1 if same and 0 otherwise |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2306 | */ |
| 2307 | static int |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2308 | xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) { |
| 2309 | int ret = 0; |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 2310 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2311 | if (atom1 == atom2) |
| 2312 | return(1); |
| 2313 | if ((atom1 == NULL) || (atom2 == NULL)) |
| 2314 | return(0); |
| 2315 | |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2316 | if (atom1->type != atom2->type) |
| 2317 | return(0); |
| 2318 | switch (atom1->type) { |
| 2319 | case XML_REGEXP_EPSILON: |
| 2320 | ret = 0; |
| 2321 | break; |
| 2322 | case XML_REGEXP_STRING: |
| 2323 | ret = xmlStrEqual((xmlChar *)atom1->valuep, |
| 2324 | (xmlChar *)atom2->valuep); |
| 2325 | break; |
| 2326 | case XML_REGEXP_CHARVAL: |
| 2327 | ret = (atom1->codepoint == atom2->codepoint); |
| 2328 | break; |
| 2329 | case XML_REGEXP_RANGES: |
| 2330 | /* too hard to do in the general case */ |
| 2331 | ret = 0; |
| 2332 | default: |
| 2333 | break; |
| 2334 | } |
| 2335 | return(ret); |
| 2336 | } |
| 2337 | |
| 2338 | /** |
| 2339 | * xmlFACompareAtoms: |
| 2340 | * @atom1: an atom |
| 2341 | * @atom2: an atom |
| 2342 | * |
| 2343 | * Compares two atoms to check whether they intersect in some ways, |
| 2344 | * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only |
| 2345 | * |
| 2346 | * Returns 1 if yes and 0 otherwise |
| 2347 | */ |
| 2348 | static int |
| 2349 | xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) { |
| 2350 | int ret = 1; |
| 2351 | |
| 2352 | if (atom1 == atom2) |
| 2353 | return(1); |
| 2354 | if ((atom1 == NULL) || (atom2 == NULL)) |
| 2355 | return(0); |
| 2356 | |
| 2357 | if ((atom1->type == XML_REGEXP_ANYCHAR) || |
| 2358 | (atom2->type == XML_REGEXP_ANYCHAR)) |
| 2359 | return(1); |
| 2360 | |
| 2361 | if (atom1->type > atom2->type) { |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2362 | xmlRegAtomPtr tmp; |
| 2363 | tmp = atom1; |
| 2364 | atom1 = atom2; |
| 2365 | atom2 = tmp; |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2366 | } |
| 2367 | if (atom1->type != atom2->type) { |
| 2368 | ret = xmlFACompareAtomTypes(atom1->type, atom2->type); |
| 2369 | /* if they can't intersect at the type level break now */ |
| 2370 | if (ret == 0) |
| 2371 | return(0); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2372 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2373 | switch (atom1->type) { |
| 2374 | case XML_REGEXP_STRING: |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 2375 | ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep, |
| 2376 | (xmlChar *)atom2->valuep); |
| 2377 | break; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2378 | case XML_REGEXP_EPSILON: |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2379 | goto not_determinist; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2380 | case XML_REGEXP_CHARVAL: |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2381 | if (atom2->type == XML_REGEXP_CHARVAL) { |
| 2382 | ret = (atom1->codepoint == atom2->codepoint); |
| 2383 | } else { |
| 2384 | ret = xmlRegCheckCharacter(atom2, atom1->codepoint); |
| 2385 | if (ret < 0) |
| 2386 | ret = 1; |
| 2387 | } |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 2388 | break; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2389 | case XML_REGEXP_RANGES: |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2390 | if (atom2->type == XML_REGEXP_RANGES) { |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2391 | int i, j, res; |
| 2392 | xmlRegRangePtr r1, r2; |
| 2393 | |
| 2394 | /* |
| 2395 | * need to check that none of the ranges eventually matches |
| 2396 | */ |
| 2397 | for (i = 0;i < atom1->nbRanges;i++) { |
| 2398 | for (j = 0;j < atom2->nbRanges;j++) { |
| 2399 | r1 = atom1->ranges[i]; |
| 2400 | r2 = atom2->ranges[j]; |
| 2401 | res = xmlFACompareRanges(r1, r2); |
| 2402 | if (res == 1) { |
| 2403 | ret = 1; |
| 2404 | goto done; |
| 2405 | } |
| 2406 | } |
| 2407 | } |
| 2408 | ret = 0; |
| 2409 | } |
| 2410 | break; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2411 | default: |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2412 | goto not_determinist; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2413 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2414 | done: |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 2415 | if (atom1->neg != atom2->neg) { |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 2416 | ret = !ret; |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 2417 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2418 | if (ret == 0) |
| 2419 | return(0); |
| 2420 | not_determinist: |
| 2421 | return(1); |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | /** |
| 2425 | * xmlFARecurseDeterminism: |
| 2426 | * @ctxt: a regexp parser context |
| 2427 | * |
| 2428 | * Check whether the associated regexp is determinist, |
| 2429 | * should be called after xmlFAEliminateEpsilonTransitions() |
| 2430 | * |
| 2431 | */ |
| 2432 | static int |
| 2433 | xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state, |
| 2434 | int to, xmlRegAtomPtr atom) { |
| 2435 | int ret = 1; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2436 | int res; |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 2437 | int transnr, nbTrans; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2438 | xmlRegTransPtr t1; |
| 2439 | |
| 2440 | if (state == NULL) |
| 2441 | return(ret); |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 2442 | /* |
| 2443 | * don't recurse on transitions potentially added in the course of |
| 2444 | * the elimination. |
| 2445 | */ |
| 2446 | nbTrans = state->nbTrans; |
| 2447 | for (transnr = 0;transnr < nbTrans;transnr++) { |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2448 | t1 = &(state->trans[transnr]); |
| 2449 | /* |
| 2450 | * check transitions conflicting with the one looked at |
| 2451 | */ |
| 2452 | if (t1->atom == NULL) { |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame^] | 2453 | if (t1->to < 0) |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2454 | continue; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2455 | res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to], |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2456 | to, atom); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2457 | if (res == 0) { |
| 2458 | ret = 0; |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 2459 | /* t1->nd = 1; */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2460 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2461 | continue; |
| 2462 | } |
| 2463 | if (t1->to != to) |
| 2464 | continue; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2465 | if (xmlFACompareAtoms(t1->atom, atom)) { |
| 2466 | ret = 0; |
| 2467 | /* mark the transition as non-deterministic */ |
| 2468 | t1->nd = 1; |
| 2469 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2470 | } |
| 2471 | return(ret); |
| 2472 | } |
| 2473 | |
| 2474 | /** |
| 2475 | * xmlFAComputesDeterminism: |
| 2476 | * @ctxt: a regexp parser context |
| 2477 | * |
| 2478 | * Check whether the associated regexp is determinist, |
| 2479 | * should be called after xmlFAEliminateEpsilonTransitions() |
| 2480 | * |
| 2481 | */ |
| 2482 | static int |
| 2483 | xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) { |
| 2484 | int statenr, transnr; |
| 2485 | xmlRegStatePtr state; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2486 | xmlRegTransPtr t1, t2, last; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2487 | int i; |
| 2488 | int ret = 1; |
| 2489 | |
Daniel Veillard | 4402ab4 | 2002-09-12 16:02:56 +0000 | [diff] [blame] | 2490 | #ifdef DEBUG_REGEXP_GRAPH |
| 2491 | printf("xmlFAComputesDeterminism\n"); |
| 2492 | xmlRegPrintCtxt(stdout, ctxt); |
| 2493 | #endif |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2494 | if (ctxt->determinist != -1) |
| 2495 | return(ctxt->determinist); |
| 2496 | |
| 2497 | /* |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2498 | * First cleanup the automata removing cancelled transitions |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2499 | */ |
| 2500 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 2501 | state = ctxt->states[statenr]; |
| 2502 | if (state == NULL) |
| 2503 | continue; |
Daniel Veillard | 4f82c8a | 2005-08-09 21:40:08 +0000 | [diff] [blame] | 2504 | if (state->nbTrans < 2) |
| 2505 | continue; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2506 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 2507 | t1 = &(state->trans[transnr]); |
| 2508 | /* |
| 2509 | * Determinism checks in case of counted or all transitions |
| 2510 | * will have to be handled separately |
| 2511 | */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2512 | if (t1->atom == NULL) { |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 2513 | /* t1->nd = 1; */ |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2514 | continue; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2515 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2516 | if (t1->to == -1) /* eliminated */ |
| 2517 | continue; |
| 2518 | for (i = 0;i < transnr;i++) { |
| 2519 | t2 = &(state->trans[i]); |
| 2520 | if (t2->to == -1) /* eliminated */ |
| 2521 | continue; |
| 2522 | if (t2->atom != NULL) { |
| 2523 | if (t1->to == t2->to) { |
Daniel Veillard | fc011b7 | 2006-02-12 19:14:15 +0000 | [diff] [blame] | 2524 | if (xmlFAEqualAtoms(t1->atom, t2->atom)) |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 2525 | t2->to = -1; /* eliminated */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2526 | } |
| 2527 | } |
| 2528 | } |
| 2529 | } |
| 2530 | } |
| 2531 | |
| 2532 | /* |
| 2533 | * Check for all states that there aren't 2 transitions |
| 2534 | * with the same atom and a different target. |
| 2535 | */ |
| 2536 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 2537 | state = ctxt->states[statenr]; |
| 2538 | if (state == NULL) |
| 2539 | continue; |
| 2540 | if (state->nbTrans < 2) |
| 2541 | continue; |
| 2542 | last = NULL; |
| 2543 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 2544 | t1 = &(state->trans[transnr]); |
| 2545 | /* |
| 2546 | * Determinism checks in case of counted or all transitions |
| 2547 | * will have to be handled separately |
| 2548 | */ |
| 2549 | if (t1->atom == NULL) { |
| 2550 | continue; |
| 2551 | } |
| 2552 | if (t1->to == -1) /* eliminated */ |
| 2553 | continue; |
| 2554 | for (i = 0;i < transnr;i++) { |
| 2555 | t2 = &(state->trans[i]); |
| 2556 | if (t2->to == -1) /* eliminated */ |
| 2557 | continue; |
| 2558 | if (t2->atom != NULL) { |
| 2559 | /* not determinist ! */ |
| 2560 | if (xmlFACompareAtoms(t1->atom, t2->atom)) { |
| 2561 | ret = 0; |
| 2562 | /* mark the transitions as non-deterministic ones */ |
| 2563 | t1->nd = 1; |
| 2564 | t2->nd = 1; |
| 2565 | last = t1; |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2566 | } |
| 2567 | } else if (t1->to != -1) { |
| 2568 | /* |
| 2569 | * do the closure in case of remaining specific |
| 2570 | * epsilon transitions like choices or all |
| 2571 | */ |
| 2572 | ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to], |
| 2573 | t2->to, t2->atom); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2574 | /* don't shortcut the computation so all non deterministic |
| 2575 | transition get marked down |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2576 | if (ret == 0) |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 2577 | return(0); |
| 2578 | */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2579 | if (ret == 0) { |
| 2580 | t1->nd = 1; |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 2581 | /* t2->nd = 1; */ |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2582 | last = t1; |
| 2583 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2584 | } |
| 2585 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2586 | /* don't shortcut the computation so all non deterministic |
| 2587 | transition get marked down |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2588 | if (ret == 0) |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2589 | break; */ |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2590 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2591 | |
| 2592 | /* |
| 2593 | * mark specifically the last non-deterministic transition |
| 2594 | * from a state since there is no need to set-up rollback |
| 2595 | * from it |
| 2596 | */ |
| 2597 | if (last != NULL) { |
| 2598 | last->nd = 2; |
| 2599 | } |
| 2600 | |
| 2601 | /* don't shortcut the computation so all non deterministic |
| 2602 | transition get marked down |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2603 | if (ret == 0) |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2604 | break; */ |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2605 | } |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 2606 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 2607 | ctxt->determinist = ret; |
| 2608 | return(ret); |
| 2609 | } |
| 2610 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2611 | /************************************************************************ |
| 2612 | * * |
| 2613 | * Routines to check input against transition atoms * |
| 2614 | * * |
| 2615 | ************************************************************************/ |
| 2616 | |
| 2617 | static int |
| 2618 | xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg, |
| 2619 | int start, int end, const xmlChar *blockName) { |
| 2620 | int ret = 0; |
| 2621 | |
| 2622 | switch (type) { |
| 2623 | case XML_REGEXP_STRING: |
| 2624 | case XML_REGEXP_SUBREG: |
| 2625 | case XML_REGEXP_RANGES: |
| 2626 | case XML_REGEXP_EPSILON: |
| 2627 | return(-1); |
| 2628 | case XML_REGEXP_ANYCHAR: |
| 2629 | ret = ((codepoint != '\n') && (codepoint != '\r')); |
| 2630 | break; |
| 2631 | case XML_REGEXP_CHARVAL: |
| 2632 | ret = ((codepoint >= start) && (codepoint <= end)); |
| 2633 | break; |
| 2634 | case XML_REGEXP_NOTSPACE: |
| 2635 | neg = !neg; |
| 2636 | case XML_REGEXP_ANYSPACE: |
| 2637 | ret = ((codepoint == '\n') || (codepoint == '\r') || |
| 2638 | (codepoint == '\t') || (codepoint == ' ')); |
| 2639 | break; |
| 2640 | case XML_REGEXP_NOTINITNAME: |
| 2641 | neg = !neg; |
| 2642 | case XML_REGEXP_INITNAME: |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2643 | ret = (IS_LETTER(codepoint) || |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2644 | (codepoint == '_') || (codepoint == ':')); |
| 2645 | break; |
| 2646 | case XML_REGEXP_NOTNAMECHAR: |
| 2647 | neg = !neg; |
| 2648 | case XML_REGEXP_NAMECHAR: |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2649 | ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) || |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2650 | (codepoint == '.') || (codepoint == '-') || |
| 2651 | (codepoint == '_') || (codepoint == ':') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2652 | IS_COMBINING(codepoint) || IS_EXTENDER(codepoint)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2653 | break; |
| 2654 | case XML_REGEXP_NOTDECIMAL: |
| 2655 | neg = !neg; |
| 2656 | case XML_REGEXP_DECIMAL: |
| 2657 | ret = xmlUCSIsCatNd(codepoint); |
| 2658 | break; |
| 2659 | case XML_REGEXP_REALCHAR: |
| 2660 | neg = !neg; |
| 2661 | case XML_REGEXP_NOTREALCHAR: |
| 2662 | ret = xmlUCSIsCatP(codepoint); |
| 2663 | if (ret == 0) |
| 2664 | ret = xmlUCSIsCatZ(codepoint); |
| 2665 | if (ret == 0) |
| 2666 | ret = xmlUCSIsCatC(codepoint); |
| 2667 | break; |
| 2668 | case XML_REGEXP_LETTER: |
| 2669 | ret = xmlUCSIsCatL(codepoint); |
| 2670 | break; |
| 2671 | case XML_REGEXP_LETTER_UPPERCASE: |
| 2672 | ret = xmlUCSIsCatLu(codepoint); |
| 2673 | break; |
| 2674 | case XML_REGEXP_LETTER_LOWERCASE: |
| 2675 | ret = xmlUCSIsCatLl(codepoint); |
| 2676 | break; |
| 2677 | case XML_REGEXP_LETTER_TITLECASE: |
| 2678 | ret = xmlUCSIsCatLt(codepoint); |
| 2679 | break; |
| 2680 | case XML_REGEXP_LETTER_MODIFIER: |
| 2681 | ret = xmlUCSIsCatLm(codepoint); |
| 2682 | break; |
| 2683 | case XML_REGEXP_LETTER_OTHERS: |
| 2684 | ret = xmlUCSIsCatLo(codepoint); |
| 2685 | break; |
| 2686 | case XML_REGEXP_MARK: |
| 2687 | ret = xmlUCSIsCatM(codepoint); |
| 2688 | break; |
| 2689 | case XML_REGEXP_MARK_NONSPACING: |
| 2690 | ret = xmlUCSIsCatMn(codepoint); |
| 2691 | break; |
| 2692 | case XML_REGEXP_MARK_SPACECOMBINING: |
| 2693 | ret = xmlUCSIsCatMc(codepoint); |
| 2694 | break; |
| 2695 | case XML_REGEXP_MARK_ENCLOSING: |
| 2696 | ret = xmlUCSIsCatMe(codepoint); |
| 2697 | break; |
| 2698 | case XML_REGEXP_NUMBER: |
| 2699 | ret = xmlUCSIsCatN(codepoint); |
| 2700 | break; |
| 2701 | case XML_REGEXP_NUMBER_DECIMAL: |
| 2702 | ret = xmlUCSIsCatNd(codepoint); |
| 2703 | break; |
| 2704 | case XML_REGEXP_NUMBER_LETTER: |
| 2705 | ret = xmlUCSIsCatNl(codepoint); |
| 2706 | break; |
| 2707 | case XML_REGEXP_NUMBER_OTHERS: |
| 2708 | ret = xmlUCSIsCatNo(codepoint); |
| 2709 | break; |
| 2710 | case XML_REGEXP_PUNCT: |
| 2711 | ret = xmlUCSIsCatP(codepoint); |
| 2712 | break; |
| 2713 | case XML_REGEXP_PUNCT_CONNECTOR: |
| 2714 | ret = xmlUCSIsCatPc(codepoint); |
| 2715 | break; |
| 2716 | case XML_REGEXP_PUNCT_DASH: |
| 2717 | ret = xmlUCSIsCatPd(codepoint); |
| 2718 | break; |
| 2719 | case XML_REGEXP_PUNCT_OPEN: |
| 2720 | ret = xmlUCSIsCatPs(codepoint); |
| 2721 | break; |
| 2722 | case XML_REGEXP_PUNCT_CLOSE: |
| 2723 | ret = xmlUCSIsCatPe(codepoint); |
| 2724 | break; |
| 2725 | case XML_REGEXP_PUNCT_INITQUOTE: |
| 2726 | ret = xmlUCSIsCatPi(codepoint); |
| 2727 | break; |
| 2728 | case XML_REGEXP_PUNCT_FINQUOTE: |
| 2729 | ret = xmlUCSIsCatPf(codepoint); |
| 2730 | break; |
| 2731 | case XML_REGEXP_PUNCT_OTHERS: |
| 2732 | ret = xmlUCSIsCatPo(codepoint); |
| 2733 | break; |
| 2734 | case XML_REGEXP_SEPAR: |
| 2735 | ret = xmlUCSIsCatZ(codepoint); |
| 2736 | break; |
| 2737 | case XML_REGEXP_SEPAR_SPACE: |
| 2738 | ret = xmlUCSIsCatZs(codepoint); |
| 2739 | break; |
| 2740 | case XML_REGEXP_SEPAR_LINE: |
| 2741 | ret = xmlUCSIsCatZl(codepoint); |
| 2742 | break; |
| 2743 | case XML_REGEXP_SEPAR_PARA: |
| 2744 | ret = xmlUCSIsCatZp(codepoint); |
| 2745 | break; |
| 2746 | case XML_REGEXP_SYMBOL: |
| 2747 | ret = xmlUCSIsCatS(codepoint); |
| 2748 | break; |
| 2749 | case XML_REGEXP_SYMBOL_MATH: |
| 2750 | ret = xmlUCSIsCatSm(codepoint); |
| 2751 | break; |
| 2752 | case XML_REGEXP_SYMBOL_CURRENCY: |
| 2753 | ret = xmlUCSIsCatSc(codepoint); |
| 2754 | break; |
| 2755 | case XML_REGEXP_SYMBOL_MODIFIER: |
| 2756 | ret = xmlUCSIsCatSk(codepoint); |
| 2757 | break; |
| 2758 | case XML_REGEXP_SYMBOL_OTHERS: |
| 2759 | ret = xmlUCSIsCatSo(codepoint); |
| 2760 | break; |
| 2761 | case XML_REGEXP_OTHER: |
| 2762 | ret = xmlUCSIsCatC(codepoint); |
| 2763 | break; |
| 2764 | case XML_REGEXP_OTHER_CONTROL: |
| 2765 | ret = xmlUCSIsCatCc(codepoint); |
| 2766 | break; |
| 2767 | case XML_REGEXP_OTHER_FORMAT: |
| 2768 | ret = xmlUCSIsCatCf(codepoint); |
| 2769 | break; |
| 2770 | case XML_REGEXP_OTHER_PRIVATE: |
| 2771 | ret = xmlUCSIsCatCo(codepoint); |
| 2772 | break; |
| 2773 | case XML_REGEXP_OTHER_NA: |
| 2774 | /* ret = xmlUCSIsCatCn(codepoint); */ |
| 2775 | /* Seems it doesn't exist anymore in recent Unicode releases */ |
| 2776 | ret = 0; |
| 2777 | break; |
| 2778 | case XML_REGEXP_BLOCK_NAME: |
| 2779 | ret = xmlUCSIsBlock(codepoint, (const char *) blockName); |
| 2780 | break; |
| 2781 | } |
| 2782 | if (neg) |
| 2783 | return(!ret); |
| 2784 | return(ret); |
| 2785 | } |
| 2786 | |
| 2787 | static int |
| 2788 | xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) { |
| 2789 | int i, ret = 0; |
| 2790 | xmlRegRangePtr range; |
| 2791 | |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2792 | if ((atom == NULL) || (!IS_CHAR(codepoint))) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2793 | return(-1); |
| 2794 | |
| 2795 | switch (atom->type) { |
| 2796 | case XML_REGEXP_SUBREG: |
| 2797 | case XML_REGEXP_EPSILON: |
| 2798 | return(-1); |
| 2799 | case XML_REGEXP_CHARVAL: |
| 2800 | return(codepoint == atom->codepoint); |
| 2801 | case XML_REGEXP_RANGES: { |
| 2802 | int accept = 0; |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 2803 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2804 | for (i = 0;i < atom->nbRanges;i++) { |
| 2805 | range = atom->ranges[i]; |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 2806 | if (range->neg == 2) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2807 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
| 2808 | 0, range->start, range->end, |
| 2809 | range->blockName); |
| 2810 | if (ret != 0) |
| 2811 | return(0); /* excluded char */ |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 2812 | } else if (range->neg) { |
| 2813 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
| 2814 | 0, range->start, range->end, |
| 2815 | range->blockName); |
| 2816 | if (ret == 0) |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 2817 | accept = 1; |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 2818 | else |
| 2819 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2820 | } else { |
| 2821 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
| 2822 | 0, range->start, range->end, |
| 2823 | range->blockName); |
| 2824 | if (ret != 0) |
| 2825 | accept = 1; /* might still be excluded */ |
| 2826 | } |
| 2827 | } |
| 2828 | return(accept); |
| 2829 | } |
| 2830 | case XML_REGEXP_STRING: |
| 2831 | printf("TODO: XML_REGEXP_STRING\n"); |
| 2832 | return(-1); |
| 2833 | case XML_REGEXP_ANYCHAR: |
| 2834 | case XML_REGEXP_ANYSPACE: |
| 2835 | case XML_REGEXP_NOTSPACE: |
| 2836 | case XML_REGEXP_INITNAME: |
| 2837 | case XML_REGEXP_NOTINITNAME: |
| 2838 | case XML_REGEXP_NAMECHAR: |
| 2839 | case XML_REGEXP_NOTNAMECHAR: |
| 2840 | case XML_REGEXP_DECIMAL: |
| 2841 | case XML_REGEXP_NOTDECIMAL: |
| 2842 | case XML_REGEXP_REALCHAR: |
| 2843 | case XML_REGEXP_NOTREALCHAR: |
| 2844 | case XML_REGEXP_LETTER: |
| 2845 | case XML_REGEXP_LETTER_UPPERCASE: |
| 2846 | case XML_REGEXP_LETTER_LOWERCASE: |
| 2847 | case XML_REGEXP_LETTER_TITLECASE: |
| 2848 | case XML_REGEXP_LETTER_MODIFIER: |
| 2849 | case XML_REGEXP_LETTER_OTHERS: |
| 2850 | case XML_REGEXP_MARK: |
| 2851 | case XML_REGEXP_MARK_NONSPACING: |
| 2852 | case XML_REGEXP_MARK_SPACECOMBINING: |
| 2853 | case XML_REGEXP_MARK_ENCLOSING: |
| 2854 | case XML_REGEXP_NUMBER: |
| 2855 | case XML_REGEXP_NUMBER_DECIMAL: |
| 2856 | case XML_REGEXP_NUMBER_LETTER: |
| 2857 | case XML_REGEXP_NUMBER_OTHERS: |
| 2858 | case XML_REGEXP_PUNCT: |
| 2859 | case XML_REGEXP_PUNCT_CONNECTOR: |
| 2860 | case XML_REGEXP_PUNCT_DASH: |
| 2861 | case XML_REGEXP_PUNCT_OPEN: |
| 2862 | case XML_REGEXP_PUNCT_CLOSE: |
| 2863 | case XML_REGEXP_PUNCT_INITQUOTE: |
| 2864 | case XML_REGEXP_PUNCT_FINQUOTE: |
| 2865 | case XML_REGEXP_PUNCT_OTHERS: |
| 2866 | case XML_REGEXP_SEPAR: |
| 2867 | case XML_REGEXP_SEPAR_SPACE: |
| 2868 | case XML_REGEXP_SEPAR_LINE: |
| 2869 | case XML_REGEXP_SEPAR_PARA: |
| 2870 | case XML_REGEXP_SYMBOL: |
| 2871 | case XML_REGEXP_SYMBOL_MATH: |
| 2872 | case XML_REGEXP_SYMBOL_CURRENCY: |
| 2873 | case XML_REGEXP_SYMBOL_MODIFIER: |
| 2874 | case XML_REGEXP_SYMBOL_OTHERS: |
| 2875 | case XML_REGEXP_OTHER: |
| 2876 | case XML_REGEXP_OTHER_CONTROL: |
| 2877 | case XML_REGEXP_OTHER_FORMAT: |
| 2878 | case XML_REGEXP_OTHER_PRIVATE: |
| 2879 | case XML_REGEXP_OTHER_NA: |
| 2880 | case XML_REGEXP_BLOCK_NAME: |
| 2881 | ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0, |
| 2882 | (const xmlChar *)atom->valuep); |
| 2883 | if (atom->neg) |
| 2884 | ret = !ret; |
| 2885 | break; |
| 2886 | } |
| 2887 | return(ret); |
| 2888 | } |
| 2889 | |
| 2890 | /************************************************************************ |
| 2891 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 2892 | * Saving and restoring state of an execution context * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2893 | * * |
| 2894 | ************************************************************************/ |
| 2895 | |
| 2896 | #ifdef DEBUG_REGEXP_EXEC |
| 2897 | static void |
| 2898 | xmlFARegDebugExec(xmlRegExecCtxtPtr exec) { |
| 2899 | printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index); |
| 2900 | if (exec->inputStack != NULL) { |
| 2901 | int i; |
| 2902 | printf(": "); |
| 2903 | for (i = 0;(i < 3) && (i < exec->inputStackNr);i++) |
Daniel Veillard | 0e05f4c | 2006-11-01 15:33:04 +0000 | [diff] [blame^] | 2904 | printf("%s ", (const char *) |
| 2905 | exec->inputStack[exec->inputStackNr - (i + 1)].value); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2906 | } else { |
| 2907 | printf(": %s", &(exec->inputString[exec->index])); |
| 2908 | } |
| 2909 | printf("\n"); |
| 2910 | } |
| 2911 | #endif |
| 2912 | |
| 2913 | static void |
| 2914 | xmlFARegExecSave(xmlRegExecCtxtPtr exec) { |
| 2915 | #ifdef DEBUG_REGEXP_EXEC |
| 2916 | printf("saving "); |
| 2917 | exec->transno++; |
| 2918 | xmlFARegDebugExec(exec); |
| 2919 | exec->transno--; |
| 2920 | #endif |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 2921 | #ifdef MAX_PUSH |
| 2922 | if (exec->nbPush > MAX_PUSH) { |
| 2923 | return; |
| 2924 | } |
| 2925 | exec->nbPush++; |
| 2926 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2927 | |
| 2928 | if (exec->maxRollbacks == 0) { |
| 2929 | exec->maxRollbacks = 4; |
| 2930 | exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks * |
| 2931 | sizeof(xmlRegExecRollback)); |
| 2932 | if (exec->rollbacks == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2933 | xmlRegexpErrMemory(NULL, "saving regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2934 | exec->maxRollbacks = 0; |
| 2935 | return; |
| 2936 | } |
| 2937 | memset(exec->rollbacks, 0, |
| 2938 | exec->maxRollbacks * sizeof(xmlRegExecRollback)); |
| 2939 | } else if (exec->nbRollbacks >= exec->maxRollbacks) { |
| 2940 | xmlRegExecRollback *tmp; |
| 2941 | int len = exec->maxRollbacks; |
| 2942 | |
| 2943 | exec->maxRollbacks *= 2; |
| 2944 | tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks, |
| 2945 | exec->maxRollbacks * sizeof(xmlRegExecRollback)); |
| 2946 | if (tmp == 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->maxRollbacks /= 2; |
| 2949 | return; |
| 2950 | } |
| 2951 | exec->rollbacks = tmp; |
| 2952 | tmp = &exec->rollbacks[len]; |
| 2953 | memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback)); |
| 2954 | } |
| 2955 | exec->rollbacks[exec->nbRollbacks].state = exec->state; |
| 2956 | exec->rollbacks[exec->nbRollbacks].index = exec->index; |
| 2957 | exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1; |
| 2958 | if (exec->comp->nbCounters > 0) { |
| 2959 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
| 2960 | exec->rollbacks[exec->nbRollbacks].counts = (int *) |
| 2961 | xmlMalloc(exec->comp->nbCounters * sizeof(int)); |
| 2962 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2963 | xmlRegexpErrMemory(NULL, "saving regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2964 | exec->status = -5; |
| 2965 | return; |
| 2966 | } |
| 2967 | } |
| 2968 | memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts, |
| 2969 | exec->comp->nbCounters * sizeof(int)); |
| 2970 | } |
| 2971 | exec->nbRollbacks++; |
| 2972 | } |
| 2973 | |
| 2974 | static void |
| 2975 | xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) { |
| 2976 | if (exec->nbRollbacks <= 0) { |
| 2977 | exec->status = -1; |
| 2978 | #ifdef DEBUG_REGEXP_EXEC |
| 2979 | printf("rollback failed on empty stack\n"); |
| 2980 | #endif |
| 2981 | return; |
| 2982 | } |
| 2983 | exec->nbRollbacks--; |
| 2984 | exec->state = exec->rollbacks[exec->nbRollbacks].state; |
| 2985 | exec->index = exec->rollbacks[exec->nbRollbacks].index; |
| 2986 | exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch; |
| 2987 | if (exec->comp->nbCounters > 0) { |
| 2988 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
| 2989 | fprintf(stderr, "exec save: allocation failed"); |
| 2990 | exec->status = -6; |
| 2991 | return; |
| 2992 | } |
| 2993 | memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts, |
| 2994 | exec->comp->nbCounters * sizeof(int)); |
| 2995 | } |
| 2996 | |
| 2997 | #ifdef DEBUG_REGEXP_EXEC |
| 2998 | printf("restored "); |
| 2999 | xmlFARegDebugExec(exec); |
| 3000 | #endif |
| 3001 | } |
| 3002 | |
| 3003 | /************************************************************************ |
| 3004 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 3005 | * Verifier, running an input against a compiled regexp * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3006 | * * |
| 3007 | ************************************************************************/ |
| 3008 | |
| 3009 | static int |
| 3010 | xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) { |
| 3011 | xmlRegExecCtxt execval; |
| 3012 | xmlRegExecCtxtPtr exec = &execval; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 3013 | int ret, codepoint = 0, len, deter; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3014 | |
| 3015 | exec->inputString = content; |
| 3016 | exec->index = 0; |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 3017 | exec->nbPush = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3018 | exec->determinist = 1; |
| 3019 | exec->maxRollbacks = 0; |
| 3020 | exec->nbRollbacks = 0; |
| 3021 | exec->rollbacks = NULL; |
| 3022 | exec->status = 0; |
| 3023 | exec->comp = comp; |
| 3024 | exec->state = comp->states[0]; |
| 3025 | exec->transno = 0; |
| 3026 | exec->transcount = 0; |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 3027 | exec->inputStack = NULL; |
| 3028 | exec->inputStackMax = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3029 | if (comp->nbCounters > 0) { |
| 3030 | exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)); |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3031 | if (exec->counts == NULL) { |
| 3032 | xmlRegexpErrMemory(NULL, "running regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3033 | return(-1); |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3034 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3035 | memset(exec->counts, 0, comp->nbCounters * sizeof(int)); |
| 3036 | } else |
| 3037 | exec->counts = NULL; |
| 3038 | while ((exec->status == 0) && |
| 3039 | ((exec->inputString[exec->index] != 0) || |
| 3040 | (exec->state->type != XML_REGEXP_FINAL_STATE))) { |
| 3041 | xmlRegTransPtr trans; |
| 3042 | xmlRegAtomPtr atom; |
| 3043 | |
| 3044 | /* |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3045 | * If end of input on non-terminal state, rollback, however we may |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3046 | * still have epsilon like transition for counted transitions |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3047 | * on counters, in that case don't break too early. Additionally, |
| 3048 | * if we are working on a range like "AB{0,2}", where B is not present, |
| 3049 | * we don't want to break. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3050 | */ |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3051 | len = 1; |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3052 | if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) { |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 3053 | /* |
| 3054 | * if there is a transition, we must check if |
| 3055 | * atom allows minOccurs of 0 |
| 3056 | */ |
| 3057 | if (exec->transno < exec->state->nbTrans) { |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3058 | trans = &exec->state->trans[exec->transno]; |
| 3059 | if (trans->to >=0) { |
| 3060 | atom = trans->atom; |
| 3061 | if (!((atom->min == 0) && (atom->max > 0))) |
| 3062 | goto rollback; |
| 3063 | } |
| 3064 | } else |
| 3065 | goto rollback; |
| 3066 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3067 | |
| 3068 | exec->transcount = 0; |
| 3069 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
| 3070 | trans = &exec->state->trans[exec->transno]; |
| 3071 | if (trans->to < 0) |
| 3072 | continue; |
| 3073 | atom = trans->atom; |
| 3074 | ret = 0; |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 3075 | deter = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3076 | if (trans->count >= 0) { |
| 3077 | int count; |
| 3078 | xmlRegCounterPtr counter; |
| 3079 | |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3080 | if (exec->counts == NULL) { |
| 3081 | exec->status = -1; |
| 3082 | goto error; |
| 3083 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3084 | /* |
| 3085 | * A counted transition. |
| 3086 | */ |
| 3087 | |
| 3088 | count = exec->counts[trans->count]; |
| 3089 | counter = &exec->comp->counters[trans->count]; |
| 3090 | #ifdef DEBUG_REGEXP_EXEC |
| 3091 | printf("testing count %d: val %d, min %d, max %d\n", |
| 3092 | trans->count, count, counter->min, counter->max); |
| 3093 | #endif |
| 3094 | ret = ((count >= counter->min) && (count <= counter->max)); |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 3095 | if ((ret) && (counter->min != counter->max)) |
| 3096 | deter = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3097 | } else if (atom == NULL) { |
| 3098 | fprintf(stderr, "epsilon transition left at runtime\n"); |
| 3099 | exec->status = -2; |
| 3100 | break; |
| 3101 | } else if (exec->inputString[exec->index] != 0) { |
| 3102 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len); |
| 3103 | ret = xmlRegCheckCharacter(atom, codepoint); |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3104 | if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3105 | xmlRegStatePtr to = comp->states[trans->to]; |
| 3106 | |
| 3107 | /* |
| 3108 | * this is a multiple input sequence |
Daniel Veillard | fc6eca0 | 2005-11-01 15:24:02 +0000 | [diff] [blame] | 3109 | * If there is a counter associated increment it now. |
| 3110 | * before potentially saving and rollback |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3111 | */ |
Daniel Veillard | fc6eca0 | 2005-11-01 15:24:02 +0000 | [diff] [blame] | 3112 | if (trans->counter >= 0) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3113 | if (exec->counts == NULL) { |
| 3114 | exec->status = -1; |
| 3115 | goto error; |
| 3116 | } |
Daniel Veillard | fc6eca0 | 2005-11-01 15:24:02 +0000 | [diff] [blame] | 3117 | #ifdef DEBUG_REGEXP_EXEC |
| 3118 | printf("Increasing count %d\n", trans->counter); |
| 3119 | #endif |
| 3120 | exec->counts[trans->counter]++; |
| 3121 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3122 | if (exec->state->nbTrans > exec->transno + 1) { |
| 3123 | xmlFARegExecSave(exec); |
| 3124 | } |
| 3125 | exec->transcount = 1; |
| 3126 | do { |
| 3127 | /* |
| 3128 | * Try to progress as much as possible on the input |
| 3129 | */ |
| 3130 | if (exec->transcount == atom->max) { |
| 3131 | break; |
| 3132 | } |
| 3133 | exec->index += len; |
| 3134 | /* |
| 3135 | * End of input: stop here |
| 3136 | */ |
| 3137 | if (exec->inputString[exec->index] == 0) { |
| 3138 | exec->index -= len; |
| 3139 | break; |
| 3140 | } |
| 3141 | if (exec->transcount >= atom->min) { |
| 3142 | int transno = exec->transno; |
| 3143 | xmlRegStatePtr state = exec->state; |
| 3144 | |
| 3145 | /* |
| 3146 | * The transition is acceptable save it |
| 3147 | */ |
| 3148 | exec->transno = -1; /* trick */ |
| 3149 | exec->state = to; |
| 3150 | xmlFARegExecSave(exec); |
| 3151 | exec->transno = transno; |
| 3152 | exec->state = state; |
| 3153 | } |
| 3154 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), |
| 3155 | len); |
| 3156 | ret = xmlRegCheckCharacter(atom, codepoint); |
| 3157 | exec->transcount++; |
| 3158 | } while (ret == 1); |
| 3159 | if (exec->transcount < atom->min) |
| 3160 | ret = 0; |
| 3161 | |
| 3162 | /* |
| 3163 | * If the last check failed but one transition was found |
| 3164 | * possible, rollback |
| 3165 | */ |
| 3166 | if (ret < 0) |
| 3167 | ret = 0; |
| 3168 | if (ret == 0) { |
| 3169 | goto rollback; |
| 3170 | } |
Daniel Veillard | fc6eca0 | 2005-11-01 15:24:02 +0000 | [diff] [blame] | 3171 | if (trans->counter >= 0) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3172 | if (exec->counts == NULL) { |
| 3173 | exec->status = -1; |
| 3174 | goto error; |
| 3175 | } |
Daniel Veillard | fc6eca0 | 2005-11-01 15:24:02 +0000 | [diff] [blame] | 3176 | #ifdef DEBUG_REGEXP_EXEC |
| 3177 | printf("Decreasing count %d\n", trans->counter); |
| 3178 | #endif |
| 3179 | exec->counts[trans->counter]--; |
| 3180 | } |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3181 | } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) { |
| 3182 | /* |
| 3183 | * we don't match on the codepoint, but minOccurs of 0 |
| 3184 | * says that's ok. Setting len to 0 inhibits stepping |
| 3185 | * over the codepoint. |
| 3186 | */ |
| 3187 | exec->transcount = 1; |
| 3188 | len = 0; |
| 3189 | ret = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3190 | } |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 3191 | } else if ((atom->min == 0) && (atom->max > 0)) { |
| 3192 | /* another spot to match when minOccurs is 0 */ |
| 3193 | exec->transcount = 1; |
| 3194 | len = 0; |
| 3195 | ret = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3196 | } |
| 3197 | if (ret == 1) { |
Daniel Veillard | 567a45b | 2005-10-18 19:11:55 +0000 | [diff] [blame] | 3198 | if ((trans->nd == 1) || |
| 3199 | ((trans->count >= 0) && (deter == 0) && |
| 3200 | (exec->state->nbTrans > exec->transno + 1))) { |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 3201 | #ifdef DEBUG_REGEXP_EXEC |
| 3202 | if (trans->nd == 1) |
| 3203 | printf("Saving on nd transition atom %d for %c at %d\n", |
| 3204 | trans->atom->no, codepoint, exec->index); |
| 3205 | else |
| 3206 | printf("Saving on counted transition count %d for %c at %d\n", |
| 3207 | trans->count, codepoint, exec->index); |
| 3208 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3209 | xmlFARegExecSave(exec); |
| 3210 | } |
| 3211 | if (trans->counter >= 0) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3212 | if (exec->counts == NULL) { |
| 3213 | exec->status = -1; |
| 3214 | goto error; |
| 3215 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3216 | #ifdef DEBUG_REGEXP_EXEC |
| 3217 | printf("Increasing count %d\n", trans->counter); |
| 3218 | #endif |
| 3219 | exec->counts[trans->counter]++; |
| 3220 | } |
Daniel Veillard | 1075228 | 2005-08-08 13:05:13 +0000 | [diff] [blame] | 3221 | if ((trans->count >= 0) && |
| 3222 | (trans->count < REGEXP_ALL_COUNTER)) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3223 | if (exec->counts == NULL) { |
| 3224 | exec->status = -1; |
| 3225 | goto error; |
| 3226 | } |
Daniel Veillard | 1075228 | 2005-08-08 13:05:13 +0000 | [diff] [blame] | 3227 | #ifdef DEBUG_REGEXP_EXEC |
| 3228 | printf("resetting count %d on transition\n", |
| 3229 | trans->count); |
| 3230 | #endif |
| 3231 | exec->counts[trans->count] = 0; |
| 3232 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3233 | #ifdef DEBUG_REGEXP_EXEC |
| 3234 | printf("entering state %d\n", trans->to); |
| 3235 | #endif |
| 3236 | exec->state = comp->states[trans->to]; |
| 3237 | exec->transno = 0; |
| 3238 | if (trans->atom != NULL) { |
| 3239 | exec->index += len; |
| 3240 | } |
| 3241 | goto progress; |
| 3242 | } else if (ret < 0) { |
| 3243 | exec->status = -4; |
| 3244 | break; |
| 3245 | } |
| 3246 | } |
| 3247 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
| 3248 | rollback: |
| 3249 | /* |
| 3250 | * Failed to find a way out |
| 3251 | */ |
| 3252 | exec->determinist = 0; |
Daniel Veillard | aa62201 | 2005-10-20 15:55:25 +0000 | [diff] [blame] | 3253 | #ifdef DEBUG_REGEXP_EXEC |
| 3254 | printf("rollback from state %d on %d:%c\n", exec->state->no, |
| 3255 | codepoint,codepoint); |
| 3256 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3257 | xmlFARegExecRollBack(exec); |
| 3258 | } |
| 3259 | progress: |
| 3260 | continue; |
| 3261 | } |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3262 | error: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3263 | if (exec->rollbacks != NULL) { |
| 3264 | if (exec->counts != NULL) { |
| 3265 | int i; |
| 3266 | |
| 3267 | for (i = 0;i < exec->maxRollbacks;i++) |
| 3268 | if (exec->rollbacks[i].counts != NULL) |
| 3269 | xmlFree(exec->rollbacks[i].counts); |
| 3270 | } |
| 3271 | xmlFree(exec->rollbacks); |
| 3272 | } |
| 3273 | if (exec->counts != NULL) |
| 3274 | xmlFree(exec->counts); |
| 3275 | if (exec->status == 0) |
| 3276 | return(1); |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 3277 | if (exec->status == -1) { |
| 3278 | if (exec->nbPush > MAX_PUSH) |
| 3279 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3280 | return(0); |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 3281 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3282 | return(exec->status); |
| 3283 | } |
| 3284 | |
| 3285 | /************************************************************************ |
| 3286 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 3287 | * Progressive interface to the verifier one atom at a time * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3288 | * * |
| 3289 | ************************************************************************/ |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3290 | #ifdef DEBUG_ERR |
| 3291 | static void testerr(xmlRegExecCtxtPtr exec); |
| 3292 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3293 | |
| 3294 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3295 | * xmlRegNewExecCtxt: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3296 | * @comp: a precompiled regular expression |
| 3297 | * @callback: a callback function used for handling progresses in the |
| 3298 | * automata matching phase |
| 3299 | * @data: the context data associated to the callback in this context |
| 3300 | * |
| 3301 | * Build a context used for progressive evaluation of a regexp. |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 3302 | * |
| 3303 | * Returns the new context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3304 | */ |
| 3305 | xmlRegExecCtxtPtr |
| 3306 | xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) { |
| 3307 | xmlRegExecCtxtPtr exec; |
| 3308 | |
| 3309 | if (comp == NULL) |
| 3310 | return(NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 3311 | if ((comp->compact == NULL) && (comp->states == NULL)) |
| 3312 | return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3313 | exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt)); |
| 3314 | if (exec == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3315 | xmlRegexpErrMemory(NULL, "creating execution context"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3316 | return(NULL); |
| 3317 | } |
| 3318 | memset(exec, 0, sizeof(xmlRegExecCtxt)); |
| 3319 | exec->inputString = NULL; |
| 3320 | exec->index = 0; |
| 3321 | exec->determinist = 1; |
| 3322 | exec->maxRollbacks = 0; |
| 3323 | exec->nbRollbacks = 0; |
| 3324 | exec->rollbacks = NULL; |
| 3325 | exec->status = 0; |
| 3326 | exec->comp = comp; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3327 | if (comp->compact == NULL) |
| 3328 | exec->state = comp->states[0]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3329 | exec->transno = 0; |
| 3330 | exec->transcount = 0; |
| 3331 | exec->callback = callback; |
| 3332 | exec->data = data; |
| 3333 | if (comp->nbCounters > 0) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3334 | /* |
| 3335 | * For error handling, exec->counts is allocated twice the size |
| 3336 | * the second half is used to store the data in case of rollback |
| 3337 | */ |
| 3338 | exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int) |
| 3339 | * 2); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3340 | if (exec->counts == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3341 | xmlRegexpErrMemory(NULL, "creating execution context"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3342 | xmlFree(exec); |
| 3343 | return(NULL); |
| 3344 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3345 | memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2); |
| 3346 | exec->errCounts = &exec->counts[comp->nbCounters]; |
| 3347 | } else { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3348 | exec->counts = NULL; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3349 | exec->errCounts = NULL; |
| 3350 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3351 | exec->inputStackMax = 0; |
| 3352 | exec->inputStackNr = 0; |
| 3353 | exec->inputStack = NULL; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3354 | exec->errStateNo = -1; |
| 3355 | exec->errString = NULL; |
Daniel Veillard | 94cc103 | 2005-09-15 13:09:00 +0000 | [diff] [blame] | 3356 | exec->nbPush = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3357 | return(exec); |
| 3358 | } |
| 3359 | |
| 3360 | /** |
| 3361 | * xmlRegFreeExecCtxt: |
| 3362 | * @exec: a regular expression evaulation context |
| 3363 | * |
| 3364 | * Free the structures associated to a regular expression evaulation context. |
| 3365 | */ |
| 3366 | void |
| 3367 | xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) { |
| 3368 | if (exec == NULL) |
| 3369 | return; |
| 3370 | |
| 3371 | if (exec->rollbacks != NULL) { |
| 3372 | if (exec->counts != NULL) { |
| 3373 | int i; |
| 3374 | |
| 3375 | for (i = 0;i < exec->maxRollbacks;i++) |
| 3376 | if (exec->rollbacks[i].counts != NULL) |
| 3377 | xmlFree(exec->rollbacks[i].counts); |
| 3378 | } |
| 3379 | xmlFree(exec->rollbacks); |
| 3380 | } |
| 3381 | if (exec->counts != NULL) |
| 3382 | xmlFree(exec->counts); |
| 3383 | if (exec->inputStack != NULL) { |
| 3384 | int i; |
| 3385 | |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 3386 | for (i = 0;i < exec->inputStackNr;i++) { |
| 3387 | if (exec->inputStack[i].value != NULL) |
| 3388 | xmlFree(exec->inputStack[i].value); |
| 3389 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3390 | xmlFree(exec->inputStack); |
| 3391 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3392 | if (exec->errString != NULL) |
| 3393 | xmlFree(exec->errString); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3394 | xmlFree(exec); |
| 3395 | } |
| 3396 | |
| 3397 | static void |
| 3398 | xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value, |
| 3399 | void *data) { |
| 3400 | #ifdef DEBUG_PUSH |
| 3401 | printf("saving value: %d:%s\n", exec->inputStackNr, value); |
| 3402 | #endif |
| 3403 | if (exec->inputStackMax == 0) { |
| 3404 | exec->inputStackMax = 4; |
| 3405 | exec->inputStack = (xmlRegInputTokenPtr) |
| 3406 | xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken)); |
| 3407 | if (exec->inputStack == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3408 | xmlRegexpErrMemory(NULL, "pushing input string"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3409 | exec->inputStackMax = 0; |
| 3410 | return; |
| 3411 | } |
| 3412 | } else if (exec->inputStackNr + 1 >= exec->inputStackMax) { |
| 3413 | xmlRegInputTokenPtr tmp; |
| 3414 | |
| 3415 | exec->inputStackMax *= 2; |
| 3416 | tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack, |
| 3417 | exec->inputStackMax * sizeof(xmlRegInputToken)); |
| 3418 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 3419 | xmlRegexpErrMemory(NULL, "pushing input string"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3420 | exec->inputStackMax /= 2; |
| 3421 | return; |
| 3422 | } |
| 3423 | exec->inputStack = tmp; |
| 3424 | } |
| 3425 | exec->inputStack[exec->inputStackNr].value = xmlStrdup(value); |
| 3426 | exec->inputStack[exec->inputStackNr].data = data; |
| 3427 | exec->inputStackNr++; |
| 3428 | exec->inputStack[exec->inputStackNr].value = NULL; |
| 3429 | exec->inputStack[exec->inputStackNr].data = NULL; |
| 3430 | } |
| 3431 | |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3432 | /** |
| 3433 | * xmlRegStrEqualWildcard: |
| 3434 | * @expStr: the string to be evaluated |
| 3435 | * @valStr: the validation string |
| 3436 | * |
| 3437 | * Checks if both strings are equal or have the same content. "*" |
| 3438 | * can be used as a wildcard in @valStr; "|" is used as a seperator of |
| 3439 | * substrings in both @expStr and @valStr. |
| 3440 | * |
| 3441 | * Returns 1 if the comparison is satisfied and the number of substrings |
| 3442 | * is equal, 0 otherwise. |
| 3443 | */ |
| 3444 | |
| 3445 | static int |
| 3446 | xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) { |
| 3447 | if (expStr == valStr) return(1); |
| 3448 | if (expStr == NULL) return(0); |
| 3449 | if (valStr == NULL) return(0); |
| 3450 | do { |
| 3451 | /* |
| 3452 | * Eval if we have a wildcard for the current item. |
| 3453 | */ |
| 3454 | if (*expStr != *valStr) { |
Daniel Veillard | 4f82c8a | 2005-08-09 21:40:08 +0000 | [diff] [blame] | 3455 | /* if one of them starts with a wildcard make valStr be it */ |
| 3456 | if (*valStr == '*') { |
| 3457 | const xmlChar *tmp; |
| 3458 | |
| 3459 | tmp = valStr; |
| 3460 | valStr = expStr; |
| 3461 | expStr = tmp; |
| 3462 | } |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3463 | if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) { |
| 3464 | do { |
| 3465 | if (*valStr == XML_REG_STRING_SEPARATOR) |
| 3466 | break; |
Kasimier T. Buchcik | c0e833f | 2005-04-19 15:02:20 +0000 | [diff] [blame] | 3467 | valStr++; |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3468 | } while (*valStr != 0); |
| 3469 | continue; |
| 3470 | } else |
| 3471 | return(0); |
| 3472 | } |
Kasimier T. Buchcik | c0e833f | 2005-04-19 15:02:20 +0000 | [diff] [blame] | 3473 | expStr++; |
| 3474 | valStr++; |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3475 | } while (*valStr != 0); |
| 3476 | if (*expStr != 0) |
| 3477 | return (0); |
| 3478 | else |
| 3479 | return (1); |
| 3480 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3481 | |
| 3482 | /** |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3483 | * xmlRegCompactPushString: |
| 3484 | * @exec: a regexp execution context |
| 3485 | * @comp: the precompiled exec with a compact table |
| 3486 | * @value: a string token input |
| 3487 | * @data: data associated to the token to reuse in callbacks |
| 3488 | * |
| 3489 | * Push one input token in the execution context |
| 3490 | * |
| 3491 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
| 3492 | * a negative value in case of error. |
| 3493 | */ |
| 3494 | static int |
| 3495 | xmlRegCompactPushString(xmlRegExecCtxtPtr exec, |
| 3496 | xmlRegexpPtr comp, |
| 3497 | const xmlChar *value, |
| 3498 | void *data) { |
| 3499 | int state = exec->index; |
| 3500 | int i, target; |
| 3501 | |
| 3502 | if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL)) |
| 3503 | return(-1); |
| 3504 | |
| 3505 | if (value == NULL) { |
| 3506 | /* |
| 3507 | * are we at a final state ? |
| 3508 | */ |
| 3509 | if (comp->compact[state * (comp->nbstrings + 1)] == |
| 3510 | XML_REGEXP_FINAL_STATE) |
| 3511 | return(1); |
| 3512 | return(0); |
| 3513 | } |
| 3514 | |
| 3515 | #ifdef DEBUG_PUSH |
| 3516 | printf("value pushed: %s\n", value); |
| 3517 | #endif |
| 3518 | |
| 3519 | /* |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 3520 | * Examine all outside transitions from current state |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3521 | */ |
| 3522 | for (i = 0;i < comp->nbstrings;i++) { |
| 3523 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
| 3524 | if ((target > 0) && (target <= comp->nbstates)) { |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3525 | target--; /* to avoid 0 */ |
| 3526 | if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) { |
| 3527 | exec->index = target; |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 3528 | if ((exec->callback != NULL) && (comp->transdata != NULL)) { |
| 3529 | exec->callback(exec->data, value, |
| 3530 | comp->transdata[state * comp->nbstrings + i], data); |
| 3531 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3532 | #ifdef DEBUG_PUSH |
| 3533 | printf("entering state %d\n", target); |
| 3534 | #endif |
| 3535 | if (comp->compact[target * (comp->nbstrings + 1)] == |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3536 | XML_REGEXP_SINK_STATE) |
| 3537 | goto error; |
| 3538 | |
| 3539 | if (comp->compact[target * (comp->nbstrings + 1)] == |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3540 | XML_REGEXP_FINAL_STATE) |
| 3541 | return(1); |
| 3542 | return(0); |
| 3543 | } |
| 3544 | } |
| 3545 | } |
| 3546 | /* |
| 3547 | * Failed to find an exit transition out from current state for the |
| 3548 | * current token |
| 3549 | */ |
| 3550 | #ifdef DEBUG_PUSH |
| 3551 | printf("failed to find a transition for %s on state %d\n", value, state); |
| 3552 | #endif |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3553 | error: |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3554 | if (exec->errString != NULL) |
| 3555 | xmlFree(exec->errString); |
| 3556 | exec->errString = xmlStrdup(value); |
| 3557 | exec->errStateNo = state; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3558 | exec->status = -1; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3559 | #ifdef DEBUG_ERR |
| 3560 | testerr(exec); |
| 3561 | #endif |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3562 | return(-1); |
| 3563 | } |
| 3564 | |
| 3565 | /** |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3566 | * xmlRegExecPushStringInternal: |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 3567 | * @exec: a regexp execution context or NULL to indicate the end |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3568 | * @value: a string token input |
| 3569 | * @data: data associated to the token to reuse in callbacks |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3570 | * @compound: value was assembled from 2 strings |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3571 | * |
| 3572 | * Push one input token in the execution context |
| 3573 | * |
| 3574 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
| 3575 | * a negative value in case of error. |
| 3576 | */ |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3577 | static int |
| 3578 | xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value, |
| 3579 | void *data, int compound) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3580 | xmlRegTransPtr trans; |
| 3581 | xmlRegAtomPtr atom; |
| 3582 | int ret; |
| 3583 | int final = 0; |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3584 | int progress = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3585 | |
| 3586 | if (exec == NULL) |
| 3587 | return(-1); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3588 | if (exec->comp == NULL) |
| 3589 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3590 | if (exec->status != 0) |
| 3591 | return(exec->status); |
| 3592 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 3593 | if (exec->comp->compact != NULL) |
| 3594 | return(xmlRegCompactPushString(exec, exec->comp, value, data)); |
| 3595 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3596 | if (value == NULL) { |
| 3597 | if (exec->state->type == XML_REGEXP_FINAL_STATE) |
| 3598 | return(1); |
| 3599 | final = 1; |
| 3600 | } |
| 3601 | |
| 3602 | #ifdef DEBUG_PUSH |
| 3603 | printf("value pushed: %s\n", value); |
| 3604 | #endif |
| 3605 | /* |
| 3606 | * If we have an active rollback stack push the new value there |
| 3607 | * and get back to where we were left |
| 3608 | */ |
| 3609 | if ((value != NULL) && (exec->inputStackNr > 0)) { |
| 3610 | xmlFARegExecSaveInputString(exec, value, data); |
| 3611 | value = exec->inputStack[exec->index].value; |
| 3612 | data = exec->inputStack[exec->index].data; |
| 3613 | #ifdef DEBUG_PUSH |
| 3614 | printf("value loaded: %s\n", value); |
| 3615 | #endif |
| 3616 | } |
| 3617 | |
| 3618 | while ((exec->status == 0) && |
| 3619 | ((value != NULL) || |
| 3620 | ((final == 1) && |
| 3621 | (exec->state->type != XML_REGEXP_FINAL_STATE)))) { |
| 3622 | |
| 3623 | /* |
| 3624 | * End of input on non-terminal state, rollback, however we may |
| 3625 | * still have epsilon like transition for counted transitions |
| 3626 | * on counters, in that case don't break too early. |
| 3627 | */ |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 3628 | if ((value == NULL) && (exec->counts == NULL)) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3629 | goto rollback; |
| 3630 | |
| 3631 | exec->transcount = 0; |
| 3632 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
| 3633 | trans = &exec->state->trans[exec->transno]; |
| 3634 | if (trans->to < 0) |
| 3635 | continue; |
| 3636 | atom = trans->atom; |
| 3637 | ret = 0; |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 3638 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
| 3639 | int i; |
| 3640 | int count; |
| 3641 | xmlRegTransPtr t; |
| 3642 | xmlRegCounterPtr counter; |
| 3643 | |
| 3644 | ret = 0; |
| 3645 | |
| 3646 | #ifdef DEBUG_PUSH |
| 3647 | printf("testing all lax %d\n", trans->count); |
| 3648 | #endif |
| 3649 | /* |
| 3650 | * Check all counted transitions from the current state |
| 3651 | */ |
| 3652 | if ((value == NULL) && (final)) { |
| 3653 | ret = 1; |
| 3654 | } else if (value != NULL) { |
| 3655 | for (i = 0;i < exec->state->nbTrans;i++) { |
| 3656 | t = &exec->state->trans[i]; |
| 3657 | if ((t->counter < 0) || (t == trans)) |
| 3658 | continue; |
| 3659 | counter = &exec->comp->counters[t->counter]; |
| 3660 | count = exec->counts[t->counter]; |
| 3661 | if ((count < counter->max) && |
| 3662 | (t->atom != NULL) && |
| 3663 | (xmlStrEqual(value, t->atom->valuep))) { |
| 3664 | ret = 0; |
| 3665 | break; |
| 3666 | } |
| 3667 | if ((count >= counter->min) && |
| 3668 | (count < counter->max) && |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 3669 | (t->atom != NULL) && |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 3670 | (xmlStrEqual(value, t->atom->valuep))) { |
| 3671 | ret = 1; |
| 3672 | break; |
| 3673 | } |
| 3674 | } |
| 3675 | } |
| 3676 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
Daniel Veillard | 8a001f6 | 2002-04-20 07:24:11 +0000 | [diff] [blame] | 3677 | int i; |
| 3678 | int count; |
| 3679 | xmlRegTransPtr t; |
| 3680 | xmlRegCounterPtr counter; |
| 3681 | |
| 3682 | ret = 1; |
| 3683 | |
| 3684 | #ifdef DEBUG_PUSH |
| 3685 | printf("testing all %d\n", trans->count); |
| 3686 | #endif |
| 3687 | /* |
| 3688 | * Check all counted transitions from the current state |
| 3689 | */ |
| 3690 | for (i = 0;i < exec->state->nbTrans;i++) { |
| 3691 | t = &exec->state->trans[i]; |
| 3692 | if ((t->counter < 0) || (t == trans)) |
| 3693 | continue; |
| 3694 | counter = &exec->comp->counters[t->counter]; |
| 3695 | count = exec->counts[t->counter]; |
| 3696 | if ((count < counter->min) || (count > counter->max)) { |
| 3697 | ret = 0; |
| 3698 | break; |
| 3699 | } |
| 3700 | } |
| 3701 | } else if (trans->count >= 0) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3702 | int count; |
| 3703 | xmlRegCounterPtr counter; |
| 3704 | |
| 3705 | /* |
| 3706 | * A counted transition. |
| 3707 | */ |
| 3708 | |
| 3709 | count = exec->counts[trans->count]; |
| 3710 | counter = &exec->comp->counters[trans->count]; |
| 3711 | #ifdef DEBUG_PUSH |
| 3712 | printf("testing count %d: val %d, min %d, max %d\n", |
| 3713 | trans->count, count, counter->min, counter->max); |
| 3714 | #endif |
| 3715 | ret = ((count >= counter->min) && (count <= counter->max)); |
| 3716 | } else if (atom == NULL) { |
| 3717 | fprintf(stderr, "epsilon transition left at runtime\n"); |
| 3718 | exec->status = -2; |
| 3719 | break; |
| 3720 | } else if (value != NULL) { |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3721 | ret = xmlRegStrEqualWildcard(atom->valuep, value); |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3722 | if (atom->neg) { |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 3723 | ret = !ret; |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3724 | if (!compound) |
| 3725 | ret = 0; |
| 3726 | } |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 3727 | if ((ret == 1) && (trans->counter >= 0)) { |
| 3728 | xmlRegCounterPtr counter; |
| 3729 | int count; |
| 3730 | |
| 3731 | count = exec->counts[trans->counter]; |
| 3732 | counter = &exec->comp->counters[trans->counter]; |
| 3733 | if (count >= counter->max) |
| 3734 | ret = 0; |
| 3735 | } |
| 3736 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3737 | if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) { |
| 3738 | xmlRegStatePtr to = exec->comp->states[trans->to]; |
| 3739 | |
| 3740 | /* |
| 3741 | * this is a multiple input sequence |
| 3742 | */ |
| 3743 | if (exec->state->nbTrans > exec->transno + 1) { |
| 3744 | if (exec->inputStackNr <= 0) { |
| 3745 | xmlFARegExecSaveInputString(exec, value, data); |
| 3746 | } |
| 3747 | xmlFARegExecSave(exec); |
| 3748 | } |
| 3749 | exec->transcount = 1; |
| 3750 | do { |
| 3751 | /* |
| 3752 | * Try to progress as much as possible on the input |
| 3753 | */ |
| 3754 | if (exec->transcount == atom->max) { |
| 3755 | break; |
| 3756 | } |
| 3757 | exec->index++; |
| 3758 | value = exec->inputStack[exec->index].value; |
| 3759 | data = exec->inputStack[exec->index].data; |
| 3760 | #ifdef DEBUG_PUSH |
| 3761 | printf("value loaded: %s\n", value); |
| 3762 | #endif |
| 3763 | |
| 3764 | /* |
| 3765 | * End of input: stop here |
| 3766 | */ |
| 3767 | if (value == NULL) { |
| 3768 | exec->index --; |
| 3769 | break; |
| 3770 | } |
| 3771 | if (exec->transcount >= atom->min) { |
| 3772 | int transno = exec->transno; |
| 3773 | xmlRegStatePtr state = exec->state; |
| 3774 | |
| 3775 | /* |
| 3776 | * The transition is acceptable save it |
| 3777 | */ |
| 3778 | exec->transno = -1; /* trick */ |
| 3779 | exec->state = to; |
| 3780 | if (exec->inputStackNr <= 0) { |
| 3781 | xmlFARegExecSaveInputString(exec, value, data); |
| 3782 | } |
| 3783 | xmlFARegExecSave(exec); |
| 3784 | exec->transno = transno; |
| 3785 | exec->state = state; |
| 3786 | } |
| 3787 | ret = xmlStrEqual(value, atom->valuep); |
| 3788 | exec->transcount++; |
| 3789 | } while (ret == 1); |
| 3790 | if (exec->transcount < atom->min) |
| 3791 | ret = 0; |
| 3792 | |
| 3793 | /* |
| 3794 | * If the last check failed but one transition was found |
| 3795 | * possible, rollback |
| 3796 | */ |
| 3797 | if (ret < 0) |
| 3798 | ret = 0; |
| 3799 | if (ret == 0) { |
| 3800 | goto rollback; |
| 3801 | } |
| 3802 | } |
| 3803 | } |
| 3804 | if (ret == 1) { |
William M. Brack | 9887395 | 2003-12-26 06:03:14 +0000 | [diff] [blame] | 3805 | if ((exec->callback != NULL) && (atom != NULL) && |
| 3806 | (data != NULL)) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3807 | exec->callback(exec->data, atom->valuep, |
| 3808 | atom->data, data); |
| 3809 | } |
| 3810 | if (exec->state->nbTrans > exec->transno + 1) { |
| 3811 | if (exec->inputStackNr <= 0) { |
| 3812 | xmlFARegExecSaveInputString(exec, value, data); |
| 3813 | } |
| 3814 | xmlFARegExecSave(exec); |
| 3815 | } |
| 3816 | if (trans->counter >= 0) { |
| 3817 | #ifdef DEBUG_PUSH |
| 3818 | printf("Increasing count %d\n", trans->counter); |
| 3819 | #endif |
| 3820 | exec->counts[trans->counter]++; |
| 3821 | } |
Daniel Veillard | 1075228 | 2005-08-08 13:05:13 +0000 | [diff] [blame] | 3822 | if ((trans->count >= 0) && |
| 3823 | (trans->count < REGEXP_ALL_COUNTER)) { |
| 3824 | #ifdef DEBUG_REGEXP_EXEC |
| 3825 | printf("resetting count %d on transition\n", |
| 3826 | trans->count); |
| 3827 | #endif |
| 3828 | exec->counts[trans->count] = 0; |
| 3829 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3830 | #ifdef DEBUG_PUSH |
| 3831 | printf("entering state %d\n", trans->to); |
| 3832 | #endif |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3833 | if ((exec->comp->states[trans->to] != NULL) && |
| 3834 | (exec->comp->states[trans->to]->type == |
| 3835 | XML_REGEXP_SINK_STATE)) { |
| 3836 | /* |
| 3837 | * entering a sink state, save the current state as error |
| 3838 | * state. |
| 3839 | */ |
| 3840 | if (exec->errString != NULL) |
| 3841 | xmlFree(exec->errString); |
| 3842 | exec->errString = xmlStrdup(value); |
| 3843 | exec->errState = exec->state; |
| 3844 | memcpy(exec->errCounts, exec->counts, |
| 3845 | exec->comp->nbCounters * sizeof(int)); |
| 3846 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3847 | exec->state = exec->comp->states[trans->to]; |
| 3848 | exec->transno = 0; |
| 3849 | if (trans->atom != NULL) { |
| 3850 | if (exec->inputStack != NULL) { |
| 3851 | exec->index++; |
| 3852 | if (exec->index < exec->inputStackNr) { |
| 3853 | value = exec->inputStack[exec->index].value; |
| 3854 | data = exec->inputStack[exec->index].data; |
| 3855 | #ifdef DEBUG_PUSH |
| 3856 | printf("value loaded: %s\n", value); |
| 3857 | #endif |
| 3858 | } else { |
| 3859 | value = NULL; |
| 3860 | data = NULL; |
| 3861 | #ifdef DEBUG_PUSH |
| 3862 | printf("end of input\n"); |
| 3863 | #endif |
| 3864 | } |
| 3865 | } else { |
| 3866 | value = NULL; |
| 3867 | data = NULL; |
| 3868 | #ifdef DEBUG_PUSH |
| 3869 | printf("end of input\n"); |
| 3870 | #endif |
| 3871 | } |
| 3872 | } |
| 3873 | goto progress; |
| 3874 | } else if (ret < 0) { |
| 3875 | exec->status = -4; |
| 3876 | break; |
| 3877 | } |
| 3878 | } |
| 3879 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
| 3880 | rollback: |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3881 | /* |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3882 | * if we didn't yet rollback on the current input |
| 3883 | * store the current state as the error state. |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3884 | */ |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3885 | if ((progress) && (exec->state != NULL) && |
| 3886 | (exec->state->type != XML_REGEXP_SINK_STATE)) { |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3887 | progress = 0; |
| 3888 | if (exec->errString != NULL) |
| 3889 | xmlFree(exec->errString); |
| 3890 | exec->errString = xmlStrdup(value); |
| 3891 | exec->errState = exec->state; |
| 3892 | memcpy(exec->errCounts, exec->counts, |
| 3893 | exec->comp->nbCounters * sizeof(int)); |
| 3894 | } |
| 3895 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3896 | /* |
| 3897 | * Failed to find a way out |
| 3898 | */ |
| 3899 | exec->determinist = 0; |
| 3900 | xmlFARegExecRollBack(exec); |
| 3901 | if (exec->status == 0) { |
| 3902 | value = exec->inputStack[exec->index].value; |
| 3903 | data = exec->inputStack[exec->index].data; |
| 3904 | #ifdef DEBUG_PUSH |
| 3905 | printf("value loaded: %s\n", value); |
| 3906 | #endif |
| 3907 | } |
| 3908 | } |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3909 | continue; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3910 | progress: |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3911 | progress = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3912 | continue; |
| 3913 | } |
| 3914 | if (exec->status == 0) { |
| 3915 | return(exec->state->type == XML_REGEXP_FINAL_STATE); |
| 3916 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3917 | #ifdef DEBUG_ERR |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3918 | if (exec->status < 0) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3919 | testerr(exec); |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3920 | } |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3921 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3922 | return(exec->status); |
| 3923 | } |
| 3924 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3925 | /** |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3926 | * xmlRegExecPushString: |
| 3927 | * @exec: a regexp execution context or NULL to indicate the end |
| 3928 | * @value: a string token input |
| 3929 | * @data: data associated to the token to reuse in callbacks |
| 3930 | * |
| 3931 | * Push one input token in the execution context |
| 3932 | * |
| 3933 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
| 3934 | * a negative value in case of error. |
| 3935 | */ |
| 3936 | int |
| 3937 | xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value, |
| 3938 | void *data) { |
| 3939 | return(xmlRegExecPushStringInternal(exec, value, data, 0)); |
| 3940 | } |
| 3941 | |
| 3942 | /** |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3943 | * xmlRegExecPushString2: |
| 3944 | * @exec: a regexp execution context or NULL to indicate the end |
| 3945 | * @value: the first string token input |
| 3946 | * @value2: the second string token input |
| 3947 | * @data: data associated to the token to reuse in callbacks |
| 3948 | * |
| 3949 | * Push one input token in the execution context |
| 3950 | * |
| 3951 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
| 3952 | * a negative value in case of error. |
| 3953 | */ |
| 3954 | int |
| 3955 | xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value, |
| 3956 | const xmlChar *value2, void *data) { |
| 3957 | xmlChar buf[150]; |
| 3958 | int lenn, lenp, ret; |
| 3959 | xmlChar *str; |
| 3960 | |
| 3961 | if (exec == NULL) |
| 3962 | return(-1); |
| 3963 | if (exec->comp == NULL) |
| 3964 | return(-1); |
| 3965 | if (exec->status != 0) |
| 3966 | return(exec->status); |
| 3967 | |
| 3968 | if (value2 == NULL) |
| 3969 | return(xmlRegExecPushString(exec, value, data)); |
| 3970 | |
| 3971 | lenn = strlen((char *) value2); |
| 3972 | lenp = strlen((char *) value); |
| 3973 | |
| 3974 | if (150 < lenn + lenp + 2) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3975 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3976 | if (str == NULL) { |
| 3977 | exec->status = -1; |
| 3978 | return(-1); |
| 3979 | } |
| 3980 | } else { |
| 3981 | str = buf; |
| 3982 | } |
| 3983 | memcpy(&str[0], value, lenp); |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3984 | str[lenp] = XML_REG_STRING_SEPARATOR; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3985 | memcpy(&str[lenp + 1], value2, lenn); |
| 3986 | str[lenn + lenp + 1] = 0; |
| 3987 | |
| 3988 | if (exec->comp->compact != NULL) |
| 3989 | ret = xmlRegCompactPushString(exec, exec->comp, str, data); |
| 3990 | else |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 3991 | ret = xmlRegExecPushStringInternal(exec, str, data, 1); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3992 | |
| 3993 | if (str != buf) |
Daniel Veillard | 0b1ff14 | 2005-12-28 21:13:33 +0000 | [diff] [blame] | 3994 | xmlFree(str); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3995 | return(ret); |
| 3996 | } |
| 3997 | |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3998 | /** |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 3999 | * xmlRegExecGetValues: |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4000 | * @exec: a regexp execution context |
| 4001 | * @err: error extraction or normal one |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4002 | * @nbval: pointer to the number of accepted values IN/OUT |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4003 | * @nbneg: return number of negative transitions |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4004 | * @values: pointer to the array of acceptable values |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4005 | * @terminal: return value if this was a terminal state |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4006 | * |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4007 | * Extract informations from the regexp execution, internal routine to |
| 4008 | * implement xmlRegExecNextValues() and xmlRegExecErrInfo() |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4009 | * |
| 4010 | * Returns: 0 in case of success or -1 in case of error. |
| 4011 | */ |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4012 | static int |
| 4013 | xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err, |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4014 | int *nbval, int *nbneg, |
| 4015 | xmlChar **values, int *terminal) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4016 | int maxval; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4017 | int nb = 0; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4018 | |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4019 | if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) || |
| 4020 | (values == NULL) || (*nbval <= 0)) |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4021 | return(-1); |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4022 | |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4023 | maxval = *nbval; |
| 4024 | *nbval = 0; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4025 | *nbneg = 0; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4026 | if ((exec->comp != NULL) && (exec->comp->compact != NULL)) { |
| 4027 | xmlRegexpPtr comp; |
| 4028 | int target, i, state; |
| 4029 | |
| 4030 | comp = exec->comp; |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4031 | |
| 4032 | if (err) { |
| 4033 | if (exec->errStateNo == -1) return(-1); |
| 4034 | state = exec->errStateNo; |
| 4035 | } else { |
| 4036 | state = exec->index; |
| 4037 | } |
| 4038 | if (terminal != NULL) { |
| 4039 | if (comp->compact[state * (comp->nbstrings + 1)] == |
| 4040 | XML_REGEXP_FINAL_STATE) |
| 4041 | *terminal = 1; |
| 4042 | else |
| 4043 | *terminal = 0; |
| 4044 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4045 | for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4046 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4047 | if ((target > 0) && (target <= comp->nbstates) && |
| 4048 | (comp->compact[(target - 1) * (comp->nbstrings + 1)] != |
| 4049 | XML_REGEXP_SINK_STATE)) { |
| 4050 | values[nb++] = comp->stringMap[i]; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4051 | (*nbval)++; |
| 4052 | } |
| 4053 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4054 | for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) { |
| 4055 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
| 4056 | if ((target > 0) && (target <= comp->nbstates) && |
| 4057 | (comp->compact[(target - 1) * (comp->nbstrings + 1)] == |
| 4058 | XML_REGEXP_SINK_STATE)) { |
| 4059 | values[nb++] = comp->stringMap[i]; |
| 4060 | (*nbneg)++; |
| 4061 | } |
| 4062 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4063 | } else { |
| 4064 | int transno; |
| 4065 | xmlRegTransPtr trans; |
| 4066 | xmlRegAtomPtr atom; |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4067 | xmlRegStatePtr state; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4068 | |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4069 | if (terminal != NULL) { |
| 4070 | if (exec->state->type == XML_REGEXP_FINAL_STATE) |
| 4071 | *terminal = 1; |
| 4072 | else |
| 4073 | *terminal = 0; |
| 4074 | } |
| 4075 | |
| 4076 | if (err) { |
| 4077 | if (exec->errState == NULL) return(-1); |
| 4078 | state = exec->errState; |
| 4079 | } else { |
| 4080 | if (exec->state == NULL) return(-1); |
| 4081 | state = exec->state; |
| 4082 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4083 | for (transno = 0; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4084 | (transno < state->nbTrans) && (nb < maxval); |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4085 | transno++) { |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4086 | trans = &state->trans[transno]; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4087 | if (trans->to < 0) |
| 4088 | continue; |
| 4089 | atom = trans->atom; |
| 4090 | if ((atom == NULL) || (atom->valuep == NULL)) |
| 4091 | continue; |
| 4092 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4093 | /* this should not be reached but ... */ |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4094 | TODO; |
| 4095 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4096 | /* this should not be reached but ... */ |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4097 | TODO; |
| 4098 | } else if (trans->counter >= 0) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 4099 | xmlRegCounterPtr counter = NULL; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4100 | int count; |
| 4101 | |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4102 | if (err) |
| 4103 | count = exec->errCounts[trans->counter]; |
| 4104 | else |
| 4105 | count = exec->counts[trans->counter]; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 4106 | if (exec->comp != NULL) |
| 4107 | counter = &exec->comp->counters[trans->counter]; |
| 4108 | if ((counter == NULL) || (count < counter->max)) { |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 4109 | if (atom->neg) |
| 4110 | values[nb++] = (xmlChar *) atom->valuep2; |
| 4111 | else |
| 4112 | values[nb++] = (xmlChar *) atom->valuep; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4113 | (*nbval)++; |
| 4114 | } |
| 4115 | } else { |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4116 | if ((exec->comp->states[trans->to] != NULL) && |
| 4117 | (exec->comp->states[trans->to]->type != |
| 4118 | XML_REGEXP_SINK_STATE)) { |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 4119 | if (atom->neg) |
| 4120 | values[nb++] = (xmlChar *) atom->valuep2; |
| 4121 | else |
| 4122 | values[nb++] = (xmlChar *) atom->valuep; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4123 | (*nbval)++; |
| 4124 | } |
| 4125 | } |
| 4126 | } |
| 4127 | for (transno = 0; |
| 4128 | (transno < state->nbTrans) && (nb < maxval); |
| 4129 | transno++) { |
| 4130 | trans = &state->trans[transno]; |
| 4131 | if (trans->to < 0) |
| 4132 | continue; |
| 4133 | atom = trans->atom; |
| 4134 | if ((atom == NULL) || (atom->valuep == NULL)) |
| 4135 | continue; |
| 4136 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
| 4137 | continue; |
| 4138 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
| 4139 | continue; |
| 4140 | } else if (trans->counter >= 0) { |
| 4141 | continue; |
| 4142 | } else { |
| 4143 | if ((exec->comp->states[trans->to] != NULL) && |
| 4144 | (exec->comp->states[trans->to]->type == |
| 4145 | XML_REGEXP_SINK_STATE)) { |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 4146 | if (atom->neg) |
| 4147 | values[nb++] = (xmlChar *) atom->valuep2; |
| 4148 | else |
| 4149 | values[nb++] = (xmlChar *) atom->valuep; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4150 | (*nbneg)++; |
| 4151 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4152 | } |
| 4153 | } |
| 4154 | } |
| 4155 | return(0); |
| 4156 | } |
| 4157 | |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4158 | /** |
| 4159 | * xmlRegExecNextValues: |
| 4160 | * @exec: a regexp execution context |
| 4161 | * @nbval: pointer to the number of accepted values IN/OUT |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4162 | * @nbneg: return number of negative transitions |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4163 | * @values: pointer to the array of acceptable values |
| 4164 | * @terminal: return value if this was a terminal state |
| 4165 | * |
| 4166 | * Extract informations from the regexp execution, |
| 4167 | * the parameter @values must point to an array of @nbval string pointers |
| 4168 | * on return nbval will contain the number of possible strings in that |
| 4169 | * state and the @values array will be updated with them. The string values |
| 4170 | * returned will be freed with the @exec context and don't need to be |
| 4171 | * deallocated. |
| 4172 | * |
| 4173 | * Returns: 0 in case of success or -1 in case of error. |
| 4174 | */ |
| 4175 | int |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4176 | xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg, |
| 4177 | xmlChar **values, int *terminal) { |
| 4178 | return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal)); |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | /** |
| 4182 | * xmlRegExecErrInfo: |
| 4183 | * @exec: a regexp execution context generating an error |
| 4184 | * @string: return value for the error string |
| 4185 | * @nbval: pointer to the number of accepted values IN/OUT |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4186 | * @nbneg: return number of negative transitions |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4187 | * @values: pointer to the array of acceptable values |
| 4188 | * @terminal: return value if this was a terminal state |
| 4189 | * |
| 4190 | * Extract error informations from the regexp execution, the parameter |
| 4191 | * @string will be updated with the value pushed and not accepted, |
| 4192 | * the parameter @values must point to an array of @nbval string pointers |
| 4193 | * on return nbval will contain the number of possible strings in that |
| 4194 | * state and the @values array will be updated with them. The string values |
| 4195 | * returned will be freed with the @exec context and don't need to be |
| 4196 | * deallocated. |
| 4197 | * |
| 4198 | * Returns: 0 in case of success or -1 in case of error. |
| 4199 | */ |
| 4200 | int |
| 4201 | xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string, |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4202 | int *nbval, int *nbneg, xmlChar **values, int *terminal) { |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4203 | if (exec == NULL) |
| 4204 | return(-1); |
| 4205 | if (string != NULL) { |
| 4206 | if (exec->status != 0) |
| 4207 | *string = exec->errString; |
| 4208 | else |
| 4209 | *string = NULL; |
| 4210 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4211 | return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal)); |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4214 | #ifdef DEBUG_ERR |
| 4215 | static void testerr(xmlRegExecCtxtPtr exec) { |
| 4216 | const xmlChar *string; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 4217 | xmlChar *values[5]; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4218 | int nb = 5; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4219 | int nbneg; |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 4220 | int terminal; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 4221 | xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal); |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 4222 | } |
| 4223 | #endif |
| 4224 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4225 | #if 0 |
| 4226 | static int |
| 4227 | xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) { |
| 4228 | xmlRegTransPtr trans; |
| 4229 | xmlRegAtomPtr atom; |
| 4230 | int ret; |
| 4231 | int codepoint, len; |
| 4232 | |
| 4233 | if (exec == NULL) |
| 4234 | return(-1); |
| 4235 | if (exec->status != 0) |
| 4236 | return(exec->status); |
| 4237 | |
| 4238 | while ((exec->status == 0) && |
| 4239 | ((exec->inputString[exec->index] != 0) || |
| 4240 | (exec->state->type != XML_REGEXP_FINAL_STATE))) { |
| 4241 | |
| 4242 | /* |
| 4243 | * End of input on non-terminal state, rollback, however we may |
| 4244 | * still have epsilon like transition for counted transitions |
| 4245 | * on counters, in that case don't break too early. |
| 4246 | */ |
| 4247 | if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) |
| 4248 | goto rollback; |
| 4249 | |
| 4250 | exec->transcount = 0; |
| 4251 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
| 4252 | trans = &exec->state->trans[exec->transno]; |
| 4253 | if (trans->to < 0) |
| 4254 | continue; |
| 4255 | atom = trans->atom; |
| 4256 | ret = 0; |
| 4257 | if (trans->count >= 0) { |
| 4258 | int count; |
| 4259 | xmlRegCounterPtr counter; |
| 4260 | |
| 4261 | /* |
| 4262 | * A counted transition. |
| 4263 | */ |
| 4264 | |
| 4265 | count = exec->counts[trans->count]; |
| 4266 | counter = &exec->comp->counters[trans->count]; |
| 4267 | #ifdef DEBUG_REGEXP_EXEC |
| 4268 | printf("testing count %d: val %d, min %d, max %d\n", |
| 4269 | trans->count, count, counter->min, counter->max); |
| 4270 | #endif |
| 4271 | ret = ((count >= counter->min) && (count <= counter->max)); |
| 4272 | } else if (atom == NULL) { |
| 4273 | fprintf(stderr, "epsilon transition left at runtime\n"); |
| 4274 | exec->status = -2; |
| 4275 | break; |
| 4276 | } else if (exec->inputString[exec->index] != 0) { |
| 4277 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len); |
| 4278 | ret = xmlRegCheckCharacter(atom, codepoint); |
| 4279 | if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) { |
| 4280 | xmlRegStatePtr to = exec->comp->states[trans->to]; |
| 4281 | |
| 4282 | /* |
| 4283 | * this is a multiple input sequence |
| 4284 | */ |
| 4285 | if (exec->state->nbTrans > exec->transno + 1) { |
| 4286 | xmlFARegExecSave(exec); |
| 4287 | } |
| 4288 | exec->transcount = 1; |
| 4289 | do { |
| 4290 | /* |
| 4291 | * Try to progress as much as possible on the input |
| 4292 | */ |
| 4293 | if (exec->transcount == atom->max) { |
| 4294 | break; |
| 4295 | } |
| 4296 | exec->index += len; |
| 4297 | /* |
| 4298 | * End of input: stop here |
| 4299 | */ |
| 4300 | if (exec->inputString[exec->index] == 0) { |
| 4301 | exec->index -= len; |
| 4302 | break; |
| 4303 | } |
| 4304 | if (exec->transcount >= atom->min) { |
| 4305 | int transno = exec->transno; |
| 4306 | xmlRegStatePtr state = exec->state; |
| 4307 | |
| 4308 | /* |
| 4309 | * The transition is acceptable save it |
| 4310 | */ |
| 4311 | exec->transno = -1; /* trick */ |
| 4312 | exec->state = to; |
| 4313 | xmlFARegExecSave(exec); |
| 4314 | exec->transno = transno; |
| 4315 | exec->state = state; |
| 4316 | } |
| 4317 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), |
| 4318 | len); |
| 4319 | ret = xmlRegCheckCharacter(atom, codepoint); |
| 4320 | exec->transcount++; |
| 4321 | } while (ret == 1); |
| 4322 | if (exec->transcount < atom->min) |
| 4323 | ret = 0; |
| 4324 | |
| 4325 | /* |
| 4326 | * If the last check failed but one transition was found |
| 4327 | * possible, rollback |
| 4328 | */ |
| 4329 | if (ret < 0) |
| 4330 | ret = 0; |
| 4331 | if (ret == 0) { |
| 4332 | goto rollback; |
| 4333 | } |
| 4334 | } |
| 4335 | } |
| 4336 | if (ret == 1) { |
| 4337 | if (exec->state->nbTrans > exec->transno + 1) { |
| 4338 | xmlFARegExecSave(exec); |
| 4339 | } |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 4340 | /* |
| 4341 | * restart count for expressions like this ((abc){2})* |
| 4342 | */ |
| 4343 | if (trans->count >= 0) { |
| 4344 | #ifdef DEBUG_REGEXP_EXEC |
| 4345 | printf("Reset count %d\n", trans->count); |
| 4346 | #endif |
| 4347 | exec->counts[trans->count] = 0; |
| 4348 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4349 | if (trans->counter >= 0) { |
| 4350 | #ifdef DEBUG_REGEXP_EXEC |
| 4351 | printf("Increasing count %d\n", trans->counter); |
| 4352 | #endif |
| 4353 | exec->counts[trans->counter]++; |
| 4354 | } |
| 4355 | #ifdef DEBUG_REGEXP_EXEC |
| 4356 | printf("entering state %d\n", trans->to); |
| 4357 | #endif |
| 4358 | exec->state = exec->comp->states[trans->to]; |
| 4359 | exec->transno = 0; |
| 4360 | if (trans->atom != NULL) { |
| 4361 | exec->index += len; |
| 4362 | } |
| 4363 | goto progress; |
| 4364 | } else if (ret < 0) { |
| 4365 | exec->status = -4; |
| 4366 | break; |
| 4367 | } |
| 4368 | } |
| 4369 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
| 4370 | rollback: |
| 4371 | /* |
| 4372 | * Failed to find a way out |
| 4373 | */ |
| 4374 | exec->determinist = 0; |
| 4375 | xmlFARegExecRollBack(exec); |
| 4376 | } |
| 4377 | progress: |
| 4378 | continue; |
| 4379 | } |
| 4380 | } |
| 4381 | #endif |
| 4382 | /************************************************************************ |
| 4383 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 4384 | * Parser for the Schemas Datatype Regular Expressions * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4385 | * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs * |
| 4386 | * * |
| 4387 | ************************************************************************/ |
| 4388 | |
| 4389 | /** |
| 4390 | * xmlFAIsChar: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4391 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4392 | * |
| 4393 | * [10] Char ::= [^.\?*+()|#x5B#x5D] |
| 4394 | */ |
| 4395 | static int |
| 4396 | xmlFAIsChar(xmlRegParserCtxtPtr ctxt) { |
| 4397 | int cur; |
| 4398 | int len; |
| 4399 | |
| 4400 | cur = CUR_SCHAR(ctxt->cur, len); |
| 4401 | if ((cur == '.') || (cur == '\\') || (cur == '?') || |
| 4402 | (cur == '*') || (cur == '+') || (cur == '(') || |
| 4403 | (cur == ')') || (cur == '|') || (cur == 0x5B) || |
| 4404 | (cur == 0x5D) || (cur == 0)) |
| 4405 | return(-1); |
| 4406 | return(cur); |
| 4407 | } |
| 4408 | |
| 4409 | /** |
| 4410 | * xmlFAParseCharProp: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4411 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4412 | * |
| 4413 | * [27] charProp ::= IsCategory | IsBlock |
| 4414 | * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation | |
| 4415 | * Separators | Symbols | Others |
| 4416 | * [29] Letters ::= 'L' [ultmo]? |
| 4417 | * [30] Marks ::= 'M' [nce]? |
| 4418 | * [31] Numbers ::= 'N' [dlo]? |
| 4419 | * [32] Punctuation ::= 'P' [cdseifo]? |
| 4420 | * [33] Separators ::= 'Z' [slp]? |
| 4421 | * [34] Symbols ::= 'S' [mcko]? |
| 4422 | * [35] Others ::= 'C' [cfon]? |
| 4423 | * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+ |
| 4424 | */ |
| 4425 | static void |
| 4426 | xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) { |
| 4427 | int cur; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 4428 | xmlRegAtomType type = (xmlRegAtomType) 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4429 | xmlChar *blockName = NULL; |
| 4430 | |
| 4431 | cur = CUR; |
| 4432 | if (cur == 'L') { |
| 4433 | NEXT; |
| 4434 | cur = CUR; |
| 4435 | if (cur == 'u') { |
| 4436 | NEXT; |
| 4437 | type = XML_REGEXP_LETTER_UPPERCASE; |
| 4438 | } else if (cur == 'l') { |
| 4439 | NEXT; |
| 4440 | type = XML_REGEXP_LETTER_LOWERCASE; |
| 4441 | } else if (cur == 't') { |
| 4442 | NEXT; |
| 4443 | type = XML_REGEXP_LETTER_TITLECASE; |
| 4444 | } else if (cur == 'm') { |
| 4445 | NEXT; |
| 4446 | type = XML_REGEXP_LETTER_MODIFIER; |
| 4447 | } else if (cur == 'o') { |
| 4448 | NEXT; |
| 4449 | type = XML_REGEXP_LETTER_OTHERS; |
| 4450 | } else { |
| 4451 | type = XML_REGEXP_LETTER; |
| 4452 | } |
| 4453 | } else if (cur == 'M') { |
| 4454 | NEXT; |
| 4455 | cur = CUR; |
| 4456 | if (cur == 'n') { |
| 4457 | NEXT; |
| 4458 | /* nonspacing */ |
| 4459 | type = XML_REGEXP_MARK_NONSPACING; |
| 4460 | } else if (cur == 'c') { |
| 4461 | NEXT; |
| 4462 | /* spacing combining */ |
| 4463 | type = XML_REGEXP_MARK_SPACECOMBINING; |
| 4464 | } else if (cur == 'e') { |
| 4465 | NEXT; |
| 4466 | /* enclosing */ |
| 4467 | type = XML_REGEXP_MARK_ENCLOSING; |
| 4468 | } else { |
| 4469 | /* all marks */ |
| 4470 | type = XML_REGEXP_MARK; |
| 4471 | } |
| 4472 | } else if (cur == 'N') { |
| 4473 | NEXT; |
| 4474 | cur = CUR; |
| 4475 | if (cur == 'd') { |
| 4476 | NEXT; |
| 4477 | /* digital */ |
| 4478 | type = XML_REGEXP_NUMBER_DECIMAL; |
| 4479 | } else if (cur == 'l') { |
| 4480 | NEXT; |
| 4481 | /* letter */ |
| 4482 | type = XML_REGEXP_NUMBER_LETTER; |
| 4483 | } else if (cur == 'o') { |
| 4484 | NEXT; |
| 4485 | /* other */ |
| 4486 | type = XML_REGEXP_NUMBER_OTHERS; |
| 4487 | } else { |
| 4488 | /* all numbers */ |
| 4489 | type = XML_REGEXP_NUMBER; |
| 4490 | } |
| 4491 | } else if (cur == 'P') { |
| 4492 | NEXT; |
| 4493 | cur = CUR; |
| 4494 | if (cur == 'c') { |
| 4495 | NEXT; |
| 4496 | /* connector */ |
| 4497 | type = XML_REGEXP_PUNCT_CONNECTOR; |
| 4498 | } else if (cur == 'd') { |
| 4499 | NEXT; |
| 4500 | /* dash */ |
| 4501 | type = XML_REGEXP_PUNCT_DASH; |
| 4502 | } else if (cur == 's') { |
| 4503 | NEXT; |
| 4504 | /* open */ |
| 4505 | type = XML_REGEXP_PUNCT_OPEN; |
| 4506 | } else if (cur == 'e') { |
| 4507 | NEXT; |
| 4508 | /* close */ |
| 4509 | type = XML_REGEXP_PUNCT_CLOSE; |
| 4510 | } else if (cur == 'i') { |
| 4511 | NEXT; |
| 4512 | /* initial quote */ |
| 4513 | type = XML_REGEXP_PUNCT_INITQUOTE; |
| 4514 | } else if (cur == 'f') { |
| 4515 | NEXT; |
| 4516 | /* final quote */ |
| 4517 | type = XML_REGEXP_PUNCT_FINQUOTE; |
| 4518 | } else if (cur == 'o') { |
| 4519 | NEXT; |
| 4520 | /* other */ |
| 4521 | type = XML_REGEXP_PUNCT_OTHERS; |
| 4522 | } else { |
| 4523 | /* all punctuation */ |
| 4524 | type = XML_REGEXP_PUNCT; |
| 4525 | } |
| 4526 | } else if (cur == 'Z') { |
| 4527 | NEXT; |
| 4528 | cur = CUR; |
| 4529 | if (cur == 's') { |
| 4530 | NEXT; |
| 4531 | /* space */ |
| 4532 | type = XML_REGEXP_SEPAR_SPACE; |
| 4533 | } else if (cur == 'l') { |
| 4534 | NEXT; |
| 4535 | /* line */ |
| 4536 | type = XML_REGEXP_SEPAR_LINE; |
| 4537 | } else if (cur == 'p') { |
| 4538 | NEXT; |
| 4539 | /* paragraph */ |
| 4540 | type = XML_REGEXP_SEPAR_PARA; |
| 4541 | } else { |
| 4542 | /* all separators */ |
| 4543 | type = XML_REGEXP_SEPAR; |
| 4544 | } |
| 4545 | } else if (cur == 'S') { |
| 4546 | NEXT; |
| 4547 | cur = CUR; |
| 4548 | if (cur == 'm') { |
| 4549 | NEXT; |
| 4550 | type = XML_REGEXP_SYMBOL_MATH; |
| 4551 | /* math */ |
| 4552 | } else if (cur == 'c') { |
| 4553 | NEXT; |
| 4554 | type = XML_REGEXP_SYMBOL_CURRENCY; |
| 4555 | /* currency */ |
| 4556 | } else if (cur == 'k') { |
| 4557 | NEXT; |
| 4558 | type = XML_REGEXP_SYMBOL_MODIFIER; |
| 4559 | /* modifiers */ |
| 4560 | } else if (cur == 'o') { |
| 4561 | NEXT; |
| 4562 | type = XML_REGEXP_SYMBOL_OTHERS; |
| 4563 | /* other */ |
| 4564 | } else { |
| 4565 | /* all symbols */ |
| 4566 | type = XML_REGEXP_SYMBOL; |
| 4567 | } |
| 4568 | } else if (cur == 'C') { |
| 4569 | NEXT; |
| 4570 | cur = CUR; |
| 4571 | if (cur == 'c') { |
| 4572 | NEXT; |
| 4573 | /* control */ |
| 4574 | type = XML_REGEXP_OTHER_CONTROL; |
| 4575 | } else if (cur == 'f') { |
| 4576 | NEXT; |
| 4577 | /* format */ |
| 4578 | type = XML_REGEXP_OTHER_FORMAT; |
| 4579 | } else if (cur == 'o') { |
| 4580 | NEXT; |
| 4581 | /* private use */ |
| 4582 | type = XML_REGEXP_OTHER_PRIVATE; |
| 4583 | } else if (cur == 'n') { |
| 4584 | NEXT; |
| 4585 | /* not assigned */ |
| 4586 | type = XML_REGEXP_OTHER_NA; |
| 4587 | } else { |
| 4588 | /* all others */ |
| 4589 | type = XML_REGEXP_OTHER; |
| 4590 | } |
| 4591 | } else if (cur == 'I') { |
| 4592 | const xmlChar *start; |
| 4593 | NEXT; |
| 4594 | cur = CUR; |
| 4595 | if (cur != 's') { |
| 4596 | ERROR("IsXXXX expected"); |
| 4597 | return; |
| 4598 | } |
| 4599 | NEXT; |
| 4600 | start = ctxt->cur; |
| 4601 | cur = CUR; |
| 4602 | if (((cur >= 'a') && (cur <= 'z')) || |
| 4603 | ((cur >= 'A') && (cur <= 'Z')) || |
| 4604 | ((cur >= '0') && (cur <= '9')) || |
| 4605 | (cur == 0x2D)) { |
| 4606 | NEXT; |
| 4607 | cur = CUR; |
| 4608 | while (((cur >= 'a') && (cur <= 'z')) || |
| 4609 | ((cur >= 'A') && (cur <= 'Z')) || |
| 4610 | ((cur >= '0') && (cur <= '9')) || |
| 4611 | (cur == 0x2D)) { |
| 4612 | NEXT; |
| 4613 | cur = CUR; |
| 4614 | } |
| 4615 | } |
| 4616 | type = XML_REGEXP_BLOCK_NAME; |
| 4617 | blockName = xmlStrndup(start, ctxt->cur - start); |
| 4618 | } else { |
| 4619 | ERROR("Unknown char property"); |
| 4620 | return; |
| 4621 | } |
| 4622 | if (ctxt->atom == NULL) { |
| 4623 | ctxt->atom = xmlRegNewAtom(ctxt, type); |
| 4624 | if (ctxt->atom != NULL) |
| 4625 | ctxt->atom->valuep = blockName; |
| 4626 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 4627 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4628 | type, 0, 0, blockName); |
| 4629 | } |
| 4630 | } |
| 4631 | |
| 4632 | /** |
| 4633 | * xmlFAParseCharClassEsc: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4634 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4635 | * |
| 4636 | * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc ) |
| 4637 | * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E] |
| 4638 | * [25] catEsc ::= '\p{' charProp '}' |
| 4639 | * [26] complEsc ::= '\P{' charProp '}' |
| 4640 | * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW]) |
| 4641 | */ |
| 4642 | static void |
| 4643 | xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) { |
| 4644 | int cur; |
| 4645 | |
| 4646 | if (CUR == '.') { |
| 4647 | if (ctxt->atom == NULL) { |
| 4648 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR); |
| 4649 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 4650 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4651 | XML_REGEXP_ANYCHAR, 0, 0, NULL); |
| 4652 | } |
| 4653 | NEXT; |
| 4654 | return; |
| 4655 | } |
| 4656 | if (CUR != '\\') { |
| 4657 | ERROR("Escaped sequence: expecting \\"); |
| 4658 | return; |
| 4659 | } |
| 4660 | NEXT; |
| 4661 | cur = CUR; |
| 4662 | if (cur == 'p') { |
| 4663 | NEXT; |
| 4664 | if (CUR != '{') { |
| 4665 | ERROR("Expecting '{'"); |
| 4666 | return; |
| 4667 | } |
| 4668 | NEXT; |
| 4669 | xmlFAParseCharProp(ctxt); |
| 4670 | if (CUR != '}') { |
| 4671 | ERROR("Expecting '}'"); |
| 4672 | return; |
| 4673 | } |
| 4674 | NEXT; |
| 4675 | } else if (cur == 'P') { |
| 4676 | NEXT; |
| 4677 | if (CUR != '{') { |
| 4678 | ERROR("Expecting '{'"); |
| 4679 | return; |
| 4680 | } |
| 4681 | NEXT; |
| 4682 | xmlFAParseCharProp(ctxt); |
| 4683 | ctxt->atom->neg = 1; |
| 4684 | if (CUR != '}') { |
| 4685 | ERROR("Expecting '}'"); |
| 4686 | return; |
| 4687 | } |
| 4688 | NEXT; |
| 4689 | } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') || |
| 4690 | (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') || |
| 4691 | (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') || |
| 4692 | (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) || |
| 4693 | (cur == 0x5E)) { |
| 4694 | if (ctxt->atom == NULL) { |
| 4695 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL); |
Daniel Veillard | 99c394d | 2005-07-14 12:58:49 +0000 | [diff] [blame] | 4696 | if (ctxt->atom != NULL) { |
| 4697 | switch (cur) { |
| 4698 | case 'n': |
| 4699 | ctxt->atom->codepoint = '\n'; |
| 4700 | break; |
| 4701 | case 'r': |
| 4702 | ctxt->atom->codepoint = '\r'; |
| 4703 | break; |
| 4704 | case 't': |
| 4705 | ctxt->atom->codepoint = '\t'; |
| 4706 | break; |
| 4707 | default: |
| 4708 | ctxt->atom->codepoint = cur; |
| 4709 | } |
| 4710 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4711 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 4712 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4713 | XML_REGEXP_CHARVAL, cur, cur, NULL); |
| 4714 | } |
| 4715 | NEXT; |
| 4716 | } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') || |
| 4717 | (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') || |
| 4718 | (cur == 'w') || (cur == 'W')) { |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 4719 | xmlRegAtomType type = XML_REGEXP_ANYSPACE; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4720 | |
| 4721 | switch (cur) { |
| 4722 | case 's': |
| 4723 | type = XML_REGEXP_ANYSPACE; |
| 4724 | break; |
| 4725 | case 'S': |
| 4726 | type = XML_REGEXP_NOTSPACE; |
| 4727 | break; |
| 4728 | case 'i': |
| 4729 | type = XML_REGEXP_INITNAME; |
| 4730 | break; |
| 4731 | case 'I': |
| 4732 | type = XML_REGEXP_NOTINITNAME; |
| 4733 | break; |
| 4734 | case 'c': |
| 4735 | type = XML_REGEXP_NAMECHAR; |
| 4736 | break; |
| 4737 | case 'C': |
| 4738 | type = XML_REGEXP_NOTNAMECHAR; |
| 4739 | break; |
| 4740 | case 'd': |
| 4741 | type = XML_REGEXP_DECIMAL; |
| 4742 | break; |
| 4743 | case 'D': |
| 4744 | type = XML_REGEXP_NOTDECIMAL; |
| 4745 | break; |
| 4746 | case 'w': |
| 4747 | type = XML_REGEXP_REALCHAR; |
| 4748 | break; |
| 4749 | case 'W': |
| 4750 | type = XML_REGEXP_NOTREALCHAR; |
| 4751 | break; |
| 4752 | } |
| 4753 | NEXT; |
| 4754 | if (ctxt->atom == NULL) { |
| 4755 | ctxt->atom = xmlRegNewAtom(ctxt, type); |
| 4756 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 4757 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4758 | type, 0, 0, NULL); |
| 4759 | } |
| 4760 | } |
| 4761 | } |
| 4762 | |
| 4763 | /** |
| 4764 | * xmlFAParseCharRef: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4765 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4766 | * |
| 4767 | * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' ) |
| 4768 | */ |
| 4769 | static int |
| 4770 | xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) { |
| 4771 | int ret = 0, cur; |
| 4772 | |
| 4773 | if ((CUR != '&') || (NXT(1) != '#')) |
| 4774 | return(-1); |
| 4775 | NEXT; |
| 4776 | NEXT; |
| 4777 | cur = CUR; |
| 4778 | if (cur == 'x') { |
| 4779 | NEXT; |
| 4780 | cur = CUR; |
| 4781 | if (((cur >= '0') && (cur <= '9')) || |
| 4782 | ((cur >= 'a') && (cur <= 'f')) || |
| 4783 | ((cur >= 'A') && (cur <= 'F'))) { |
| 4784 | while (((cur >= '0') && (cur <= '9')) || |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 4785 | ((cur >= 'a') && (cur <= 'f')) || |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4786 | ((cur >= 'A') && (cur <= 'F'))) { |
| 4787 | if ((cur >= '0') && (cur <= '9')) |
| 4788 | ret = ret * 16 + cur - '0'; |
| 4789 | else if ((cur >= 'a') && (cur <= 'f')) |
| 4790 | ret = ret * 16 + 10 + (cur - 'a'); |
| 4791 | else |
| 4792 | ret = ret * 16 + 10 + (cur - 'A'); |
| 4793 | NEXT; |
| 4794 | cur = CUR; |
| 4795 | } |
| 4796 | } else { |
| 4797 | ERROR("Char ref: expecting [0-9A-F]"); |
| 4798 | return(-1); |
| 4799 | } |
| 4800 | } else { |
| 4801 | if ((cur >= '0') && (cur <= '9')) { |
| 4802 | while ((cur >= '0') && (cur <= '9')) { |
| 4803 | ret = ret * 10 + cur - '0'; |
| 4804 | NEXT; |
| 4805 | cur = CUR; |
| 4806 | } |
| 4807 | } else { |
| 4808 | ERROR("Char ref: expecting [0-9]"); |
| 4809 | return(-1); |
| 4810 | } |
| 4811 | } |
| 4812 | if (cur != ';') { |
| 4813 | ERROR("Char ref: expecting ';'"); |
| 4814 | return(-1); |
| 4815 | } else { |
| 4816 | NEXT; |
| 4817 | } |
| 4818 | return(ret); |
| 4819 | } |
| 4820 | |
| 4821 | /** |
| 4822 | * xmlFAParseCharRange: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4823 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4824 | * |
| 4825 | * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash |
| 4826 | * [18] seRange ::= charOrEsc '-' charOrEsc |
| 4827 | * [20] charOrEsc ::= XmlChar | SingleCharEsc |
| 4828 | * [21] XmlChar ::= [^\#x2D#x5B#x5D] |
| 4829 | * [22] XmlCharIncDash ::= [^\#x5B#x5D] |
| 4830 | */ |
| 4831 | static void |
| 4832 | xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) { |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4833 | int cur, len; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4834 | int start = -1; |
| 4835 | int end = -1; |
| 4836 | |
Daniel Veillard | 777737e | 2006-10-17 21:23:17 +0000 | [diff] [blame] | 4837 | if (CUR == '\0') { |
| 4838 | ERROR("Expecting ']'"); |
| 4839 | return; |
| 4840 | } |
| 4841 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4842 | if ((CUR == '&') && (NXT(1) == '#')) { |
| 4843 | end = start = xmlFAParseCharRef(ctxt); |
| 4844 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4845 | XML_REGEXP_CHARVAL, start, end, NULL); |
| 4846 | return; |
| 4847 | } |
| 4848 | cur = CUR; |
| 4849 | if (cur == '\\') { |
| 4850 | NEXT; |
| 4851 | cur = CUR; |
| 4852 | switch (cur) { |
| 4853 | case 'n': start = 0xA; break; |
| 4854 | case 'r': start = 0xD; break; |
| 4855 | case 't': start = 0x9; break; |
| 4856 | case '\\': case '|': case '.': case '-': case '^': case '?': |
| 4857 | case '*': case '+': case '{': case '}': case '(': case ')': |
| 4858 | case '[': case ']': |
| 4859 | start = cur; break; |
| 4860 | default: |
| 4861 | ERROR("Invalid escape value"); |
| 4862 | return; |
| 4863 | } |
| 4864 | end = start; |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4865 | len = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4866 | } else if ((cur != 0x5B) && (cur != 0x5D)) { |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4867 | end = start = CUR_SCHAR(ctxt->cur, len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4868 | } else { |
| 4869 | ERROR("Expecting a char range"); |
| 4870 | return; |
| 4871 | } |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4872 | NEXTL(len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4873 | if (start == '-') { |
| 4874 | return; |
| 4875 | } |
| 4876 | cur = CUR; |
William M. Brack | 10f1ef4 | 2004-03-20 14:51:25 +0000 | [diff] [blame] | 4877 | if ((cur != '-') || (NXT(1) == ']')) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4878 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4879 | XML_REGEXP_CHARVAL, start, end, NULL); |
| 4880 | return; |
| 4881 | } |
| 4882 | NEXT; |
| 4883 | cur = CUR; |
| 4884 | if (cur == '\\') { |
| 4885 | NEXT; |
| 4886 | cur = CUR; |
| 4887 | switch (cur) { |
| 4888 | case 'n': end = 0xA; break; |
| 4889 | case 'r': end = 0xD; break; |
| 4890 | case 't': end = 0x9; break; |
| 4891 | case '\\': case '|': case '.': case '-': case '^': case '?': |
| 4892 | case '*': case '+': case '{': case '}': case '(': case ')': |
| 4893 | case '[': case ']': |
| 4894 | end = cur; break; |
| 4895 | default: |
| 4896 | ERROR("Invalid escape value"); |
| 4897 | return; |
| 4898 | } |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4899 | len = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4900 | } else if ((cur != 0x5B) && (cur != 0x5D)) { |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4901 | end = CUR_SCHAR(ctxt->cur, len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4902 | } else { |
| 4903 | ERROR("Expecting the end of a char range"); |
| 4904 | return; |
| 4905 | } |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4906 | NEXTL(len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4907 | /* TODO check that the values are acceptable character ranges for XML */ |
| 4908 | if (end < start) { |
| 4909 | ERROR("End of range is before start of range"); |
| 4910 | } else { |
| 4911 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4912 | XML_REGEXP_CHARVAL, start, end, NULL); |
| 4913 | } |
| 4914 | return; |
| 4915 | } |
| 4916 | |
| 4917 | /** |
| 4918 | * xmlFAParsePosCharGroup: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4919 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4920 | * |
| 4921 | * [14] posCharGroup ::= ( charRange | charClassEsc )+ |
| 4922 | */ |
| 4923 | static void |
| 4924 | xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) { |
| 4925 | do { |
| 4926 | if ((CUR == '\\') || (CUR == '.')) { |
| 4927 | xmlFAParseCharClassEsc(ctxt); |
| 4928 | } else { |
| 4929 | xmlFAParseCharRange(ctxt); |
| 4930 | } |
| 4931 | } while ((CUR != ']') && (CUR != '^') && (CUR != '-') && |
Daniel Veillard | 777737e | 2006-10-17 21:23:17 +0000 | [diff] [blame] | 4932 | (CUR != 0) && (ctxt->error == 0)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
| 4935 | /** |
| 4936 | * xmlFAParseCharGroup: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4937 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4938 | * |
| 4939 | * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub |
| 4940 | * [15] negCharGroup ::= '^' posCharGroup |
| 4941 | * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr |
| 4942 | * [12] charClassExpr ::= '[' charGroup ']' |
| 4943 | */ |
| 4944 | static void |
| 4945 | xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) { |
| 4946 | int n = ctxt->neg; |
| 4947 | while ((CUR != ']') && (ctxt->error == 0)) { |
| 4948 | if (CUR == '^') { |
| 4949 | int neg = ctxt->neg; |
| 4950 | |
| 4951 | NEXT; |
| 4952 | ctxt->neg = !ctxt->neg; |
| 4953 | xmlFAParsePosCharGroup(ctxt); |
| 4954 | ctxt->neg = neg; |
William M. Brack | 10f1ef4 | 2004-03-20 14:51:25 +0000 | [diff] [blame] | 4955 | } else if ((CUR == '-') && (NXT(1) == '[')) { |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 4956 | int neg = ctxt->neg; |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 4957 | ctxt->neg = 2; |
William M. Brack | 10f1ef4 | 2004-03-20 14:51:25 +0000 | [diff] [blame] | 4958 | NEXT; /* eat the '-' */ |
| 4959 | NEXT; /* eat the '[' */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4960 | xmlFAParseCharGroup(ctxt); |
| 4961 | if (CUR == ']') { |
| 4962 | NEXT; |
| 4963 | } else { |
| 4964 | ERROR("charClassExpr: ']' expected"); |
| 4965 | break; |
| 4966 | } |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 4967 | ctxt->neg = neg; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4968 | break; |
| 4969 | } else if (CUR != ']') { |
| 4970 | xmlFAParsePosCharGroup(ctxt); |
| 4971 | } |
| 4972 | } |
| 4973 | ctxt->neg = n; |
| 4974 | } |
| 4975 | |
| 4976 | /** |
| 4977 | * xmlFAParseCharClass: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4978 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4979 | * |
| 4980 | * [11] charClass ::= charClassEsc | charClassExpr |
| 4981 | * [12] charClassExpr ::= '[' charGroup ']' |
| 4982 | */ |
| 4983 | static void |
| 4984 | xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) { |
| 4985 | if (CUR == '[') { |
| 4986 | NEXT; |
| 4987 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES); |
| 4988 | if (ctxt->atom == NULL) |
| 4989 | return; |
| 4990 | xmlFAParseCharGroup(ctxt); |
| 4991 | if (CUR == ']') { |
| 4992 | NEXT; |
| 4993 | } else { |
| 4994 | ERROR("xmlFAParseCharClass: ']' expected"); |
| 4995 | } |
| 4996 | } else { |
| 4997 | xmlFAParseCharClassEsc(ctxt); |
| 4998 | } |
| 4999 | } |
| 5000 | |
| 5001 | /** |
| 5002 | * xmlFAParseQuantExact: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5003 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5004 | * |
| 5005 | * [8] QuantExact ::= [0-9]+ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5006 | * |
| 5007 | * Returns 0 if success or -1 in case of error |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5008 | */ |
| 5009 | static int |
| 5010 | xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) { |
| 5011 | int ret = 0; |
| 5012 | int ok = 0; |
| 5013 | |
| 5014 | while ((CUR >= '0') && (CUR <= '9')) { |
| 5015 | ret = ret * 10 + (CUR - '0'); |
| 5016 | ok = 1; |
| 5017 | NEXT; |
| 5018 | } |
| 5019 | if (ok != 1) { |
| 5020 | return(-1); |
| 5021 | } |
| 5022 | return(ret); |
| 5023 | } |
| 5024 | |
| 5025 | /** |
| 5026 | * xmlFAParseQuantifier: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5027 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5028 | * |
| 5029 | * [4] quantifier ::= [?*+] | ( '{' quantity '}' ) |
| 5030 | * [5] quantity ::= quantRange | quantMin | QuantExact |
| 5031 | * [6] quantRange ::= QuantExact ',' QuantExact |
| 5032 | * [7] quantMin ::= QuantExact ',' |
| 5033 | * [8] QuantExact ::= [0-9]+ |
| 5034 | */ |
| 5035 | static int |
| 5036 | xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) { |
| 5037 | int cur; |
| 5038 | |
| 5039 | cur = CUR; |
| 5040 | if ((cur == '?') || (cur == '*') || (cur == '+')) { |
| 5041 | if (ctxt->atom != NULL) { |
| 5042 | if (cur == '?') |
| 5043 | ctxt->atom->quant = XML_REGEXP_QUANT_OPT; |
| 5044 | else if (cur == '*') |
| 5045 | ctxt->atom->quant = XML_REGEXP_QUANT_MULT; |
| 5046 | else if (cur == '+') |
| 5047 | ctxt->atom->quant = XML_REGEXP_QUANT_PLUS; |
| 5048 | } |
| 5049 | NEXT; |
| 5050 | return(1); |
| 5051 | } |
| 5052 | if (cur == '{') { |
| 5053 | int min = 0, max = 0; |
| 5054 | |
| 5055 | NEXT; |
| 5056 | cur = xmlFAParseQuantExact(ctxt); |
| 5057 | if (cur >= 0) |
| 5058 | min = cur; |
| 5059 | if (CUR == ',') { |
| 5060 | NEXT; |
Daniel Veillard | ebe48c6 | 2003-12-03 12:12:27 +0000 | [diff] [blame] | 5061 | if (CUR == '}') |
| 5062 | max = INT_MAX; |
| 5063 | else { |
| 5064 | cur = xmlFAParseQuantExact(ctxt); |
| 5065 | if (cur >= 0) |
| 5066 | max = cur; |
| 5067 | else { |
| 5068 | ERROR("Improper quantifier"); |
| 5069 | } |
| 5070 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5071 | } |
| 5072 | if (CUR == '}') { |
| 5073 | NEXT; |
| 5074 | } else { |
| 5075 | ERROR("Unterminated quantifier"); |
| 5076 | } |
| 5077 | if (max == 0) |
| 5078 | max = min; |
| 5079 | if (ctxt->atom != NULL) { |
| 5080 | ctxt->atom->quant = XML_REGEXP_QUANT_RANGE; |
| 5081 | ctxt->atom->min = min; |
| 5082 | ctxt->atom->max = max; |
| 5083 | } |
| 5084 | return(1); |
| 5085 | } |
| 5086 | return(0); |
| 5087 | } |
| 5088 | |
| 5089 | /** |
| 5090 | * xmlFAParseAtom: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5091 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5092 | * |
| 5093 | * [9] atom ::= Char | charClass | ( '(' regExp ')' ) |
| 5094 | */ |
| 5095 | static int |
| 5096 | xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) { |
| 5097 | int codepoint, len; |
| 5098 | |
| 5099 | codepoint = xmlFAIsChar(ctxt); |
| 5100 | if (codepoint > 0) { |
| 5101 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL); |
| 5102 | if (ctxt->atom == NULL) |
| 5103 | return(-1); |
| 5104 | codepoint = CUR_SCHAR(ctxt->cur, len); |
| 5105 | ctxt->atom->codepoint = codepoint; |
| 5106 | NEXTL(len); |
| 5107 | return(1); |
| 5108 | } else if (CUR == '|') { |
| 5109 | return(0); |
| 5110 | } else if (CUR == 0) { |
| 5111 | return(0); |
| 5112 | } else if (CUR == ')') { |
| 5113 | return(0); |
| 5114 | } else if (CUR == '(') { |
| 5115 | xmlRegStatePtr start, oldend; |
| 5116 | |
| 5117 | NEXT; |
| 5118 | xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL); |
| 5119 | start = ctxt->state; |
| 5120 | oldend = ctxt->end; |
| 5121 | ctxt->end = NULL; |
| 5122 | ctxt->atom = NULL; |
| 5123 | xmlFAParseRegExp(ctxt, 0); |
| 5124 | if (CUR == ')') { |
| 5125 | NEXT; |
| 5126 | } else { |
| 5127 | ERROR("xmlFAParseAtom: expecting ')'"); |
| 5128 | } |
| 5129 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG); |
| 5130 | if (ctxt->atom == NULL) |
| 5131 | return(-1); |
| 5132 | ctxt->atom->start = start; |
| 5133 | ctxt->atom->stop = ctxt->state; |
| 5134 | ctxt->end = oldend; |
| 5135 | return(1); |
| 5136 | } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) { |
| 5137 | xmlFAParseCharClass(ctxt); |
| 5138 | return(1); |
| 5139 | } |
| 5140 | return(0); |
| 5141 | } |
| 5142 | |
| 5143 | /** |
| 5144 | * xmlFAParsePiece: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5145 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5146 | * |
| 5147 | * [3] piece ::= atom quantifier? |
| 5148 | */ |
| 5149 | static int |
| 5150 | xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) { |
| 5151 | int ret; |
| 5152 | |
| 5153 | ctxt->atom = NULL; |
| 5154 | ret = xmlFAParseAtom(ctxt); |
| 5155 | if (ret == 0) |
| 5156 | return(0); |
| 5157 | if (ctxt->atom == NULL) { |
| 5158 | ERROR("internal: no atom generated"); |
| 5159 | } |
| 5160 | xmlFAParseQuantifier(ctxt); |
| 5161 | return(1); |
| 5162 | } |
| 5163 | |
| 5164 | /** |
| 5165 | * xmlFAParseBranch: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5166 | * @ctxt: a regexp parser context |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5167 | * @to: optional target to the end of the branch |
| 5168 | * |
| 5169 | * @to is used to optimize by removing duplicate path in automata |
| 5170 | * in expressions like (a|b)(c|d) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5171 | * |
| 5172 | * [2] branch ::= piece* |
| 5173 | */ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5174 | static int |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5175 | xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5176 | xmlRegStatePtr previous; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5177 | int ret; |
| 5178 | |
| 5179 | previous = ctxt->state; |
| 5180 | ret = xmlFAParsePiece(ctxt); |
| 5181 | if (ret != 0) { |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5182 | if (xmlFAGenerateTransitions(ctxt, previous, |
| 5183 | (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0) |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5184 | return(-1); |
| 5185 | previous = ctxt->state; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5186 | ctxt->atom = NULL; |
| 5187 | } |
| 5188 | while ((ret != 0) && (ctxt->error == 0)) { |
| 5189 | ret = xmlFAParsePiece(ctxt); |
| 5190 | if (ret != 0) { |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5191 | if (xmlFAGenerateTransitions(ctxt, previous, |
| 5192 | (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5193 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5194 | previous = ctxt->state; |
| 5195 | ctxt->atom = NULL; |
| 5196 | } |
| 5197 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5198 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5199 | } |
| 5200 | |
| 5201 | /** |
| 5202 | * xmlFAParseRegExp: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5203 | * @ctxt: a regexp parser context |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5204 | * @top: is this the top-level expression ? |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5205 | * |
| 5206 | * [1] regExp ::= branch ( '|' branch )* |
| 5207 | */ |
| 5208 | static void |
| 5209 | xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) { |
Daniel Veillard | c7e3cc4 | 2004-09-28 12:33:52 +0000 | [diff] [blame] | 5210 | xmlRegStatePtr start, end; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5211 | |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5212 | /* if not top start should have been generated by an epsilon trans */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5213 | start = ctxt->state; |
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, NULL); |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5216 | if (top) { |
| 5217 | #ifdef DEBUG_REGEXP_GRAPH |
| 5218 | printf("State %d is final\n", ctxt->state->no); |
| 5219 | #endif |
| 5220 | ctxt->state->type = XML_REGEXP_FINAL_STATE; |
| 5221 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5222 | if (CUR != '|') { |
| 5223 | ctxt->end = ctxt->state; |
| 5224 | return; |
| 5225 | } |
| 5226 | end = ctxt->state; |
| 5227 | while ((CUR == '|') && (ctxt->error == 0)) { |
| 5228 | NEXT; |
| 5229 | ctxt->state = start; |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5230 | ctxt->end = NULL; |
Daniel Veillard | 54eb024 | 2006-03-21 23:17:57 +0000 | [diff] [blame] | 5231 | xmlFAParseBranch(ctxt, end); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5232 | } |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 5233 | if (!top) { |
| 5234 | ctxt->state = end; |
| 5235 | ctxt->end = end; |
| 5236 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5237 | } |
| 5238 | |
| 5239 | /************************************************************************ |
| 5240 | * * |
| 5241 | * The basic API * |
| 5242 | * * |
| 5243 | ************************************************************************/ |
| 5244 | |
| 5245 | /** |
| 5246 | * xmlRegexpPrint: |
| 5247 | * @output: the file for the output debug |
| 5248 | * @regexp: the compiled regexp |
| 5249 | * |
| 5250 | * Print the content of the compiled regular expression |
| 5251 | */ |
| 5252 | void |
| 5253 | xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) { |
| 5254 | int i; |
| 5255 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 5256 | if (output == NULL) |
| 5257 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5258 | fprintf(output, " regexp: "); |
| 5259 | if (regexp == NULL) { |
| 5260 | fprintf(output, "NULL\n"); |
| 5261 | return; |
| 5262 | } |
| 5263 | fprintf(output, "'%s' ", regexp->string); |
| 5264 | fprintf(output, "\n"); |
| 5265 | fprintf(output, "%d atoms:\n", regexp->nbAtoms); |
| 5266 | for (i = 0;i < regexp->nbAtoms; i++) { |
| 5267 | fprintf(output, " %02d ", i); |
| 5268 | xmlRegPrintAtom(output, regexp->atoms[i]); |
| 5269 | } |
| 5270 | fprintf(output, "%d states:", regexp->nbStates); |
| 5271 | fprintf(output, "\n"); |
| 5272 | for (i = 0;i < regexp->nbStates; i++) { |
| 5273 | xmlRegPrintState(output, regexp->states[i]); |
| 5274 | } |
| 5275 | fprintf(output, "%d counters:\n", regexp->nbCounters); |
| 5276 | for (i = 0;i < regexp->nbCounters; i++) { |
| 5277 | fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min, |
| 5278 | regexp->counters[i].max); |
| 5279 | } |
| 5280 | } |
| 5281 | |
| 5282 | /** |
| 5283 | * xmlRegexpCompile: |
| 5284 | * @regexp: a regular expression string |
| 5285 | * |
| 5286 | * Parses a regular expression conforming to XML Schemas Part 2 Datatype |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5287 | * Appendix F and builds an automata suitable for testing strings against |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5288 | * that regular expression |
| 5289 | * |
| 5290 | * Returns the compiled expression or NULL in case of error |
| 5291 | */ |
| 5292 | xmlRegexpPtr |
| 5293 | xmlRegexpCompile(const xmlChar *regexp) { |
| 5294 | xmlRegexpPtr ret; |
| 5295 | xmlRegParserCtxtPtr ctxt; |
| 5296 | |
| 5297 | ctxt = xmlRegNewParserCtxt(regexp); |
| 5298 | if (ctxt == NULL) |
| 5299 | return(NULL); |
| 5300 | |
| 5301 | /* initialize the parser */ |
| 5302 | ctxt->end = NULL; |
| 5303 | ctxt->start = ctxt->state = xmlRegNewState(ctxt); |
| 5304 | xmlRegStatePush(ctxt, ctxt->start); |
| 5305 | |
| 5306 | /* parse the expression building an automata */ |
| 5307 | xmlFAParseRegExp(ctxt, 1); |
| 5308 | if (CUR != 0) { |
| 5309 | ERROR("xmlFAParseRegExp: extra characters"); |
| 5310 | } |
| 5311 | ctxt->end = ctxt->state; |
| 5312 | ctxt->start->type = XML_REGEXP_START_STATE; |
| 5313 | ctxt->end->type = XML_REGEXP_FINAL_STATE; |
| 5314 | |
| 5315 | /* remove the Epsilon except for counted transitions */ |
| 5316 | xmlFAEliminateEpsilonTransitions(ctxt); |
| 5317 | |
| 5318 | |
| 5319 | if (ctxt->error != 0) { |
| 5320 | xmlRegFreeParserCtxt(ctxt); |
| 5321 | return(NULL); |
| 5322 | } |
| 5323 | ret = xmlRegEpxFromParse(ctxt); |
| 5324 | xmlRegFreeParserCtxt(ctxt); |
| 5325 | return(ret); |
| 5326 | } |
| 5327 | |
| 5328 | /** |
| 5329 | * xmlRegexpExec: |
| 5330 | * @comp: the compiled regular expression |
| 5331 | * @content: the value to check against the regular expression |
| 5332 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5333 | * Check if the regular expression generates the value |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5334 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5335 | * 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] | 5336 | */ |
| 5337 | int |
| 5338 | xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) { |
| 5339 | if ((comp == NULL) || (content == NULL)) |
| 5340 | return(-1); |
| 5341 | return(xmlFARegExec(comp, content)); |
| 5342 | } |
| 5343 | |
| 5344 | /** |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5345 | * xmlRegexpIsDeterminist: |
| 5346 | * @comp: the compiled regular expression |
| 5347 | * |
| 5348 | * Check if the regular expression is determinist |
| 5349 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5350 | * 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] | 5351 | */ |
| 5352 | int |
| 5353 | xmlRegexpIsDeterminist(xmlRegexpPtr comp) { |
| 5354 | xmlAutomataPtr am; |
| 5355 | int ret; |
| 5356 | |
| 5357 | if (comp == NULL) |
| 5358 | return(-1); |
| 5359 | if (comp->determinist != -1) |
| 5360 | return(comp->determinist); |
| 5361 | |
| 5362 | am = xmlNewAutomata(); |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 5363 | if (am->states != NULL) { |
| 5364 | int i; |
| 5365 | |
| 5366 | for (i = 0;i < am->nbStates;i++) |
| 5367 | xmlRegFreeState(am->states[i]); |
| 5368 | xmlFree(am->states); |
| 5369 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5370 | am->nbAtoms = comp->nbAtoms; |
| 5371 | am->atoms = comp->atoms; |
| 5372 | am->nbStates = comp->nbStates; |
| 5373 | am->states = comp->states; |
| 5374 | am->determinist = -1; |
| 5375 | ret = xmlFAComputesDeterminism(am); |
| 5376 | am->atoms = NULL; |
| 5377 | am->states = NULL; |
| 5378 | xmlFreeAutomata(am); |
| 5379 | return(ret); |
| 5380 | } |
| 5381 | |
| 5382 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5383 | * xmlRegFreeRegexp: |
| 5384 | * @regexp: the regexp |
| 5385 | * |
| 5386 | * Free a regexp |
| 5387 | */ |
| 5388 | void |
| 5389 | xmlRegFreeRegexp(xmlRegexpPtr regexp) { |
| 5390 | int i; |
| 5391 | if (regexp == NULL) |
| 5392 | return; |
| 5393 | |
| 5394 | if (regexp->string != NULL) |
| 5395 | xmlFree(regexp->string); |
| 5396 | if (regexp->states != NULL) { |
| 5397 | for (i = 0;i < regexp->nbStates;i++) |
| 5398 | xmlRegFreeState(regexp->states[i]); |
| 5399 | xmlFree(regexp->states); |
| 5400 | } |
| 5401 | if (regexp->atoms != NULL) { |
| 5402 | for (i = 0;i < regexp->nbAtoms;i++) |
| 5403 | xmlRegFreeAtom(regexp->atoms[i]); |
| 5404 | xmlFree(regexp->atoms); |
| 5405 | } |
| 5406 | if (regexp->counters != NULL) |
| 5407 | xmlFree(regexp->counters); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5408 | if (regexp->compact != NULL) |
| 5409 | xmlFree(regexp->compact); |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 5410 | if (regexp->transdata != NULL) |
| 5411 | xmlFree(regexp->transdata); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5412 | if (regexp->stringMap != NULL) { |
| 5413 | for (i = 0; i < regexp->nbstrings;i++) |
| 5414 | xmlFree(regexp->stringMap[i]); |
| 5415 | xmlFree(regexp->stringMap); |
| 5416 | } |
| 5417 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5418 | xmlFree(regexp); |
| 5419 | } |
| 5420 | |
| 5421 | #ifdef LIBXML_AUTOMATA_ENABLED |
| 5422 | /************************************************************************ |
| 5423 | * * |
| 5424 | * The Automata interface * |
| 5425 | * * |
| 5426 | ************************************************************************/ |
| 5427 | |
| 5428 | /** |
| 5429 | * xmlNewAutomata: |
| 5430 | * |
| 5431 | * Create a new automata |
| 5432 | * |
| 5433 | * Returns the new object or NULL in case of failure |
| 5434 | */ |
| 5435 | xmlAutomataPtr |
| 5436 | xmlNewAutomata(void) { |
| 5437 | xmlAutomataPtr ctxt; |
| 5438 | |
| 5439 | ctxt = xmlRegNewParserCtxt(NULL); |
| 5440 | if (ctxt == NULL) |
| 5441 | return(NULL); |
| 5442 | |
| 5443 | /* initialize the parser */ |
| 5444 | ctxt->end = NULL; |
| 5445 | ctxt->start = ctxt->state = xmlRegNewState(ctxt); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5446 | if (ctxt->start == NULL) { |
| 5447 | xmlFreeAutomata(ctxt); |
| 5448 | return(NULL); |
| 5449 | } |
Daniel Veillard | d027147 | 2006-01-02 10:22:02 +0000 | [diff] [blame] | 5450 | ctxt->start->type = XML_REGEXP_START_STATE; |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5451 | if (xmlRegStatePush(ctxt, ctxt->start) < 0) { |
| 5452 | xmlRegFreeState(ctxt->start); |
| 5453 | xmlFreeAutomata(ctxt); |
| 5454 | return(NULL); |
| 5455 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5456 | |
| 5457 | return(ctxt); |
| 5458 | } |
| 5459 | |
| 5460 | /** |
| 5461 | * xmlFreeAutomata: |
| 5462 | * @am: an automata |
| 5463 | * |
| 5464 | * Free an automata |
| 5465 | */ |
| 5466 | void |
| 5467 | xmlFreeAutomata(xmlAutomataPtr am) { |
| 5468 | if (am == NULL) |
| 5469 | return; |
| 5470 | xmlRegFreeParserCtxt(am); |
| 5471 | } |
| 5472 | |
| 5473 | /** |
| 5474 | * xmlAutomataGetInitState: |
| 5475 | * @am: an automata |
| 5476 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5477 | * Initial state lookup |
| 5478 | * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5479 | * Returns the initial state of the automata |
| 5480 | */ |
| 5481 | xmlAutomataStatePtr |
| 5482 | xmlAutomataGetInitState(xmlAutomataPtr am) { |
| 5483 | if (am == NULL) |
| 5484 | return(NULL); |
| 5485 | return(am->start); |
| 5486 | } |
| 5487 | |
| 5488 | /** |
| 5489 | * xmlAutomataSetFinalState: |
| 5490 | * @am: an automata |
| 5491 | * @state: a state in this automata |
| 5492 | * |
| 5493 | * Makes that state a final state |
| 5494 | * |
| 5495 | * Returns 0 or -1 in case of error |
| 5496 | */ |
| 5497 | int |
| 5498 | xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) { |
| 5499 | if ((am == NULL) || (state == NULL)) |
| 5500 | return(-1); |
| 5501 | state->type = XML_REGEXP_FINAL_STATE; |
| 5502 | return(0); |
| 5503 | } |
| 5504 | |
| 5505 | /** |
| 5506 | * xmlAutomataNewTransition: |
| 5507 | * @am: an automata |
| 5508 | * @from: the starting point of the transition |
| 5509 | * @to: the target point of the transition or NULL |
| 5510 | * @token: the input string associated to that transition |
| 5511 | * @data: data passed to the callback function if the transition is activated |
| 5512 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5513 | * 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] | 5514 | * and then adds a transition from the @from state to the target state |
| 5515 | * activated by the value of @token |
| 5516 | * |
| 5517 | * Returns the target state or NULL in case of error |
| 5518 | */ |
| 5519 | xmlAutomataStatePtr |
| 5520 | xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5521 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5522 | void *data) { |
| 5523 | xmlRegAtomPtr atom; |
| 5524 | |
| 5525 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5526 | return(NULL); |
| 5527 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5528 | if (atom == NULL) |
| 5529 | return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5530 | atom->data = data; |
| 5531 | if (atom == NULL) |
| 5532 | return(NULL); |
| 5533 | atom->valuep = xmlStrdup(token); |
| 5534 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5535 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
| 5536 | xmlRegFreeAtom(atom); |
| 5537 | return(NULL); |
| 5538 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5539 | if (to == NULL) |
| 5540 | return(am->state); |
| 5541 | return(to); |
| 5542 | } |
| 5543 | |
| 5544 | /** |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5545 | * xmlAutomataNewTransition2: |
| 5546 | * @am: an automata |
| 5547 | * @from: the starting point of the transition |
| 5548 | * @to: the target point of the transition or NULL |
| 5549 | * @token: the first input string associated to that transition |
| 5550 | * @token2: the second input string associated to that transition |
| 5551 | * @data: data passed to the callback function if the transition is activated |
| 5552 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5553 | * 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] | 5554 | * and then adds a transition from the @from state to the target state |
| 5555 | * activated by the value of @token |
| 5556 | * |
| 5557 | * Returns the target state or NULL in case of error |
| 5558 | */ |
| 5559 | xmlAutomataStatePtr |
| 5560 | xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5561 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5562 | const xmlChar *token2, void *data) { |
| 5563 | xmlRegAtomPtr atom; |
| 5564 | |
| 5565 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5566 | return(NULL); |
| 5567 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5568 | if (atom == NULL) |
| 5569 | return(NULL); |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5570 | atom->data = data; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5571 | if ((token2 == NULL) || (*token2 == 0)) { |
| 5572 | atom->valuep = xmlStrdup(token); |
| 5573 | } else { |
| 5574 | int lenn, lenp; |
| 5575 | xmlChar *str; |
| 5576 | |
| 5577 | lenn = strlen((char *) token2); |
| 5578 | lenp = strlen((char *) token); |
| 5579 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 5580 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5581 | if (str == NULL) { |
| 5582 | xmlRegFreeAtom(atom); |
| 5583 | return(NULL); |
| 5584 | } |
| 5585 | memcpy(&str[0], token, lenp); |
| 5586 | str[lenp] = '|'; |
| 5587 | memcpy(&str[lenp + 1], token2, lenn); |
| 5588 | str[lenn + lenp + 1] = 0; |
| 5589 | |
| 5590 | atom->valuep = str; |
| 5591 | } |
| 5592 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5593 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
| 5594 | xmlRegFreeAtom(atom); |
| 5595 | return(NULL); |
| 5596 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 5597 | if (to == NULL) |
| 5598 | return(am->state); |
| 5599 | return(to); |
| 5600 | } |
| 5601 | |
| 5602 | /** |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 5603 | * xmlAutomataNewNegTrans: |
| 5604 | * @am: an automata |
| 5605 | * @from: the starting point of the transition |
| 5606 | * @to: the target point of the transition or NULL |
| 5607 | * @token: the first input string associated to that transition |
| 5608 | * @token2: the second input string associated to that transition |
| 5609 | * @data: data passed to the callback function if the transition is activated |
| 5610 | * |
| 5611 | * If @to is NULL, this creates first a new target state in the automata |
| 5612 | * and then adds a transition from the @from state to the target state |
| 5613 | * activated by any value except (@token,@token2) |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 5614 | * Note that if @token2 is not NULL, then (X, NULL) won't match to follow |
| 5615 | # the semantic of XSD ##other |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 5616 | * |
| 5617 | * Returns the target state or NULL in case of error |
| 5618 | */ |
| 5619 | xmlAutomataStatePtr |
| 5620 | xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5621 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5622 | const xmlChar *token2, void *data) { |
| 5623 | xmlRegAtomPtr atom; |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 5624 | xmlChar err_msg[200]; |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 5625 | |
| 5626 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5627 | return(NULL); |
| 5628 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5629 | if (atom == NULL) |
| 5630 | return(NULL); |
| 5631 | atom->data = data; |
| 5632 | atom->neg = 1; |
| 5633 | if ((token2 == NULL) || (*token2 == 0)) { |
| 5634 | atom->valuep = xmlStrdup(token); |
| 5635 | } else { |
| 5636 | int lenn, lenp; |
| 5637 | xmlChar *str; |
| 5638 | |
| 5639 | lenn = strlen((char *) token2); |
| 5640 | lenp = strlen((char *) token); |
| 5641 | |
| 5642 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
| 5643 | if (str == NULL) { |
| 5644 | xmlRegFreeAtom(atom); |
| 5645 | return(NULL); |
| 5646 | } |
| 5647 | memcpy(&str[0], token, lenp); |
| 5648 | str[lenp] = '|'; |
| 5649 | memcpy(&str[lenp + 1], token2, lenn); |
| 5650 | str[lenn + lenp + 1] = 0; |
| 5651 | |
| 5652 | atom->valuep = str; |
| 5653 | } |
Daniel Veillard | db68b74 | 2005-07-30 13:18:24 +0000 | [diff] [blame] | 5654 | snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep); |
Daniel Veillard | 77005e6 | 2005-07-19 16:26:18 +0000 | [diff] [blame] | 5655 | err_msg[199] = 0; |
| 5656 | atom->valuep2 = xmlStrdup(err_msg); |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 5657 | |
| 5658 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
| 5659 | xmlRegFreeAtom(atom); |
| 5660 | return(NULL); |
| 5661 | } |
Daniel Veillard | 6e65e15 | 2005-08-09 11:09:52 +0000 | [diff] [blame] | 5662 | am->negs++; |
Daniel Veillard | 9efc476 | 2005-07-19 14:33:55 +0000 | [diff] [blame] | 5663 | if (to == NULL) |
| 5664 | return(am->state); |
| 5665 | return(to); |
| 5666 | } |
| 5667 | |
| 5668 | /** |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 5669 | * xmlAutomataNewCountTrans2: |
| 5670 | * @am: an automata |
| 5671 | * @from: the starting point of the transition |
| 5672 | * @to: the target point of the transition or NULL |
| 5673 | * @token: the input string associated to that transition |
| 5674 | * @token2: the second input string associated to that transition |
| 5675 | * @min: the minimum successive occurences of token |
| 5676 | * @max: the maximum successive occurences of token |
| 5677 | * @data: data associated to the transition |
| 5678 | * |
| 5679 | * If @to is NULL, this creates first a new target state in the automata |
| 5680 | * and then adds a transition from the @from state to the target state |
| 5681 | * activated by a succession of input of value @token and @token2 and |
| 5682 | * whose number is between @min and @max |
| 5683 | * |
| 5684 | * Returns the target state or NULL in case of error |
| 5685 | */ |
| 5686 | xmlAutomataStatePtr |
| 5687 | xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5688 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5689 | const xmlChar *token2, |
| 5690 | int min, int max, void *data) { |
| 5691 | xmlRegAtomPtr atom; |
| 5692 | int counter; |
| 5693 | |
| 5694 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5695 | return(NULL); |
| 5696 | if (min < 0) |
| 5697 | return(NULL); |
| 5698 | if ((max < min) || (max < 1)) |
| 5699 | return(NULL); |
| 5700 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5701 | if (atom == NULL) |
| 5702 | return(NULL); |
| 5703 | if ((token2 == NULL) || (*token2 == 0)) { |
| 5704 | atom->valuep = xmlStrdup(token); |
| 5705 | } else { |
| 5706 | int lenn, lenp; |
| 5707 | xmlChar *str; |
| 5708 | |
| 5709 | lenn = strlen((char *) token2); |
| 5710 | lenp = strlen((char *) token); |
| 5711 | |
| 5712 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
| 5713 | if (str == NULL) { |
| 5714 | xmlRegFreeAtom(atom); |
| 5715 | return(NULL); |
| 5716 | } |
| 5717 | memcpy(&str[0], token, lenp); |
| 5718 | str[lenp] = '|'; |
| 5719 | memcpy(&str[lenp + 1], token2, lenn); |
| 5720 | str[lenn + lenp + 1] = 0; |
| 5721 | |
| 5722 | atom->valuep = str; |
| 5723 | } |
| 5724 | atom->data = data; |
| 5725 | if (min == 0) |
| 5726 | atom->min = 1; |
| 5727 | else |
| 5728 | atom->min = min; |
| 5729 | atom->max = max; |
| 5730 | |
| 5731 | /* |
| 5732 | * associate a counter to the transition. |
| 5733 | */ |
| 5734 | counter = xmlRegGetCounter(am); |
| 5735 | am->counters[counter].min = min; |
| 5736 | am->counters[counter].max = max; |
| 5737 | |
| 5738 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 5739 | if (to == NULL) { |
| 5740 | to = xmlRegNewState(am); |
| 5741 | xmlRegStatePush(am, to); |
| 5742 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 5743 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 5744 | xmlRegAtomPush(am, atom); |
| 5745 | am->state = to; |
| 5746 | |
| 5747 | if (to == NULL) |
| 5748 | to = am->state; |
| 5749 | if (to == NULL) |
| 5750 | return(NULL); |
| 5751 | if (min == 0) |
| 5752 | xmlFAGenerateEpsilonTransition(am, from, to); |
| 5753 | return(to); |
| 5754 | } |
| 5755 | |
| 5756 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5757 | * xmlAutomataNewCountTrans: |
| 5758 | * @am: an automata |
| 5759 | * @from: the starting point of the transition |
| 5760 | * @to: the target point of the transition or NULL |
| 5761 | * @token: the input string associated to that transition |
| 5762 | * @min: the minimum successive occurences of token |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5763 | * @max: the maximum successive occurences of token |
| 5764 | * @data: data associated to the transition |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5765 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5766 | * 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] | 5767 | * and then adds a transition from the @from state to the target state |
| 5768 | * activated by a succession of input of value @token and whose number |
| 5769 | * is between @min and @max |
| 5770 | * |
| 5771 | * Returns the target state or NULL in case of error |
| 5772 | */ |
| 5773 | xmlAutomataStatePtr |
| 5774 | xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5775 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5776 | int min, int max, void *data) { |
| 5777 | xmlRegAtomPtr atom; |
Daniel Veillard | 0ddb21c | 2004-02-12 12:43:49 +0000 | [diff] [blame] | 5778 | int counter; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5779 | |
| 5780 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5781 | return(NULL); |
| 5782 | if (min < 0) |
| 5783 | return(NULL); |
| 5784 | if ((max < min) || (max < 1)) |
| 5785 | return(NULL); |
| 5786 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5787 | if (atom == NULL) |
| 5788 | return(NULL); |
| 5789 | atom->valuep = xmlStrdup(token); |
| 5790 | atom->data = data; |
| 5791 | if (min == 0) |
| 5792 | atom->min = 1; |
| 5793 | else |
| 5794 | atom->min = min; |
| 5795 | atom->max = max; |
| 5796 | |
Daniel Veillard | 0ddb21c | 2004-02-12 12:43:49 +0000 | [diff] [blame] | 5797 | /* |
| 5798 | * associate a counter to the transition. |
| 5799 | */ |
| 5800 | counter = xmlRegGetCounter(am); |
| 5801 | am->counters[counter].min = min; |
| 5802 | am->counters[counter].max = max; |
| 5803 | |
| 5804 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 5805 | if (to == NULL) { |
| 5806 | to = xmlRegNewState(am); |
| 5807 | xmlRegStatePush(am, to); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5808 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 5809 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
Daniel Veillard | 0ddb21c | 2004-02-12 12:43:49 +0000 | [diff] [blame] | 5810 | xmlRegAtomPush(am, atom); |
| 5811 | am->state = to; |
| 5812 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5813 | if (to == NULL) |
| 5814 | to = am->state; |
| 5815 | if (to == NULL) |
| 5816 | return(NULL); |
| 5817 | if (min == 0) |
| 5818 | xmlFAGenerateEpsilonTransition(am, from, to); |
| 5819 | return(to); |
| 5820 | } |
| 5821 | |
| 5822 | /** |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 5823 | * xmlAutomataNewOnceTrans2: |
| 5824 | * @am: an automata |
| 5825 | * @from: the starting point of the transition |
| 5826 | * @to: the target point of the transition or NULL |
| 5827 | * @token: the input string associated to that transition |
| 5828 | * @token2: the second input string associated to that transition |
| 5829 | * @min: the minimum successive occurences of token |
| 5830 | * @max: the maximum successive occurences of token |
| 5831 | * @data: data associated to the transition |
| 5832 | * |
| 5833 | * If @to is NULL, this creates first a new target state in the automata |
| 5834 | * and then adds a transition from the @from state to the target state |
| 5835 | * activated by a succession of input of value @token and @token2 and whose |
| 5836 | * number is between @min and @max, moreover that transition can only be |
| 5837 | * crossed once. |
| 5838 | * |
| 5839 | * Returns the target state or NULL in case of error |
| 5840 | */ |
| 5841 | xmlAutomataStatePtr |
| 5842 | xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5843 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5844 | const xmlChar *token2, |
| 5845 | int min, int max, void *data) { |
| 5846 | xmlRegAtomPtr atom; |
| 5847 | int counter; |
| 5848 | |
| 5849 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5850 | return(NULL); |
| 5851 | if (min < 1) |
| 5852 | return(NULL); |
| 5853 | if ((max < min) || (max < 1)) |
| 5854 | return(NULL); |
| 5855 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5856 | if (atom == NULL) |
| 5857 | return(NULL); |
| 5858 | if ((token2 == NULL) || (*token2 == 0)) { |
| 5859 | atom->valuep = xmlStrdup(token); |
| 5860 | } else { |
| 5861 | int lenn, lenp; |
| 5862 | xmlChar *str; |
| 5863 | |
| 5864 | lenn = strlen((char *) token2); |
| 5865 | lenp = strlen((char *) token); |
| 5866 | |
| 5867 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
| 5868 | if (str == NULL) { |
| 5869 | xmlRegFreeAtom(atom); |
| 5870 | return(NULL); |
| 5871 | } |
| 5872 | memcpy(&str[0], token, lenp); |
| 5873 | str[lenp] = '|'; |
| 5874 | memcpy(&str[lenp + 1], token2, lenn); |
| 5875 | str[lenn + lenp + 1] = 0; |
| 5876 | |
| 5877 | atom->valuep = str; |
| 5878 | } |
| 5879 | atom->data = data; |
| 5880 | atom->quant = XML_REGEXP_QUANT_ONCEONLY; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5881 | atom->min = min; |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 5882 | atom->max = max; |
| 5883 | /* |
| 5884 | * associate a counter to the transition. |
| 5885 | */ |
| 5886 | counter = xmlRegGetCounter(am); |
| 5887 | am->counters[counter].min = 1; |
| 5888 | am->counters[counter].max = 1; |
| 5889 | |
| 5890 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 5891 | if (to == NULL) { |
| 5892 | to = xmlRegNewState(am); |
| 5893 | xmlRegStatePush(am, to); |
| 5894 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 5895 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 5896 | xmlRegAtomPush(am, atom); |
| 5897 | am->state = to; |
| 5898 | return(to); |
| 5899 | } |
| 5900 | |
| 5901 | |
| 5902 | |
| 5903 | /** |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5904 | * xmlAutomataNewOnceTrans: |
| 5905 | * @am: an automata |
| 5906 | * @from: the starting point of the transition |
| 5907 | * @to: the target point of the transition or NULL |
| 5908 | * @token: the input string associated to that transition |
| 5909 | * @min: the minimum successive occurences of token |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5910 | * @max: the maximum successive occurences of token |
| 5911 | * @data: data associated to the transition |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5912 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5913 | * 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] | 5914 | * and then adds a transition from the @from state to the target state |
| 5915 | * 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] | 5916 | * is between @min and @max, moreover that transition can only be crossed |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5917 | * once. |
| 5918 | * |
| 5919 | * Returns the target state or NULL in case of error |
| 5920 | */ |
| 5921 | xmlAutomataStatePtr |
| 5922 | xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5923 | xmlAutomataStatePtr to, const xmlChar *token, |
| 5924 | int min, int max, void *data) { |
| 5925 | xmlRegAtomPtr atom; |
| 5926 | int counter; |
| 5927 | |
| 5928 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5929 | return(NULL); |
| 5930 | if (min < 1) |
| 5931 | return(NULL); |
| 5932 | if ((max < min) || (max < 1)) |
| 5933 | return(NULL); |
| 5934 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5935 | if (atom == NULL) |
| 5936 | return(NULL); |
| 5937 | atom->valuep = xmlStrdup(token); |
| 5938 | atom->data = data; |
| 5939 | atom->quant = XML_REGEXP_QUANT_ONCEONLY; |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 5940 | atom->min = min; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5941 | atom->max = max; |
| 5942 | /* |
| 5943 | * associate a counter to the transition. |
| 5944 | */ |
| 5945 | counter = xmlRegGetCounter(am); |
| 5946 | am->counters[counter].min = 1; |
| 5947 | am->counters[counter].max = 1; |
| 5948 | |
| 5949 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 5950 | if (to == NULL) { |
| 5951 | to = xmlRegNewState(am); |
| 5952 | xmlRegStatePush(am, to); |
| 5953 | } |
Daniel Veillard | 5de0938 | 2005-09-26 17:18:17 +0000 | [diff] [blame] | 5954 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5955 | xmlRegAtomPush(am, atom); |
| 5956 | am->state = to; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5957 | return(to); |
| 5958 | } |
| 5959 | |
| 5960 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5961 | * xmlAutomataNewState: |
| 5962 | * @am: an automata |
| 5963 | * |
| 5964 | * Create a new disconnected state in the automata |
| 5965 | * |
| 5966 | * Returns the new state or NULL in case of error |
| 5967 | */ |
| 5968 | xmlAutomataStatePtr |
| 5969 | xmlAutomataNewState(xmlAutomataPtr am) { |
| 5970 | xmlAutomataStatePtr to; |
| 5971 | |
| 5972 | if (am == NULL) |
| 5973 | return(NULL); |
| 5974 | to = xmlRegNewState(am); |
| 5975 | xmlRegStatePush(am, to); |
| 5976 | return(to); |
| 5977 | } |
| 5978 | |
| 5979 | /** |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5980 | * xmlAutomataNewEpsilon: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5981 | * @am: an automata |
| 5982 | * @from: the starting point of the transition |
| 5983 | * @to: the target point of the transition or NULL |
| 5984 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5985 | * If @to is NULL, this creates first a new target state in the automata |
| 5986 | * and then adds an epsilon transition from the @from state to the |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5987 | * target state |
| 5988 | * |
| 5989 | * Returns the target state or NULL in case of error |
| 5990 | */ |
| 5991 | xmlAutomataStatePtr |
| 5992 | xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5993 | xmlAutomataStatePtr to) { |
| 5994 | if ((am == NULL) || (from == NULL)) |
| 5995 | return(NULL); |
| 5996 | xmlFAGenerateEpsilonTransition(am, from, to); |
| 5997 | if (to == NULL) |
| 5998 | return(am->state); |
| 5999 | return(to); |
| 6000 | } |
| 6001 | |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 6002 | /** |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 6003 | * xmlAutomataNewAllTrans: |
| 6004 | * @am: an automata |
| 6005 | * @from: the starting point of the transition |
| 6006 | * @to: the target point of the transition or NULL |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 6007 | * @lax: allow to transition if not all all transitions have been activated |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 6008 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 6009 | * 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] | 6010 | * and then adds a an ALL transition from the @from state to the |
| 6011 | * target state. That transition is an epsilon transition allowed only when |
| 6012 | * all transitions from the @from node have been activated. |
| 6013 | * |
| 6014 | * Returns the target state or NULL in case of error |
| 6015 | */ |
| 6016 | xmlAutomataStatePtr |
| 6017 | xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 6018 | xmlAutomataStatePtr to, int lax) { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 6019 | if ((am == NULL) || (from == NULL)) |
| 6020 | return(NULL); |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 6021 | xmlFAGenerateAllTransition(am, from, to, lax); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 6022 | if (to == NULL) |
| 6023 | return(am->state); |
| 6024 | return(to); |
| 6025 | } |
| 6026 | |
| 6027 | /** |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 6028 | * xmlAutomataNewCounter: |
| 6029 | * @am: an automata |
| 6030 | * @min: the minimal value on the counter |
| 6031 | * @max: the maximal value on the counter |
| 6032 | * |
| 6033 | * Create a new counter |
| 6034 | * |
| 6035 | * Returns the counter number or -1 in case of error |
| 6036 | */ |
| 6037 | int |
| 6038 | xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) { |
| 6039 | int ret; |
| 6040 | |
| 6041 | if (am == NULL) |
| 6042 | return(-1); |
| 6043 | |
| 6044 | ret = xmlRegGetCounter(am); |
| 6045 | if (ret < 0) |
| 6046 | return(-1); |
| 6047 | am->counters[ret].min = min; |
| 6048 | am->counters[ret].max = max; |
| 6049 | return(ret); |
| 6050 | } |
| 6051 | |
| 6052 | /** |
| 6053 | * xmlAutomataNewCountedTrans: |
| 6054 | * @am: an automata |
| 6055 | * @from: the starting point of the transition |
| 6056 | * @to: the target point of the transition or NULL |
| 6057 | * @counter: the counter associated to that transition |
| 6058 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 6059 | * 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] | 6060 | * and then adds an epsilon transition from the @from state to the target state |
| 6061 | * which will increment the counter provided |
| 6062 | * |
| 6063 | * Returns the target state or NULL in case of error |
| 6064 | */ |
| 6065 | xmlAutomataStatePtr |
| 6066 | xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 6067 | xmlAutomataStatePtr to, int counter) { |
| 6068 | if ((am == NULL) || (from == NULL) || (counter < 0)) |
| 6069 | return(NULL); |
| 6070 | xmlFAGenerateCountedEpsilonTransition(am, from, to, counter); |
| 6071 | if (to == NULL) |
| 6072 | return(am->state); |
| 6073 | return(to); |
| 6074 | } |
| 6075 | |
| 6076 | /** |
| 6077 | * xmlAutomataNewCounterTrans: |
| 6078 | * @am: an automata |
| 6079 | * @from: the starting point of the transition |
| 6080 | * @to: the target point of the transition or NULL |
| 6081 | * @counter: the counter associated to that transition |
| 6082 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 6083 | * 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] | 6084 | * and then adds an epsilon transition from the @from state to the target state |
| 6085 | * which will be allowed only if the counter is within the right range. |
| 6086 | * |
| 6087 | * Returns the target state or NULL in case of error |
| 6088 | */ |
| 6089 | xmlAutomataStatePtr |
| 6090 | xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 6091 | xmlAutomataStatePtr to, int counter) { |
| 6092 | if ((am == NULL) || (from == NULL) || (counter < 0)) |
| 6093 | return(NULL); |
| 6094 | xmlFAGenerateCountedTransition(am, from, to, counter); |
| 6095 | if (to == NULL) |
| 6096 | return(am->state); |
| 6097 | return(to); |
| 6098 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6099 | |
| 6100 | /** |
| 6101 | * xmlAutomataCompile: |
| 6102 | * @am: an automata |
| 6103 | * |
| 6104 | * Compile the automata into a Reg Exp ready for being executed. |
| 6105 | * The automata should be free after this point. |
| 6106 | * |
| 6107 | * Returns the compiled regexp or NULL in case of error |
| 6108 | */ |
| 6109 | xmlRegexpPtr |
| 6110 | xmlAutomataCompile(xmlAutomataPtr am) { |
| 6111 | xmlRegexpPtr ret; |
| 6112 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 6113 | if ((am == NULL) || (am->error != 0)) return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6114 | xmlFAEliminateEpsilonTransitions(am); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 6115 | /* xmlFAComputesDeterminism(am); */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6116 | ret = xmlRegEpxFromParse(am); |
| 6117 | |
| 6118 | return(ret); |
| 6119 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 6120 | |
| 6121 | /** |
| 6122 | * xmlAutomataIsDeterminist: |
| 6123 | * @am: an automata |
| 6124 | * |
| 6125 | * Checks if an automata is determinist. |
| 6126 | * |
| 6127 | * Returns 1 if true, 0 if not, and -1 in case of error |
| 6128 | */ |
| 6129 | int |
| 6130 | xmlAutomataIsDeterminist(xmlAutomataPtr am) { |
| 6131 | int ret; |
| 6132 | |
| 6133 | if (am == NULL) |
| 6134 | return(-1); |
| 6135 | |
| 6136 | ret = xmlFAComputesDeterminism(am); |
| 6137 | return(ret); |
| 6138 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 6139 | #endif /* LIBXML_AUTOMATA_ENABLED */ |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6140 | |
| 6141 | #ifdef LIBXML_EXPR_ENABLED |
| 6142 | /************************************************************************ |
| 6143 | * * |
| 6144 | * Formal Expression handling code * |
| 6145 | * * |
| 6146 | ************************************************************************/ |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6147 | /************************************************************************ |
| 6148 | * * |
| 6149 | * Expression handling context * |
| 6150 | * * |
| 6151 | ************************************************************************/ |
| 6152 | |
| 6153 | struct _xmlExpCtxt { |
| 6154 | xmlDictPtr dict; |
| 6155 | xmlExpNodePtr *table; |
| 6156 | int size; |
| 6157 | int nbElems; |
| 6158 | int nb_nodes; |
| 6159 | const char *expr; |
| 6160 | const char *cur; |
| 6161 | int nb_cons; |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6162 | int tabSize; |
| 6163 | }; |
| 6164 | |
| 6165 | /** |
| 6166 | * xmlExpNewCtxt: |
| 6167 | * @maxNodes: the maximum number of nodes |
| 6168 | * @dict: optional dictionnary to use internally |
| 6169 | * |
| 6170 | * Creates a new context for manipulating expressions |
| 6171 | * |
| 6172 | * Returns the context or NULL in case of error |
| 6173 | */ |
| 6174 | xmlExpCtxtPtr |
| 6175 | xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) { |
| 6176 | xmlExpCtxtPtr ret; |
| 6177 | int size = 256; |
| 6178 | |
| 6179 | if (maxNodes <= 4096) |
| 6180 | maxNodes = 4096; |
| 6181 | |
| 6182 | ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt)); |
| 6183 | if (ret == NULL) |
| 6184 | return(NULL); |
| 6185 | memset(ret, 0, sizeof(xmlExpCtxt)); |
| 6186 | ret->size = size; |
| 6187 | ret->nbElems = 0; |
| 6188 | ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr)); |
| 6189 | if (ret->table == NULL) { |
| 6190 | xmlFree(ret); |
| 6191 | return(NULL); |
| 6192 | } |
| 6193 | memset(ret->table, 0, size * sizeof(xmlExpNodePtr)); |
| 6194 | if (dict == NULL) { |
| 6195 | ret->dict = xmlDictCreate(); |
| 6196 | if (ret->dict == NULL) { |
| 6197 | xmlFree(ret->table); |
| 6198 | xmlFree(ret); |
| 6199 | return(NULL); |
| 6200 | } |
| 6201 | } else { |
| 6202 | ret->dict = dict; |
| 6203 | xmlDictReference(ret->dict); |
| 6204 | } |
| 6205 | return(ret); |
| 6206 | } |
| 6207 | |
| 6208 | /** |
| 6209 | * xmlExpFreeCtxt: |
| 6210 | * @ctxt: an expression context |
| 6211 | * |
| 6212 | * Free an expression context |
| 6213 | */ |
| 6214 | void |
| 6215 | xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) { |
| 6216 | if (ctxt == NULL) |
| 6217 | return; |
| 6218 | xmlDictFree(ctxt->dict); |
| 6219 | if (ctxt->table != NULL) |
| 6220 | xmlFree(ctxt->table); |
| 6221 | xmlFree(ctxt); |
| 6222 | } |
| 6223 | |
| 6224 | /************************************************************************ |
| 6225 | * * |
| 6226 | * Structure associated to an expression node * |
| 6227 | * * |
| 6228 | ************************************************************************/ |
Daniel Veillard | 465a000 | 2005-08-22 12:07:04 +0000 | [diff] [blame] | 6229 | #define MAX_NODES 10000 |
| 6230 | |
| 6231 | /* #define DEBUG_DERIV */ |
| 6232 | |
| 6233 | /* |
| 6234 | * TODO: |
| 6235 | * - Wildcards |
| 6236 | * - public API for creation |
| 6237 | * |
| 6238 | * Started |
| 6239 | * - regression testing |
| 6240 | * |
| 6241 | * Done |
| 6242 | * - split into module and test tool |
| 6243 | * - memleaks |
| 6244 | */ |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6245 | |
| 6246 | typedef enum { |
| 6247 | XML_EXP_NILABLE = (1 << 0) |
| 6248 | } xmlExpNodeInfo; |
| 6249 | |
| 6250 | #define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE) |
| 6251 | |
| 6252 | struct _xmlExpNode { |
| 6253 | unsigned char type;/* xmlExpNodeType */ |
| 6254 | unsigned char info;/* OR of xmlExpNodeInfo */ |
| 6255 | unsigned short key; /* the hash key */ |
| 6256 | unsigned int ref; /* The number of references */ |
| 6257 | int c_max; /* the maximum length it can consume */ |
| 6258 | xmlExpNodePtr exp_left; |
| 6259 | xmlExpNodePtr next;/* the next node in the hash table or free list */ |
| 6260 | union { |
| 6261 | struct { |
| 6262 | int f_min; |
| 6263 | int f_max; |
| 6264 | } count; |
| 6265 | struct { |
| 6266 | xmlExpNodePtr f_right; |
| 6267 | } children; |
| 6268 | const xmlChar *f_str; |
| 6269 | } field; |
| 6270 | }; |
| 6271 | |
| 6272 | #define exp_min field.count.f_min |
| 6273 | #define exp_max field.count.f_max |
| 6274 | /* #define exp_left field.children.f_left */ |
| 6275 | #define exp_right field.children.f_right |
| 6276 | #define exp_str field.f_str |
| 6277 | |
| 6278 | static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type); |
| 6279 | static xmlExpNode forbiddenExpNode = { |
| 6280 | XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}} |
| 6281 | }; |
| 6282 | xmlExpNodePtr forbiddenExp = &forbiddenExpNode; |
| 6283 | static xmlExpNode emptyExpNode = { |
| 6284 | XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}} |
| 6285 | }; |
| 6286 | xmlExpNodePtr emptyExp = &emptyExpNode; |
| 6287 | |
| 6288 | /************************************************************************ |
| 6289 | * * |
| 6290 | * The custom hash table for unicity and canonicalization * |
| 6291 | * of sub-expressions pointers * |
| 6292 | * * |
| 6293 | ************************************************************************/ |
| 6294 | /* |
| 6295 | * xmlExpHashNameComputeKey: |
| 6296 | * Calculate the hash key for a token |
| 6297 | */ |
| 6298 | static unsigned short |
| 6299 | xmlExpHashNameComputeKey(const xmlChar *name) { |
| 6300 | unsigned short value = 0L; |
| 6301 | char ch; |
| 6302 | |
| 6303 | if (name != NULL) { |
| 6304 | value += 30 * (*name); |
| 6305 | while ((ch = *name++) != 0) { |
| 6306 | value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch); |
| 6307 | } |
| 6308 | } |
| 6309 | return (value); |
| 6310 | } |
| 6311 | |
| 6312 | /* |
| 6313 | * xmlExpHashComputeKey: |
| 6314 | * Calculate the hash key for a compound expression |
| 6315 | */ |
| 6316 | static unsigned short |
| 6317 | xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left, |
| 6318 | xmlExpNodePtr right) { |
| 6319 | unsigned long value; |
| 6320 | unsigned short ret; |
| 6321 | |
| 6322 | switch (type) { |
| 6323 | case XML_EXP_SEQ: |
| 6324 | value = left->key; |
| 6325 | value += right->key; |
| 6326 | value *= 3; |
| 6327 | ret = (unsigned short) value; |
| 6328 | break; |
| 6329 | case XML_EXP_OR: |
| 6330 | value = left->key; |
| 6331 | value += right->key; |
| 6332 | value *= 7; |
| 6333 | ret = (unsigned short) value; |
| 6334 | break; |
| 6335 | case XML_EXP_COUNT: |
| 6336 | value = left->key; |
| 6337 | value += right->key; |
| 6338 | ret = (unsigned short) value; |
| 6339 | break; |
| 6340 | default: |
| 6341 | ret = 0; |
| 6342 | } |
| 6343 | return(ret); |
| 6344 | } |
| 6345 | |
| 6346 | |
| 6347 | static xmlExpNodePtr |
| 6348 | xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) { |
| 6349 | xmlExpNodePtr ret; |
| 6350 | |
| 6351 | if (ctxt->nb_nodes >= MAX_NODES) |
| 6352 | return(NULL); |
| 6353 | ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode)); |
| 6354 | if (ret == NULL) |
| 6355 | return(NULL); |
| 6356 | memset(ret, 0, sizeof(xmlExpNode)); |
| 6357 | ret->type = type; |
| 6358 | ret->next = NULL; |
| 6359 | ctxt->nb_nodes++; |
| 6360 | ctxt->nb_cons++; |
| 6361 | return(ret); |
| 6362 | } |
| 6363 | |
| 6364 | /** |
| 6365 | * xmlExpHashGetEntry: |
| 6366 | * @table: the hash table |
| 6367 | * |
| 6368 | * Get the unique entry from the hash table. The entry is created if |
| 6369 | * needed. @left and @right are consumed, i.e. their ref count will |
| 6370 | * be decremented by the operation. |
| 6371 | * |
| 6372 | * Returns the pointer or NULL in case of error |
| 6373 | */ |
| 6374 | static xmlExpNodePtr |
| 6375 | xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type, |
| 6376 | xmlExpNodePtr left, xmlExpNodePtr right, |
| 6377 | const xmlChar *name, int min, int max) { |
| 6378 | unsigned short kbase, key; |
| 6379 | xmlExpNodePtr entry; |
| 6380 | xmlExpNodePtr insert; |
| 6381 | |
| 6382 | if (ctxt == NULL) |
| 6383 | return(NULL); |
| 6384 | |
| 6385 | /* |
| 6386 | * Check for duplicate and insertion location. |
| 6387 | */ |
| 6388 | if (type == XML_EXP_ATOM) { |
| 6389 | kbase = xmlExpHashNameComputeKey(name); |
| 6390 | } else if (type == XML_EXP_COUNT) { |
| 6391 | /* COUNT reduction rule 1 */ |
| 6392 | /* a{1} -> a */ |
| 6393 | if (min == max) { |
| 6394 | if (min == 1) { |
| 6395 | return(left); |
| 6396 | } |
| 6397 | if (min == 0) { |
| 6398 | xmlExpFree(ctxt, left); |
| 6399 | return(emptyExp); |
| 6400 | } |
| 6401 | } |
| 6402 | if (min < 0) { |
| 6403 | xmlExpFree(ctxt, left); |
| 6404 | return(forbiddenExp); |
| 6405 | } |
| 6406 | if (max == -1) |
| 6407 | kbase = min + 79; |
| 6408 | else |
| 6409 | kbase = max - min; |
| 6410 | kbase += left->key; |
| 6411 | } else if (type == XML_EXP_OR) { |
| 6412 | /* Forbid reduction rules */ |
| 6413 | if (left->type == XML_EXP_FORBID) { |
| 6414 | xmlExpFree(ctxt, left); |
| 6415 | return(right); |
| 6416 | } |
| 6417 | if (right->type == XML_EXP_FORBID) { |
| 6418 | xmlExpFree(ctxt, right); |
| 6419 | return(left); |
| 6420 | } |
| 6421 | |
| 6422 | /* OR reduction rule 1 */ |
| 6423 | /* a | a reduced to a */ |
| 6424 | if (left == right) { |
| 6425 | left->ref--; |
| 6426 | return(left); |
| 6427 | } |
| 6428 | /* OR canonicalization rule 1 */ |
| 6429 | /* linearize (a | b) | c into a | (b | c) */ |
| 6430 | if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) { |
| 6431 | xmlExpNodePtr tmp = left; |
| 6432 | left = right; |
| 6433 | right = tmp; |
| 6434 | } |
| 6435 | /* OR reduction rule 2 */ |
| 6436 | /* a | (a | b) and b | (a | b) are reduced to a | b */ |
| 6437 | if (right->type == XML_EXP_OR) { |
| 6438 | if ((left == right->exp_left) || |
| 6439 | (left == right->exp_right)) { |
| 6440 | xmlExpFree(ctxt, left); |
| 6441 | return(right); |
| 6442 | } |
| 6443 | } |
| 6444 | /* OR canonicalization rule 2 */ |
| 6445 | /* linearize (a | b) | c into a | (b | c) */ |
| 6446 | if (left->type == XML_EXP_OR) { |
| 6447 | xmlExpNodePtr tmp; |
| 6448 | |
| 6449 | /* OR canonicalization rule 2 */ |
| 6450 | if ((left->exp_right->type != XML_EXP_OR) && |
| 6451 | (left->exp_right->key < left->exp_left->key)) { |
| 6452 | tmp = left->exp_right; |
| 6453 | left->exp_right = left->exp_left; |
| 6454 | left->exp_left = tmp; |
| 6455 | } |
| 6456 | left->exp_right->ref++; |
| 6457 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right, |
| 6458 | NULL, 0, 0); |
| 6459 | left->exp_left->ref++; |
| 6460 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp, |
| 6461 | NULL, 0, 0); |
| 6462 | |
| 6463 | xmlExpFree(ctxt, left); |
| 6464 | return(tmp); |
| 6465 | } |
| 6466 | if (right->type == XML_EXP_OR) { |
| 6467 | /* Ordering in the tree */ |
| 6468 | /* C | (A | B) -> A | (B | C) */ |
| 6469 | if (left->key > right->exp_right->key) { |
| 6470 | xmlExpNodePtr tmp; |
| 6471 | right->exp_right->ref++; |
| 6472 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right, |
| 6473 | left, NULL, 0, 0); |
| 6474 | right->exp_left->ref++; |
| 6475 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left, |
| 6476 | tmp, NULL, 0, 0); |
| 6477 | xmlExpFree(ctxt, right); |
| 6478 | return(tmp); |
| 6479 | } |
| 6480 | /* Ordering in the tree */ |
| 6481 | /* B | (A | C) -> A | (B | C) */ |
| 6482 | if (left->key > right->exp_left->key) { |
| 6483 | xmlExpNodePtr tmp; |
| 6484 | right->exp_right->ref++; |
| 6485 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, |
| 6486 | right->exp_right, NULL, 0, 0); |
| 6487 | right->exp_left->ref++; |
| 6488 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left, |
| 6489 | tmp, NULL, 0, 0); |
| 6490 | xmlExpFree(ctxt, right); |
| 6491 | return(tmp); |
| 6492 | } |
| 6493 | } |
| 6494 | /* we know both types are != XML_EXP_OR here */ |
| 6495 | else if (left->key > right->key) { |
| 6496 | xmlExpNodePtr tmp = left; |
| 6497 | left = right; |
| 6498 | right = tmp; |
| 6499 | } |
| 6500 | kbase = xmlExpHashComputeKey(type, left, right); |
| 6501 | } else if (type == XML_EXP_SEQ) { |
| 6502 | /* Forbid reduction rules */ |
| 6503 | if (left->type == XML_EXP_FORBID) { |
| 6504 | xmlExpFree(ctxt, right); |
| 6505 | return(left); |
| 6506 | } |
| 6507 | if (right->type == XML_EXP_FORBID) { |
| 6508 | xmlExpFree(ctxt, left); |
| 6509 | return(right); |
| 6510 | } |
| 6511 | /* Empty reduction rules */ |
| 6512 | if (right->type == XML_EXP_EMPTY) { |
| 6513 | return(left); |
| 6514 | } |
| 6515 | if (left->type == XML_EXP_EMPTY) { |
| 6516 | return(right); |
| 6517 | } |
| 6518 | kbase = xmlExpHashComputeKey(type, left, right); |
| 6519 | } else |
| 6520 | return(NULL); |
| 6521 | |
| 6522 | key = kbase % ctxt->size; |
| 6523 | if (ctxt->table[key] != NULL) { |
| 6524 | for (insert = ctxt->table[key]; insert != NULL; |
| 6525 | insert = insert->next) { |
| 6526 | if ((insert->key == kbase) && |
| 6527 | (insert->type == type)) { |
| 6528 | if (type == XML_EXP_ATOM) { |
| 6529 | if (name == insert->exp_str) { |
| 6530 | insert->ref++; |
| 6531 | return(insert); |
| 6532 | } |
| 6533 | } else if (type == XML_EXP_COUNT) { |
| 6534 | if ((insert->exp_min == min) && (insert->exp_max == max) && |
| 6535 | (insert->exp_left == left)) { |
| 6536 | insert->ref++; |
| 6537 | left->ref--; |
| 6538 | return(insert); |
| 6539 | } |
| 6540 | } else if ((insert->exp_left == left) && |
| 6541 | (insert->exp_right == right)) { |
| 6542 | insert->ref++; |
| 6543 | left->ref--; |
| 6544 | right->ref--; |
| 6545 | return(insert); |
| 6546 | } |
| 6547 | } |
| 6548 | } |
| 6549 | } |
| 6550 | |
| 6551 | entry = xmlExpNewNode(ctxt, type); |
| 6552 | if (entry == NULL) |
| 6553 | return(NULL); |
| 6554 | entry->key = kbase; |
| 6555 | if (type == XML_EXP_ATOM) { |
| 6556 | entry->exp_str = name; |
| 6557 | entry->c_max = 1; |
| 6558 | } else if (type == XML_EXP_COUNT) { |
| 6559 | entry->exp_min = min; |
| 6560 | entry->exp_max = max; |
| 6561 | entry->exp_left = left; |
| 6562 | if ((min == 0) || (IS_NILLABLE(left))) |
| 6563 | entry->info |= XML_EXP_NILABLE; |
| 6564 | if (max < 0) |
| 6565 | entry->c_max = -1; |
| 6566 | else |
| 6567 | entry->c_max = max * entry->exp_left->c_max; |
| 6568 | } else { |
| 6569 | entry->exp_left = left; |
| 6570 | entry->exp_right = right; |
| 6571 | if (type == XML_EXP_OR) { |
| 6572 | if ((IS_NILLABLE(left)) || (IS_NILLABLE(right))) |
| 6573 | entry->info |= XML_EXP_NILABLE; |
| 6574 | if ((entry->exp_left->c_max == -1) || |
| 6575 | (entry->exp_right->c_max == -1)) |
| 6576 | entry->c_max = -1; |
| 6577 | else if (entry->exp_left->c_max > entry->exp_right->c_max) |
| 6578 | entry->c_max = entry->exp_left->c_max; |
| 6579 | else |
| 6580 | entry->c_max = entry->exp_right->c_max; |
| 6581 | } else { |
| 6582 | if ((IS_NILLABLE(left)) && (IS_NILLABLE(right))) |
| 6583 | entry->info |= XML_EXP_NILABLE; |
| 6584 | if ((entry->exp_left->c_max == -1) || |
| 6585 | (entry->exp_right->c_max == -1)) |
| 6586 | entry->c_max = -1; |
| 6587 | else |
| 6588 | entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max; |
| 6589 | } |
| 6590 | } |
| 6591 | entry->ref = 1; |
| 6592 | if (ctxt->table[key] != NULL) |
| 6593 | entry->next = ctxt->table[key]; |
| 6594 | |
| 6595 | ctxt->table[key] = entry; |
| 6596 | ctxt->nbElems++; |
| 6597 | |
| 6598 | return(entry); |
| 6599 | } |
| 6600 | |
| 6601 | /** |
| 6602 | * xmlExpFree: |
| 6603 | * @ctxt: the expression context |
| 6604 | * @exp: the expression |
| 6605 | * |
| 6606 | * Dereference the expression |
| 6607 | */ |
| 6608 | void |
| 6609 | xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) { |
| 6610 | if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp)) |
| 6611 | return; |
| 6612 | exp->ref--; |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6613 | if (exp->ref == 0) { |
| 6614 | unsigned short key; |
| 6615 | |
| 6616 | /* Unlink it first from the hash table */ |
| 6617 | key = exp->key % ctxt->size; |
| 6618 | if (ctxt->table[key] == exp) { |
| 6619 | ctxt->table[key] = exp->next; |
| 6620 | } else { |
| 6621 | xmlExpNodePtr tmp; |
| 6622 | |
| 6623 | tmp = ctxt->table[key]; |
| 6624 | while (tmp != NULL) { |
| 6625 | if (tmp->next == exp) { |
| 6626 | tmp->next = exp->next; |
| 6627 | break; |
| 6628 | } |
| 6629 | tmp = tmp->next; |
| 6630 | } |
| 6631 | } |
| 6632 | |
| 6633 | if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) { |
| 6634 | xmlExpFree(ctxt, exp->exp_left); |
| 6635 | xmlExpFree(ctxt, exp->exp_right); |
| 6636 | } else if (exp->type == XML_EXP_COUNT) { |
| 6637 | xmlExpFree(ctxt, exp->exp_left); |
| 6638 | } |
| 6639 | xmlFree(exp); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6640 | ctxt->nb_nodes--; |
| 6641 | } |
| 6642 | } |
| 6643 | |
| 6644 | /** |
| 6645 | * xmlExpRef: |
| 6646 | * @exp: the expression |
| 6647 | * |
| 6648 | * Increase the reference count of the expression |
| 6649 | */ |
| 6650 | void |
| 6651 | xmlExpRef(xmlExpNodePtr exp) { |
| 6652 | if (exp != NULL) |
| 6653 | exp->ref++; |
| 6654 | } |
| 6655 | |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 6656 | /** |
| 6657 | * xmlExpNewAtom: |
| 6658 | * @ctxt: the expression context |
| 6659 | * @name: the atom name |
| 6660 | * @len: the atom name lenght in byte (or -1); |
| 6661 | * |
| 6662 | * Get the atom associated to this name from that context |
| 6663 | * |
| 6664 | * Returns the node or NULL in case of error |
| 6665 | */ |
| 6666 | xmlExpNodePtr |
| 6667 | xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) { |
| 6668 | if ((ctxt == NULL) || (name == NULL)) |
| 6669 | return(NULL); |
| 6670 | name = xmlDictLookup(ctxt->dict, name, len); |
| 6671 | if (name == NULL) |
| 6672 | return(NULL); |
| 6673 | return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0)); |
| 6674 | } |
| 6675 | |
| 6676 | /** |
| 6677 | * xmlExpNewOr: |
| 6678 | * @ctxt: the expression context |
| 6679 | * @left: left expression |
| 6680 | * @right: right expression |
| 6681 | * |
| 6682 | * Get the atom associated to the choice @left | @right |
| 6683 | * Note that @left and @right are consumed in the operation, to keep |
| 6684 | * an handle on them use xmlExpRef() and use xmlExpFree() to release them, |
| 6685 | * this is true even in case of failure (unless ctxt == NULL). |
| 6686 | * |
| 6687 | * Returns the node or NULL in case of error |
| 6688 | */ |
| 6689 | xmlExpNodePtr |
| 6690 | xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 6691 | if (ctxt == NULL) |
| 6692 | return(NULL); |
| 6693 | if ((left == NULL) || (right == NULL)) { |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 6694 | xmlExpFree(ctxt, left); |
| 6695 | xmlExpFree(ctxt, right); |
| 6696 | return(NULL); |
| 6697 | } |
| 6698 | return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0)); |
| 6699 | } |
| 6700 | |
| 6701 | /** |
| 6702 | * xmlExpNewSeq: |
| 6703 | * @ctxt: the expression context |
| 6704 | * @left: left expression |
| 6705 | * @right: right expression |
| 6706 | * |
| 6707 | * Get the atom associated to the sequence @left , @right |
| 6708 | * Note that @left and @right are consumed in the operation, to keep |
| 6709 | * an handle on them use xmlExpRef() and use xmlExpFree() to release them, |
| 6710 | * this is true even in case of failure (unless ctxt == NULL). |
| 6711 | * |
| 6712 | * Returns the node or NULL in case of error |
| 6713 | */ |
| 6714 | xmlExpNodePtr |
| 6715 | xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 6716 | if (ctxt == NULL) |
| 6717 | return(NULL); |
| 6718 | if ((left == NULL) || (right == NULL)) { |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 6719 | xmlExpFree(ctxt, left); |
| 6720 | xmlExpFree(ctxt, right); |
| 6721 | return(NULL); |
| 6722 | } |
| 6723 | return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0)); |
| 6724 | } |
| 6725 | |
| 6726 | /** |
| 6727 | * xmlExpNewRange: |
| 6728 | * @ctxt: the expression context |
| 6729 | * @subset: the expression to be repeated |
| 6730 | * @min: the lower bound for the repetition |
| 6731 | * @max: the upper bound for the repetition, -1 means infinite |
| 6732 | * |
| 6733 | * Get the atom associated to the range (@subset){@min, @max} |
| 6734 | * Note that @subset is consumed in the operation, to keep |
| 6735 | * an handle on it use xmlExpRef() and use xmlExpFree() to release it, |
| 6736 | * this is true even in case of failure (unless ctxt == NULL). |
| 6737 | * |
| 6738 | * Returns the node or NULL in case of error |
| 6739 | */ |
| 6740 | xmlExpNodePtr |
| 6741 | xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) { |
Daniel Veillard | 11ce400 | 2006-03-10 00:36:23 +0000 | [diff] [blame] | 6742 | if (ctxt == NULL) |
| 6743 | return(NULL); |
| 6744 | if ((subset == NULL) || (min < 0) || (max < -1) || |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 6745 | ((max >= 0) && (min > max))) { |
| 6746 | xmlExpFree(ctxt, subset); |
| 6747 | return(NULL); |
| 6748 | } |
| 6749 | return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset, |
| 6750 | NULL, NULL, min, max)); |
| 6751 | } |
| 6752 | |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6753 | /************************************************************************ |
| 6754 | * * |
| 6755 | * Public API for operations on expressions * |
| 6756 | * * |
| 6757 | ************************************************************************/ |
| 6758 | |
| 6759 | static int |
| 6760 | xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
| 6761 | const xmlChar**list, int len, int nb) { |
| 6762 | int tmp, tmp2; |
| 6763 | tail: |
| 6764 | switch (exp->type) { |
| 6765 | case XML_EXP_EMPTY: |
| 6766 | return(0); |
| 6767 | case XML_EXP_ATOM: |
| 6768 | for (tmp = 0;tmp < nb;tmp++) |
| 6769 | if (list[tmp] == exp->exp_str) |
| 6770 | return(0); |
| 6771 | if (nb >= len) |
| 6772 | return(-2); |
| 6773 | list[nb++] = exp->exp_str; |
| 6774 | return(1); |
| 6775 | case XML_EXP_COUNT: |
| 6776 | exp = exp->exp_left; |
| 6777 | goto tail; |
| 6778 | case XML_EXP_SEQ: |
| 6779 | case XML_EXP_OR: |
| 6780 | tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb); |
| 6781 | if (tmp < 0) |
| 6782 | return(tmp); |
| 6783 | tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len, |
| 6784 | nb + tmp); |
| 6785 | if (tmp2 < 0) |
| 6786 | return(tmp2); |
| 6787 | return(tmp + tmp2); |
| 6788 | } |
| 6789 | return(-1); |
| 6790 | } |
| 6791 | |
| 6792 | /** |
| 6793 | * xmlExpGetLanguage: |
| 6794 | * @ctxt: the expression context |
| 6795 | * @exp: the expression |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6796 | * @langList: where to store the tokens |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6797 | * @len: the allocated lenght of @list |
| 6798 | * |
| 6799 | * Find all the strings used in @exp and store them in @list |
| 6800 | * |
| 6801 | * Returns the number of unique strings found, -1 in case of errors and |
| 6802 | * -2 if there is more than @len strings |
| 6803 | */ |
| 6804 | int |
| 6805 | xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6806 | const xmlChar**langList, int len) { |
| 6807 | if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0)) |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6808 | return(-1); |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6809 | return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0)); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6810 | } |
| 6811 | |
| 6812 | static int |
| 6813 | xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
| 6814 | const xmlChar**list, int len, int nb) { |
| 6815 | int tmp, tmp2; |
| 6816 | tail: |
| 6817 | switch (exp->type) { |
| 6818 | case XML_EXP_FORBID: |
| 6819 | return(0); |
| 6820 | case XML_EXP_EMPTY: |
| 6821 | return(0); |
| 6822 | case XML_EXP_ATOM: |
| 6823 | for (tmp = 0;tmp < nb;tmp++) |
| 6824 | if (list[tmp] == exp->exp_str) |
| 6825 | return(0); |
| 6826 | if (nb >= len) |
| 6827 | return(-2); |
| 6828 | list[nb++] = exp->exp_str; |
| 6829 | return(1); |
| 6830 | case XML_EXP_COUNT: |
| 6831 | exp = exp->exp_left; |
| 6832 | goto tail; |
| 6833 | case XML_EXP_SEQ: |
| 6834 | tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb); |
| 6835 | if (tmp < 0) |
| 6836 | return(tmp); |
| 6837 | if (IS_NILLABLE(exp->exp_left)) { |
| 6838 | tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len, |
| 6839 | nb + tmp); |
| 6840 | if (tmp2 < 0) |
| 6841 | return(tmp2); |
| 6842 | tmp += tmp2; |
| 6843 | } |
| 6844 | return(tmp); |
| 6845 | case XML_EXP_OR: |
| 6846 | tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb); |
| 6847 | if (tmp < 0) |
| 6848 | return(tmp); |
| 6849 | tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len, |
| 6850 | nb + tmp); |
| 6851 | if (tmp2 < 0) |
| 6852 | return(tmp2); |
| 6853 | return(tmp + tmp2); |
| 6854 | } |
| 6855 | return(-1); |
| 6856 | } |
| 6857 | |
| 6858 | /** |
| 6859 | * xmlExpGetStart: |
| 6860 | * @ctxt: the expression context |
| 6861 | * @exp: the expression |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6862 | * @tokList: where to store the tokens |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6863 | * @len: the allocated lenght of @list |
| 6864 | * |
| 6865 | * Find all the strings that appears at the start of the languages |
| 6866 | * accepted by @exp and store them in @list. E.g. for (a, b) | c |
| 6867 | * it will return the list [a, c] |
| 6868 | * |
| 6869 | * Returns the number of unique strings found, -1 in case of errors and |
| 6870 | * -2 if there is more than @len strings |
| 6871 | */ |
| 6872 | int |
| 6873 | xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6874 | const xmlChar**tokList, int len) { |
| 6875 | if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0)) |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6876 | return(-1); |
Daniel Veillard | 7802ba5 | 2005-10-27 11:56:20 +0000 | [diff] [blame] | 6877 | return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0)); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 6878 | } |
| 6879 | |
| 6880 | /** |
| 6881 | * xmlExpIsNillable: |
| 6882 | * @exp: the expression |
| 6883 | * |
| 6884 | * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce |
| 6885 | * |
| 6886 | * Returns 1 if nillable, 0 if not and -1 in case of error |
| 6887 | */ |
| 6888 | int |
| 6889 | xmlExpIsNillable(xmlExpNodePtr exp) { |
| 6890 | if (exp == NULL) |
| 6891 | return(-1); |
| 6892 | return(IS_NILLABLE(exp) != 0); |
| 6893 | } |
| 6894 | |
| 6895 | static xmlExpNodePtr |
| 6896 | xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str) |
| 6897 | { |
| 6898 | xmlExpNodePtr ret; |
| 6899 | |
| 6900 | switch (exp->type) { |
| 6901 | case XML_EXP_EMPTY: |
| 6902 | return(forbiddenExp); |
| 6903 | case XML_EXP_FORBID: |
| 6904 | return(forbiddenExp); |
| 6905 | case XML_EXP_ATOM: |
| 6906 | if (exp->exp_str == str) { |
| 6907 | #ifdef DEBUG_DERIV |
| 6908 | printf("deriv atom: equal => Empty\n"); |
| 6909 | #endif |
| 6910 | ret = emptyExp; |
| 6911 | } else { |
| 6912 | #ifdef DEBUG_DERIV |
| 6913 | printf("deriv atom: mismatch => forbid\n"); |
| 6914 | #endif |
| 6915 | /* TODO wildcards here */ |
| 6916 | ret = forbiddenExp; |
| 6917 | } |
| 6918 | return(ret); |
| 6919 | case XML_EXP_OR: { |
| 6920 | xmlExpNodePtr tmp; |
| 6921 | |
| 6922 | #ifdef DEBUG_DERIV |
| 6923 | printf("deriv or: => or(derivs)\n"); |
| 6924 | #endif |
| 6925 | tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str); |
| 6926 | if (tmp == NULL) { |
| 6927 | return(NULL); |
| 6928 | } |
| 6929 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str); |
| 6930 | if (ret == NULL) { |
| 6931 | xmlExpFree(ctxt, tmp); |
| 6932 | return(NULL); |
| 6933 | } |
| 6934 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, |
| 6935 | NULL, 0, 0); |
| 6936 | return(ret); |
| 6937 | } |
| 6938 | case XML_EXP_SEQ: |
| 6939 | #ifdef DEBUG_DERIV |
| 6940 | printf("deriv seq: starting with left\n"); |
| 6941 | #endif |
| 6942 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str); |
| 6943 | if (ret == NULL) { |
| 6944 | return(NULL); |
| 6945 | } else if (ret == forbiddenExp) { |
| 6946 | if (IS_NILLABLE(exp->exp_left)) { |
| 6947 | #ifdef DEBUG_DERIV |
| 6948 | printf("deriv seq: left failed but nillable\n"); |
| 6949 | #endif |
| 6950 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str); |
| 6951 | } |
| 6952 | } else { |
| 6953 | #ifdef DEBUG_DERIV |
| 6954 | printf("deriv seq: left match => sequence\n"); |
| 6955 | #endif |
| 6956 | exp->exp_right->ref++; |
| 6957 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right, |
| 6958 | NULL, 0, 0); |
| 6959 | } |
| 6960 | return(ret); |
| 6961 | case XML_EXP_COUNT: { |
| 6962 | int min, max; |
| 6963 | xmlExpNodePtr tmp; |
| 6964 | |
| 6965 | if (exp->exp_max == 0) |
| 6966 | return(forbiddenExp); |
| 6967 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str); |
| 6968 | if (ret == NULL) |
| 6969 | return(NULL); |
| 6970 | if (ret == forbiddenExp) { |
| 6971 | #ifdef DEBUG_DERIV |
| 6972 | printf("deriv count: pattern mismatch => forbid\n"); |
| 6973 | #endif |
| 6974 | return(ret); |
| 6975 | } |
| 6976 | if (exp->exp_max == 1) |
| 6977 | return(ret); |
| 6978 | if (exp->exp_max < 0) /* unbounded */ |
| 6979 | max = -1; |
| 6980 | else |
| 6981 | max = exp->exp_max - 1; |
| 6982 | if (exp->exp_min > 0) |
| 6983 | min = exp->exp_min - 1; |
| 6984 | else |
| 6985 | min = 0; |
| 6986 | exp->exp_left->ref++; |
| 6987 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL, |
| 6988 | NULL, min, max); |
| 6989 | if (ret == emptyExp) { |
| 6990 | #ifdef DEBUG_DERIV |
| 6991 | printf("deriv count: match to empty => new count\n"); |
| 6992 | #endif |
| 6993 | return(tmp); |
| 6994 | } |
| 6995 | #ifdef DEBUG_DERIV |
| 6996 | printf("deriv count: match => sequence with new count\n"); |
| 6997 | #endif |
| 6998 | return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp, |
| 6999 | NULL, 0, 0)); |
| 7000 | } |
| 7001 | } |
| 7002 | return(NULL); |
| 7003 | } |
| 7004 | |
| 7005 | /** |
| 7006 | * xmlExpStringDerive: |
| 7007 | * @ctxt: the expression context |
| 7008 | * @exp: the expression |
| 7009 | * @str: the string |
| 7010 | * @len: the string len in bytes if available |
| 7011 | * |
| 7012 | * Do one step of Brzozowski derivation of the expression @exp with |
| 7013 | * respect to the input string |
| 7014 | * |
| 7015 | * Returns the resulting expression or NULL in case of internal error |
| 7016 | */ |
| 7017 | xmlExpNodePtr |
| 7018 | xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
| 7019 | const xmlChar *str, int len) { |
| 7020 | const xmlChar *input; |
| 7021 | |
| 7022 | if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) { |
| 7023 | return(NULL); |
| 7024 | } |
| 7025 | /* |
| 7026 | * check the string is in the dictionnary, if yes use an interned |
| 7027 | * copy, otherwise we know it's not an acceptable input |
| 7028 | */ |
| 7029 | input = xmlDictExists(ctxt->dict, str, len); |
| 7030 | if (input == NULL) { |
| 7031 | return(forbiddenExp); |
| 7032 | } |
| 7033 | return(xmlExpStringDeriveInt(ctxt, exp, input)); |
| 7034 | } |
| 7035 | |
| 7036 | static int |
| 7037 | xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) { |
| 7038 | int ret = 1; |
| 7039 | |
| 7040 | if (sub->c_max == -1) { |
| 7041 | if (exp->c_max != -1) |
| 7042 | ret = 0; |
| 7043 | } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) { |
| 7044 | ret = 0; |
| 7045 | } |
| 7046 | #if 0 |
| 7047 | if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp))) |
| 7048 | ret = 0; |
| 7049 | #endif |
| 7050 | return(ret); |
| 7051 | } |
| 7052 | |
| 7053 | static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, |
| 7054 | xmlExpNodePtr sub); |
| 7055 | /** |
| 7056 | * xmlExpDivide: |
| 7057 | * @ctxt: the expressions context |
| 7058 | * @exp: the englobing expression |
| 7059 | * @sub: the subexpression |
| 7060 | * @mult: the multiple expression |
| 7061 | * @remain: the remain from the derivation of the multiple |
| 7062 | * |
| 7063 | * Check if exp is a multiple of sub, i.e. if there is a finite number n |
| 7064 | * so that sub{n} subsume exp |
| 7065 | * |
| 7066 | * Returns the multiple value if successful, 0 if it is not a multiple |
| 7067 | * and -1 in case of internel error. |
| 7068 | */ |
| 7069 | |
| 7070 | static int |
| 7071 | xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub, |
| 7072 | xmlExpNodePtr *mult, xmlExpNodePtr *remain) { |
| 7073 | int i; |
| 7074 | xmlExpNodePtr tmp, tmp2; |
| 7075 | |
| 7076 | if (mult != NULL) *mult = NULL; |
| 7077 | if (remain != NULL) *remain = NULL; |
| 7078 | if (exp->c_max == -1) return(0); |
| 7079 | if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0); |
| 7080 | |
| 7081 | for (i = 1;i <= exp->c_max;i++) { |
| 7082 | sub->ref++; |
| 7083 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, |
| 7084 | sub, NULL, NULL, i, i); |
| 7085 | if (tmp == NULL) { |
| 7086 | return(-1); |
| 7087 | } |
| 7088 | if (!xmlExpCheckCard(tmp, exp)) { |
| 7089 | xmlExpFree(ctxt, tmp); |
| 7090 | continue; |
| 7091 | } |
| 7092 | tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp); |
| 7093 | if (tmp2 == NULL) { |
| 7094 | xmlExpFree(ctxt, tmp); |
| 7095 | return(-1); |
| 7096 | } |
| 7097 | if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) { |
| 7098 | if (remain != NULL) |
| 7099 | *remain = tmp2; |
| 7100 | else |
| 7101 | xmlExpFree(ctxt, tmp2); |
| 7102 | if (mult != NULL) |
| 7103 | *mult = tmp; |
| 7104 | else |
| 7105 | xmlExpFree(ctxt, tmp); |
| 7106 | #ifdef DEBUG_DERIV |
| 7107 | printf("Divide succeeded %d\n", i); |
| 7108 | #endif |
| 7109 | return(i); |
| 7110 | } |
| 7111 | xmlExpFree(ctxt, tmp); |
| 7112 | xmlExpFree(ctxt, tmp2); |
| 7113 | } |
| 7114 | #ifdef DEBUG_DERIV |
| 7115 | printf("Divide failed\n"); |
| 7116 | #endif |
| 7117 | return(0); |
| 7118 | } |
| 7119 | |
| 7120 | /** |
| 7121 | * xmlExpExpDeriveInt: |
| 7122 | * @ctxt: the expressions context |
| 7123 | * @exp: the englobing expression |
| 7124 | * @sub: the subexpression |
| 7125 | * |
| 7126 | * Try to do a step of Brzozowski derivation but at a higher level |
| 7127 | * the input being a subexpression. |
| 7128 | * |
| 7129 | * Returns the resulting expression or NULL in case of internal error |
| 7130 | */ |
| 7131 | static xmlExpNodePtr |
| 7132 | xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) { |
| 7133 | xmlExpNodePtr ret, tmp, tmp2, tmp3; |
| 7134 | const xmlChar **tab; |
| 7135 | int len, i; |
| 7136 | |
| 7137 | /* |
| 7138 | * In case of equality and if the expression can only consume a finite |
| 7139 | * amount, then the derivation is empty |
| 7140 | */ |
| 7141 | if ((exp == sub) && (exp->c_max >= 0)) { |
| 7142 | #ifdef DEBUG_DERIV |
| 7143 | printf("Equal(exp, sub) and finite -> Empty\n"); |
| 7144 | #endif |
| 7145 | return(emptyExp); |
| 7146 | } |
| 7147 | /* |
| 7148 | * decompose sub sequence first |
| 7149 | */ |
| 7150 | if (sub->type == XML_EXP_EMPTY) { |
| 7151 | #ifdef DEBUG_DERIV |
| 7152 | printf("Empty(sub) -> Empty\n"); |
| 7153 | #endif |
| 7154 | exp->ref++; |
| 7155 | return(exp); |
| 7156 | } |
| 7157 | if (sub->type == XML_EXP_SEQ) { |
| 7158 | #ifdef DEBUG_DERIV |
| 7159 | printf("Seq(sub) -> decompose\n"); |
| 7160 | #endif |
| 7161 | tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left); |
| 7162 | if (tmp == NULL) |
| 7163 | return(NULL); |
| 7164 | if (tmp == forbiddenExp) |
| 7165 | return(tmp); |
| 7166 | ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right); |
| 7167 | xmlExpFree(ctxt, tmp); |
| 7168 | return(ret); |
| 7169 | } |
| 7170 | if (sub->type == XML_EXP_OR) { |
| 7171 | #ifdef DEBUG_DERIV |
| 7172 | printf("Or(sub) -> decompose\n"); |
| 7173 | #endif |
| 7174 | tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left); |
| 7175 | if (tmp == forbiddenExp) |
| 7176 | return(tmp); |
| 7177 | if (tmp == NULL) |
| 7178 | return(NULL); |
| 7179 | ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right); |
| 7180 | if ((ret == NULL) || (ret == forbiddenExp)) { |
| 7181 | xmlExpFree(ctxt, tmp); |
| 7182 | return(ret); |
| 7183 | } |
| 7184 | return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0)); |
| 7185 | } |
| 7186 | if (!xmlExpCheckCard(exp, sub)) { |
| 7187 | #ifdef DEBUG_DERIV |
| 7188 | printf("CheckCard(exp, sub) failed -> Forbid\n"); |
| 7189 | #endif |
| 7190 | return(forbiddenExp); |
| 7191 | } |
| 7192 | switch (exp->type) { |
| 7193 | case XML_EXP_EMPTY: |
| 7194 | if (sub == emptyExp) |
| 7195 | return(emptyExp); |
| 7196 | #ifdef DEBUG_DERIV |
| 7197 | printf("Empty(exp) -> Forbid\n"); |
| 7198 | #endif |
| 7199 | return(forbiddenExp); |
| 7200 | case XML_EXP_FORBID: |
| 7201 | #ifdef DEBUG_DERIV |
| 7202 | printf("Forbid(exp) -> Forbid\n"); |
| 7203 | #endif |
| 7204 | return(forbiddenExp); |
| 7205 | case XML_EXP_ATOM: |
| 7206 | if (sub->type == XML_EXP_ATOM) { |
| 7207 | /* TODO: handle wildcards */ |
| 7208 | if (exp->exp_str == sub->exp_str) { |
| 7209 | #ifdef DEBUG_DERIV |
| 7210 | printf("Atom match -> Empty\n"); |
| 7211 | #endif |
| 7212 | return(emptyExp); |
| 7213 | } |
| 7214 | #ifdef DEBUG_DERIV |
| 7215 | printf("Atom mismatch -> Forbid\n"); |
| 7216 | #endif |
| 7217 | return(forbiddenExp); |
| 7218 | } |
| 7219 | if ((sub->type == XML_EXP_COUNT) && |
| 7220 | (sub->exp_max == 1) && |
| 7221 | (sub->exp_left->type == XML_EXP_ATOM)) { |
| 7222 | /* TODO: handle wildcards */ |
| 7223 | if (exp->exp_str == sub->exp_left->exp_str) { |
| 7224 | #ifdef DEBUG_DERIV |
| 7225 | printf("Atom match -> Empty\n"); |
| 7226 | #endif |
| 7227 | return(emptyExp); |
| 7228 | } |
| 7229 | #ifdef DEBUG_DERIV |
| 7230 | printf("Atom mismatch -> Forbid\n"); |
| 7231 | #endif |
| 7232 | return(forbiddenExp); |
| 7233 | } |
| 7234 | #ifdef DEBUG_DERIV |
| 7235 | printf("Compex exp vs Atom -> Forbid\n"); |
| 7236 | #endif |
| 7237 | return(forbiddenExp); |
| 7238 | case XML_EXP_SEQ: |
| 7239 | /* try to get the sequence consumed only if possible */ |
| 7240 | if (xmlExpCheckCard(exp->exp_left, sub)) { |
| 7241 | /* See if the sequence can be consumed directly */ |
| 7242 | #ifdef DEBUG_DERIV |
| 7243 | printf("Seq trying left only\n"); |
| 7244 | #endif |
| 7245 | ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub); |
| 7246 | if ((ret != forbiddenExp) && (ret != NULL)) { |
| 7247 | #ifdef DEBUG_DERIV |
| 7248 | printf("Seq trying left only worked\n"); |
| 7249 | #endif |
| 7250 | /* |
| 7251 | * TODO: assumption here that we are determinist |
| 7252 | * i.e. we won't get to a nillable exp left |
| 7253 | * subset which could be matched by the right |
| 7254 | * part too. |
| 7255 | * e.g.: (a | b)+,(a | c) and 'a+,a' |
| 7256 | */ |
| 7257 | exp->exp_right->ref++; |
| 7258 | return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, |
| 7259 | exp->exp_right, NULL, 0, 0)); |
| 7260 | } |
| 7261 | #ifdef DEBUG_DERIV |
| 7262 | } else { |
| 7263 | printf("Seq: left too short\n"); |
| 7264 | #endif |
| 7265 | } |
| 7266 | /* Try instead to decompose */ |
| 7267 | if (sub->type == XML_EXP_COUNT) { |
| 7268 | int min, max; |
| 7269 | |
| 7270 | #ifdef DEBUG_DERIV |
| 7271 | printf("Seq: sub is a count\n"); |
| 7272 | #endif |
| 7273 | ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left); |
| 7274 | if (ret == NULL) |
| 7275 | return(NULL); |
| 7276 | if (ret != forbiddenExp) { |
| 7277 | #ifdef DEBUG_DERIV |
| 7278 | printf("Seq , Count match on left\n"); |
| 7279 | #endif |
| 7280 | if (sub->exp_max < 0) |
| 7281 | max = -1; |
| 7282 | else |
| 7283 | max = sub->exp_max -1; |
| 7284 | if (sub->exp_min > 0) |
| 7285 | min = sub->exp_min -1; |
| 7286 | else |
| 7287 | min = 0; |
| 7288 | exp->exp_right->ref++; |
| 7289 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, |
| 7290 | exp->exp_right, NULL, 0, 0); |
| 7291 | if (tmp == NULL) |
| 7292 | return(NULL); |
| 7293 | |
| 7294 | sub->exp_left->ref++; |
| 7295 | tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, |
| 7296 | sub->exp_left, NULL, NULL, min, max); |
| 7297 | if (tmp2 == NULL) { |
| 7298 | xmlExpFree(ctxt, tmp); |
| 7299 | return(NULL); |
| 7300 | } |
| 7301 | ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2); |
| 7302 | xmlExpFree(ctxt, tmp); |
| 7303 | xmlExpFree(ctxt, tmp2); |
| 7304 | return(ret); |
| 7305 | } |
| 7306 | } |
| 7307 | /* we made no progress on structured operations */ |
| 7308 | break; |
| 7309 | case XML_EXP_OR: |
| 7310 | #ifdef DEBUG_DERIV |
| 7311 | printf("Or , trying both side\n"); |
| 7312 | #endif |
| 7313 | ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub); |
| 7314 | if (ret == NULL) |
| 7315 | return(NULL); |
| 7316 | tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub); |
| 7317 | if (tmp == NULL) { |
| 7318 | xmlExpFree(ctxt, ret); |
| 7319 | return(NULL); |
| 7320 | } |
| 7321 | return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0)); |
| 7322 | case XML_EXP_COUNT: { |
| 7323 | int min, max; |
| 7324 | |
| 7325 | if (sub->type == XML_EXP_COUNT) { |
| 7326 | /* |
| 7327 | * Try to see if the loop is completely subsumed |
| 7328 | */ |
| 7329 | tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left); |
| 7330 | if (tmp == NULL) |
| 7331 | return(NULL); |
| 7332 | if (tmp == forbiddenExp) { |
| 7333 | int mult; |
| 7334 | |
| 7335 | #ifdef DEBUG_DERIV |
| 7336 | printf("Count, Count inner don't subsume\n"); |
| 7337 | #endif |
| 7338 | mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left, |
| 7339 | NULL, &tmp); |
| 7340 | if (mult <= 0) { |
| 7341 | #ifdef DEBUG_DERIV |
| 7342 | printf("Count, Count not multiple => forbidden\n"); |
| 7343 | #endif |
| 7344 | return(forbiddenExp); |
| 7345 | } |
| 7346 | if (sub->exp_max == -1) { |
| 7347 | max = -1; |
| 7348 | if (exp->exp_max == -1) { |
| 7349 | if (exp->exp_min <= sub->exp_min * mult) |
| 7350 | min = 0; |
| 7351 | else |
| 7352 | min = exp->exp_min - sub->exp_min * mult; |
| 7353 | } else { |
| 7354 | #ifdef DEBUG_DERIV |
| 7355 | printf("Count, Count finite can't subsume infinite\n"); |
| 7356 | #endif |
| 7357 | xmlExpFree(ctxt, tmp); |
| 7358 | return(forbiddenExp); |
| 7359 | } |
| 7360 | } else { |
| 7361 | if (exp->exp_max == -1) { |
| 7362 | #ifdef DEBUG_DERIV |
| 7363 | printf("Infinite loop consume mult finite loop\n"); |
| 7364 | #endif |
| 7365 | if (exp->exp_min > sub->exp_min * mult) { |
| 7366 | max = -1; |
| 7367 | min = exp->exp_min - sub->exp_min * mult; |
| 7368 | } else { |
| 7369 | max = -1; |
| 7370 | min = 0; |
| 7371 | } |
| 7372 | } else { |
| 7373 | if (exp->exp_max < sub->exp_max * mult) { |
| 7374 | #ifdef DEBUG_DERIV |
| 7375 | printf("loops max mult mismatch => forbidden\n"); |
| 7376 | #endif |
| 7377 | xmlExpFree(ctxt, tmp); |
| 7378 | return(forbiddenExp); |
| 7379 | } |
| 7380 | if (sub->exp_max * mult > exp->exp_min) |
| 7381 | min = 0; |
| 7382 | else |
| 7383 | min = exp->exp_min - sub->exp_max * mult; |
| 7384 | max = exp->exp_max - sub->exp_max * mult; |
| 7385 | } |
| 7386 | } |
| 7387 | } else if (!IS_NILLABLE(tmp)) { |
| 7388 | /* |
| 7389 | * TODO: loop here to try to grow if working on finite |
| 7390 | * blocks. |
| 7391 | */ |
| 7392 | #ifdef DEBUG_DERIV |
| 7393 | printf("Count, Count remain not nillable => forbidden\n"); |
| 7394 | #endif |
| 7395 | xmlExpFree(ctxt, tmp); |
| 7396 | return(forbiddenExp); |
| 7397 | } else if (sub->exp_max == -1) { |
| 7398 | if (exp->exp_max == -1) { |
| 7399 | if (exp->exp_min <= sub->exp_min) { |
| 7400 | #ifdef DEBUG_DERIV |
| 7401 | printf("Infinite loops Okay => COUNT(0,Inf)\n"); |
| 7402 | #endif |
| 7403 | max = -1; |
| 7404 | min = 0; |
| 7405 | } else { |
| 7406 | #ifdef DEBUG_DERIV |
| 7407 | printf("Infinite loops min => Count(X,Inf)\n"); |
| 7408 | #endif |
| 7409 | max = -1; |
| 7410 | min = exp->exp_min - sub->exp_min; |
| 7411 | } |
| 7412 | } else if (exp->exp_min > sub->exp_min) { |
| 7413 | #ifdef DEBUG_DERIV |
| 7414 | printf("loops min mismatch 1 => forbidden ???\n"); |
| 7415 | #endif |
| 7416 | xmlExpFree(ctxt, tmp); |
| 7417 | return(forbiddenExp); |
| 7418 | } else { |
| 7419 | max = -1; |
| 7420 | min = 0; |
| 7421 | } |
| 7422 | } else { |
| 7423 | if (exp->exp_max == -1) { |
| 7424 | #ifdef DEBUG_DERIV |
| 7425 | printf("Infinite loop consume finite loop\n"); |
| 7426 | #endif |
| 7427 | if (exp->exp_min > sub->exp_min) { |
| 7428 | max = -1; |
| 7429 | min = exp->exp_min - sub->exp_min; |
| 7430 | } else { |
| 7431 | max = -1; |
| 7432 | min = 0; |
| 7433 | } |
| 7434 | } else { |
| 7435 | if (exp->exp_max < sub->exp_max) { |
| 7436 | #ifdef DEBUG_DERIV |
| 7437 | printf("loops max mismatch => forbidden\n"); |
| 7438 | #endif |
| 7439 | xmlExpFree(ctxt, tmp); |
| 7440 | return(forbiddenExp); |
| 7441 | } |
| 7442 | if (sub->exp_max > exp->exp_min) |
| 7443 | min = 0; |
| 7444 | else |
| 7445 | min = exp->exp_min - sub->exp_max; |
| 7446 | max = exp->exp_max - sub->exp_max; |
| 7447 | } |
| 7448 | } |
| 7449 | #ifdef DEBUG_DERIV |
| 7450 | printf("loops match => SEQ(COUNT())\n"); |
| 7451 | #endif |
| 7452 | exp->exp_left->ref++; |
| 7453 | tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, |
| 7454 | NULL, NULL, min, max); |
| 7455 | if (tmp2 == NULL) { |
| 7456 | return(NULL); |
| 7457 | } |
| 7458 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2, |
| 7459 | NULL, 0, 0); |
| 7460 | return(ret); |
| 7461 | } |
| 7462 | tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub); |
| 7463 | if (tmp == NULL) |
| 7464 | return(NULL); |
| 7465 | if (tmp == forbiddenExp) { |
| 7466 | #ifdef DEBUG_DERIV |
| 7467 | printf("loop mismatch => forbidden\n"); |
| 7468 | #endif |
| 7469 | return(forbiddenExp); |
| 7470 | } |
| 7471 | if (exp->exp_min > 0) |
| 7472 | min = exp->exp_min - 1; |
| 7473 | else |
| 7474 | min = 0; |
| 7475 | if (exp->exp_max < 0) |
| 7476 | max = -1; |
| 7477 | else |
| 7478 | max = exp->exp_max - 1; |
| 7479 | |
| 7480 | #ifdef DEBUG_DERIV |
| 7481 | printf("loop match => SEQ(COUNT())\n"); |
| 7482 | #endif |
| 7483 | exp->exp_left->ref++; |
| 7484 | tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, |
| 7485 | NULL, NULL, min, max); |
| 7486 | if (tmp2 == NULL) |
| 7487 | return(NULL); |
| 7488 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2, |
| 7489 | NULL, 0, 0); |
| 7490 | return(ret); |
| 7491 | } |
| 7492 | } |
| 7493 | |
Daniel Veillard | ccb4d41 | 2005-08-23 13:41:17 +0000 | [diff] [blame] | 7494 | #ifdef DEBUG_DERIV |
| 7495 | printf("Fallback to derivative\n"); |
| 7496 | #endif |
| 7497 | if (IS_NILLABLE(sub)) { |
| 7498 | if (!(IS_NILLABLE(exp))) |
| 7499 | return(forbiddenExp); |
| 7500 | else |
| 7501 | ret = emptyExp; |
| 7502 | } else |
| 7503 | ret = NULL; |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7504 | /* |
| 7505 | * here the structured derivation made no progress so |
| 7506 | * we use the default token based derivation to force one more step |
| 7507 | */ |
| 7508 | if (ctxt->tabSize == 0) |
| 7509 | ctxt->tabSize = 40; |
| 7510 | |
| 7511 | tab = (const xmlChar **) xmlMalloc(ctxt->tabSize * |
| 7512 | sizeof(const xmlChar *)); |
| 7513 | if (tab == NULL) { |
| 7514 | return(NULL); |
| 7515 | } |
| 7516 | |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7517 | /* |
| 7518 | * collect all the strings accepted by the subexpression on input |
| 7519 | */ |
| 7520 | len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0); |
| 7521 | while (len < 0) { |
| 7522 | const xmlChar **temp; |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7523 | temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 * |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7524 | sizeof(const xmlChar *)); |
| 7525 | if (temp == NULL) { |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7526 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7527 | return(NULL); |
| 7528 | } |
| 7529 | tab = temp; |
| 7530 | ctxt->tabSize *= 2; |
| 7531 | len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0); |
| 7532 | } |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7533 | for (i = 0;i < len;i++) { |
| 7534 | tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]); |
| 7535 | if ((tmp == NULL) || (tmp == 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(tmp); |
| 7539 | } |
| 7540 | tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]); |
| 7541 | if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) { |
| 7542 | xmlExpFree(ctxt, tmp); |
| 7543 | xmlExpFree(ctxt, ret); |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7544 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7545 | return(tmp); |
| 7546 | } |
| 7547 | tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2); |
| 7548 | xmlExpFree(ctxt, tmp); |
| 7549 | xmlExpFree(ctxt, tmp2); |
| 7550 | |
| 7551 | if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) { |
| 7552 | xmlExpFree(ctxt, ret); |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7553 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7554 | return(tmp3); |
| 7555 | } |
| 7556 | |
| 7557 | if (ret == NULL) |
| 7558 | ret = tmp3; |
| 7559 | else { |
| 7560 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0); |
| 7561 | if (ret == NULL) { |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7562 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7563 | return(NULL); |
| 7564 | } |
| 7565 | } |
| 7566 | } |
Rob Richards | 54a8f67 | 2005-10-07 02:33:00 +0000 | [diff] [blame] | 7567 | xmlFree((xmlChar **) tab); |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7568 | return(ret); |
| 7569 | } |
| 7570 | |
| 7571 | /** |
Daniel Veillard | 0090bd5 | 2005-08-22 14:43:43 +0000 | [diff] [blame] | 7572 | * xmlExpExpDerive: |
| 7573 | * @ctxt: the expressions context |
| 7574 | * @exp: the englobing expression |
| 7575 | * @sub: the subexpression |
| 7576 | * |
| 7577 | * Evaluates the expression resulting from @exp consuming a sub expression @sub |
| 7578 | * Based on algebraic derivation and sometimes direct Brzozowski derivation |
| 7579 | * it usually tatkes less than linear time and can handle expressions generating |
| 7580 | * infinite languages. |
| 7581 | * |
| 7582 | * Returns the resulting expression or NULL in case of internal error, the |
| 7583 | * result must be freed |
| 7584 | */ |
| 7585 | xmlExpNodePtr |
| 7586 | xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) { |
| 7587 | if ((exp == NULL) || (ctxt == NULL) || (sub == NULL)) |
| 7588 | return(NULL); |
| 7589 | |
| 7590 | /* |
| 7591 | * O(1) speedups |
| 7592 | */ |
| 7593 | if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) { |
| 7594 | #ifdef DEBUG_DERIV |
| 7595 | printf("Sub nillable and not exp : can't subsume\n"); |
| 7596 | #endif |
| 7597 | return(forbiddenExp); |
| 7598 | } |
| 7599 | if (xmlExpCheckCard(exp, sub) == 0) { |
| 7600 | #ifdef DEBUG_DERIV |
| 7601 | printf("sub generate longuer sequances than exp : can't subsume\n"); |
| 7602 | #endif |
| 7603 | return(forbiddenExp); |
| 7604 | } |
| 7605 | return(xmlExpExpDeriveInt(ctxt, exp, sub)); |
| 7606 | } |
| 7607 | |
| 7608 | /** |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7609 | * xmlExpSubsume: |
| 7610 | * @ctxt: the expressions context |
| 7611 | * @exp: the englobing expression |
| 7612 | * @sub: the subexpression |
| 7613 | * |
| 7614 | * Check whether @exp accepts all the languages accexpted by @sub |
| 7615 | * the input being a subexpression. |
| 7616 | * |
| 7617 | * Returns 1 if true 0 if false and -1 in case of failure. |
| 7618 | */ |
| 7619 | int |
| 7620 | xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) { |
| 7621 | xmlExpNodePtr tmp; |
| 7622 | |
| 7623 | if ((exp == NULL) || (ctxt == NULL) || (sub == NULL)) |
| 7624 | return(-1); |
| 7625 | |
| 7626 | /* |
| 7627 | * TODO: speedup by checking the language of sub is a subset of the |
| 7628 | * language of exp |
| 7629 | */ |
| 7630 | /* |
| 7631 | * O(1) speedups |
| 7632 | */ |
| 7633 | if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) { |
| 7634 | #ifdef DEBUG_DERIV |
| 7635 | printf("Sub nillable and not exp : can't subsume\n"); |
| 7636 | #endif |
| 7637 | return(0); |
| 7638 | } |
| 7639 | if (xmlExpCheckCard(exp, sub) == 0) { |
| 7640 | #ifdef DEBUG_DERIV |
| 7641 | printf("sub generate longuer sequances than exp : can't subsume\n"); |
| 7642 | #endif |
| 7643 | return(0); |
| 7644 | } |
| 7645 | tmp = xmlExpExpDeriveInt(ctxt, exp, sub); |
| 7646 | #ifdef DEBUG_DERIV |
| 7647 | printf("Result derivation :\n"); |
| 7648 | PRINT_EXP(tmp); |
| 7649 | #endif |
| 7650 | if (tmp == NULL) |
| 7651 | return(-1); |
| 7652 | if (tmp == forbiddenExp) |
| 7653 | return(0); |
| 7654 | if (tmp == emptyExp) |
| 7655 | return(1); |
| 7656 | if ((tmp != NULL) && (IS_NILLABLE(tmp))) { |
| 7657 | xmlExpFree(ctxt, tmp); |
| 7658 | return(1); |
| 7659 | } |
| 7660 | xmlExpFree(ctxt, tmp); |
| 7661 | return(0); |
| 7662 | } |
Daniel Veillard | 465a000 | 2005-08-22 12:07:04 +0000 | [diff] [blame] | 7663 | |
| 7664 | /************************************************************************ |
| 7665 | * * |
| 7666 | * Parsing expression * |
| 7667 | * * |
| 7668 | ************************************************************************/ |
| 7669 | |
| 7670 | static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt); |
| 7671 | |
| 7672 | #undef CUR |
| 7673 | #define CUR (*ctxt->cur) |
| 7674 | #undef NEXT |
| 7675 | #define NEXT ctxt->cur++; |
| 7676 | #undef IS_BLANK |
| 7677 | #define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t')) |
| 7678 | #define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++; |
| 7679 | |
| 7680 | static int |
| 7681 | xmlExpParseNumber(xmlExpCtxtPtr ctxt) { |
| 7682 | int ret = 0; |
| 7683 | |
| 7684 | SKIP_BLANKS |
| 7685 | if (CUR == '*') { |
| 7686 | NEXT |
| 7687 | return(-1); |
| 7688 | } |
| 7689 | if ((CUR < '0') || (CUR > '9')) |
| 7690 | return(-1); |
| 7691 | while ((CUR >= '0') && (CUR <= '9')) { |
| 7692 | ret = ret * 10 + (CUR - '0'); |
| 7693 | NEXT |
| 7694 | } |
| 7695 | return(ret); |
| 7696 | } |
| 7697 | |
| 7698 | static xmlExpNodePtr |
| 7699 | xmlExpParseOr(xmlExpCtxtPtr ctxt) { |
| 7700 | const char *base; |
| 7701 | xmlExpNodePtr ret; |
| 7702 | const xmlChar *val; |
| 7703 | |
| 7704 | SKIP_BLANKS |
| 7705 | base = ctxt->cur; |
| 7706 | if (*ctxt->cur == '(') { |
| 7707 | NEXT |
| 7708 | ret = xmlExpParseExpr(ctxt); |
| 7709 | SKIP_BLANKS |
| 7710 | if (*ctxt->cur != ')') { |
| 7711 | fprintf(stderr, "unbalanced '(' : %s\n", base); |
| 7712 | xmlExpFree(ctxt, ret); |
| 7713 | return(NULL); |
| 7714 | } |
| 7715 | NEXT; |
| 7716 | SKIP_BLANKS |
| 7717 | goto parse_quantifier; |
| 7718 | } |
| 7719 | while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') && |
| 7720 | (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') && |
| 7721 | (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}')) |
| 7722 | NEXT; |
| 7723 | val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base); |
| 7724 | if (val == NULL) |
| 7725 | return(NULL); |
| 7726 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0); |
| 7727 | if (ret == NULL) |
| 7728 | return(NULL); |
| 7729 | SKIP_BLANKS |
| 7730 | parse_quantifier: |
| 7731 | if (CUR == '{') { |
| 7732 | int min, max; |
| 7733 | |
| 7734 | NEXT |
| 7735 | min = xmlExpParseNumber(ctxt); |
| 7736 | if (min < 0) { |
| 7737 | xmlExpFree(ctxt, ret); |
| 7738 | return(NULL); |
| 7739 | } |
| 7740 | SKIP_BLANKS |
| 7741 | if (CUR == ',') { |
| 7742 | NEXT |
| 7743 | max = xmlExpParseNumber(ctxt); |
| 7744 | SKIP_BLANKS |
| 7745 | } else |
| 7746 | max = min; |
| 7747 | if (CUR != '}') { |
| 7748 | xmlExpFree(ctxt, ret); |
| 7749 | return(NULL); |
| 7750 | } |
| 7751 | NEXT |
| 7752 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
| 7753 | min, max); |
| 7754 | SKIP_BLANKS |
| 7755 | } else if (CUR == '?') { |
| 7756 | NEXT |
| 7757 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
| 7758 | 0, 1); |
| 7759 | SKIP_BLANKS |
| 7760 | } else if (CUR == '+') { |
| 7761 | NEXT |
| 7762 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
| 7763 | 1, -1); |
| 7764 | SKIP_BLANKS |
| 7765 | } else if (CUR == '*') { |
| 7766 | NEXT |
| 7767 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL, |
| 7768 | 0, -1); |
| 7769 | SKIP_BLANKS |
| 7770 | } |
| 7771 | return(ret); |
| 7772 | } |
| 7773 | |
| 7774 | |
| 7775 | static xmlExpNodePtr |
| 7776 | xmlExpParseSeq(xmlExpCtxtPtr ctxt) { |
| 7777 | xmlExpNodePtr ret, right; |
| 7778 | |
| 7779 | ret = xmlExpParseOr(ctxt); |
| 7780 | SKIP_BLANKS |
| 7781 | while (CUR == '|') { |
| 7782 | NEXT |
| 7783 | right = xmlExpParseOr(ctxt); |
| 7784 | if (right == NULL) { |
| 7785 | xmlExpFree(ctxt, ret); |
| 7786 | return(NULL); |
| 7787 | } |
| 7788 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0); |
| 7789 | if (ret == NULL) |
| 7790 | return(NULL); |
| 7791 | } |
| 7792 | return(ret); |
| 7793 | } |
| 7794 | |
| 7795 | static xmlExpNodePtr |
| 7796 | xmlExpParseExpr(xmlExpCtxtPtr ctxt) { |
| 7797 | xmlExpNodePtr ret, right; |
| 7798 | |
| 7799 | ret = xmlExpParseSeq(ctxt); |
| 7800 | SKIP_BLANKS |
| 7801 | while (CUR == ',') { |
| 7802 | NEXT |
| 7803 | right = xmlExpParseSeq(ctxt); |
| 7804 | if (right == NULL) { |
| 7805 | xmlExpFree(ctxt, ret); |
| 7806 | return(NULL); |
| 7807 | } |
| 7808 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0); |
| 7809 | if (ret == NULL) |
| 7810 | return(NULL); |
| 7811 | } |
| 7812 | return(ret); |
| 7813 | } |
| 7814 | |
| 7815 | /** |
| 7816 | * xmlExpParse: |
| 7817 | * @ctxt: the expressions context |
| 7818 | * @expr: the 0 terminated string |
| 7819 | * |
| 7820 | * Minimal parser for regexps, it understand the following constructs |
| 7821 | * - string terminals |
| 7822 | * - choice operator | |
| 7823 | * - sequence operator , |
| 7824 | * - subexpressions (...) |
| 7825 | * - usual cardinality operators + * and ? |
| 7826 | * - finite sequences { min, max } |
| 7827 | * - infinite sequences { min, * } |
| 7828 | * There is minimal checkings made especially no checking on strings values |
| 7829 | * |
| 7830 | * Returns a new expression or NULL in case of failure |
| 7831 | */ |
| 7832 | xmlExpNodePtr |
| 7833 | xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) { |
| 7834 | xmlExpNodePtr ret; |
| 7835 | |
| 7836 | ctxt->expr = expr; |
| 7837 | ctxt->cur = expr; |
| 7838 | |
| 7839 | ret = xmlExpParseExpr(ctxt); |
| 7840 | SKIP_BLANKS |
| 7841 | if (*ctxt->cur != 0) { |
| 7842 | xmlExpFree(ctxt, ret); |
| 7843 | return(NULL); |
| 7844 | } |
| 7845 | return(ret); |
| 7846 | } |
| 7847 | |
| 7848 | static void |
| 7849 | xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) { |
| 7850 | xmlExpNodePtr c; |
| 7851 | |
| 7852 | if (expr == NULL) return; |
| 7853 | if (glob) xmlBufferWriteChar(buf, "("); |
| 7854 | switch (expr->type) { |
| 7855 | case XML_EXP_EMPTY: |
| 7856 | xmlBufferWriteChar(buf, "empty"); |
| 7857 | break; |
| 7858 | case XML_EXP_FORBID: |
| 7859 | xmlBufferWriteChar(buf, "forbidden"); |
| 7860 | break; |
| 7861 | case XML_EXP_ATOM: |
| 7862 | xmlBufferWriteCHAR(buf, expr->exp_str); |
| 7863 | break; |
| 7864 | case XML_EXP_SEQ: |
| 7865 | c = expr->exp_left; |
| 7866 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
| 7867 | xmlExpDumpInt(buf, c, 1); |
| 7868 | else |
| 7869 | xmlExpDumpInt(buf, c, 0); |
| 7870 | xmlBufferWriteChar(buf, " , "); |
| 7871 | c = expr->exp_right; |
| 7872 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
| 7873 | xmlExpDumpInt(buf, c, 1); |
| 7874 | else |
| 7875 | xmlExpDumpInt(buf, c, 0); |
| 7876 | break; |
| 7877 | case XML_EXP_OR: |
| 7878 | c = expr->exp_left; |
| 7879 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
| 7880 | xmlExpDumpInt(buf, c, 1); |
| 7881 | else |
| 7882 | xmlExpDumpInt(buf, c, 0); |
| 7883 | xmlBufferWriteChar(buf, " | "); |
| 7884 | c = expr->exp_right; |
| 7885 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
| 7886 | xmlExpDumpInt(buf, c, 1); |
| 7887 | else |
| 7888 | xmlExpDumpInt(buf, c, 0); |
| 7889 | break; |
| 7890 | case XML_EXP_COUNT: { |
| 7891 | char rep[40]; |
| 7892 | |
| 7893 | c = expr->exp_left; |
| 7894 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR)) |
| 7895 | xmlExpDumpInt(buf, c, 1); |
| 7896 | else |
| 7897 | xmlExpDumpInt(buf, c, 0); |
| 7898 | if ((expr->exp_min == 0) && (expr->exp_max == 1)) { |
| 7899 | rep[0] = '?'; |
| 7900 | rep[1] = 0; |
| 7901 | } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) { |
| 7902 | rep[0] = '*'; |
| 7903 | rep[1] = 0; |
| 7904 | } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) { |
| 7905 | rep[0] = '+'; |
| 7906 | rep[1] = 0; |
| 7907 | } else if (expr->exp_max == expr->exp_min) { |
| 7908 | snprintf(rep, 39, "{%d}", expr->exp_min); |
| 7909 | } else if (expr->exp_max < 0) { |
| 7910 | snprintf(rep, 39, "{%d,inf}", expr->exp_min); |
| 7911 | } else { |
| 7912 | snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max); |
| 7913 | } |
| 7914 | rep[39] = 0; |
| 7915 | xmlBufferWriteChar(buf, rep); |
| 7916 | break; |
| 7917 | } |
| 7918 | default: |
| 7919 | fprintf(stderr, "Error in tree\n"); |
| 7920 | } |
| 7921 | if (glob) |
| 7922 | xmlBufferWriteChar(buf, ")"); |
| 7923 | } |
| 7924 | /** |
| 7925 | * xmlExpDump: |
| 7926 | * @buf: a buffer to receive the output |
| 7927 | * @expr: the compiled expression |
| 7928 | * |
| 7929 | * Serialize the expression as compiled to the buffer |
| 7930 | */ |
| 7931 | void |
Daniel Veillard | 5eee767 | 2005-08-22 21:22:27 +0000 | [diff] [blame] | 7932 | xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) { |
| 7933 | if ((buf == NULL) || (expr == NULL)) |
Daniel Veillard | 465a000 | 2005-08-22 12:07:04 +0000 | [diff] [blame] | 7934 | return; |
Daniel Veillard | 5eee767 | 2005-08-22 21:22:27 +0000 | [diff] [blame] | 7935 | xmlExpDumpInt(buf, expr, 0); |
Daniel Veillard | 465a000 | 2005-08-22 12:07:04 +0000 | [diff] [blame] | 7936 | } |
| 7937 | |
| 7938 | /** |
| 7939 | * xmlExpMaxToken: |
| 7940 | * @expr: a compiled expression |
| 7941 | * |
| 7942 | * Indicate the maximum number of input a expression can accept |
| 7943 | * |
| 7944 | * Returns the maximum length or -1 in case of error |
| 7945 | */ |
| 7946 | int |
| 7947 | xmlExpMaxToken(xmlExpNodePtr expr) { |
| 7948 | if (expr == NULL) |
| 7949 | return(-1); |
| 7950 | return(expr->c_max); |
| 7951 | } |
| 7952 | |
| 7953 | /** |
| 7954 | * xmlExpCtxtNbNodes: |
| 7955 | * @ctxt: an expression context |
| 7956 | * |
| 7957 | * Debugging facility provides the number of allocated nodes at a that point |
| 7958 | * |
| 7959 | * Returns the number of nodes in use or -1 in case of error |
| 7960 | */ |
| 7961 | int |
| 7962 | xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) { |
| 7963 | if (ctxt == NULL) |
| 7964 | return(-1); |
| 7965 | return(ctxt->nb_nodes); |
| 7966 | } |
| 7967 | |
| 7968 | /** |
| 7969 | * xmlExpCtxtNbCons: |
| 7970 | * @ctxt: an expression context |
| 7971 | * |
| 7972 | * Debugging facility provides the number of allocated nodes over lifetime |
| 7973 | * |
| 7974 | * Returns the number of nodes ever allocated or -1 in case of error |
| 7975 | */ |
| 7976 | int |
| 7977 | xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) { |
| 7978 | if (ctxt == NULL) |
| 7979 | return(-1); |
| 7980 | return(ctxt->nb_cons); |
| 7981 | } |
| 7982 | |
Daniel Veillard | 81a8ec6 | 2005-08-22 00:20:58 +0000 | [diff] [blame] | 7983 | #endif /* LIBXML_EXPR_ENABLED */ |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 7984 | #define bottom_xmlregexp |
| 7985 | #include "elfgcchack.h" |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 7986 | #endif /* LIBXML_REGEXP_ENABLED */ |