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 |
Antoine Pitrou | b83ea14 | 2012-11-20 22:30:42 +0100 | [diff] [blame] | 17 | large enough to hold a UCS4 character) */ |
Benjamin Peterson | 5ca88d2 | 2013-01-01 23:04:16 -0600 | [diff] [blame] | 18 | #ifdef Py_USING_UNICODE |
Serhiy Storchaka | e18e05c | 2013-02-16 16:47:15 +0200 | [diff] [blame] | 19 | # define SRE_CODE Py_UCS4 |
| 20 | # if SIZEOF_SIZE_T > 4 |
| 21 | # define SRE_MAXREPEAT (~(SRE_CODE)0) |
| 22 | # else |
Serhiy Storchaka | 3ade66c | 2013-08-03 19:26:33 +0300 | [diff] [blame] | 23 | # define SRE_MAXREPEAT ((SRE_CODE)PY_SSIZE_T_MAX) |
Serhiy Storchaka | e18e05c | 2013-02-16 16:47:15 +0200 | [diff] [blame] | 24 | # endif |
Benjamin Peterson | 5ca88d2 | 2013-01-01 23:04:16 -0600 | [diff] [blame] | 25 | #else |
Serhiy Storchaka | cb1a028 | 2013-05-21 22:53:33 +0300 | [diff] [blame] | 26 | # define SRE_CODE unsigned int |
| 27 | # if SIZEOF_SIZE_T > SIZEOF_INT |
Serhiy Storchaka | e18e05c | 2013-02-16 16:47:15 +0200 | [diff] [blame] | 28 | # define SRE_MAXREPEAT (~(SRE_CODE)0) |
| 29 | # else |
Serhiy Storchaka | 3ade66c | 2013-08-03 19:26:33 +0300 | [diff] [blame] | 30 | # define SRE_MAXREPEAT ((SRE_CODE)PY_SSIZE_T_MAX) |
Serhiy Storchaka | e18e05c | 2013-02-16 16:47:15 +0200 | [diff] [blame] | 31 | # endif |
Benjamin Peterson | 5ca88d2 | 2013-01-01 23:04:16 -0600 | [diff] [blame] | 32 | #endif |
Fredrik Lundh | 102f3ad | 2000-06-29 08:55:54 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 34 | typedef struct { |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 35 | PyObject_VAR_HEAD |
Neal Norwitz | a6d80fa | 2006-06-12 03:05:40 +0000 | [diff] [blame] | 36 | Py_ssize_t groups; /* must be first! */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 37 | PyObject* groupindex; |
Fredrik Lundh | c230173 | 2000-07-02 22:25:39 +0000 | [diff] [blame] | 38 | PyObject* indexgroup; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 39 | /* compatibility */ |
| 40 | PyObject* pattern; /* pattern source (or None) */ |
| 41 | int flags; /* flags used when compiling pattern source */ |
Raymond Hettinger | 027bb63 | 2004-05-31 03:09:25 +0000 | [diff] [blame] | 42 | PyObject *weakreflist; /* List of weak references */ |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 43 | /* pattern code */ |
Neal Norwitz | a6d80fa | 2006-06-12 03:05:40 +0000 | [diff] [blame] | 44 | Py_ssize_t codesize; |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 45 | SRE_CODE code[1]; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 46 | } PatternObject; |
| 47 | |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 48 | #define PatternObject_GetCode(o) (((PatternObject*)(o))->code) |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 49 | |
| 50 | typedef struct { |
Fredrik Lundh | 6f01398 | 2000-07-03 18:44:21 +0000 | [diff] [blame] | 51 | PyObject_VAR_HEAD |
Fredrik Lundh | b0f05bd | 2001-07-02 16:42:49 +0000 | [diff] [blame] | 52 | PyObject* string; /* link to the target string (must be first) */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 53 | PyObject* regs; /* cached list of matching spans */ |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 54 | PatternObject* pattern; /* link to the regex (pattern) object */ |
Neal Norwitz | a6d80fa | 2006-06-12 03:05:40 +0000 | [diff] [blame] | 55 | Py_ssize_t pos, endpos; /* current target slice */ |
| 56 | Py_ssize_t lastindex; /* last index marker seen by the engine (-1 if none) */ |
| 57 | Py_ssize_t groups; /* number of groups (start/end marks) */ |
| 58 | Py_ssize_t mark[1]; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 59 | } MatchObject; |
| 60 | |
Fredrik Lundh | 102f3ad | 2000-06-29 08:55:54 +0000 | [diff] [blame] | 61 | typedef unsigned int (*SRE_TOLOWER_HOOK)(unsigned int ch); |
| 62 | |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 63 | /* FIXME: <fl> shouldn't be a constant, really... */ |
| 64 | #define SRE_MARK_SIZE 200 |
| 65 | |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 66 | typedef struct SRE_REPEAT_T { |
Neal Norwitz | a6d80fa | 2006-06-12 03:05:40 +0000 | [diff] [blame] | 67 | Py_ssize_t count; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 68 | SRE_CODE* pattern; /* points to REPEAT operator arguments */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 69 | void* last_ptr; /* helper to check for infinite loops */ |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 70 | struct SRE_REPEAT_T *prev; /* points to previous repeat context */ |
| 71 | } SRE_REPEAT; |
| 72 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 73 | typedef struct { |
| 74 | /* string pointers */ |
| 75 | void* ptr; /* current position (also end of current slice) */ |
| 76 | void* beginning; /* start of original string */ |
| 77 | void* start; /* start of current slice */ |
| 78 | void* end; /* end of original string */ |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 79 | /* attributes for the match object */ |
| 80 | PyObject* string; |
Neal Norwitz | a6d80fa | 2006-06-12 03:05:40 +0000 | [diff] [blame] | 81 | Py_ssize_t pos, endpos; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 82 | /* character size */ |
| 83 | int charsize; |
| 84 | /* registers */ |
Neal Norwitz | a6d80fa | 2006-06-12 03:05:40 +0000 | [diff] [blame] | 85 | Py_ssize_t lastindex; |
| 86 | Py_ssize_t lastmark; |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 87 | void* mark[SRE_MARK_SIZE]; |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 88 | /* dynamically allocated stuff */ |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 89 | char* data_stack; |
Neal Norwitz | a6d80fa | 2006-06-12 03:05:40 +0000 | [diff] [blame] | 90 | size_t data_stack_size; |
| 91 | size_t data_stack_base; |
Gustavo Niemeyer | ad3fc44 | 2003-10-17 22:13:16 +0000 | [diff] [blame] | 92 | /* current repeat context */ |
| 93 | SRE_REPEAT *repeat; |
Fredrik Lundh | 102f3ad | 2000-06-29 08:55:54 +0000 | [diff] [blame] | 94 | /* hooks */ |
Fredrik Lundh | b389df3 | 2000-06-29 12:48:37 +0000 | [diff] [blame] | 95 | SRE_TOLOWER_HOOK lower; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 96 | } SRE_STATE; |
Guido van Rossum | b700df9 | 2000-03-31 14:59:30 +0000 | [diff] [blame] | 97 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 98 | typedef struct { |
| 99 | PyObject_HEAD |
| 100 | PyObject* pattern; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 101 | SRE_STATE state; |
Fredrik Lundh | be2211e | 2000-06-29 16:57:40 +0000 | [diff] [blame] | 102 | } ScannerObject; |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 103 | |
| 104 | #endif |