Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Secret Labs' Regular Expression Engine |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 3 | * |
Fredrik Lundh | 8094611 | 2000-06-29 18:03:25 +0000 | [diff] [blame] | 4 | * regular expression matching engine |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 5 | * |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 6 | * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved. |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 7 | * |
| 8 | * See the _sre.c file for information on usage and redistribution. |
| 9 | */ |
| 10 | |
| 11 | #ifndef SRE_INCLUDED |
| 12 | #define SRE_INCLUDED |
| 13 | |
| 14 | #include "sre_constants.h" |
| 15 | |
Fredrik Lundh | 8f45585 | 2001-06-27 18:59:43 +0000 | [diff] [blame] | 16 | /* size of a code word (must be unsigned short or larger, and |
| 17 | large enough to hold a Py_UNICODE character) */ |
| 18 | #ifdef Py_UNICODE_WIDE |
Fredrik Lundh | 1294ad0 | 2001-06-26 17:17:07 +0000 | [diff] [blame] | 19 | #define SRE_CODE unsigned long |
| 20 | #else |
Fredrik Lundh | 102f3ad | 2000-06-29 08:55:54 +0000 | [diff] [blame] | 21 | #define SRE_CODE unsigned short |
Fredrik Lundh | 1294ad0 | 2001-06-26 17:17:07 +0000 | [diff] [blame] | 22 | #endif |
Fredrik Lundh | 102f3ad | 2000-06-29 08:55:54 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 24 | typedef struct { |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 25 | PyObject_VAR_HEAD |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 26 | int groups; /* must be first! */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 27 | PyObject* groupindex; |
Fredrik Lundh | c230173 | 2000-07-02 22:25:39 +0000 | [diff] [blame] | 28 | PyObject* indexgroup; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 29 | /* compatibility */ |
| 30 | PyObject* pattern; /* pattern source (or None) */ |
| 31 | int flags; /* flags used when compiling pattern source */ |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 32 | /* pattern code */ |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 33 | int codesize; |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 34 | SRE_CODE code[1]; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 35 | } PatternObject; |
| 36 | |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 37 | #define PatternObject_GetCode(o) (((PatternObject*)(o))->code) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 38 | |
| 39 | typedef struct { |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 40 | PyObject_VAR_HEAD |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 41 | PyObject* string; /* link to the target string (must be first) */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 42 | PyObject* regs; /* cached list of matching spans */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 43 | PatternObject* pattern; /* link to the regex (pattern) object */ |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 44 | int pos, endpos; /* current target slice */ |
| 45 | int lastindex; /* last index marker seen by the engine (-1 if none) */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 46 | int groups; /* number of groups (start/end marks) */ |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 47 | int mark[1]; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 48 | } MatchObject; |
| 49 | |
Fredrik Lundh | 102f3ad | 2000-06-29 08:55:54 +0000 | [diff] [blame] | 50 | typedef unsigned int (*SRE_TOLOWER_HOOK)(unsigned int ch); |
| 51 | |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 52 | /* FIXME: <fl> shouldn't be a constant, really... */ |
| 53 | #define SRE_MARK_SIZE 200 |
| 54 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 55 | typedef struct SRE_REPEAT_T { |
| 56 | int count; |
| 57 | SRE_CODE* pattern; /* points to REPEAT operator arguments */ |
| 58 | struct SRE_REPEAT_T *prev; /* points to previous repeat context */ |
| 59 | } SRE_REPEAT; |
| 60 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 61 | typedef struct { |
| 62 | /* string pointers */ |
| 63 | void* ptr; /* current position (also end of current slice) */ |
| 64 | void* beginning; /* start of original string */ |
| 65 | void* start; /* start of current slice */ |
| 66 | void* end; /* end of original string */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 67 | /* attributes for the match object */ |
| 68 | PyObject* string; |
| 69 | int pos, endpos; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 70 | /* character size */ |
| 71 | int charsize; |
| 72 | /* registers */ |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 73 | int lastindex; |
Fredrik Lundh | 102f3ad | 2000-06-29 08:55:54 +0000 | [diff] [blame] | 74 | int lastmark; |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 75 | void* mark[SRE_MARK_SIZE]; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 76 | /* dynamically allocated stuff */ |
| 77 | void** mark_stack; |
| 78 | int mark_stack_size; |
| 79 | int mark_stack_base; |
| 80 | SRE_REPEAT *repeat; /* current repeat context */ |
Fredrik Lundh | 102f3ad | 2000-06-29 08:55:54 +0000 | [diff] [blame] | 81 | /* hooks */ |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 82 | SRE_TOLOWER_HOOK lower; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 83 | } SRE_STATE; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 84 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 85 | typedef struct { |
| 86 | PyObject_HEAD |
| 87 | PyObject* pattern; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 88 | SRE_STATE state; |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 89 | } ScannerObject; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 90 | |
| 91 | #endif |