blob: a7fdfbab647f177b75681b44a4e1381cd58d853e [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
17 large enough to hold a Py_UNICODE character) */
18#ifdef Py_UNICODE_WIDE
Fredrik Lundh1294ad02001-06-26 17:17:07 +000019#define SRE_CODE unsigned long
20#else
Fredrik Lundh102f3ad2000-06-29 08:55:54 +000021#define SRE_CODE unsigned short
Fredrik Lundh1294ad02001-06-26 17:17:07 +000022#endif
Fredrik Lundh102f3ad2000-06-29 08:55:54 +000023
Guido van Rossumb700df92000-03-31 14:59:30 +000024typedef struct {
Fredrik Lundh6f013982000-07-03 18:44:21 +000025 PyObject_VAR_HEAD
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +000026 int groups; /* must be first! */
Guido van Rossumb700df92000-03-31 14:59:30 +000027 PyObject* groupindex;
Fredrik Lundhc2301732000-07-02 22:25:39 +000028 PyObject* indexgroup;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000029 /* compatibility */
30 PyObject* pattern; /* pattern source (or None) */
31 int flags; /* flags used when compiling pattern source */
Fredrik Lundh6f013982000-07-03 18:44:21 +000032 /* pattern code */
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +000033 int codesize;
Fredrik Lundh6f013982000-07-03 18:44:21 +000034 SRE_CODE code[1];
Guido van Rossumb700df92000-03-31 14:59:30 +000035} PatternObject;
36
Fredrik Lundh6f013982000-07-03 18:44:21 +000037#define PatternObject_GetCode(o) (((PatternObject*)(o))->code)
Guido van Rossumb700df92000-03-31 14:59:30 +000038
39typedef struct {
Fredrik Lundh6f013982000-07-03 18:44:21 +000040 PyObject_VAR_HEAD
Fredrik Lundhb0f05bd2001-07-02 16:42:49 +000041 PyObject* string; /* link to the target string (must be first) */
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +000042 PyObject* regs; /* cached list of matching spans */
Guido van Rossumb700df92000-03-31 14:59:30 +000043 PatternObject* pattern; /* link to the regex (pattern) object */
Fredrik Lundh6f013982000-07-03 18:44:21 +000044 int pos, endpos; /* current target slice */
45 int lastindex; /* last index marker seen by the engine (-1 if none) */
Guido van Rossumb700df92000-03-31 14:59:30 +000046 int groups; /* number of groups (start/end marks) */
Fredrik Lundh6f013982000-07-03 18:44:21 +000047 int mark[1];
Guido van Rossumb700df92000-03-31 14:59:30 +000048} MatchObject;
49
Fredrik Lundh102f3ad2000-06-29 08:55:54 +000050typedef unsigned int (*SRE_TOLOWER_HOOK)(unsigned int ch);
51
Fredrik Lundhbe2211e2000-06-29 16:57:40 +000052/* FIXME: <fl> shouldn't be a constant, really... */
53#define SRE_MARK_SIZE 200
54
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000055typedef 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 Hyltonb1aa1952000-06-01 17:39:12 +000061typedef 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 Lundh8a3ebf82000-07-23 21:46:17 +000067 /* attributes for the match object */
68 PyObject* string;
69 int pos, endpos;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000070 /* character size */
71 int charsize;
72 /* registers */
Fredrik Lundh6f013982000-07-03 18:44:21 +000073 int lastindex;
Fredrik Lundh102f3ad2000-06-29 08:55:54 +000074 int lastmark;
Fredrik Lundhbe2211e2000-06-29 16:57:40 +000075 void* mark[SRE_MARK_SIZE];
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000076 /* 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 Lundh102f3ad2000-06-29 08:55:54 +000081 /* hooks */
Fredrik Lundhb389df32000-06-29 12:48:37 +000082 SRE_TOLOWER_HOOK lower;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000083} SRE_STATE;
Guido van Rossumb700df92000-03-31 14:59:30 +000084
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000085typedef struct {
86 PyObject_HEAD
87 PyObject* pattern;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000088 SRE_STATE state;
Fredrik Lundhbe2211e2000-06-29 16:57:40 +000089} ScannerObject;
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000090
91#endif