sre.h 2.58 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 17 18
/* size of a code word (must be unsigned short or larger, and
   large enough to hold a Py_UNICODE character) */
#ifdef Py_UNICODE_WIDE
19
#define SRE_CODE Py_UCS4
20
#else
Fredrik Lundh's avatar
Fredrik Lundh committed
21
#define SRE_CODE unsigned short
22
#endif
Fredrik Lundh's avatar
Fredrik Lundh committed
23

24
typedef struct {
25
    PyObject_VAR_HEAD
26
    int groups; /* must be first! */
27
    PyObject* groupindex;
28
    PyObject* indexgroup;
29 30 31
    /* compatibility */
    PyObject* pattern; /* pattern source (or None) */
    int flags; /* flags used when compiling pattern source */
32
    PyObject *weakreflist; /* List of weak references */
33
    /* pattern code */
34
    int codesize;
35
    SRE_CODE code[1];
36 37
} PatternObject;

38
#define PatternObject_GetCode(o) (((PatternObject*)(o))->code)
39 40

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

Fredrik Lundh's avatar
Fredrik Lundh committed
51 52
typedef unsigned int (*SRE_TOLOWER_HOOK)(unsigned int ch);

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

56 57 58
typedef struct SRE_REPEAT_T {
    int count;
    SRE_CODE* pattern; /* points to REPEAT operator arguments */
59
    void* last_ptr; /* helper to check for infinite loops */
60 61 62
    struct SRE_REPEAT_T *prev; /* points to previous repeat context */
} SRE_REPEAT;

63 64 65 66 67 68
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 */
69 70 71
    /* attributes for the match object */
    PyObject* string;
    int pos, endpos;
72 73 74
    /* character size */
    int charsize;
    /* registers */
75
    int lastindex;
Fredrik Lundh's avatar
Fredrik Lundh committed
76
    int lastmark;
Fredrik Lundh's avatar
Fredrik Lundh committed
77
    void* mark[SRE_MARK_SIZE];
78
    /* dynamically allocated stuff */
79
    char* data_stack;
80 81
    unsigned int data_stack_size;
    unsigned int data_stack_base;
82 83
    /* current repeat context */
    SRE_REPEAT *repeat;
Fredrik Lundh's avatar
Fredrik Lundh committed
84
    /* hooks */
85
    SRE_TOLOWER_HOOK lower;
86
} SRE_STATE;
87

88 89 90 91
typedef struct {
    PyObject_HEAD
    PyObject* pattern;
    SRE_STATE state;
Fredrik Lundh's avatar
Fredrik Lundh committed
92
} ScannerObject;
93 94

#endif