blob: 0a8f0cfe3d92de2b6087fb120ba101573a40b508 [file] [log] [blame]
Guido van Rossumb700df92000-03-31 14:59:30 +00001/*
2 * Secret Labs' Regular Expression Engine
Guido van Rossumb700df92000-03-31 14:59:30 +00003 *
Fredrik Lundh80946112000-06-29 18:03:25 +00004 * regular expression matching engine
Guido van Rossumb700df92000-03-31 14:59:30 +00005 *
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +00006 * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
Guido van Rossumb700df92000-03-31 14:59:30 +00007 *
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 Lundh8f455852001-06-27 18:59:43 +000016/* size of a code word (must be unsigned short or larger, and
Antoine Pitrou766a16e2012-06-23 14:17:39 +020017 large enough to hold a UCS4 character) */
Martin v. Löwis7d9c6c72004-05-07 07:18:13 +000018#define SRE_CODE Py_UCS4
Serhiy Storchaka70ca0212013-02-16 16:47:47 +020019#if SIZEOF_SIZE_T > 4
20# define SRE_MAXREPEAT (~(SRE_CODE)0)
21#else
Serhiy Storchaka1f35ae02013-08-03 19:18:38 +030022# define SRE_MAXREPEAT ((SRE_CODE)PY_SSIZE_T_MAX)
Serhiy Storchaka70ca0212013-02-16 16:47:47 +020023#endif
Fredrik Lundh102f3ad2000-06-29 08:55:54 +000024
Guido van Rossumb700df92000-03-31 14:59:30 +000025typedef struct {
Fredrik Lundh6f013982000-07-03 18:44:21 +000026 PyObject_VAR_HEAD
Thomas Wouters0e3f5912006-08-11 14:57:12 +000027 Py_ssize_t groups; /* must be first! */
Guido van Rossumb700df92000-03-31 14:59:30 +000028 PyObject* groupindex;
Fredrik Lundhc2301732000-07-02 22:25:39 +000029 PyObject* indexgroup;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000030 /* compatibility */
31 PyObject* pattern; /* pattern source (or None) */
32 int flags; /* flags used when compiling pattern source */
Raymond Hettinger027bb632004-05-31 03:09:25 +000033 PyObject *weakreflist; /* List of weak references */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020034 int logical_charsize; /* pattern charsize (or -1) */
35 int charsize;
Benjamin Petersone48944b2012-03-07 14:50:25 -060036 Py_buffer view;
Fredrik Lundh6f013982000-07-03 18:44:21 +000037 /* pattern code */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000038 Py_ssize_t codesize;
Fredrik Lundh6f013982000-07-03 18:44:21 +000039 SRE_CODE code[1];
Guido van Rossumb700df92000-03-31 14:59:30 +000040} PatternObject;
41
Fredrik Lundh6f013982000-07-03 18:44:21 +000042#define PatternObject_GetCode(o) (((PatternObject*)(o))->code)
Guido van Rossumb700df92000-03-31 14:59:30 +000043
44typedef struct {
Fredrik Lundh6f013982000-07-03 18:44:21 +000045 PyObject_VAR_HEAD
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +000046 PyObject* string; /* link to the target string (must be first) */
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000047 PyObject* regs; /* cached list of matching spans */
Guido van Rossumb700df92000-03-31 14:59:30 +000048 PatternObject* pattern; /* link to the regex (pattern) object */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000049 Py_ssize_t pos, endpos; /* current target slice */
50 Py_ssize_t lastindex; /* last index marker seen by the engine (-1 if none) */
51 Py_ssize_t groups; /* number of groups (start/end marks) */
52 Py_ssize_t mark[1];
Guido van Rossumb700df92000-03-31 14:59:30 +000053} MatchObject;
54
Fredrik Lundh102f3ad2000-06-29 08:55:54 +000055typedef unsigned int (*SRE_TOLOWER_HOOK)(unsigned int ch);
56
Fredrik Lundhbe2211e2000-06-29 16:57:40 +000057/* FIXME: <fl> shouldn't be a constant, really... */
58#define SRE_MARK_SIZE 200
59
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000060typedef struct SRE_REPEAT_T {
Thomas Wouters0e3f5912006-08-11 14:57:12 +000061 Py_ssize_t count;
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000062 SRE_CODE* pattern; /* points to REPEAT operator arguments */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +000063 void* last_ptr; /* helper to check for infinite loops */
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000064 struct SRE_REPEAT_T *prev; /* points to previous repeat context */
65} SRE_REPEAT;
66
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000067typedef struct {
68 /* string pointers */
69 void* ptr; /* current position (also end of current slice) */
70 void* beginning; /* start of original string */
71 void* start; /* start of current slice */
72 void* end; /* end of original string */
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000073 /* attributes for the match object */
74 PyObject* string;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075 Py_ssize_t pos, endpos;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000076 /* character size */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +020077 int logical_charsize; /* kind of thing: 1 - bytes, 2/4 - unicode */
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000078 int charsize;
79 /* registers */
Thomas Wouters0e3f5912006-08-11 14:57:12 +000080 Py_ssize_t lastindex;
81 Py_ssize_t lastmark;
Fredrik Lundhbe2211e2000-06-29 16:57:40 +000082 void* mark[SRE_MARK_SIZE];
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000083 /* dynamically allocated stuff */
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +000084 char* data_stack;
Thomas Wouters0e3f5912006-08-11 14:57:12 +000085 size_t data_stack_size;
86 size_t data_stack_base;
Benjamin Petersone48944b2012-03-07 14:50:25 -060087 Py_buffer buffer;
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +000088 /* current repeat context */
89 SRE_REPEAT *repeat;
Fredrik Lundh102f3ad2000-06-29 08:55:54 +000090 /* hooks */
Fredrik Lundhb389df32000-06-29 12:48:37 +000091 SRE_TOLOWER_HOOK lower;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000092} SRE_STATE;
Guido van Rossumb700df92000-03-31 14:59:30 +000093
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000094typedef struct {
95 PyObject_HEAD
96 PyObject* pattern;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000097 SRE_STATE state;
Fredrik Lundhbe2211e2000-06-29 16:57:40 +000098} ScannerObject;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000099
100#endif