Back to index

lightning-sunbird  0.9+nobinonly
jsregexp.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1998
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef jsregexp_h___
00041 #define jsregexp_h___
00042 /*
00043  * JS regular expression interface.
00044  */
00045 #include <stddef.h>
00046 #include "jspubtd.h"
00047 #include "jsstr.h"
00048 
00049 #ifdef JS_THREADSAFE
00050 #include "jsdhash.h"
00051 #endif
00052 
00053 struct JSRegExpStatics {
00054     JSString    *input;         /* input string to match (perl $_, GC root) */
00055     JSBool      multiline;      /* whether input contains newlines (perl $*) */
00056     uint16      parenCount;     /* number of valid elements in parens[] */
00057     uint16      moreLength;     /* number of allocated elements in moreParens */
00058     JSSubString parens[9];      /* last set of parens matched (perl $1, $2) */
00059     JSSubString *moreParens;    /* null or realloc'd vector for $10, etc. */
00060     JSSubString lastMatch;      /* last string matched (perl $&) */
00061     JSSubString lastParen;      /* last paren matched (perl $+) */
00062     JSSubString leftContext;    /* input to left of last match (perl $`) */
00063     JSSubString rightContext;   /* input to right of last match (perl $') */
00064 };
00065 
00066 /*
00067  * This struct holds a bitmap representation of a class from a regexp.
00068  * There's a list of these referenced by the classList field in the JSRegExp
00069  * struct below. The initial state has startIndex set to the offset in the
00070  * original regexp source of the beginning of the class contents. The first
00071  * use of the class converts the source representation into a bitmap.
00072  *
00073  */
00074 typedef struct RECharSet {
00075     JSPackedBool    converted;
00076     JSPackedBool    sense;
00077     uint16          length;
00078     union {
00079         uint8       *bits;
00080         struct {
00081             size_t  startIndex;
00082             size_t  length;
00083         } src;
00084     } u;
00085 } RECharSet;
00086 
00087 /*
00088  * This macro is safe because moreParens is guaranteed to be allocated and big
00089  * enough to hold parenCount, or else be null when parenCount is 0.
00090  */
00091 #define REGEXP_PAREN_SUBSTRING(res, num)                                      \
00092     (((jsuint)(num) < (jsuint)(res)->parenCount)                              \
00093      ? ((jsuint)(num) < 9)                                                    \
00094        ? &(res)->parens[num]                                                  \
00095        : &(res)->moreParens[(num) - 9]                                        \
00096      : &js_EmptySubString)
00097 
00098 typedef struct RENode RENode;
00099 
00100 struct JSRegExp {
00101     jsrefcount   nrefs;         /* reference count */
00102     uint16       flags;         /* flags, see jsapi.h's JSREG_* defines */
00103     uint16       cloneIndex;    /* index in fp->vars or funobj->slots of
00104                                    cloned regexp object */
00105     size_t       parenCount;    /* number of parenthesized submatches */
00106     size_t       classCount;    /* count [...] bitmaps */
00107     RECharSet    *classList;    /* list of [...] bitmaps */
00108     JSString     *source;       /* locked source string, sans // */
00109     jsbytecode   program[1];    /* regular expression bytecode */
00110 };
00111 
00112 extern JSRegExp *
00113 js_NewRegExp(JSContext *cx, JSTokenStream *ts,
00114              JSString *str, uintN flags, JSBool flat);
00115 
00116 extern JSRegExp *
00117 js_NewRegExpOpt(JSContext *cx, JSTokenStream *ts,
00118                 JSString *str, JSString *opt, JSBool flat);
00119 
00120 #define HOLD_REGEXP(cx, re) JS_ATOMIC_INCREMENT(&(re)->nrefs)
00121 #define DROP_REGEXP(cx, re) js_DestroyRegExp(cx, re)
00122 
00123 extern void
00124 js_DestroyRegExp(JSContext *cx, JSRegExp *re);
00125 
00126 /*
00127  * Execute re on input str at *indexp, returning null in *rval on mismatch.
00128  * On match, return true if test is true, otherwise return an array object.
00129  * Update *indexp and cx->regExpStatics always on match.
00130  */
00131 extern JSBool
00132 js_ExecuteRegExp(JSContext *cx, JSRegExp *re, JSString *str, size_t *indexp,
00133                  JSBool test, jsval *rval);
00134 
00135 /*
00136  * These two add and remove GC roots, respectively, so their calls must be
00137  * well-ordered.
00138  */
00139 extern JSBool
00140 js_InitRegExpStatics(JSContext *cx, JSRegExpStatics *res);
00141 
00142 extern void
00143 js_FreeRegExpStatics(JSContext *cx, JSRegExpStatics *res);
00144 
00145 #define JSVAL_IS_REGEXP(cx, v)                                                \
00146     (JSVAL_IS_OBJECT(v) && JSVAL_TO_OBJECT(v) &&                              \
00147      OBJ_GET_CLASS(cx, JSVAL_TO_OBJECT(v)) == &js_RegExpClass)
00148 
00149 extern JSClass js_RegExpClass;
00150 
00151 extern JSObject *
00152 js_InitRegExpClass(JSContext *cx, JSObject *obj);
00153 
00154 /*
00155  * Export js_regexp_toString to the decompiler.
00156  */
00157 extern JSBool
00158 js_regexp_toString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
00159                    jsval *rval);
00160 
00161 /*
00162  * Create, serialize/deserialize, or clone a RegExp object.
00163  */
00164 extern JSObject *
00165 js_NewRegExpObject(JSContext *cx, JSTokenStream *ts,
00166                    jschar *chars, size_t length, uintN flags);
00167 
00168 extern JSBool
00169 js_XDRRegExp(JSXDRState *xdr, JSObject **objp);
00170 
00171 extern JSObject *
00172 js_CloneRegExpObject(JSContext *cx, JSObject *obj, JSObject *parent);
00173 
00174 /*
00175  * Get and set the per-object (clone or clone-parent) lastIndex slot.
00176  */
00177 extern JSBool
00178 js_GetLastIndex(JSContext *cx, JSObject *obj, jsdouble *lastIndex);
00179 
00180 extern JSBool
00181 js_SetLastIndex(JSContext *cx, JSObject *obj, jsdouble lastIndex);
00182 
00183 #endif /* jsregexp_h___ */