PageRenderTime 24ms CodeModel.GetById 6ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

/js/src/yarr/pcre/pcre_internal.h

http://github.com/zpao/v8monkey
C Header | 434 lines | 264 code | 67 blank | 103 comment | 23 complexity | 987e0b3c7cd60a45c658677bc7a893a1 MD5 | raw file
  1/* This is JavaScriptCore's variant of the PCRE library. While this library
  2started out as a copy of PCRE, many of the features of PCRE have been
  3removed. This library now supports only the regular expression features
  4required by the JavaScript language specification, and has only the functions
  5needed by JavaScriptCore and the rest of WebKit.
  6
  7                 Originally written by Philip Hazel
  8           Copyright (c) 1997-2006 University of Cambridge
  9    Copyright (C) 2002, 2004, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
 10
 11-----------------------------------------------------------------------------
 12Redistribution and use in source and binary forms, with or without
 13modification, are permitted provided that the following conditions are met:
 14
 15    * Redistributions of source code must retain the above copyright notice,
 16      this list of conditions and the following disclaimer.
 17
 18    * Redistributions in binary form must reproduce the above copyright
 19      notice, this list of conditions and the following disclaimer in the
 20      documentation and/or other materials provided with the distribution.
 21
 22    * Neither the name of the University of Cambridge nor the names of its
 23      contributors may be used to endorse or promote products derived from
 24      this software without specific prior written permission.
 25
 26THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 27AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 28IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 29ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 30LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 31CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 32SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 33INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 34CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 35ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 36POSSIBILITY OF SUCH DAMAGE.
 37-----------------------------------------------------------------------------
 38*/
 39
 40/* This header contains definitions that are shared between the different
 41modules, but which are not relevant to the exported API. This includes some
 42functions whose names all begin with "_pcre_". */
 43
 44#ifndef PCRE_INTERNAL_H
 45#define PCRE_INTERNAL_H
 46
 47/* Bit definitions for entries in the pcre_ctypes table. */
 48
 49#define ctype_space   0x01
 50#define ctype_xdigit  0x08
 51#define ctype_word    0x10   /* alphameric or '_' */
 52
 53/* Offsets for the bitmap tables in pcre_cbits. Each table contains a set
 54of bits for a class map. Some classes are built by combining these tables. */
 55
 56#define cbit_space     0      /* \s */
 57#define cbit_digit    32      /* \d */
 58#define cbit_word     64      /* \w */
 59#define cbit_length   96      /* Length of the cbits table */
 60
 61/* Offsets of the various tables from the base tables pointer, and
 62total length. */
 63
 64#define lcc_offset      0
 65#define fcc_offset    128
 66#define cbits_offset  256
 67#define ctypes_offset (cbits_offset + cbit_length)
 68#define tables_length (ctypes_offset + 128)
 69
 70#ifndef DFTABLES
 71
 72#include "pcre.h"
 73
 74/* The value of LINK_SIZE determines the number of bytes used to store links as
 75offsets within the compiled regex. The default is 2, which allows for compiled
 76patterns up to 64K long. */
 77
 78#define LINK_SIZE   3
 79
 80/* Use a macro for debugging printing, 'cause that eliminates the use of #ifdef
 81inline, and there are *still* stupid compilers about that don't like indented
 82pre-processor statements, or at least there were when I first wrote this. After
 83all, it had only been about 10 years then... */
 84
 85#ifdef DEBUG
 86#define DPRINTF(p) /*printf p; fflush(stdout);*/
 87#else
 88#define DPRINTF(p) /*nothing*/
 89#endif
 90
 91/* PCRE keeps offsets in its compiled code as 2-byte quantities (always stored
 92in big-endian order) by default. These are used, for example, to link from the
 93start of a subpattern to its alternatives and its end. The use of 2 bytes per
 94offset limits the size of the compiled regex to around 64K, which is big enough
 95for almost everybody. However, I received a request for an even bigger limit.
 96For this reason, and also to make the code easier to maintain, the storing and
 97loading of offsets from the byte string is now handled by the functions that are
 98defined here. */
 99
100/* PCRE uses some other 2-byte quantities that do not change when the size of
101offsets changes. There are used for repeat counts and for other things such as
102capturing parenthesis numbers in back references. */
103
104static inline void put2ByteValue(unsigned char* opcodePtr, int value)
105{
106    JS_ASSERT(value >= 0 && value <= 0xFFFF);
107    opcodePtr[0] = value >> 8;
108    opcodePtr[1] = value;
109}
110
111static inline void put3ByteValue(unsigned char* opcodePtr, int value)
112{
113    JS_ASSERT(value >= 0 && value <= 0xFFFFFF);
114    opcodePtr[0] = value >> 16;
115    opcodePtr[1] = value >> 8;
116    opcodePtr[2] = value;
117}
118
119static inline int get2ByteValue(const unsigned char* opcodePtr)
120{
121    return (opcodePtr[0] << 8) | opcodePtr[1];
122}
123
124static inline int get3ByteValue(const unsigned char* opcodePtr)
125{
126    return (opcodePtr[0] << 16) | (opcodePtr[1] << 8) | opcodePtr[2];
127}
128
129static inline void put2ByteValueAndAdvance(unsigned char*& opcodePtr, int value)
130{
131    put2ByteValue(opcodePtr, value);
132    opcodePtr += 2;
133}
134
135static inline void put3ByteValueAndAdvance(unsigned char*& opcodePtr, int value)
136{
137    put3ByteValue(opcodePtr, value);
138    opcodePtr += 3;
139}
140
141static inline void putLinkValueAllowZero(unsigned char* opcodePtr, int value)
142{
143#if LINK_SIZE == 3
144    put3ByteValue(opcodePtr, value);
145#elif LINK_SIZE == 2
146    put2ByteValue(opcodePtr, value);
147#else
148#   error LINK_SIZE not supported.
149#endif
150}
151
152static inline int getLinkValueAllowZero(const unsigned char* opcodePtr)
153{
154#if LINK_SIZE == 3
155    return get3ByteValue(opcodePtr);
156#elif LINK_SIZE == 2
157    return get2ByteValue(opcodePtr);
158#else
159#   error LINK_SIZE not supported.
160#endif
161}
162
163#define MAX_PATTERN_SIZE 4096 * 1024 // Derived by empirical testing of compile time in PCRE and WREC.
164JS_STATIC_ASSERT(MAX_PATTERN_SIZE < (1 << (8 * LINK_SIZE)));
165
166static inline void putLinkValue(unsigned char* opcodePtr, int value)
167{
168    JS_ASSERT(value);
169    putLinkValueAllowZero(opcodePtr, value);
170}
171
172static inline int getLinkValue(const unsigned char* opcodePtr)
173{
174    int value = getLinkValueAllowZero(opcodePtr);
175    JS_ASSERT(value);
176    return value;
177}
178
179static inline void putLinkValueAndAdvance(unsigned char*& opcodePtr, int value)
180{
181    putLinkValue(opcodePtr, value);
182    opcodePtr += LINK_SIZE;
183}
184
185static inline void putLinkValueAllowZeroAndAdvance(unsigned char*& opcodePtr, int value)
186{
187    putLinkValueAllowZero(opcodePtr, value);
188    opcodePtr += LINK_SIZE;
189}
190
191// FIXME: These are really more of a "compiled regexp state" than "regexp options"
192enum RegExpOptions {
193    UseFirstByteOptimizationOption = 0x40000000,  /* firstByte is set */
194    UseRequiredByteOptimizationOption = 0x20000000,  /* reqByte is set */
195    UseMultiLineFirstByteOptimizationOption = 0x10000000,  /* start after \n for multiline */
196    IsAnchoredOption = 0x02000000,  /* can't use partial with this regex */
197    IgnoreCaseOption = 0x00000001,
198    MatchAcrossMultipleLinesOption = 0x00000002
199};
200
201/* Flags added to firstByte or reqByte; a "non-literal" item is either a
202variable-length repeat, or a anything other than literal characters. */
203
204#define REQ_IGNORE_CASE 0x0100    /* indicates should ignore case */
205#define REQ_VARY     0x0200    /* reqByte followed non-literal item */
206
207/* Miscellaneous definitions */
208
209/* Flag bits and data types for the extended class (OP_XCLASS) for classes that
210contain UTF-8 characters with values greater than 255. */
211
212#define XCL_NOT    0x01    /* Flag: this is a negative class */
213#define XCL_MAP    0x02    /* Flag: a 32-byte map is present */
214
215#define XCL_END       0    /* Marks end of individual items */
216#define XCL_SINGLE    1    /* Single item (one multibyte char) follows */
217#define XCL_RANGE     2    /* A range (two multibyte chars) follows */
218
219/* These are escaped items that aren't just an encoding of a particular data
220value such as \n. They must have non-zero values, as check_escape() returns
221their negation. Also, they must appear in the same order as in the opcode
222definitions below, up to ESC_w. The final one must be
223ESC_REF as subsequent values are used for \1, \2, \3, etc. There is are two
224tests in the code for an escape > ESC_b and <= ESC_w to
225detect the types that may be repeated. These are the types that consume
226characters. If any new escapes are put in between that don't consume a
227character, that code will have to change. */
228
229enum { ESC_B = 1, ESC_b, ESC_D, ESC_d, ESC_S, ESC_s, ESC_W, ESC_w, ESC_REF };
230
231/* Opcode table: OP_BRA must be last, as all values >= it are used for brackets
232that extract substrings. Starting from 1 (i.e. after OP_END), the values up to
233OP_EOD must correspond in order to the list of escapes immediately above.
234Note that whenever this list is updated, the two macro definitions that follow
235must also be updated to match. */
236
237#define FOR_EACH_OPCODE(macro) \
238    macro(END) \
239    \
240    , macro(NOT_WORD_BOUNDARY) \
241    , macro(WORD_BOUNDARY) \
242    , macro(NOT_DIGIT) \
243    , macro(DIGIT) \
244    , macro(NOT_WHITESPACE) \
245    , macro(WHITESPACE) \
246    , macro(NOT_WORDCHAR) \
247    , macro(WORDCHAR) \
248    \
249    , macro(NOT_NEWLINE) \
250    \
251    , macro(CIRC) \
252    , macro(DOLL) \
253    , macro(BOL) \
254    , macro(EOL) \
255    , macro(CHAR) \
256    , macro(CHAR_IGNORING_CASE) \
257    , macro(ASCII_CHAR) \
258    , macro(ASCII_LETTER_IGNORING_CASE) \
259    , macro(NOT) \
260    \
261    , macro(STAR) \
262    , macro(MINSTAR) \
263    , macro(PLUS) \
264    , macro(MINPLUS) \
265    , macro(QUERY) \
266    , macro(MINQUERY) \
267    , macro(UPTO) \
268    , macro(MINUPTO) \
269    , macro(EXACT) \
270    \
271    , macro(NOTSTAR) \
272    , macro(NOTMINSTAR) \
273    , macro(NOTPLUS) \
274    , macro(NOTMINPLUS) \
275    , macro(NOTQUERY) \
276    , macro(NOTMINQUERY) \
277    , macro(NOTUPTO) \
278    , macro(NOTMINUPTO) \
279    , macro(NOTEXACT) \
280    \
281    , macro(TYPESTAR) \
282    , macro(TYPEMINSTAR) \
283    , macro(TYPEPLUS) \
284    , macro(TYPEMINPLUS) \
285    , macro(TYPEQUERY) \
286    , macro(TYPEMINQUERY) \
287    , macro(TYPEUPTO) \
288    , macro(TYPEMINUPTO) \
289    , macro(TYPEEXACT) \
290    \
291    , macro(CRSTAR) \
292    , macro(CRMINSTAR) \
293    , macro(CRPLUS) \
294    , macro(CRMINPLUS) \
295    , macro(CRQUERY) \
296    , macro(CRMINQUERY) \
297    , macro(CRRANGE) \
298    , macro(CRMINRANGE) \
299    \
300    , macro(CLASS) \
301    , macro(NCLASS) \
302    , macro(XCLASS) \
303    \
304    , macro(REF) \
305    \
306    , macro(ALT) \
307    , macro(KET) \
308    , macro(KETRMAX) \
309    , macro(KETRMIN) \
310    \
311    , macro(ASSERT) \
312    , macro(ASSERT_NOT) \
313    \
314    , macro(BRAZERO) \
315    , macro(BRAMINZERO) \
316    , macro(BRANUMBER) \
317    , macro(BRA)
318
319#define OPCODE_ENUM_VALUE(opcode) OP_##opcode
320enum { FOR_EACH_OPCODE(OPCODE_ENUM_VALUE) };
321
322/* WARNING WARNING WARNING: There is an implicit assumption in pcre.c and
323study.c that all opcodes are less than 128 in value. This makes handling UTF-8
324character sequences easier. */
325
326/* The highest extraction number before we have to start using additional
327bytes. (Originally PCRE didn't have support for extraction counts higher than
328this number.) The value is limited by the number of opcodes left after OP_BRA,
329i.e. 255 - OP_BRA. We actually set it a bit lower to leave room for additional
330opcodes. */
331
332/* FIXME: Note that OP_BRA + 100 is > 128, so the two comments above
333are in conflict! */
334
335#define EXTRACT_BASIC_MAX  100
336
337/* The code vector runs on as long as necessary after the end. */
338
339struct JSRegExp {
340    unsigned options;
341
342    unsigned short topBracket;
343    unsigned short topBackref;
344    
345    unsigned short firstByte;
346    unsigned short reqByte;
347};
348
349/* Internal shared data tables. These are tables that are used by more than one
350 of the exported public functions. They have to be "external" in the C sense,
351 but are not part of the PCRE public API. The data for these tables is in the
352 pcre_tables.c module. */
353
354#define jsc_pcre_utf8_table1_size 6
355
356extern const int    jsc_pcre_utf8_table1[6];
357extern const int    jsc_pcre_utf8_table2[6];
358extern const int    jsc_pcre_utf8_table3[6];
359extern const unsigned char jsc_pcre_utf8_table4[0x40];
360
361extern const unsigned char jsc_pcre_default_tables[tables_length];
362
363static inline unsigned char toLowerCase(unsigned char c)
364{
365    static const unsigned char* lowerCaseChars = jsc_pcre_default_tables + lcc_offset;
366    return lowerCaseChars[c];
367}
368
369static inline unsigned char flipCase(unsigned char c)
370{
371    static const unsigned char* flippedCaseChars = jsc_pcre_default_tables + fcc_offset;
372    return flippedCaseChars[c];
373}
374
375static inline unsigned char classBitmapForChar(unsigned char c)
376{
377    static const unsigned char* charClassBitmaps = jsc_pcre_default_tables + cbits_offset;
378    return charClassBitmaps[c];
379}
380
381static inline unsigned char charTypeForChar(unsigned char c)
382{
383    const unsigned char* charTypeMap = jsc_pcre_default_tables + ctypes_offset;
384    return charTypeMap[c];
385}
386
387static inline bool isWordChar(UChar c)
388{
389    return c < 128 && (charTypeForChar(c) & ctype_word);
390}
391
392static inline bool isSpaceChar(UChar c)
393{
394    return (c < 128 && (charTypeForChar(c) & ctype_space)) || c == 0x00A0;
395}
396
397static inline bool isNewline(UChar nl)
398{
399    return (nl == 0xA || nl == 0xD || nl == 0x2028 || nl == 0x2029);
400}
401
402static inline bool isBracketStartOpcode(unsigned char opcode)
403{
404    if (opcode >= OP_BRA)
405        return true;
406    switch (opcode) {
407        case OP_ASSERT:
408        case OP_ASSERT_NOT:
409            return true;
410        default:
411            return false;
412    }
413}
414
415static inline void advanceToEndOfBracket(const unsigned char*& opcodePtr)
416{
417    JS_ASSERT(isBracketStartOpcode(*opcodePtr) || *opcodePtr == OP_ALT);
418    do
419        opcodePtr += getLinkValue(opcodePtr + 1);
420    while (*opcodePtr == OP_ALT);
421}
422
423/* Internal shared functions. These are functions that are used in more
424that one of the source files. They have to have external linkage, but
425but are not part of the public API and so not exported from the library. */
426
427extern int jsc_pcre_ucp_othercase(unsigned);
428extern bool jsc_pcre_xclass(int, const unsigned char*);
429
430#endif
431
432#endif
433
434/* End of pcre_internal.h */