sre.h 2.87 KB
Newer Older
1 2 3
/*
 * Secret Labs' Regular Expression Engine
 *
4
 * regular expression matching engine
5
 *
6
 * Copyright (c) 1997-2001 by Secret Labs AB.  All rights reserved.
7 8 9 10 11 12 13 14 15
 *
 * See the _sre.c file for information on usage and redistribution.
 */

#ifndef SRE_INCLUDED
#define SRE_INCLUDED

#include "sre_constants.h"

16
/* size of a code word (must be unsigned short or larger, and
17
   large enough to hold a UCS4 character) */
18
#define SRE_CODE Py_UCS4
19 20 21
#if SIZEOF_SIZE_T > 4
# define SRE_MAXREPEAT (~(SRE_CODE)0)
#else
22
# define SRE_MAXREPEAT ((SRE_CODE)PY_SSIZE_T_MAX)
23
#endif
Fredrik Lundh's avatar
Fredrik Lundh committed
24

25
typedef struct {
26
    PyObject_VAR_HEAD
27
    Py_ssize_t groups; /* must be first! */
28
    PyObject* groupindex;
29
    PyObject* indexgroup;
30 31 32
    /* compatibility */
    PyObject* pattern; /* pattern source (or None) */
    int flags; /* flags used when compiling pattern source */
33
    PyObject *weakreflist; /* List of weak references */
Martin v. Löwis's avatar
Martin v. Löwis committed
34 35
    int logical_charsize; /* pattern charsize (or -1) */
    int charsize;
36
    Py_buffer view;
37
    /* pattern code */
38
    Py_ssize_t codesize;
39
    SRE_CODE code[1];
40 41
} PatternObject;

42
#define PatternObject_GetCode(o) (((PatternObject*)(o))->code)
43 44

typedef struct {
45
    PyObject_VAR_HEAD
46
    PyObject* string; /* link to the target string (must be first) */
47
    PyObject* regs; /* cached list of matching spans */
48
    PatternObject* pattern; /* link to the regex (pattern) object */
49 50 51 52
    Py_ssize_t pos, endpos; /* current target slice */
    Py_ssize_t lastindex; /* last index marker seen by the engine (-1 if none) */
    Py_ssize_t groups; /* number of groups (start/end marks) */
    Py_ssize_t mark[1];
53 54
} MatchObject;

Fredrik Lundh's avatar
Fredrik Lundh committed
55 56
typedef unsigned int (*SRE_TOLOWER_HOOK)(unsigned int ch);

Fredrik Lundh's avatar
Fredrik Lundh committed
57 58 59
/* FIXME: <fl> shouldn't be a constant, really... */
#define SRE_MARK_SIZE 200

60
typedef struct SRE_REPEAT_T {
61
    Py_ssize_t count;
62
    SRE_CODE* pattern; /* points to REPEAT operator arguments */
63
    void* last_ptr; /* helper to check for infinite loops */
64 65 66
    struct SRE_REPEAT_T *prev; /* points to previous repeat context */
} SRE_REPEAT;

67 68 69 70 71 72
typedef struct {
    /* string pointers */
    void* ptr; /* current position (also end of current slice) */
    void* beginning; /* start of original string */
    void* start; /* start of current slice */
    void* end; /* end of original string */
73 74
    /* attributes for the match object */
    PyObject* string;
75
    Py_ssize_t pos, endpos;
76
    /* character size */
Martin v. Löwis's avatar
Martin v. Löwis committed
77
    int logical_charsize; /* kind of thing: 1 - bytes, 2/4 - unicode */
78 79
    int charsize;
    /* registers */
80 81
    Py_ssize_t lastindex;
    Py_ssize_t lastmark;
Fredrik Lundh's avatar
Fredrik Lundh committed
82
    void* mark[SRE_MARK_SIZE];
83
    /* dynamically allocated stuff */
84
    char* data_stack;
85 86
    size_t data_stack_size;
    size_t data_stack_base;
87
    Py_buffer buffer;
88 89
    /* current repeat context */
    SRE_REPEAT *repeat;
Fredrik Lundh's avatar
Fredrik Lundh committed
90
    /* hooks */
91
    SRE_TOLOWER_HOOK lower;
92
} SRE_STATE;
93

94 95 96 97
typedef struct {
    PyObject_HEAD
    PyObject* pattern;
    SRE_STATE state;
Fredrik Lundh's avatar
Fredrik Lundh committed
98
} ScannerObject;
99 100

#endif