home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / js / src / jsregexp.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.1 KB  |  107 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifndef jsregexp_h___
  20. #define jsregexp_h___
  21. /*
  22.  * JS regular expression interface.
  23.  */
  24. #include <stddef.h>
  25. #include "jspubtd.h"
  26. #include "jsstr.h"
  27.  
  28. struct JSRegExpStatics {
  29.     JSString    *input;         /* input string to match (perl $_, GC root) */
  30.     JSBool      multiline;      /* whether input contains newlines (perl $*) */
  31.     uintN       parenCount;     /* number of valid elements in parens[] */
  32.     uintN       moreLength;     /* number of allocated elements in moreParens */
  33.     JSSubString parens[9];      /* last set of parens matched (perl $1, $2) */
  34.     JSSubString *moreParens;    /* null or realloc'd vector for $10, etc. */
  35.     JSSubString lastMatch;      /* last string matched (perl $&) */
  36.     JSSubString lastParen;      /* last paren matched (perl $+) */
  37.     JSSubString leftContext;    /* input to left of last match (perl $`) */
  38.     JSSubString rightContext;   /* input to right of last match (perl $') */
  39.     JSObject    *execWrapper;   /* exec() wrapper function (GC root) */
  40.     JSObject    *execObject;    /* weak link to this param of /x+/("xyz") */
  41. };
  42.  
  43. /*
  44.  * This macro is safe because moreParens is guaranteed to be allocated and big
  45.  * enough to hold parenCount, or else be null when parenCount is 0.
  46.  */
  47. #define REGEXP_PAREN_SUBSTRING(res, num)                                      \
  48.     (((jsuint)(num) < (jsuint)(res)->parenCount)                              \
  49.      ? ((jsuint)(num) < 9)                                                    \
  50.        ? &(res)->parens[num]                                                  \
  51.        : &(res)->moreParens[(num) - 9]                                        \
  52.      : &js_EmptySubString)
  53.  
  54. struct JSRegExp {
  55.     JSString    *source;        /* locked source string, sans // */
  56.     size_t      length;         /* program length in bytes */
  57.     size_t      lastIndex;      /* index after last match, for //g iterator */
  58.     uintN       parenCount;     /* number of parenthesized submatches */
  59.     uint8       flags;          /* flags, see jsapi.h */
  60.     jsbytecode  program[1];     /* regular expression bytecode */
  61. };
  62.  
  63. extern JSRegExp *
  64. js_NewRegExp(JSContext *cx, JSString *str, uintN flags);
  65.  
  66. extern JSRegExp *
  67. js_NewRegExpOpt(JSContext *cx, JSString *str, JSString *opt);
  68.  
  69. extern void
  70. js_DestroyRegExp(JSContext *cx, JSRegExp *re);
  71.  
  72. /*
  73.  * Execute re on input str at *indexp, returning null in *rval on mismatch.
  74.  * On match, return true if test is true, otherwise return an array object.
  75.  * Update *indexp and cx->regExpStatics always on match.
  76.  */
  77. extern JSBool
  78. js_ExecuteRegExp(JSContext *cx, JSRegExp *re, JSString *str, size_t *indexp,
  79.          JSBool test, jsval *rval);
  80.  
  81. /*
  82.  * These two add and remove GC roots, respectively, so their calls must be
  83.  * well-ordered.
  84.  */
  85. extern JSBool
  86. js_InitRegExpStatics(JSContext *cx, JSRegExpStatics *res);
  87.  
  88. extern void
  89. js_FreeRegExpStatics(JSContext *cx, JSRegExpStatics *res);
  90.  
  91. #define JSVAL_IS_REGEXP(v)                                                    \
  92.     (JSVAL_IS_OBJECT(v) && JSVAL_TO_OBJECT(v) &&                              \
  93.      JSVAL_TO_OBJECT(v)->map->clasp == &js_RegExpClass)
  94.  
  95. extern JSClass js_RegExpClass;
  96.  
  97. extern JSObject *
  98. js_InitRegExpClass(JSContext *cx, JSObject *obj);
  99.  
  100. /*
  101.  * Create a new RegExp object.
  102.  */
  103. extern JSObject *
  104. js_NewRegExpObject(JSContext *cx, jschar *chars, size_t length, uintN flags);
  105.  
  106. #endif /* jsregexp_h___ */
  107.