2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Code in this file is based on files:
23 * from Mozilla project, released under LGPL 2.1 or later.
25 * The Original Code is Mozilla Communicator client code, released
28 * The Initial Developer of the Original Code is
29 * Netscape Communications Corporation.
30 * Portions created by the Initial Developer are Copyright (C) 1998
31 * the Initial Developer. All Rights Reserved.
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(jscript);
43 #define JSREG_FOLD 0x01 /* fold uppercase to lowercase */
44 #define JSREG_GLOB 0x02 /* global exec, creates array of matches */
45 #define JSREG_MULTILINE 0x04 /* treat ^ and $ as begin and end of line */
46 #define JSREG_STICKY 0x08 /* only match starting at lastIndex */
48 typedef BYTE JSPackedBool;
49 typedef BYTE jsbytecode;
52 * This struct holds a bitmap representation of a class from a regexp.
53 * There's a list of these referenced by the classList field in the JSRegExp
54 * struct below. The initial state has startIndex set to the offset in the
55 * original regexp source of the beginning of the class contents. The first
56 * use of the class converts the source representation into a bitmap.
59 typedef struct RECharSet {
60 JSPackedBool converted;
73 WORD flags; /* flags, see jsapi.h's JSREG_* defines */
74 size_t parenCount; /* number of parenthesized submatches */
75 size_t classCount; /* count [...] bitmaps */
76 RECharSet *classList; /* list of [...] bitmaps */
77 BSTR source; /* locked source string, sans // */
78 jsbytecode program[1]; /* regular expression bytecode */
87 VARIANT last_index_var;
90 static const WCHAR sourceW[] = {'s','o','u','r','c','e',0};
91 static const WCHAR globalW[] = {'g','l','o','b','a','l',0};
92 static const WCHAR ignoreCaseW[] = {'i','g','n','o','r','e','C','a','s','e',0};
93 static const WCHAR multilineW[] = {'m','u','l','t','i','l','i','n','e',0};
94 static const WCHAR lastIndexW[] = {'l','a','s','t','I','n','d','e','x',0};
95 static const WCHAR toStringW[] = {'t','o','S','t','r','i','n','g',0};
96 static const WCHAR execW[] = {'e','x','e','c',0};
97 static const WCHAR testW[] = {'t','e','s','t',0};
99 static const WCHAR emptyW[] = {0};
101 /* FIXME: Better error handling */
102 #define ReportRegExpError(a,b,c)
103 #define ReportRegExpErrorHelper(a,b,c,d)
104 #define JS_ReportErrorNumber(a,b,c,d)
105 #define JS_ReportErrorFlagsAndNumber(a,b,c,d,e,f)
106 #define js_ReportOutOfScriptQuota(a)
107 #define JS_ReportOutOfMemory(a)
108 #define JS_COUNT_OPERATION(a,b)
110 #define JSMSG_MIN_TOO_BIG 47
111 #define JSMSG_MAX_TOO_BIG 48
112 #define JSMSG_OUT_OF_ORDER 49
113 #define JSMSG_OUT_OF_MEMORY 137
115 #define LINE_SEPARATOR 0x2028
116 #define PARA_SEPARATOR 0x2029
118 #define RE_IS_LETTER(c) (((c >= 'A') && (c <= 'Z')) || \
119 ((c >= 'a') && (c <= 'z')) )
120 #define RE_IS_LINE_TERM(c) ((c == '\n') || (c == '\r') || \
121 (c == LINE_SEPARATOR) || (c == PARA_SEPARATOR))
123 #define JS_ISWORD(c) ((c) < 128 && (isalnum(c) || (c) == '_'))
125 #define JS7_ISDEC(c) ((((unsigned)(c)) - '0') <= 9)
126 #define JS7_UNDEC(c) ((c) - '0')
178 REOP_LIMIT /* META: no operator >= to this */
181 #define REOP_IS_SIMPLE(op) ((op) <= REOP_NCLASS)
183 static const char *reop_names[] = {
236 typedef struct RECapture {
237 ptrdiff_t index; /* start of contents, -1 for empty */
238 size_t length; /* length of capture */
241 typedef struct REMatchState {
243 RECapture parens[1]; /* first of 're->parenCount' captures,
244 allocated at end of this struct */
247 typedef struct REProgState {
248 jsbytecode *continue_pc; /* current continuation data */
249 jsbytecode continue_op;
250 ptrdiff_t index; /* progress in text */
251 size_t parenSoFar; /* highest indexed paren started */
254 UINT min; /* current quantifier limits */
258 size_t top; /* backtrack stack state */
264 typedef struct REBackTrackData {
265 size_t sz; /* size of previous stack entry */
266 jsbytecode *backtrack_pc; /* where to backtrack to */
267 jsbytecode backtrack_op;
268 const WCHAR *cp; /* index in text of match at backtrack */
269 size_t parenIndex; /* start index of saved paren contents */
270 size_t parenCount; /* # of saved paren contents */
271 size_t saveStateStackTop; /* number of parent states */
272 /* saved parent states follow */
273 /* saved paren contents follow */
276 #define INITIAL_STATESTACK 100
277 #define INITIAL_BACKTRACK 8000
279 typedef struct REGlobalData {
281 JSRegExp *regexp; /* the RE in execution */
282 BOOL ok; /* runtime error (out_of_memory only?) */
283 size_t start; /* offset to start at */
284 ptrdiff_t skipped; /* chars skipped anchoring this r.e. */
285 const WCHAR *cpbegin; /* text base address */
286 const WCHAR *cpend; /* text limit address */
288 REProgState *stateStack; /* stack of state of current parents */
289 size_t stateStackTop;
290 size_t stateStackLimit;
292 REBackTrackData *backTrackStack;/* stack of matched-so-far positions */
293 REBackTrackData *backTrackSP;
294 size_t backTrackStackSize;
295 size_t cursz; /* size of current stack entry */
296 size_t backTrackCount; /* how many times we've backtracked */
297 size_t backTrackLimit; /* upper limit on backtrack states */
299 jsheap_t *pool; /* It's faster to use one malloc'd pool
300 than to malloc/free the three items
301 that are allocated from this pool */
304 typedef struct RENode RENode;
306 REOp op; /* r.e. op bytecode */
307 RENode *next; /* next in concatenation order */
308 void *kid; /* first operand */
310 void *kid2; /* second operand */
311 INT num; /* could be a number */
312 size_t parenIndex; /* or a parenthesis index */
313 struct { /* or a quantifier range */
318 struct { /* or a character class */
320 size_t kidlen; /* length of string at kid, in jschars */
321 size_t index; /* index into class list */
322 WORD bmsize; /* bitmap size, based on max char code */
325 struct { /* or a literal sequence */
326 WCHAR chr; /* of one character */
327 size_t length; /* or many (via the kid) */
330 RENode *kid2; /* second operand from ALT */
331 WCHAR ch1; /* match char for ALTPREREQ */
332 WCHAR ch2; /* ditto, or class index for ALTPREREQ2 */
337 #define CLASS_CACHE_SIZE 4
339 typedef struct CompilerState {
340 script_ctx_t *context;
341 const WCHAR *cpbegin;
345 size_t classCount; /* number of [] encountered */
346 size_t treeDepth; /* maximum depth of parse tree */
347 size_t progLength; /* estimated bytecode length */
349 size_t classBitmapsMem; /* memory to hold all class bitmaps */
351 const WCHAR *start; /* small cache of class strings */
352 size_t length; /* since they're often the same */
354 } classCache[CLASS_CACHE_SIZE];
358 typedef struct EmitStateStackEntry {
359 jsbytecode *altHead; /* start of REOP_ALT* opcode */
360 jsbytecode *nextAltFixup; /* fixup pointer to next-alt offset */
361 jsbytecode *nextTermFixup; /* fixup ptr. to REOP_JUMP offset */
362 jsbytecode *endTermFixup; /* fixup ptr. to REOPT_ALTPREREQ* offset */
363 RENode *continueNode; /* original REOP_ALT* node being stacked */
364 jsbytecode continueOp; /* REOP_JUMP or REOP_ENDALT continuation */
365 JSPackedBool jumpToJumpFlag; /* true if we've patched jump-to-jump to
366 avoid 16-bit unsigned offset overflow */
367 } EmitStateStackEntry;
370 * Immediate operand sizes and getter/setters. Unlike the ones in jsopcode.h,
371 * the getters and setters take the pc of the offset, not of the opcode before
375 #define GET_ARG(pc) ((WORD)(((pc)[0] << 8) | (pc)[1]))
376 #define SET_ARG(pc, arg) ((pc)[0] = (jsbytecode) ((arg) >> 8), \
377 (pc)[1] = (jsbytecode) (arg))
379 #define OFFSET_LEN ARG_LEN
380 #define OFFSET_MAX ((1 << (ARG_LEN * 8)) - 1)
381 #define GET_OFFSET(pc) GET_ARG(pc)
383 static BOOL ParseRegExp(CompilerState*);
386 * Maximum supported tree depth is maximum size of EmitStateStackEntry stack.
387 * For sanity, we limit it to 2^24 bytes.
389 #define TREE_DEPTH_MAX ((1 << 24) / sizeof(EmitStateStackEntry))
392 * The maximum memory that can be allocated for class bitmaps.
393 * For sanity, we limit it to 2^24 bytes.
395 #define CLASS_BITMAPS_MEM_LIMIT (1 << 24)
398 * Functions to get size and write/read bytecode that represent small indexes
400 * Each byte in the code represent 7-bit chunk of the index. 8th bit when set
401 * indicates that the following byte brings more bits to the index. Otherwise
402 * this is the last byte in the index bytecode representing highest index bits.
405 GetCompactIndexWidth(size_t index)
409 for (width = 1; (index >>= 7) != 0; ++width) { }
413 static inline jsbytecode *
414 WriteCompactIndex(jsbytecode *pc, size_t index)
418 while ((next = index >> 7) != 0) {
419 *pc++ = (jsbytecode)(index | 0x80);
422 *pc++ = (jsbytecode)index;
426 static inline jsbytecode *
427 ReadCompactIndex(jsbytecode *pc, size_t *result)
432 if ((nextByte & 0x80) == 0) {
434 * Short-circuit the most common case when compact index <= 127.
439 *result = 0x7F & nextByte;
442 *result |= (nextByte & 0x7F) << shift;
444 } while ((nextByte & 0x80) != 0);
449 /* Construct and initialize an RENode, returning NULL for out-of-memory */
451 NewRENode(CompilerState *state, REOp op)
455 ren = jsheap_alloc(&state->context->tmp_heap, sizeof(*ren));
457 /* js_ReportOutOfScriptQuota(cx); */
467 * Validates and converts hex ascii value.
470 isASCIIHexDigit(WCHAR c, UINT *digit)
481 if (cv >= 'a' && cv <= 'f') {
482 *digit = cv - 'a' + 10;
494 #define JUMP_OFFSET_HI(off) ((jsbytecode)((off) >> 8))
495 #define JUMP_OFFSET_LO(off) ((jsbytecode)(off))
498 SetForwardJumpOffset(jsbytecode *jump, jsbytecode *target)
500 ptrdiff_t offset = target - jump;
502 /* Check that target really points forward. */
504 if ((size_t)offset > OFFSET_MAX)
507 jump[0] = JUMP_OFFSET_HI(offset);
508 jump[1] = JUMP_OFFSET_LO(offset);
513 * Generate bytecode for the tree rooted at t using an explicit stack instead
517 EmitREBytecode(CompilerState *state, JSRegExp *re, size_t treeDepth,
518 jsbytecode *pc, RENode *t)
520 EmitStateStackEntry *emitStateSP, *emitStateStack;
524 if (treeDepth == 0) {
525 emitStateStack = NULL;
527 emitStateStack = heap_alloc(sizeof(EmitStateStackEntry) * treeDepth);
531 emitStateSP = emitStateStack;
533 assert(op < REOP_LIMIT);
542 case REOP_ALTPREREQ2:
545 emitStateSP->altHead = pc - 1;
546 emitStateSP->endTermFixup = pc;
548 SET_ARG(pc, t->u.altprereq.ch1);
550 SET_ARG(pc, t->u.altprereq.ch2);
553 emitStateSP->nextAltFixup = pc; /* offset to next alternate */
556 emitStateSP->continueNode = t;
557 emitStateSP->continueOp = REOP_JUMP;
558 emitStateSP->jumpToJumpFlag = FALSE;
560 assert((size_t)(emitStateSP - emitStateStack) <= treeDepth);
563 assert(op < REOP_LIMIT);
567 emitStateSP->nextTermFixup = pc; /* offset to following term */
569 if (!SetForwardJumpOffset(emitStateSP->nextAltFixup, pc))
571 emitStateSP->continueOp = REOP_ENDALT;
573 assert((size_t)(emitStateSP - emitStateStack) <= treeDepth);
576 assert(op < REOP_LIMIT);
581 * If we already patched emitStateSP->nextTermFixup to jump to
582 * a nearer jump, to avoid 16-bit immediate offset overflow, we
585 if (emitStateSP->jumpToJumpFlag)
589 * Fix up the REOP_JUMP offset to go to the op after REOP_ENDALT.
590 * REOP_ENDALT is executed only on successful match of the last
591 * alternate in a group.
593 if (!SetForwardJumpOffset(emitStateSP->nextTermFixup, pc))
595 if (t->op != REOP_ALT) {
596 if (!SetForwardJumpOffset(emitStateSP->endTermFixup, pc))
601 * If the program is bigger than the REOP_JUMP offset range, then
602 * we must check for alternates before this one that are part of
603 * the same group, and fix up their jump offsets to target jumps
604 * close enough to fit in a 16-bit unsigned offset immediate.
606 if ((size_t)(pc - re->program) > OFFSET_MAX &&
607 emitStateSP > emitStateStack) {
608 EmitStateStackEntry *esp, *esp2;
609 jsbytecode *alt, *jump;
610 ptrdiff_t span, header;
614 for (esp = esp2 - 1; esp >= emitStateStack; --esp) {
615 if (esp->continueOp == REOP_ENDALT &&
616 !esp->jumpToJumpFlag &&
617 esp->nextTermFixup + OFFSET_LEN == alt &&
618 (size_t)(pc - ((esp->continueNode->op != REOP_ALT)
620 : esp->nextTermFixup)) > OFFSET_MAX) {
622 jump = esp->nextTermFixup;
625 * The span must be 1 less than the distance from
626 * jump offset to jump offset, so we actually jump
627 * to a REOP_JUMP bytecode, not to its offset!
630 assert(jump < esp2->nextTermFixup);
631 span = esp2->nextTermFixup - jump - 1;
632 if ((size_t)span <= OFFSET_MAX)
637 } while (esp2->continueOp != REOP_ENDALT);
640 jump[0] = JUMP_OFFSET_HI(span);
641 jump[1] = JUMP_OFFSET_LO(span);
643 if (esp->continueNode->op != REOP_ALT) {
645 * We must patch the offset at esp->endTermFixup
646 * as well, for the REOP_ALTPREREQ{,2} opcodes.
647 * If we're unlucky and endTermFixup is more than
648 * OFFSET_MAX bytes from its target, we cheat by
649 * jumping 6 bytes to the jump whose offset is at
650 * esp->nextTermFixup, which has the same target.
652 jump = esp->endTermFixup;
653 header = esp->nextTermFixup - jump;
655 if ((size_t)span > OFFSET_MAX)
658 jump[0] = JUMP_OFFSET_HI(span);
659 jump[1] = JUMP_OFFSET_LO(span);
662 esp->jumpToJumpFlag = TRUE;
670 emitStateSP->altHead = pc - 1;
671 emitStateSP->nextAltFixup = pc; /* offset to next alternate */
673 emitStateSP->continueNode = t;
674 emitStateSP->continueOp = REOP_JUMP;
675 emitStateSP->jumpToJumpFlag = FALSE;
677 assert((size_t)(emitStateSP - emitStateStack) <= treeDepth);
680 assert(op < REOP_LIMIT);
685 * Coalesce FLATs if possible and if it would not increase bytecode
686 * beyond preallocated limit. The latter happens only when bytecode
687 * size for coalesced string with offset p and length 2 exceeds 6
688 * bytes preallocated for 2 single char nodes, i.e. when
689 * 1 + GetCompactIndexWidth(p) + GetCompactIndexWidth(2) > 6 or
690 * GetCompactIndexWidth(p) > 4.
691 * Since when GetCompactIndexWidth(p) <= 4 coalescing of 3 or more
692 * nodes strictly decreases bytecode size, the check has to be
693 * done only for the first coalescing.
696 GetCompactIndexWidth((WCHAR*)t->kid - state->cpbegin) <= 4)
699 t->next->op == REOP_FLAT &&
700 (WCHAR*)t->kid + t->u.flat.length ==
702 t->u.flat.length += t->next->u.flat.length;
703 t->next = t->next->next;
706 if (t->kid && t->u.flat.length > 1) {
707 pc[-1] = (state->flags & JSREG_FOLD) ? REOP_FLATi : REOP_FLAT;
708 pc = WriteCompactIndex(pc, (WCHAR*)t->kid - state->cpbegin);
709 pc = WriteCompactIndex(pc, t->u.flat.length);
710 } else if (t->u.flat.chr < 256) {
711 pc[-1] = (state->flags & JSREG_FOLD) ? REOP_FLAT1i : REOP_FLAT1;
712 *pc++ = (jsbytecode) t->u.flat.chr;
714 pc[-1] = (state->flags & JSREG_FOLD)
717 SET_ARG(pc, t->u.flat.chr);
724 pc = WriteCompactIndex(pc, t->u.parenIndex);
725 emitStateSP->continueNode = t;
726 emitStateSP->continueOp = REOP_RPAREN;
728 assert((size_t)(emitStateSP - emitStateStack) <= treeDepth);
734 pc = WriteCompactIndex(pc, t->u.parenIndex);
738 pc = WriteCompactIndex(pc, t->u.parenIndex);
743 emitStateSP->nextTermFixup = pc;
745 emitStateSP->continueNode = t;
746 emitStateSP->continueOp = REOP_ASSERTTEST;
748 assert((size_t)(emitStateSP - emitStateStack) <= treeDepth);
753 case REOP_ASSERTTEST:
754 case REOP_ASSERTNOTTEST:
755 if (!SetForwardJumpOffset(emitStateSP->nextTermFixup, pc))
759 case REOP_ASSERT_NOT:
761 emitStateSP->nextTermFixup = pc;
763 emitStateSP->continueNode = t;
764 emitStateSP->continueOp = REOP_ASSERTNOTTEST;
766 assert((size_t)(emitStateSP - emitStateStack) <= treeDepth);
773 if (t->u.range.min == 0 && t->u.range.max == (UINT)-1) {
774 pc[-1] = (t->u.range.greedy) ? REOP_STAR : REOP_MINIMALSTAR;
775 } else if (t->u.range.min == 0 && t->u.range.max == 1) {
776 pc[-1] = (t->u.range.greedy) ? REOP_OPT : REOP_MINIMALOPT;
777 } else if (t->u.range.min == 1 && t->u.range.max == (UINT) -1) {
778 pc[-1] = (t->u.range.greedy) ? REOP_PLUS : REOP_MINIMALPLUS;
780 if (!t->u.range.greedy)
781 pc[-1] = REOP_MINIMALQUANT;
782 pc = WriteCompactIndex(pc, t->u.range.min);
784 * Write max + 1 to avoid using size_t(max) + 1 bytes
785 * for (UINT)-1 sentinel.
787 pc = WriteCompactIndex(pc, t->u.range.max + 1);
789 emitStateSP->nextTermFixup = pc;
791 emitStateSP->continueNode = t;
792 emitStateSP->continueOp = REOP_ENDCHILD;
794 assert((size_t)(emitStateSP - emitStateStack) <= treeDepth);
800 if (!SetForwardJumpOffset(emitStateSP->nextTermFixup, pc))
805 if (!t->u.ucclass.sense)
806 pc[-1] = REOP_NCLASS;
807 pc = WriteCompactIndex(pc, t->u.ucclass.index);
808 charSet = &re->classList[t->u.ucclass.index];
809 charSet->converted = FALSE;
810 charSet->length = t->u.ucclass.bmsize;
811 charSet->u.src.startIndex = t->u.ucclass.startIndex;
812 charSet->u.src.length = t->u.ucclass.kidlen;
813 charSet->sense = t->u.ucclass.sense;
824 if (emitStateSP == emitStateStack)
827 t = emitStateSP->continueNode;
828 op = (REOp) emitStateSP->continueOp;
833 heap_free(emitStateStack);
837 ReportRegExpError(state, JSREPORT_ERROR, JSMSG_REGEXP_TOO_COMPLEX);
843 * Process the op against the two top operands, reducing them to a single
844 * operand in the penultimate slot. Update progLength and treeDepth.
847 ProcessOp(CompilerState *state, REOpData *opData, RENode **operandStack,
852 switch (opData->op) {
854 result = NewRENode(state, REOP_ALT);
857 result->kid = operandStack[operandSP - 2];
858 result->u.kid2 = operandStack[operandSP - 1];
859 operandStack[operandSP - 2] = result;
861 if (state->treeDepth == TREE_DEPTH_MAX) {
862 ReportRegExpError(state, JSREPORT_ERROR, JSMSG_REGEXP_TOO_COMPLEX);
868 * Look at both alternates to see if there's a FLAT or a CLASS at
869 * the start of each. If so, use a prerequisite match.
871 if (((RENode *) result->kid)->op == REOP_FLAT &&
872 ((RENode *) result->u.kid2)->op == REOP_FLAT &&
873 (state->flags & JSREG_FOLD) == 0) {
874 result->op = REOP_ALTPREREQ;
875 result->u.altprereq.ch1 = ((RENode *) result->kid)->u.flat.chr;
876 result->u.altprereq.ch2 = ((RENode *) result->u.kid2)->u.flat.chr;
877 /* ALTPREREQ, <end>, uch1, uch2, <next>, ...,
878 JUMP, <end> ... ENDALT */
879 state->progLength += 13;
882 if (((RENode *) result->kid)->op == REOP_CLASS &&
883 ((RENode *) result->kid)->u.ucclass.index < 256 &&
884 ((RENode *) result->u.kid2)->op == REOP_FLAT &&
885 (state->flags & JSREG_FOLD) == 0) {
886 result->op = REOP_ALTPREREQ2;
887 result->u.altprereq.ch1 = ((RENode *) result->u.kid2)->u.flat.chr;
888 result->u.altprereq.ch2 = ((RENode *) result->kid)->u.ucclass.index;
889 /* ALTPREREQ2, <end>, uch1, uch2, <next>, ...,
890 JUMP, <end> ... ENDALT */
891 state->progLength += 13;
894 if (((RENode *) result->kid)->op == REOP_FLAT &&
895 ((RENode *) result->u.kid2)->op == REOP_CLASS &&
896 ((RENode *) result->u.kid2)->u.ucclass.index < 256 &&
897 (state->flags & JSREG_FOLD) == 0) {
898 result->op = REOP_ALTPREREQ2;
899 result->u.altprereq.ch1 = ((RENode *) result->kid)->u.flat.chr;
900 result->u.altprereq.ch2 =
901 ((RENode *) result->u.kid2)->u.ucclass.index;
902 /* ALTPREREQ2, <end>, uch1, uch2, <next>, ...,
903 JUMP, <end> ... ENDALT */
904 state->progLength += 13;
907 /* ALT, <next>, ..., JUMP, <end> ... ENDALT */
908 state->progLength += 7;
913 result = operandStack[operandSP - 2];
915 result = result->next;
916 result->next = operandStack[operandSP - 1];
920 case REOP_ASSERT_NOT:
923 /* These should have been processed by a close paren. */
924 ReportRegExpErrorHelper(state, JSREPORT_ERROR, JSMSG_MISSING_PAREN,
934 * Hack two bits in CompilerState.flags, for use within FindParenCount to flag
935 * its being on the stack, and to propagate errors to its callers.
937 #define JSREG_FIND_PAREN_COUNT 0x8000
938 #define JSREG_FIND_PAREN_ERROR 0x4000
941 * Magic return value from FindParenCount and GetDecimalValue, to indicate
942 * overflow beyond GetDecimalValue's max parameter, or a computed maximum if
943 * its findMax parameter is non-null.
945 #define OVERFLOW_VALUE ((UINT)-1)
948 FindParenCount(CompilerState *state)
953 if (state->flags & JSREG_FIND_PAREN_COUNT)
954 return OVERFLOW_VALUE;
957 * Copy state into temp, flag it so we never report an invalid backref,
958 * and reset its members to parse the entire regexp. This is obviously
959 * suboptimal, but GetDecimalValue calls us only if a backref appears to
960 * refer to a forward parenthetical, which is rare.
963 temp.flags |= JSREG_FIND_PAREN_COUNT;
964 temp.cp = temp.cpbegin;
969 temp.classBitmapsMem = 0;
970 for (i = 0; i < CLASS_CACHE_SIZE; i++)
971 temp.classCache[i].start = NULL;
973 if (!ParseRegExp(&temp)) {
974 state->flags |= JSREG_FIND_PAREN_ERROR;
975 return OVERFLOW_VALUE;
977 return temp.parenCount;
981 * Extract and return a decimal value at state->cp. The initial character c
982 * has already been read. Return OVERFLOW_VALUE if the result exceeds max.
983 * Callers who pass a non-null findMax should test JSREG_FIND_PAREN_ERROR in
984 * state->flags to discover whether an error occurred under findMax.
987 GetDecimalValue(WCHAR c, UINT max, UINT (*findMax)(CompilerState *state),
988 CompilerState *state)
990 UINT value = JS7_UNDEC(c);
991 BOOL overflow = (value > max && (!findMax || value > findMax(state)));
993 /* The following restriction allows simpler overflow checks. */
994 assert(max <= ((UINT)-1 - 9) / 10);
995 while (state->cp < state->cpend) {
999 value = 10 * value + JS7_UNDEC(c);
1000 if (!overflow && value > max && (!findMax || value > findMax(state)))
1004 return overflow ? OVERFLOW_VALUE : value;
1008 * Calculate the total size of the bitmap required for a class expression.
1011 CalculateBitmapSize(CompilerState *state, RENode *target, const WCHAR *src,
1015 BOOL inRange = FALSE;
1016 WCHAR c, rangeStart = 0;
1017 UINT n, digit, nDigits, i;
1019 target->u.ucclass.bmsize = 0;
1020 target->u.ucclass.sense = TRUE;
1027 target->u.ucclass.sense = FALSE;
1030 while (src != end) {
1031 BOOL canStartRange = TRUE;
1058 if (src < end && RE_IS_LETTER(*src)) {
1059 localMax = (UINT) (*src++) & 0x1F;
1072 for (i = 0; (i < nDigits) && (src < end); i++) {
1074 if (!isASCIIHexDigit(c, &digit)) {
1076 * Back off to accepting the original
1083 n = (n << 4) | digit;
1088 canStartRange = FALSE;
1090 JS_ReportErrorNumber(state->context,
1091 js_GetErrorMessage, NULL,
1092 JSMSG_BAD_CLASS_RANGE);
1102 canStartRange = FALSE;
1104 JS_ReportErrorNumber(state->context,
1105 js_GetErrorMessage, NULL,
1106 JSMSG_BAD_CLASS_RANGE);
1112 * If this is the start of a range, ensure that it's less than
1126 * This is a non-ECMA extension - decimal escapes (in this
1127 * case, octal!) are supposed to be an error inside class
1128 * ranges, but supported here for backwards compatibility.
1133 if ('0' <= c && c <= '7') {
1135 n = 8 * n + JS7_UNDEC(c);
1137 if ('0' <= c && c <= '7') {
1139 i = 8 * n + JS7_UNDEC(c);
1160 /* Throw a SyntaxError here, per ECMA-262, 15.10.2.15. */
1161 if (rangeStart > localMax) {
1162 JS_ReportErrorNumber(state->context,
1163 js_GetErrorMessage, NULL,
1164 JSMSG_BAD_CLASS_RANGE);
1169 if (canStartRange && src < end - 1) {
1173 rangeStart = (WCHAR)localMax;
1177 if (state->flags & JSREG_FOLD)
1178 rangeStart = localMax; /* one run of the uc/dc loop below */
1181 if (state->flags & JSREG_FOLD) {
1182 WCHAR maxch = localMax;
1184 for (i = rangeStart; i <= localMax; i++) {
1200 target->u.ucclass.bmsize = max;
1205 ParseMinMaxQuantifier(CompilerState *state, BOOL ignoreValues)
1209 const WCHAR *errp = state->cp++;
1214 min = GetDecimalValue(c, 0xFFFF, NULL, state);
1217 if (!ignoreValues && min == OVERFLOW_VALUE)
1218 return JSMSG_MIN_TOO_BIG;
1224 max = GetDecimalValue(c, 0xFFFF, NULL, state);
1226 if (!ignoreValues && max == OVERFLOW_VALUE)
1227 return JSMSG_MAX_TOO_BIG;
1228 if (!ignoreValues && min > max)
1229 return JSMSG_OUT_OF_ORDER;
1237 state->result = NewRENode(state, REOP_QUANT);
1239 return JSMSG_OUT_OF_MEMORY;
1240 state->result->u.range.min = min;
1241 state->result->u.range.max = max;
1243 * QUANT, <min>, <max>, <next> ... <ENDCHILD>
1244 * where <max> is written as compact(max+1) to make
1245 * (UINT)-1 sentinel to occupy 1 byte, not width_of(max)+1.
1247 state->progLength += (1 + GetCompactIndexWidth(min)
1248 + GetCompactIndexWidth(max + 1)
1259 ParseQuantifier(CompilerState *state)
1262 term = state->result;
1263 if (state->cp < state->cpend) {
1264 switch (*state->cp) {
1266 state->result = NewRENode(state, REOP_QUANT);
1269 state->result->u.range.min = 1;
1270 state->result->u.range.max = (UINT)-1;
1271 /* <PLUS>, <next> ... <ENDCHILD> */
1272 state->progLength += 4;
1275 state->result = NewRENode(state, REOP_QUANT);
1278 state->result->u.range.min = 0;
1279 state->result->u.range.max = (UINT)-1;
1280 /* <STAR>, <next> ... <ENDCHILD> */
1281 state->progLength += 4;
1284 state->result = NewRENode(state, REOP_QUANT);
1287 state->result->u.range.min = 0;
1288 state->result->u.range.max = 1;
1289 /* <OPT>, <next> ... <ENDCHILD> */
1290 state->progLength += 4;
1292 case '{': /* balance '}' */
1296 err = ParseMinMaxQuantifier(state, FALSE);
1302 ReportRegExpErrorHelper(state, JSREPORT_ERROR, err, errp);
1311 if (state->treeDepth == TREE_DEPTH_MAX) {
1312 ReportRegExpError(state, JSREPORT_ERROR, JSMSG_REGEXP_TOO_COMPLEX);
1318 state->result->kid = term;
1319 if (state->cp < state->cpend && *state->cp == '?') {
1321 state->result->u.range.greedy = FALSE;
1323 state->result->u.range.greedy = TRUE;
1329 * item: assertion An item is either an assertion or
1330 * quantatom a quantified atom.
1332 * assertion: '^' Assertions match beginning of string
1333 * (or line if the class static property
1334 * RegExp.multiline is true).
1335 * '$' End of string (or line if the class
1336 * static property RegExp.multiline is
1338 * '\b' Word boundary (between \w and \W).
1339 * '\B' Word non-boundary.
1341 * quantatom: atom An unquantified atom.
1342 * quantatom '{' n ',' m '}'
1343 * Atom must occur between n and m times.
1344 * quantatom '{' n ',' '}' Atom must occur at least n times.
1345 * quantatom '{' n '}' Atom must occur exactly n times.
1346 * quantatom '*' Zero or more times (same as {0,}).
1347 * quantatom '+' One or more times (same as {1,}).
1348 * quantatom '?' Zero or one time (same as {0,1}).
1350 * any of which can be optionally followed by '?' for ungreedy
1352 * atom: '(' regexp ')' A parenthesized regexp (what matched
1353 * can be addressed using a backreference,
1355 * '.' Matches any char except '\n'.
1356 * '[' classlist ']' A character class.
1357 * '[' '^' classlist ']' A negated character class.
1359 * '\n' Newline (Line Feed).
1360 * '\r' Carriage Return.
1361 * '\t' Horizontal Tab.
1362 * '\v' Vertical Tab.
1363 * '\d' A digit (same as [0-9]).
1365 * '\w' A word character, [0-9a-z_A-Z].
1366 * '\W' A non-word character.
1367 * '\s' A whitespace character, [ \b\f\n\r\t\v].
1368 * '\S' A non-whitespace character.
1369 * '\' n A backreference to the nth (n decimal
1370 * and positive) parenthesized expression.
1371 * '\' octal An octal escape sequence (octal must be
1372 * two or three digits long, unless it is
1373 * 0 for the null character).
1374 * '\x' hex A hex escape (hex must be two digits).
1375 * '\u' unicode A unicode escape (must be four digits).
1376 * '\c' ctrl A control character, ctrl is a letter.
1377 * '\' literalatomchar Any character except one of the above
1378 * that follow '\' in an atom.
1379 * otheratomchar Any character not first among the other
1380 * atom right-hand sides.
1383 ParseTerm(CompilerState *state)
1385 WCHAR c = *state->cp++;
1387 UINT num, tmp, n, i;
1388 const WCHAR *termStart;
1391 /* assertions and atoms */
1393 state->result = NewRENode(state, REOP_BOL);
1396 state->progLength++;
1399 state->result = NewRENode(state, REOP_EOL);
1402 state->progLength++;
1405 if (state->cp >= state->cpend) {
1406 /* a trailing '\' is an error */
1407 ReportRegExpError(state, JSREPORT_ERROR, JSMSG_TRAILING_SLASH);
1412 /* assertion escapes */
1414 state->result = NewRENode(state, REOP_WBDRY);
1417 state->progLength++;
1420 state->result = NewRENode(state, REOP_WNONBDRY);
1423 state->progLength++;
1425 /* Decimal escape */
1427 /* Give a strict warning. See also the note below. */
1428 WARN("non-octal digit in an escape sequence that doesn't match a back-reference\n");
1431 while (state->cp < state->cpend) {
1433 if (c < '0' || '7' < c)
1436 tmp = 8 * num + (UINT)JS7_UNDEC(c);
1443 state->result = NewRENode(state, REOP_FLAT);
1446 state->result->u.flat.chr = c;
1447 state->result->u.flat.length = 1;
1448 state->progLength += 3;
1459 termStart = state->cp - 1;
1460 num = GetDecimalValue(c, state->parenCount, FindParenCount, state);
1461 if (state->flags & JSREG_FIND_PAREN_ERROR)
1463 if (num == OVERFLOW_VALUE) {
1464 /* Give a strict mode warning. */
1465 WARN("back-reference exceeds number of capturing parentheses\n");
1468 * Note: ECMA 262, 15.10.2.9 says that we should throw a syntax
1469 * error here. However, for compatibility with IE, we treat the
1470 * whole backref as flat if the first character in it is not a
1471 * valid octal character, and as an octal escape otherwise.
1473 state->cp = termStart;
1475 /* Treat this as flat. termStart - 1 is the \. */
1480 /* Treat this as an octal escape. */
1483 assert(1 <= num && num <= 0x10000);
1484 state->result = NewRENode(state, REOP_BACKREF);
1487 state->result->u.parenIndex = num - 1;
1489 += 1 + GetCompactIndexWidth(state->result->u.parenIndex);
1491 /* Control escape */
1507 /* Control letter */
1509 if (state->cp < state->cpend && RE_IS_LETTER(*state->cp)) {
1510 c = (WCHAR) (*state->cp++ & 0x1F);
1512 /* back off to accepting the original '\' as a literal */
1517 /* HexEscapeSequence */
1521 /* UnicodeEscapeSequence */
1526 for (i = 0; i < nDigits && state->cp < state->cpend; i++) {
1529 if (!isASCIIHexDigit(c, &digit)) {
1531 * Back off to accepting the original 'u' or 'x' as a
1538 n = (n << 4) | digit;
1542 /* Character class escapes */
1544 state->result = NewRENode(state, REOP_DIGIT);
1548 state->progLength++;
1551 state->result = NewRENode(state, REOP_NONDIGIT);
1554 state->result = NewRENode(state, REOP_SPACE);
1557 state->result = NewRENode(state, REOP_NONSPACE);
1560 state->result = NewRENode(state, REOP_ALNUM);
1563 state->result = NewRENode(state, REOP_NONALNUM);
1565 /* IdentityEscape */
1567 state->result = NewRENode(state, REOP_FLAT);
1570 state->result->u.flat.chr = c;
1571 state->result->u.flat.length = 1;
1572 state->result->kid = (void *) (state->cp - 1);
1573 state->progLength += 3;
1578 state->result = NewRENode(state, REOP_CLASS);
1581 termStart = state->cp;
1582 state->result->u.ucclass.startIndex = termStart - state->cpbegin;
1584 if (state->cp == state->cpend) {
1585 ReportRegExpErrorHelper(state, JSREPORT_ERROR,
1586 JSMSG_UNTERM_CLASS, termStart);
1590 if (*state->cp == '\\') {
1592 if (state->cp != state->cpend)
1596 if (*state->cp == ']') {
1597 state->result->u.ucclass.kidlen = state->cp - termStart;
1602 for (i = 0; i < CLASS_CACHE_SIZE; i++) {
1603 if (!state->classCache[i].start) {
1604 state->classCache[i].start = termStart;
1605 state->classCache[i].length = state->result->u.ucclass.kidlen;
1606 state->classCache[i].index = state->classCount;
1609 if (state->classCache[i].length ==
1610 state->result->u.ucclass.kidlen) {
1611 for (n = 0; ; n++) {
1612 if (n == state->classCache[i].length) {
1613 state->result->u.ucclass.index
1614 = state->classCache[i].index;
1617 if (state->classCache[i].start[n] != termStart[n])
1622 state->result->u.ucclass.index = state->classCount++;
1626 * Call CalculateBitmapSize now as we want any errors it finds
1627 * to be reported during the parse phase, not at execution.
1629 if (!CalculateBitmapSize(state, state->result, termStart, state->cp++))
1632 * Update classBitmapsMem with number of bytes to hold bmsize bits,
1633 * which is (bitsCount + 7) / 8 or (highest_bit + 1 + 7) / 8
1634 * or highest_bit / 8 + 1 where highest_bit is u.ucclass.bmsize.
1636 n = (state->result->u.ucclass.bmsize >> 3) + 1;
1637 if (n > CLASS_BITMAPS_MEM_LIMIT - state->classBitmapsMem) {
1638 ReportRegExpError(state, JSREPORT_ERROR, JSMSG_REGEXP_TOO_COMPLEX);
1641 state->classBitmapsMem += n;
1642 /* CLASS, <index> */
1644 += 1 + GetCompactIndexWidth(state->result->u.ucclass.index);
1648 state->result = NewRENode(state, REOP_DOT);
1653 const WCHAR *errp = state->cp--;
1656 err = ParseMinMaxQuantifier(state, TRUE);
1667 ReportRegExpErrorHelper(state, JSREPORT_ERROR,
1668 JSMSG_BAD_QUANTIFIER, state->cp - 1);
1672 state->result = NewRENode(state, REOP_FLAT);
1675 state->result->u.flat.chr = c;
1676 state->result->u.flat.length = 1;
1677 state->result->kid = (void *) (state->cp - 1);
1678 state->progLength += 3;
1681 return ParseQuantifier(state);
1685 * Top-down regular expression grammar, based closely on Perl4.
1687 * regexp: altern A regular expression is one or more
1688 * altern '|' regexp alternatives separated by vertical bar.
1690 #define INITIAL_STACK_SIZE 128
1693 ParseRegExp(CompilerState *state)
1697 REOpData *operatorStack;
1698 RENode **operandStack;
1701 BOOL result = FALSE;
1703 INT operatorSP = 0, operatorStackSize = INITIAL_STACK_SIZE;
1704 INT operandSP = 0, operandStackSize = INITIAL_STACK_SIZE;
1706 /* Watch out for empty regexp */
1707 if (state->cp == state->cpend) {
1708 state->result = NewRENode(state, REOP_EMPTY);
1709 return (state->result != NULL);
1712 operatorStack = heap_alloc(sizeof(REOpData) * operatorStackSize);
1716 operandStack = heap_alloc(sizeof(RENode *) * operandStackSize);
1721 parenIndex = state->parenCount;
1722 if (state->cp == state->cpend) {
1724 * If we are at the end of the regexp and we're short one or more
1725 * operands, the regexp must have the form /x|/ or some such, with
1726 * left parentheses making us short more than one operand.
1728 if (operatorSP >= operandSP) {
1729 operand = NewRENode(state, REOP_EMPTY);
1735 switch (*state->cp) {
1738 if (state->cp + 1 < state->cpend &&
1739 *state->cp == '?' &&
1740 (state->cp[1] == '=' ||
1741 state->cp[1] == '!' ||
1742 state->cp[1] == ':')) {
1743 switch (state->cp[1]) {
1746 /* ASSERT, <next>, ... ASSERTTEST */
1747 state->progLength += 4;
1750 op = REOP_ASSERT_NOT;
1751 /* ASSERTNOT, <next>, ... ASSERTNOTTEST */
1752 state->progLength += 4;
1755 op = REOP_LPARENNON;
1761 /* LPAREN, <index>, ... RPAREN, <index> */
1763 += 2 * (1 + GetCompactIndexWidth(parenIndex));
1764 state->parenCount++;
1765 if (state->parenCount == 65535) {
1766 ReportRegExpError(state, JSREPORT_ERROR,
1767 JSMSG_TOO_MANY_PARENS);
1775 * If there's no stacked open parenthesis, throw syntax error.
1777 for (i = operatorSP - 1; ; i--) {
1779 ReportRegExpError(state, JSREPORT_ERROR,
1780 JSMSG_UNMATCHED_RIGHT_PAREN);
1783 if (operatorStack[i].op == REOP_ASSERT ||
1784 operatorStack[i].op == REOP_ASSERT_NOT ||
1785 operatorStack[i].op == REOP_LPARENNON ||
1786 operatorStack[i].op == REOP_LPAREN) {
1793 /* Expected an operand before these, so make an empty one */
1794 operand = NewRENode(state, REOP_EMPTY);
1800 if (!ParseTerm(state))
1802 operand = state->result;
1804 if (operandSP == operandStackSize) {
1806 operandStackSize += operandStackSize;
1807 tmp = heap_realloc(operandStack, sizeof(RENode *) * operandStackSize);
1812 operandStack[operandSP++] = operand;
1817 /* At the end; process remaining operators. */
1819 if (state->cp == state->cpend) {
1820 while (operatorSP) {
1822 if (!ProcessOp(state, &operatorStack[operatorSP],
1823 operandStack, operandSP))
1827 assert(operandSP == 1);
1828 state->result = operandStack[0];
1833 switch (*state->cp) {
1835 /* Process any stacked 'concat' operators */
1837 while (operatorSP &&
1838 operatorStack[operatorSP - 1].op == REOP_CONCAT) {
1840 if (!ProcessOp(state, &operatorStack[operatorSP],
1841 operandStack, operandSP)) {
1851 * If there's no stacked open parenthesis, throw syntax error.
1853 for (i = operatorSP - 1; ; i--) {
1855 ReportRegExpError(state, JSREPORT_ERROR,
1856 JSMSG_UNMATCHED_RIGHT_PAREN);
1859 if (operatorStack[i].op == REOP_ASSERT ||
1860 operatorStack[i].op == REOP_ASSERT_NOT ||
1861 operatorStack[i].op == REOP_LPARENNON ||
1862 operatorStack[i].op == REOP_LPAREN) {
1868 /* Process everything on the stack until the open parenthesis. */
1872 switch (operatorStack[operatorSP].op) {
1874 case REOP_ASSERT_NOT:
1876 operand = NewRENode(state, operatorStack[operatorSP].op);
1879 operand->u.parenIndex =
1880 operatorStack[operatorSP].parenIndex;
1882 operand->kid = operandStack[operandSP - 1];
1883 operandStack[operandSP - 1] = operand;
1884 if (state->treeDepth == TREE_DEPTH_MAX) {
1885 ReportRegExpError(state, JSREPORT_ERROR,
1886 JSMSG_REGEXP_TOO_COMPLEX);
1892 case REOP_LPARENNON:
1893 state->result = operandStack[operandSP - 1];
1894 if (!ParseQuantifier(state))
1896 operandStack[operandSP - 1] = state->result;
1897 goto restartOperator;
1899 if (!ProcessOp(state, &operatorStack[operatorSP],
1900 operandStack, operandSP))
1910 const WCHAR *errp = state->cp;
1912 if (ParseMinMaxQuantifier(state, TRUE) < 0) {
1914 * This didn't even scan correctly as a quantifier, so we should
1928 ReportRegExpErrorHelper(state, JSREPORT_ERROR, JSMSG_BAD_QUANTIFIER,
1934 /* Anything else is the start of the next term. */
1937 if (operatorSP == operatorStackSize) {
1939 operatorStackSize += operatorStackSize;
1940 tmp = heap_realloc(operatorStack, sizeof(REOpData) * operatorStackSize);
1943 operatorStack = tmp;
1945 operatorStack[operatorSP].op = op;
1946 operatorStack[operatorSP].errPos = state->cp;
1947 operatorStack[operatorSP++].parenIndex = parenIndex;
1952 heap_free(operatorStack);
1953 heap_free(operandStack);
1958 * Save the current state of the match - the position in the input
1959 * text as well as the position in the bytecode. The state of any
1960 * parent expressions is also saved (preceding state).
1961 * Contents of parenCount parentheses from parenIndex are also saved.
1963 static REBackTrackData *
1964 PushBackTrackState(REGlobalData *gData, REOp op,
1965 jsbytecode *target, REMatchState *x, const WCHAR *cp,
1966 size_t parenIndex, size_t parenCount)
1969 REBackTrackData *result =
1970 (REBackTrackData *) ((char *)gData->backTrackSP + gData->cursz);
1972 size_t sz = sizeof(REBackTrackData) +
1973 gData->stateStackTop * sizeof(REProgState) +
1974 parenCount * sizeof(RECapture);
1976 ptrdiff_t btsize = gData->backTrackStackSize;
1977 ptrdiff_t btincr = ((char *)result + sz) -
1978 ((char *)gData->backTrackStack + btsize);
1980 TRACE("\tBT_Push: %lu,%lu\n", (unsigned long) parenIndex, (unsigned long) parenCount);
1982 JS_COUNT_OPERATION(gData->cx, JSOW_JUMP * (1 + parenCount));
1984 ptrdiff_t offset = (char *)result - (char *)gData->backTrackStack;
1986 JS_COUNT_OPERATION(gData->cx, JSOW_ALLOCATION);
1987 btincr = ((btincr+btsize-1)/btsize)*btsize;
1988 gData->backTrackStack = jsheap_grow(gData->pool, gData->backTrackStack, btsize, btincr);
1989 if (!gData->backTrackStack) {
1990 js_ReportOutOfScriptQuota(gData->cx);
1994 gData->backTrackStackSize = btsize + btincr;
1995 result = (REBackTrackData *) ((char *)gData->backTrackStack + offset);
1997 gData->backTrackSP = result;
1998 result->sz = gData->cursz;
2001 result->backtrack_op = op;
2002 result->backtrack_pc = target;
2004 result->parenCount = parenCount;
2005 result->parenIndex = parenIndex;
2007 result->saveStateStackTop = gData->stateStackTop;
2008 assert(gData->stateStackTop);
2009 memcpy(result + 1, gData->stateStack,
2010 sizeof(REProgState) * result->saveStateStackTop);
2012 if (parenCount != 0) {
2013 memcpy((char *)(result + 1) +
2014 sizeof(REProgState) * result->saveStateStackTop,
2015 &x->parens[parenIndex],
2016 sizeof(RECapture) * parenCount);
2017 for (i = 0; i != parenCount; i++)
2018 x->parens[parenIndex + i].index = -1;
2024 static inline REMatchState *
2025 FlatNIMatcher(REGlobalData *gData, REMatchState *x, WCHAR *matchChars,
2029 assert(gData->cpend >= x->cp);
2030 if (length > (size_t)(gData->cpend - x->cp))
2032 for (i = 0; i != length; i++) {
2033 if (toupperW(matchChars[i]) != toupperW(x->cp[i]))
2041 * 1. Evaluate DecimalEscape to obtain an EscapeValue E.
2042 * 2. If E is not a character then go to step 6.
2043 * 3. Let ch be E's character.
2044 * 4. Let A be a one-element RECharSet containing the character ch.
2045 * 5. Call CharacterSetMatcher(A, false) and return its Matcher result.
2046 * 6. E must be an integer. Let n be that integer.
2047 * 7. If n=0 or n>NCapturingParens then throw a SyntaxError exception.
2048 * 8. Return an internal Matcher closure that takes two arguments, a State x
2049 * and a Continuation c, and performs the following:
2050 * 1. Let cap be x's captures internal array.
2051 * 2. Let s be cap[n].
2052 * 3. If s is undefined, then call c(x) and return its result.
2053 * 4. Let e be x's endIndex.
2054 * 5. Let len be s's length.
2055 * 6. Let f be e+len.
2056 * 7. If f>InputLength, return failure.
2057 * 8. If there exists an integer i between 0 (inclusive) and len (exclusive)
2058 * such that Canonicalize(s[i]) is not the same character as
2059 * Canonicalize(Input [e+i]), then return failure.
2060 * 9. Let y be the State (f, cap).
2061 * 10. Call c(y) and return its result.
2063 static REMatchState *
2064 BackrefMatcher(REGlobalData *gData, REMatchState *x, size_t parenIndex)
2067 const WCHAR *parenContent;
2068 RECapture *cap = &x->parens[parenIndex];
2070 if (cap->index == -1)
2074 if (x->cp + len > gData->cpend)
2077 parenContent = &gData->cpbegin[cap->index];
2078 if (gData->regexp->flags & JSREG_FOLD) {
2079 for (i = 0; i < len; i++) {
2080 if (toupperW(parenContent[i]) != toupperW(x->cp[i]))
2084 for (i = 0; i < len; i++) {
2085 if (parenContent[i] != x->cp[i])
2093 /* Add a single character to the RECharSet */
2095 AddCharacterToCharSet(RECharSet *cs, WCHAR c)
2097 UINT byteIndex = (UINT)(c >> 3);
2098 assert(c <= cs->length);
2099 cs->u.bits[byteIndex] |= 1 << (c & 0x7);
2103 /* Add a character range, c1 to c2 (inclusive) to the RECharSet */
2105 AddCharacterRangeToCharSet(RECharSet *cs, UINT c1, UINT c2)
2109 UINT byteIndex1 = c1 >> 3;
2110 UINT byteIndex2 = c2 >> 3;
2112 assert(c2 <= cs->length && c1 <= c2);
2117 if (byteIndex1 == byteIndex2) {
2118 cs->u.bits[byteIndex1] |= ((BYTE)0xFF >> (7 - (c2 - c1))) << c1;
2120 cs->u.bits[byteIndex1] |= 0xFF << c1;
2121 for (i = byteIndex1 + 1; i < byteIndex2; i++)
2122 cs->u.bits[i] = 0xFF;
2123 cs->u.bits[byteIndex2] |= (BYTE)0xFF >> (7 - c2);
2127 /* Compile the source of the class into a RECharSet */
2129 ProcessCharSet(REGlobalData *gData, RECharSet *charSet)
2131 const WCHAR *src, *end;
2132 BOOL inRange = FALSE;
2133 WCHAR rangeStart = 0;
2138 assert(!charSet->converted);
2140 * Assert that startIndex and length points to chars inside [] inside
2143 assert(1 <= charSet->u.src.startIndex);
2144 assert(charSet->u.src.startIndex
2145 < SysStringLen(gData->regexp->source));
2146 assert(charSet->u.src.length <= SysStringLen(gData->regexp->source)
2147 - 1 - charSet->u.src.startIndex);
2149 charSet->converted = TRUE;
2150 src = gData->regexp->source + charSet->u.src.startIndex;
2152 end = src + charSet->u.src.length;
2154 assert(src[-1] == '[' && end[0] == ']');
2156 byteLength = (charSet->length >> 3) + 1;
2157 charSet->u.bits = heap_alloc(byteLength);
2158 if (!charSet->u.bits) {
2159 JS_ReportOutOfMemory(gData->cx);
2163 memset(charSet->u.bits, 0, byteLength);
2169 assert(charSet->sense == FALSE);
2172 assert(charSet->sense == TRUE);
2175 while (src != end) {
2200 if (src < end && JS_ISWORD(*src)) {
2201 thisCh = (WCHAR)(*src++ & 0x1F);
2214 for (i = 0; (i < nDigits) && (src < end); i++) {
2217 if (!isASCIIHexDigit(c, &digit)) {
2219 * Back off to accepting the original '\'
2226 n = (n << 4) | digit;
2239 * This is a non-ECMA extension - decimal escapes (in this
2240 * case, octal!) are supposed to be an error inside class
2241 * ranges, but supported here for backwards compatibility.
2245 if ('0' <= c && c <= '7') {
2247 n = 8 * n + JS7_UNDEC(c);
2249 if ('0' <= c && c <= '7') {
2251 i = 8 * n + JS7_UNDEC(c);
2262 AddCharacterRangeToCharSet(charSet, '0', '9');
2263 continue; /* don't need range processing */
2265 AddCharacterRangeToCharSet(charSet, 0, '0' - 1);
2266 AddCharacterRangeToCharSet(charSet,
2268 (WCHAR)charSet->length);
2271 for (i = (INT)charSet->length; i >= 0; i--)
2273 AddCharacterToCharSet(charSet, (WCHAR)i);
2276 for (i = (INT)charSet->length; i >= 0; i--)
2278 AddCharacterToCharSet(charSet, (WCHAR)i);
2281 for (i = (INT)charSet->length; i >= 0; i--)
2283 AddCharacterToCharSet(charSet, (WCHAR)i);
2286 for (i = (INT)charSet->length; i >= 0; i--)
2288 AddCharacterToCharSet(charSet, (WCHAR)i);
2303 if (gData->regexp->flags & JSREG_FOLD) {
2306 assert(rangeStart <= thisCh);
2307 for (i = rangeStart; i <= thisCh; i++) {
2310 AddCharacterToCharSet(charSet, i);
2314 AddCharacterToCharSet(charSet, uch);
2316 AddCharacterToCharSet(charSet, dch);
2319 AddCharacterRangeToCharSet(charSet, rangeStart, thisCh);
2323 if (gData->regexp->flags & JSREG_FOLD) {
2324 AddCharacterToCharSet(charSet, toupperW(thisCh));
2325 AddCharacterToCharSet(charSet, tolowerW(thisCh));
2327 AddCharacterToCharSet(charSet, thisCh);
2329 if (src < end - 1) {
2333 rangeStart = thisCh;
2342 ReallocStateStack(REGlobalData *gData)
2344 size_t limit = gData->stateStackLimit;
2345 size_t sz = sizeof(REProgState) * limit;
2347 gData->stateStack = jsheap_grow(gData->pool, gData->stateStack, sz, sz);
2348 if (!gData->stateStack) {
2349 js_ReportOutOfScriptQuota(gData->cx);
2353 gData->stateStackLimit = limit + limit;
2357 #define PUSH_STATE_STACK(data) \
2359 ++(data)->stateStackTop; \
2360 if ((data)->stateStackTop == (data)->stateStackLimit && \
2361 !ReallocStateStack((data))) { \
2367 * Apply the current op against the given input to see if it's going to match
2368 * or fail. Return false if we don't get a match, true if we do. If updatecp is
2369 * true, then update the current state's cp. Always update startpc to the next
2372 static inline REMatchState *
2373 SimpleMatch(REGlobalData *gData, REMatchState *x, REOp op,
2374 jsbytecode **startpc, BOOL updatecp)
2376 REMatchState *result = NULL;
2379 size_t offset, length, index;
2380 jsbytecode *pc = *startpc; /* pc has already been incremented past op */
2382 const WCHAR *startcp = x->cp;
2386 const char *opname = reop_names[op];
2387 TRACE("\n%06d: %*s%s\n", pc - gData->regexp->program,
2388 (int)gData->stateStackTop * 2, "", opname);
2395 if (x->cp != gData->cpbegin) {
2396 if (/*!gData->cx->regExpStatics.multiline && FIXME !!! */
2397 !(gData->regexp->flags & JSREG_MULTILINE)) {
2400 if (!RE_IS_LINE_TERM(x->cp[-1]))
2406 if (x->cp != gData->cpend) {
2407 if (/*!gData->cx->regExpStatics.multiline &&*/
2408 !(gData->regexp->flags & JSREG_MULTILINE)) {
2411 if (!RE_IS_LINE_TERM(*x->cp))
2417 if ((x->cp == gData->cpbegin || !JS_ISWORD(x->cp[-1])) ^
2418 !(x->cp != gData->cpend && JS_ISWORD(*x->cp))) {
2423 if ((x->cp == gData->cpbegin || !JS_ISWORD(x->cp[-1])) ^
2424 (x->cp != gData->cpend && JS_ISWORD(*x->cp))) {
2429 if (x->cp != gData->cpend && !RE_IS_LINE_TERM(*x->cp)) {
2435 if (x->cp != gData->cpend && JS7_ISDEC(*x->cp)) {
2441 if (x->cp != gData->cpend && !JS7_ISDEC(*x->cp)) {
2447 if (x->cp != gData->cpend && JS_ISWORD(*x->cp)) {
2453 if (x->cp != gData->cpend && !JS_ISWORD(*x->cp)) {
2459 if (x->cp != gData->cpend && isspaceW(*x->cp)) {
2465 if (x->cp != gData->cpend && !isspaceW(*x->cp)) {
2471 pc = ReadCompactIndex(pc, &parenIndex);
2472 assert(parenIndex < gData->regexp->parenCount);
2473 result = BackrefMatcher(gData, x, parenIndex);
2476 pc = ReadCompactIndex(pc, &offset);
2477 assert(offset < SysStringLen(gData->regexp->source));
2478 pc = ReadCompactIndex(pc, &length);
2479 assert(1 <= length);
2480 assert(length <= SysStringLen(gData->regexp->source) - offset);
2481 if (length <= (size_t)(gData->cpend - x->cp)) {
2482 source = gData->regexp->source + offset;
2483 TRACE("%s\n", debugstr_wn(source, length));
2484 for (index = 0; index != length; index++) {
2485 if (source[index] != x->cp[index])
2494 TRACE(" '%c' == '%c'\n", (char)matchCh, (char)*x->cp);
2495 if (x->cp != gData->cpend && *x->cp == matchCh) {
2501 pc = ReadCompactIndex(pc, &offset);
2502 assert(offset < SysStringLen(gData->regexp->source));
2503 pc = ReadCompactIndex(pc, &length);
2504 assert(1 <= length);
2505 assert(length <= SysStringLen(gData->regexp->source) - offset);
2506 source = gData->regexp->source;
2507 result = FlatNIMatcher(gData, x, source + offset, length);
2511 if (x->cp != gData->cpend && toupperW(*x->cp) == toupperW(matchCh)) {
2517 matchCh = GET_ARG(pc);
2518 TRACE(" '%c' == '%c'\n", (char)matchCh, (char)*x->cp);
2520 if (x->cp != gData->cpend && *x->cp == matchCh) {
2526 matchCh = GET_ARG(pc);
2528 if (x->cp != gData->cpend && toupperW(*x->cp) == toupperW(matchCh)) {
2534 pc = ReadCompactIndex(pc, &index);
2535 assert(index < gData->regexp->classCount);
2536 if (x->cp != gData->cpend) {
2537 charSet = &gData->regexp->classList[index];
2538 assert(charSet->converted);
2541 if (charSet->length != 0 &&
2542 ch <= charSet->length &&
2543 (charSet->u.bits[index] & (1 << (ch & 0x7)))) {
2550 pc = ReadCompactIndex(pc, &index);
2551 assert(index < gData->regexp->classCount);
2552 if (x->cp != gData->cpend) {
2553 charSet = &gData->regexp->classList[index];
2554 assert(charSet->converted);
2557 if (charSet->length == 0 ||
2558 ch > charSet->length ||
2559 !(charSet->u.bits[index] & (1 << (ch & 0x7)))) {
2580 static inline REMatchState *
2581 ExecuteREBytecode(REGlobalData *gData, REMatchState *x)
2583 REMatchState *result = NULL;
2584 REBackTrackData *backTrackData;
2585 jsbytecode *nextpc, *testpc;
2588 REProgState *curState;
2589 const WCHAR *startcp;
2590 size_t parenIndex, k;
2591 size_t parenSoFar = 0;
2593 WCHAR matchCh1, matchCh2;
2597 jsbytecode *pc = gData->regexp->program;
2598 REOp op = (REOp) *pc++;
2601 * If the first node is a simple match, step the index into the string
2602 * until that match is made, or fail if it can't be found at all.
2604 if (REOP_IS_SIMPLE(op) && !(gData->regexp->flags & JSREG_STICKY)) {
2606 while (x->cp <= gData->cpend) {
2607 nextpc = pc; /* reset back to start each time */
2608 result = SimpleMatch(gData, x, op, &nextpc, TRUE);
2612 pc = nextpc; /* accept skip to next opcode */
2614 assert(op < REOP_LIMIT);
2625 const char *opname = reop_names[op];
2626 TRACE("\n%06d: %*s%s\n", pc - gData->regexp->program,
2627 (int)gData->stateStackTop * 2, "", opname);
2629 if (REOP_IS_SIMPLE(op)) {
2630 result = SimpleMatch(gData, x, op, &pc, TRUE);
2632 curState = &gData->stateStack[gData->stateStackTop];
2636 case REOP_ALTPREREQ2:
2637 nextpc = pc + GET_OFFSET(pc); /* start of next op */
2639 matchCh2 = GET_ARG(pc);
2644 if (x->cp != gData->cpend) {
2645 if (*x->cp == matchCh2)
2648 charSet = &gData->regexp->classList[k];
2649 if (!charSet->converted && !ProcessCharSet(gData, charSet))
2653 if ((charSet->length == 0 ||
2654 matchCh1 > charSet->length ||
2655 !(charSet->u.bits[k] & (1 << (matchCh1 & 0x7)))) ^
2663 case REOP_ALTPREREQ:
2664 nextpc = pc + GET_OFFSET(pc); /* start of next op */
2666 matchCh1 = GET_ARG(pc);
2668 matchCh2 = GET_ARG(pc);
2670 if (x->cp == gData->cpend ||
2671 (*x->cp != matchCh1 && *x->cp != matchCh2)) {
2675 /* else false thru... */
2679 nextpc = pc + GET_OFFSET(pc); /* start of next alternate */
2680 pc += ARG_LEN; /* start of this alternate */
2681 curState->parenSoFar = parenSoFar;
2682 PUSH_STATE_STACK(gData);
2685 if (REOP_IS_SIMPLE(op)) {
2686 if (!SimpleMatch(gData, x, op, &pc, TRUE)) {
2687 op = (REOp) *nextpc++;
2694 nextop = (REOp) *nextpc++;
2695 if (!PushBackTrackState(gData, nextop, nextpc, x, startcp, 0, 0))
2700 * Occurs at (successful) end of REOP_ALT,
2704 * If we have not gotten a result here, it is because of an
2705 * empty match. Do the same thing REOP_EMPTY would do.
2710 --gData->stateStackTop;
2711 pc += GET_OFFSET(pc);
2716 * Occurs at last (successful) end of REOP_ALT,
2720 * If we have not gotten a result here, it is because of an
2721 * empty match. Do the same thing REOP_EMPTY would do.
2726 --gData->stateStackTop;
2731 pc = ReadCompactIndex(pc, &parenIndex);
2732 TRACE("[ %lu ]\n", (unsigned long) parenIndex);
2733 assert(parenIndex < gData->regexp->parenCount);
2734 if (parenIndex + 1 > parenSoFar)
2735 parenSoFar = parenIndex + 1;
2736 x->parens[parenIndex].index = x->cp - gData->cpbegin;
2737 x->parens[parenIndex].length = 0;
2745 pc = ReadCompactIndex(pc, &parenIndex);
2746 assert(parenIndex < gData->regexp->parenCount);
2747 cap = &x->parens[parenIndex];
2748 delta = x->cp - (gData->cpbegin + cap->index);
2749 cap->length = (delta < 0) ? 0 : (size_t) delta;
2757 nextpc = pc + GET_OFFSET(pc); /* start of term after ASSERT */
2758 pc += ARG_LEN; /* start of ASSERT child */
2761 if (REOP_IS_SIMPLE(op) &&
2762 !SimpleMatch(gData, x, op, &testpc, FALSE)) {
2766 curState->u.assertion.top =
2767 (char *)gData->backTrackSP - (char *)gData->backTrackStack;
2768 curState->u.assertion.sz = gData->cursz;
2769 curState->index = x->cp - gData->cpbegin;
2770 curState->parenSoFar = parenSoFar;
2771 PUSH_STATE_STACK(gData);
2772 if (!PushBackTrackState(gData, REOP_ASSERTTEST,
2773 nextpc, x, x->cp, 0, 0)) {
2778 case REOP_ASSERT_NOT:
2779 nextpc = pc + GET_OFFSET(pc);
2783 if (REOP_IS_SIMPLE(op) /* Note - fail to fail! */ &&
2784 SimpleMatch(gData, x, op, &testpc, FALSE) &&
2785 *testpc == REOP_ASSERTNOTTEST) {
2789 curState->u.assertion.top
2790 = (char *)gData->backTrackSP -
2791 (char *)gData->backTrackStack;
2792 curState->u.assertion.sz = gData->cursz;
2793 curState->index = x->cp - gData->cpbegin;
2794 curState->parenSoFar = parenSoFar;
2795 PUSH_STATE_STACK(gData);
2796 if (!PushBackTrackState(gData, REOP_ASSERTNOTTEST,
2797 nextpc, x, x->cp, 0, 0)) {
2802 case REOP_ASSERTTEST:
2803 --gData->stateStackTop;
2805 x->cp = gData->cpbegin + curState->index;
2806 gData->backTrackSP =
2807 (REBackTrackData *) ((char *)gData->backTrackStack +
2808 curState->u.assertion.top);
2809 gData->cursz = curState->u.assertion.sz;
2814 case REOP_ASSERTNOTTEST:
2815 --gData->stateStackTop;
2817 x->cp = gData->cpbegin + curState->index;
2818 gData->backTrackSP =
2819 (REBackTrackData *) ((char *)gData->backTrackStack +
2820 curState->u.assertion.top);
2821 gData->cursz = curState->u.assertion.sz;
2822 result = (!result) ? x : NULL;
2825 curState->u.quantifier.min = 0;
2826 curState->u.quantifier.max = (UINT)-1;
2829 curState->u.quantifier.min = 1;
2830 curState->u.quantifier.max = (UINT)-1;
2833 curState->u.quantifier.min = 0;
2834 curState->u.quantifier.max = 1;
2837 pc = ReadCompactIndex(pc, &k);
2838 curState->u.quantifier.min = k;
2839 pc = ReadCompactIndex(pc, &k);
2840 /* max is k - 1 to use one byte for (UINT)-1 sentinel. */
2841 curState->u.quantifier.max = k - 1;
2842 assert(curState->u.quantifier.min <= curState->u.quantifier.max);
2844 if (curState->u.quantifier.max == 0) {
2845 pc = pc + GET_OFFSET(pc);
2850 /* Step over <next> */
2851 nextpc = pc + ARG_LEN;
2852 op = (REOp) *nextpc++;
2854 if (REOP_IS_SIMPLE(op)) {
2855 if (!SimpleMatch(gData, x, op, &nextpc, TRUE)) {
2856 if (curState->u.quantifier.min == 0)
2860 pc = pc + GET_OFFSET(pc);
2863 op = (REOp) *nextpc++;
2866 curState->index = startcp - gData->cpbegin;
2867 curState->continue_op = REOP_REPEAT;
2868 curState->continue_pc = pc;
2869 curState->parenSoFar = parenSoFar;
2870 PUSH_STATE_STACK(gData);
2871 if (curState->u.quantifier.min == 0 &&
2872 !PushBackTrackState(gData, REOP_REPEAT, pc, x, startcp,
2879 case REOP_ENDCHILD: /* marks the end of a quantifier child */
2880 pc = curState[-1].continue_pc;
2881 op = (REOp) curState[-1].continue_op;
2890 --gData->stateStackTop;
2892 /* Failed, see if we have enough children. */
2893 if (curState->u.quantifier.min == 0)
2897 if (curState->u.quantifier.min == 0 &&
2898 x->cp == gData->cpbegin + curState->index) {
2899 /* matched an empty string, that'll get us nowhere */
2903 if (curState->u.quantifier.min != 0)
2904 curState->u.quantifier.min--;
2905 if (curState->u.quantifier.max != (UINT) -1)
2906 curState->u.quantifier.max--;
2907 if (curState->u.quantifier.max == 0)
2909 nextpc = pc + ARG_LEN;
2910 nextop = (REOp) *nextpc;
2912 if (REOP_IS_SIMPLE(nextop)) {
2914 if (!SimpleMatch(gData, x, nextop, &nextpc, TRUE)) {
2915 if (curState->u.quantifier.min == 0)
2922 curState->index = startcp - gData->cpbegin;
2923 PUSH_STATE_STACK(gData);
2924 if (curState->u.quantifier.min == 0 &&
2925 !PushBackTrackState(gData, REOP_REPEAT,
2927 curState->parenSoFar,
2929 curState->parenSoFar)) {
2932 } while (*nextpc == REOP_ENDCHILD);
2935 parenSoFar = curState->parenSoFar;
2940 pc += GET_OFFSET(pc);
2943 case REOP_MINIMALSTAR:
2944 curState->u.quantifier.min = 0;
2945 curState->u.quantifier.max = (UINT)-1;
2946 goto minimalquantcommon;
2947 case REOP_MINIMALPLUS:
2948 curState->u.quantifier.min = 1;
2949 curState->u.quantifier.max = (UINT)-1;
2950 goto minimalquantcommon;
2951 case REOP_MINIMALOPT:
2952 curState->u.quantifier.min = 0;
2953 curState->u.quantifier.max = 1;
2954 goto minimalquantcommon;
2955 case REOP_MINIMALQUANT:
2956 pc = ReadCompactIndex(pc, &k);
2957 curState->u.quantifier.min = k;
2958 pc = ReadCompactIndex(pc, &k);
2959 /* See REOP_QUANT comments about k - 1. */
2960 curState->u.quantifier.max = k - 1;
2961 assert(curState->u.quantifier.min
2962 <= curState->u.quantifier.max);
2964 curState->index = x->cp - gData->cpbegin;
2965 curState->parenSoFar = parenSoFar;
2966 PUSH_STATE_STACK(gData);
2967 if (curState->u.quantifier.min != 0) {
2968 curState->continue_op = REOP_MINIMALREPEAT;
2969 curState->continue_pc = pc;
2970 /* step over <next> */
2974 if (!PushBackTrackState(gData, REOP_MINIMALREPEAT,
2975 pc, x, x->cp, 0, 0)) {
2978 --gData->stateStackTop;
2979 pc = pc + GET_OFFSET(pc);
2984 case REOP_MINIMALREPEAT:
2985 --gData->stateStackTop;
2988 TRACE("{%d,%d}\n", curState->u.quantifier.min, curState->u.quantifier.max);
2989 #define PREPARE_REPEAT() \
2991 curState->index = x->cp - gData->cpbegin; \
2992 curState->continue_op = REOP_MINIMALREPEAT; \
2993 curState->continue_pc = pc; \
2995 for (k = curState->parenSoFar; k < parenSoFar; k++) \
2996 x->parens[k].index = -1; \
2997 PUSH_STATE_STACK(gData); \
2998 op = (REOp) *pc++; \
2999 assert(op < REOP_LIMIT); \
3005 * Non-greedy failure - try to consume another child.
3007 if (curState->u.quantifier.max == (UINT) -1 ||
3008 curState->u.quantifier.max > 0) {
3012 /* Don't need to adjust pc since we're going to pop. */
3015 if (curState->u.quantifier.min == 0 &&
3016 x->cp == gData->cpbegin + curState->index) {
3017 /* Matched an empty string, that'll get us nowhere. */
3021 if (curState->u.quantifier.min != 0)
3022 curState->u.quantifier.min--;
3023 if (curState->u.quantifier.max != (UINT) -1)
3024 curState->u.quantifier.max--;
3025 if (curState->u.quantifier.min != 0) {
3029 curState->index = x->cp - gData->cpbegin;
3030 curState->parenSoFar = parenSoFar;
3031 PUSH_STATE_STACK(gData);
3032 if (!PushBackTrackState(gData, REOP_MINIMALREPEAT,
3034 curState->parenSoFar,
3035 parenSoFar - curState->parenSoFar)) {
3038 --gData->stateStackTop;
3039 pc = pc + GET_OFFSET(pc);
3041 assert(op < REOP_LIMIT);
3051 * If the match failed and there's a backtrack option, take it.
3052 * Otherwise this is a complete and utter failure.
3055 if (gData->cursz == 0)
3058 /* Potentially detect explosive regex here. */
3059 gData->backTrackCount++;
3060 if (gData->backTrackLimit &&
3061 gData->backTrackCount >= gData->backTrackLimit) {
3062 JS_ReportErrorNumber(gData->cx, js_GetErrorMessage, NULL,
3063 JSMSG_REGEXP_TOO_COMPLEX);
3068 backTrackData = gData->backTrackSP;
3069 gData->cursz = backTrackData->sz;
3070 gData->backTrackSP =
3071 (REBackTrackData *) ((char *)backTrackData - backTrackData->sz);
3072 x->cp = backTrackData->cp;
3073 pc = backTrackData->backtrack_pc;
3074 op = (REOp) backTrackData->backtrack_op;
3075 assert(op < REOP_LIMIT);
3076 gData->stateStackTop = backTrackData->saveStateStackTop;
3077 assert(gData->stateStackTop);
3079 memcpy(gData->stateStack, backTrackData + 1,
3080 sizeof(REProgState) * backTrackData->saveStateStackTop);
3081 curState = &gData->stateStack[gData->stateStackTop - 1];
3083 if (backTrackData->parenCount) {
3084 memcpy(&x->parens[backTrackData->parenIndex],
3085 (char *)(backTrackData + 1) +
3086 sizeof(REProgState) * backTrackData->saveStateStackTop,
3087 sizeof(RECapture) * backTrackData->parenCount);
3088 parenSoFar = backTrackData->parenIndex + backTrackData->parenCount;
3090 for (k = curState->parenSoFar; k < parenSoFar; k++)
3091 x->parens[k].index = -1;
3092 parenSoFar = curState->parenSoFar;
3095 TRACE("\tBT_Pop: %ld,%ld\n",
3096 (unsigned long) backTrackData->parenIndex,
3097 (unsigned long) backTrackData->parenCount);
3103 * Continue with the expression.
3106 assert(op < REOP_LIMIT);
3118 static REMatchState *MatchRegExp(REGlobalData *gData, REMatchState *x)
3120 REMatchState *result;
3121 const WCHAR *cp = x->cp;
3126 * Have to include the position beyond the last character
3127 * in order to detect end-of-input/line condition.
3129 for (cp2 = cp; cp2 <= gData->cpend; cp2++) {
3130 gData->skipped = cp2 - cp;
3132 for (j = 0; j < gData->regexp->parenCount; j++)
3133 x->parens[j].index = -1;
3134 result = ExecuteREBytecode(gData, x);
3135 if (!gData->ok || result || (gData->regexp->flags & JSREG_STICKY))
3137 gData->backTrackSP = gData->backTrackStack;
3139 gData->stateStackTop = 0;
3140 cp2 = cp + gData->skipped;
3145 #define MIN_BACKTRACK_LIMIT 400000
3147 static REMatchState *InitMatch(script_ctx_t *cx, REGlobalData *gData, JSRegExp *re, size_t length)
3149 REMatchState *result;
3152 gData->backTrackStackSize = INITIAL_BACKTRACK;
3153 gData->backTrackStack = jsheap_alloc(gData->pool, INITIAL_BACKTRACK);
3154 if (!gData->backTrackStack)
3157 gData->backTrackSP = gData->backTrackStack;
3159 gData->backTrackCount = 0;
3160 gData->backTrackLimit = 0;
3162 gData->stateStackLimit = INITIAL_STATESTACK;
3163 gData->stateStack = jsheap_alloc(gData->pool, sizeof(REProgState) * INITIAL_STATESTACK);
3164 if (!gData->stateStack)
3167 gData->stateStackTop = 0;
3172 result = jsheap_alloc(gData->pool, offsetof(REMatchState, parens) + re->parenCount * sizeof(RECapture));
3176 for (i = 0; i < re->classCount; i++) {
3177 if (!re->classList[i].converted &&
3178 !ProcessCharSet(gData, &re->classList[i])) {
3186 js_ReportOutOfScriptQuota(cx);
3192 js_DestroyRegExp(JSRegExp *re)
3194 if (re->classList) {
3196 for (i = 0; i < re->classCount; i++) {
3197 if (re->classList[i].converted)
3198 heap_free(re->classList[i].u.bits);
3199 re->classList[i].u.bits = NULL;
3201 heap_free(re->classList);
3207 js_NewRegExp(script_ctx_t *cx, BSTR str, UINT flags, BOOL flat)
3211 CompilerState state;
3218 mark = jsheap_mark(&cx->tmp_heap);
3219 len = SysStringLen(str);
3225 state.cpbegin = state.cp;
3226 state.cpend = state.cp + len;
3227 state.flags = flags;
3228 state.parenCount = 0;
3229 state.classCount = 0;
3230 state.progLength = 0;
3231 state.treeDepth = 0;
3232 state.classBitmapsMem = 0;
3233 for (i = 0; i < CLASS_CACHE_SIZE; i++)
3234 state.classCache[i].start = NULL;
3236 if (len != 0 && flat) {
3237 state.result = NewRENode(&state, REOP_FLAT);
3240 state.result->u.flat.chr = *state.cpbegin;
3241 state.result->u.flat.length = len;
3242 state.result->kid = (void *) state.cpbegin;
3243 /* Flat bytecode: REOP_FLAT compact(string_offset) compact(len). */
3244 state.progLength += 1 + GetCompactIndexWidth(0)
3245 + GetCompactIndexWidth(len);
3247 if (!ParseRegExp(&state))
3250 resize = offsetof(JSRegExp, program) + state.progLength + 1;
3251 re = heap_alloc(resize);
3255 assert(state.classBitmapsMem <= CLASS_BITMAPS_MEM_LIMIT);
3256 re->classCount = state.classCount;
3257 if (re->classCount) {
3258 re->classList = heap_alloc(re->classCount * sizeof(RECharSet));
3259 if (!re->classList) {
3260 js_DestroyRegExp(re);
3264 for (i = 0; i < re->classCount; i++)
3265 re->classList[i].converted = FALSE;
3267 re->classList = NULL;
3269 endPC = EmitREBytecode(&state, re, state.treeDepth, re->program, state.result);
3271 js_DestroyRegExp(re);
3275 *endPC++ = REOP_END;
3277 * Check whether size was overestimated and shrink using realloc.
3278 * This is safe since no pointers to newly parsed regexp or its parts
3279 * besides re exist here.
3281 if ((size_t)(endPC - re->program) != state.progLength + 1) {
3283 assert((size_t)(endPC - re->program) < state.progLength + 1);
3284 resize = offsetof(JSRegExp, program) + (endPC - re->program);
3285 tmp = heap_realloc(re, resize);
3291 re->parenCount = state.parenCount;
3299 static inline RegExpInstance *regexp_from_vdisp(vdisp_t *vdisp)
3301 return (RegExpInstance*)vdisp->u.jsdisp;
3304 static HRESULT do_regexp_match_next(script_ctx_t *ctx, RegExpInstance *regexp, const WCHAR *str, DWORD len,
3305 const WCHAR **cp, match_result_t **parens, DWORD *parens_size, DWORD *parens_cnt, match_result_t *ret)
3307 REMatchState *x, *result;
3311 gData.cpbegin = *cp;
3312 gData.cpend = str + len;
3313 gData.start = *cp-str;
3315 gData.pool = &ctx->tmp_heap;
3317 x = InitMatch(NULL, &gData, regexp->jsregexp, gData.cpend - gData.cpbegin);
3319 WARN("InitMatch failed\n");
3324 result = MatchRegExp(&gData, x);
3326 WARN("MatchRegExp failed\n");
3336 if(regexp->jsregexp->parenCount > *parens_size) {
3337 match_result_t *new_parens;
3340 new_parens = heap_realloc(*parens, sizeof(match_result_t)*regexp->jsregexp->parenCount);
3342 new_parens = heap_alloc(sizeof(match_result_t)*regexp->jsregexp->parenCount);
3344 return E_OUTOFMEMORY;
3346 *parens = new_parens;
3349 *parens_cnt = regexp->jsregexp->parenCount;
3351 for(i=0; i < regexp->jsregexp->parenCount; i++) {
3352 (*parens)[i].str = *cp + result->parens[i].index;
3353 (*parens)[i].len = result->parens[i].length;
3357 matchlen = (result->cp-*cp) - gData.skipped;
3359 ret->str = result->cp-matchlen;
3360 ret->len = matchlen;
3365 HRESULT regexp_match_next(script_ctx_t *ctx, DispatchEx *dispex, BOOL gcheck, const WCHAR *str, DWORD len,
3366 const WCHAR **cp, match_result_t **parens, DWORD *parens_size, DWORD *parens_cnt, match_result_t *ret)
3368 RegExpInstance *regexp = (RegExpInstance*)dispex;
3372 if(gcheck && !(regexp->jsregexp->flags & JSREG_GLOB))
3375 mark = jsheap_mark(&ctx->tmp_heap);
3377 hres = do_regexp_match_next(ctx, regexp, str, len, cp, parens, parens_size, parens_cnt, ret);
3383 HRESULT regexp_match(script_ctx_t *ctx, DispatchEx *dispex, const WCHAR *str, DWORD len, BOOL gflag,
3384 match_result_t **match_result, DWORD *result_cnt)
3386 RegExpInstance *This = (RegExpInstance*)dispex;
3387 match_result_t *ret = NULL, cres;
3388 const WCHAR *cp = str;
3389 DWORD i=0, ret_size = 0;
3393 mark = jsheap_mark(&ctx->tmp_heap);
3396 hres = do_regexp_match_next(ctx, This, str, len, &cp, NULL, NULL, NULL, &cres);
3397 if(hres == S_FALSE) {
3407 ret = heap_realloc(ret, (ret_size <<= 1) * sizeof(match_result_t));
3409 ret = heap_alloc((ret_size=4) * sizeof(match_result_t));
3411 hres = E_OUTOFMEMORY;
3418 if(!gflag && !(This->jsregexp->flags & JSREG_GLOB)) {
3430 *match_result = ret;
3435 static void set_last_index(RegExpInstance *This, DWORD last_index)
3437 This->last_index = last_index;
3438 VariantClear(&This->last_index_var);
3439 num_set_val(&This->last_index_var, last_index);
3442 static HRESULT RegExp_source(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
3443 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
3448 case DISPATCH_PROPERTYGET: {
3449 RegExpInstance *This = regexp_from_vdisp(jsthis);
3451 V_VT(retv) = VT_BSTR;
3452 V_BSTR(retv) = SysAllocString(This->str);
3454 return E_OUTOFMEMORY;
3458 FIXME("Unimplemnted flags %x\n", flags);
3465 static HRESULT RegExp_global(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
3466 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
3472 static HRESULT RegExp_ignoreCase(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
3473 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
3479 static HRESULT RegExp_multiline(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
3480 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
3486 static INT index_from_var(script_ctx_t *ctx, VARIANT *v)
3492 memset(&ei, 0, sizeof(ei));
3493 hres = to_number(ctx, v, &ei, &num);
3494 if(FAILED(hres)) { /* FIXME: Move ignoring exceptions to to_promitive */
3495 VariantClear(&ei.var);
3499 if(V_VT(&num) == VT_R8) {
3500 DOUBLE d = floor(V_R8(&num));
3501 return (DOUBLE)(INT)d == d ? d : 0;
3507 static HRESULT RegExp_lastIndex(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
3508 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
3513 case DISPATCH_PROPERTYGET: {
3514 RegExpInstance *regexp = regexp_from_vdisp(jsthis);
3516 V_VT(retv) = VT_EMPTY;
3517 return VariantCopy(retv, ®exp->last_index_var);
3519 case DISPATCH_PROPERTYPUT: {
3520 RegExpInstance *regexp = regexp_from_vdisp(jsthis);
3524 arg = get_arg(dp,0);
3525 hres = VariantCopy(®exp->last_index_var, arg);
3529 regexp->last_index = index_from_var(ctx, arg);
3533 FIXME("unimplemented flags: %x\n", flags);
3540 static HRESULT RegExp_toString(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
3541 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
3547 static HRESULT create_match_array(script_ctx_t *ctx, BSTR input, const match_result_t *result,
3548 const match_result_t *parens, DWORD parens_cnt, jsexcept_t *ei, IDispatch **ret)
3553 HRESULT hres = S_OK;
3555 static const WCHAR indexW[] = {'i','n','d','e','x',0};
3556 static const WCHAR inputW[] = {'i','n','p','u','t',0};
3557 static const WCHAR zeroW[] = {'0',0};
3559 hres = create_array(ctx, parens_cnt+1, &array);
3563 for(i=0; i < parens_cnt; i++) {
3564 V_VT(&var) = VT_BSTR;
3565 V_BSTR(&var) = SysAllocStringLen(parens[i].str, parens[i].len);
3567 hres = E_OUTOFMEMORY;
3571 hres = jsdisp_propput_idx(array, i+1, &var, ei, NULL/*FIXME*/);
3572 SysFreeString(V_BSTR(&var));
3577 while(SUCCEEDED(hres)) {
3579 V_I4(&var) = result->str-input;
3580 hres = jsdisp_propput_name(array, indexW, &var, ei, NULL/*FIXME*/);
3584 V_VT(&var) = VT_BSTR;
3585 V_BSTR(&var) = input;
3586 hres = jsdisp_propput_name(array, inputW, &var, ei, NULL/*FIXME*/);
3590 V_BSTR(&var) = SysAllocStringLen(result->str, result->len);
3592 hres = E_OUTOFMEMORY;
3595 hres = jsdisp_propput_name(array, zeroW, &var, ei, NULL/*FIXME*/);
3596 SysFreeString(V_BSTR(&var));
3601 jsdisp_release(array);
3605 *ret = (IDispatch*)_IDispatchEx_(array);
3609 static HRESULT run_exec(script_ctx_t *ctx, vdisp_t *jsthis, VARIANT *arg, jsexcept_t *ei, BSTR *input,
3610 match_result_t *match, match_result_t **parens, DWORD *parens_cnt, VARIANT_BOOL *ret)
3612 RegExpInstance *regexp;
3613 DWORD parens_size = 0, last_index = 0, length;
3618 if(!is_vclass(jsthis, JSCLASS_REGEXP)) {
3619 FIXME("Not a RegExp\n");
3623 regexp = regexp_from_vdisp(jsthis);
3626 hres = to_string(ctx, arg, ei, &string);
3630 string = SysAllocStringLen(NULL, 0);
3632 return E_OUTOFMEMORY;
3635 if(regexp->last_index < 0) {
3636 SysFreeString(string);
3637 set_last_index(regexp, 0);
3638 *ret = VARIANT_FALSE;
3642 length = SysStringLen(string);
3643 if(regexp->jsregexp->flags & JSREG_GLOB)
3644 last_index = regexp->last_index;
3646 cp = string + last_index;
3647 hres = regexp_match_next(ctx, ®exp->dispex, FALSE, string, length, &cp, parens, parens ? &parens_size : NULL,
3650 SysFreeString(string);
3655 set_last_index(regexp, cp-string);
3656 *ret = VARIANT_TRUE;
3658 set_last_index(regexp, 0);
3659 *ret = VARIANT_FALSE;
3667 static HRESULT RegExp_exec(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
3668 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
3670 match_result_t *parens = NULL, match;
3671 DWORD parens_cnt = 0;
3678 hres = run_exec(ctx, jsthis, arg_cnt(dp) ? get_arg(dp,0) : NULL, ei, &string, &match, &parens, &parens_cnt, &b);
3686 hres = create_match_array(ctx, string, &match, parens, parens_cnt, ei, &ret);
3687 if(SUCCEEDED(hres)) {
3688 V_VT(retv) = VT_DISPATCH;
3689 V_DISPATCH(retv) = ret;
3692 V_VT(retv) = VT_NULL;
3697 SysFreeString(string);
3701 static HRESULT RegExp_test(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
3702 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
3704 match_result_t match;
3710 hres = run_exec(ctx, jsthis, arg_cnt(dp) ? get_arg(dp,0) : NULL, ei, NULL, &match, NULL, NULL, &b);
3715 V_VT(retv) = VT_BOOL;
3721 static HRESULT RegExp_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
3722 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
3728 return throw_type_error(ctx, ei, IDS_NOT_FUNC, NULL);
3730 FIXME("unimplemented flags %x\n", flags);
3737 static void RegExp_destructor(DispatchEx *dispex)
3739 RegExpInstance *This = (RegExpInstance*)dispex;
3742 js_DestroyRegExp(This->jsregexp);
3743 VariantClear(&This->last_index_var);
3744 SysFreeString(This->str);
3748 static const builtin_prop_t RegExp_props[] = {
3749 {execW, RegExp_exec, PROPF_METHOD|1},
3750 {globalW, RegExp_global, 0},
3751 {ignoreCaseW, RegExp_ignoreCase, 0},
3752 {lastIndexW, RegExp_lastIndex, 0},
3753 {multilineW, RegExp_multiline, 0},
3754 {sourceW, RegExp_source, 0},
3755 {testW, RegExp_test, PROPF_METHOD|1},
3756 {toStringW, RegExp_toString, PROPF_METHOD}
3759 static const builtin_info_t RegExp_info = {
3761 {NULL, RegExp_value, 0},
3762 sizeof(RegExp_props)/sizeof(*RegExp_props),
3768 static HRESULT alloc_regexp(script_ctx_t *ctx, DispatchEx *object_prototype, RegExpInstance **ret)
3770 RegExpInstance *regexp;
3773 regexp = heap_alloc_zero(sizeof(RegExpInstance));
3775 return E_OUTOFMEMORY;
3777 if(object_prototype)
3778 hres = init_dispex(®exp->dispex, ctx, &RegExp_info, object_prototype);
3780 hres = init_dispex_from_constr(®exp->dispex, ctx, &RegExp_info, ctx->regexp_constr);
3791 HRESULT create_regexp(script_ctx_t *ctx, const WCHAR *exp, int len, DWORD flags, DispatchEx **ret)
3793 RegExpInstance *regexp;
3796 TRACE("%s %x\n", debugstr_w(exp), flags);
3798 hres = alloc_regexp(ctx, NULL, ®exp);
3803 regexp->str = SysAllocString(exp);
3805 regexp->str = SysAllocStringLen(exp, len);
3807 jsdisp_release(®exp->dispex);
3808 return E_OUTOFMEMORY;
3811 regexp->jsregexp = js_NewRegExp(ctx, regexp->str, flags, FALSE);
3812 if(!regexp->jsregexp) {
3813 WARN("js_NewRegExp failed\n");
3814 jsdisp_release(®exp->dispex);
3818 V_VT(®exp->last_index_var) = VT_I4;
3819 V_I4(®exp->last_index_var) = 0;
3821 *ret = ®exp->dispex;
3825 static HRESULT regexp_constructor(script_ctx_t *ctx, DISPPARAMS *dp, VARIANT *retv)
3827 const WCHAR *opt = emptyW, *src;
3838 arg = get_arg(dp,0);
3839 if(V_VT(arg) == VT_DISPATCH) {
3842 obj = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
3844 if(is_class(obj, JSCLASS_REGEXP)) {
3845 RegExpInstance *regexp = (RegExpInstance*)obj;
3847 hres = create_regexp(ctx, regexp->str, -1, regexp->jsregexp->flags, &ret);
3848 jsdisp_release(obj);
3852 V_VT(retv) = VT_DISPATCH;
3853 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
3857 jsdisp_release(obj);
3861 if(V_VT(arg) != VT_BSTR) {
3862 FIXME("vt arg0 = %d\n", V_VT(arg));
3868 if(arg_cnt(dp) >= 2) {
3869 arg = get_arg(dp,1);
3870 if(V_VT(arg) != VT_BSTR) {
3871 FIXME("unimplemented for vt %d\n", V_VT(arg));
3878 hres = parse_regexp_flags(opt, strlenW(opt), &flags);
3882 hres = create_regexp(ctx, src, -1, flags, &ret);
3887 V_VT(retv) = VT_DISPATCH;
3888 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(ret);
3890 jsdisp_release(ret);
3895 static HRESULT RegExpConstr_value(script_ctx_t *ctx, vdisp_t *jsthis, WORD flags, DISPPARAMS *dp,
3896 VARIANT *retv, jsexcept_t *ei, IServiceProvider *sp)
3901 case DISPATCH_METHOD:
3903 VARIANT *arg = get_arg(dp,0);
3904 if(V_VT(arg) == VT_DISPATCH) {
3905 DispatchEx *jsdisp = iface_to_jsdisp((IUnknown*)V_DISPATCH(arg));
3907 if(is_class(jsdisp, JSCLASS_REGEXP)) {
3908 if(arg_cnt(dp) > 1 && V_VT(get_arg(dp,1)) != VT_EMPTY) {
3909 jsdisp_release(jsdisp);
3910 return throw_regexp_error(ctx, ei, IDS_REGEXP_SYNTAX_ERROR, NULL);
3914 V_VT(retv) = VT_DISPATCH;
3915 V_DISPATCH(retv) = (IDispatch*)_IDispatchEx_(jsdisp);
3917 jsdisp_release(jsdisp);
3921 jsdisp_release(jsdisp);
3926 case DISPATCH_CONSTRUCT:
3927 return regexp_constructor(ctx, dp, retv);
3929 FIXME("unimplemented flags: %x\n", flags);
3936 HRESULT create_regexp_constr(script_ctx_t *ctx, DispatchEx *object_prototype, DispatchEx **ret)
3938 RegExpInstance *regexp;
3941 static const WCHAR RegExpW[] = {'R','e','g','E','x','p',0};
3943 hres = alloc_regexp(ctx, object_prototype, ®exp);
3947 hres = create_builtin_function(ctx, RegExpConstr_value, RegExpW, NULL,
3948 PROPF_CONSTR|2, ®exp->dispex, ret);
3950 jsdisp_release(®exp->dispex);
3954 HRESULT parse_regexp_flags(const WCHAR *str, DWORD str_len, DWORD *ret)
3959 for (p = str; p < str+str_len; p++) {
3962 flags |= JSREG_GLOB;
3965 flags |= JSREG_FOLD;
3968 flags |= JSREG_MULTILINE;
3971 flags |= JSREG_STICKY;
3974 WARN("wrong flag %c\n", *p);