blob: 94962cbc39b1fe6dedf34dcd08dec5c6b5d6998c [file] [log] [blame]
Guido van Rossum7627c0d2000-03-31 14:58:54 +00001#
2# Secret Labs' Regular Expression Engine
Guido van Rossum7627c0d2000-03-31 14:58:54 +00003#
4# various symbols used by the regular expression engine.
5# run this script to update the _sre include files!
6#
Fredrik Lundh770617b2001-01-14 15:06:11 +00007# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved.
Guido van Rossum7627c0d2000-03-31 14:58:54 +00008#
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00009# See the sre.py file for information on usage and redistribution.
Guido van Rossum7627c0d2000-03-31 14:58:54 +000010#
11
Fred Drakeb8f22742001-09-04 19:10:20 +000012"""Internal support module for sre"""
13
Fredrik Lundhb35ffc02001-01-15 12:46:09 +000014# update when constants are added or removed
15
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +000016MAGIC = 20031017
Fredrik Lundhb35ffc02001-01-15 12:46:09 +000017
18# max code word in this release
19
Fredrik Lundh13ac9922000-10-07 17:38:23 +000020MAXREPEAT = 65535
21
Fredrik Lundhb35ffc02001-01-15 12:46:09 +000022# SRE standard exception (access as sre.error)
Fredrik Lundh436c3d582000-06-29 08:58:44 +000023# should this really be here?
24
25class error(Exception):
26 pass
27
Guido van Rossum7627c0d2000-03-31 14:58:54 +000028# operators
29
30FAILURE = "failure"
31SUCCESS = "success"
32
33ANY = "any"
Fredrik Lundhe1869832000-08-01 22:47:49 +000034ANY_ALL = "any_all"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000035ASSERT = "assert"
Fredrik Lundh43b3b492000-06-30 10:41:31 +000036ASSERT_NOT = "assert_not"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000037AT = "at"
Fredrik Lundh19af43d2001-07-02 16:58:38 +000038BIGCHARSET = "bigcharset"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000039BRANCH = "branch"
40CALL = "call"
41CATEGORY = "category"
Fredrik Lundh3562f112000-07-02 12:00:07 +000042CHARSET = "charset"
Fredrik Lundh72b82ba2000-07-03 21:31:48 +000043GROUPREF = "groupref"
44GROUPREF_IGNORE = "groupref_ignore"
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +000045GROUPREF_EXISTS = "groupref_exists"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000046IN = "in"
47IN_IGNORE = "in_ignore"
Fredrik Lundh436c3d582000-06-29 08:58:44 +000048INFO = "info"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000049JUMP = "jump"
50LITERAL = "literal"
51LITERAL_IGNORE = "literal_ignore"
52MARK = "mark"
53MAX_REPEAT = "max_repeat"
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000054MAX_UNTIL = "max_until"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000055MIN_REPEAT = "min_repeat"
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000056MIN_UNTIL = "min_until"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000057NEGATE = "negate"
58NOT_LITERAL = "not_literal"
59NOT_LITERAL_IGNORE = "not_literal_ignore"
60RANGE = "range"
61REPEAT = "repeat"
Fredrik Lundh436c3d582000-06-29 08:58:44 +000062REPEAT_ONE = "repeat_one"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000063SUBPATTERN = "subpattern"
Guido van Rossum41c99e72003-04-14 17:59:34 +000064MIN_REPEAT_ONE = "min_repeat_one"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000065
66# positions
67AT_BEGINNING = "at_beginning"
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000068AT_BEGINNING_LINE = "at_beginning_line"
Fredrik Lundh770617b2001-01-14 15:06:11 +000069AT_BEGINNING_STRING = "at_beginning_string"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000070AT_BOUNDARY = "at_boundary"
71AT_NON_BOUNDARY = "at_non_boundary"
72AT_END = "at_end"
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000073AT_END_LINE = "at_end_line"
Fredrik Lundh770617b2001-01-14 15:06:11 +000074AT_END_STRING = "at_end_string"
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +000075AT_LOC_BOUNDARY = "at_loc_boundary"
76AT_LOC_NON_BOUNDARY = "at_loc_non_boundary"
77AT_UNI_BOUNDARY = "at_uni_boundary"
78AT_UNI_NON_BOUNDARY = "at_uni_non_boundary"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000079
80# categories
Guido van Rossum7627c0d2000-03-31 14:58:54 +000081CATEGORY_DIGIT = "category_digit"
82CATEGORY_NOT_DIGIT = "category_not_digit"
83CATEGORY_SPACE = "category_space"
84CATEGORY_NOT_SPACE = "category_not_space"
85CATEGORY_WORD = "category_word"
86CATEGORY_NOT_WORD = "category_not_word"
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000087CATEGORY_LINEBREAK = "category_linebreak"
88CATEGORY_NOT_LINEBREAK = "category_not_linebreak"
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000089CATEGORY_LOC_WORD = "category_loc_word"
90CATEGORY_LOC_NOT_WORD = "category_loc_not_word"
Fredrik Lundh436c3d582000-06-29 08:58:44 +000091CATEGORY_UNI_DIGIT = "category_uni_digit"
92CATEGORY_UNI_NOT_DIGIT = "category_uni_not_digit"
93CATEGORY_UNI_SPACE = "category_uni_space"
94CATEGORY_UNI_NOT_SPACE = "category_uni_not_space"
95CATEGORY_UNI_WORD = "category_uni_word"
96CATEGORY_UNI_NOT_WORD = "category_uni_not_word"
97CATEGORY_UNI_LINEBREAK = "category_uni_linebreak"
98CATEGORY_UNI_NOT_LINEBREAK = "category_uni_not_linebreak"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000099
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000100OPCODES = [
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000101
102 # failure=0 success=1 (just because it looks better that way :-)
103 FAILURE, SUCCESS,
104
Fredrik Lundhe1869832000-08-01 22:47:49 +0000105 ANY, ANY_ALL,
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000106 ASSERT, ASSERT_NOT,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000107 AT,
108 BRANCH,
109 CALL,
110 CATEGORY,
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000111 CHARSET, BIGCHARSET,
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000112 GROUPREF, GROUPREF_EXISTS, GROUPREF_IGNORE,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000113 IN, IN_IGNORE,
Fredrik Lundh436c3d582000-06-29 08:58:44 +0000114 INFO,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000115 JUMP,
116 LITERAL, LITERAL_IGNORE,
117 MARK,
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000118 MAX_UNTIL,
119 MIN_UNTIL,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000120 NOT_LITERAL, NOT_LITERAL_IGNORE,
121 NEGATE,
122 RANGE,
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000123 REPEAT,
124 REPEAT_ONE,
Guido van Rossum41c99e72003-04-14 17:59:34 +0000125 SUBPATTERN,
126 MIN_REPEAT_ONE
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000127
128]
129
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000130ATCODES = [
Fredrik Lundh770617b2001-01-14 15:06:11 +0000131 AT_BEGINNING, AT_BEGINNING_LINE, AT_BEGINNING_STRING, AT_BOUNDARY,
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000132 AT_NON_BOUNDARY, AT_END, AT_END_LINE, AT_END_STRING,
133 AT_LOC_BOUNDARY, AT_LOC_NON_BOUNDARY, AT_UNI_BOUNDARY,
134 AT_UNI_NON_BOUNDARY
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000135]
136
137CHCODES = [
138 CATEGORY_DIGIT, CATEGORY_NOT_DIGIT, CATEGORY_SPACE,
139 CATEGORY_NOT_SPACE, CATEGORY_WORD, CATEGORY_NOT_WORD,
Fredrik Lundh436c3d582000-06-29 08:58:44 +0000140 CATEGORY_LINEBREAK, CATEGORY_NOT_LINEBREAK, CATEGORY_LOC_WORD,
141 CATEGORY_LOC_NOT_WORD, CATEGORY_UNI_DIGIT, CATEGORY_UNI_NOT_DIGIT,
142 CATEGORY_UNI_SPACE, CATEGORY_UNI_NOT_SPACE, CATEGORY_UNI_WORD,
143 CATEGORY_UNI_NOT_WORD, CATEGORY_UNI_LINEBREAK,
144 CATEGORY_UNI_NOT_LINEBREAK
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000145]
146
147def makedict(list):
148 d = {}
149 i = 0
150 for item in list:
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000151 d[item] = i
152 i = i + 1
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000153 return d
154
155OPCODES = makedict(OPCODES)
156ATCODES = makedict(ATCODES)
157CHCODES = makedict(CHCODES)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000158
159# replacement operations for "ignore case" mode
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000160OP_IGNORE = {
Fredrik Lundh72b82ba2000-07-03 21:31:48 +0000161 GROUPREF: GROUPREF_IGNORE,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000162 IN: IN_IGNORE,
163 LITERAL: LITERAL_IGNORE,
164 NOT_LITERAL: NOT_LITERAL_IGNORE
165}
166
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000167AT_MULTILINE = {
168 AT_BEGINNING: AT_BEGINNING_LINE,
169 AT_END: AT_END_LINE
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000170}
171
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000172AT_LOCALE = {
173 AT_BOUNDARY: AT_LOC_BOUNDARY,
174 AT_NON_BOUNDARY: AT_LOC_NON_BOUNDARY
175}
176
177AT_UNICODE = {
178 AT_BOUNDARY: AT_UNI_BOUNDARY,
179 AT_NON_BOUNDARY: AT_UNI_NON_BOUNDARY
180}
181
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000182CH_LOCALE = {
Fredrik Lundh436c3d582000-06-29 08:58:44 +0000183 CATEGORY_DIGIT: CATEGORY_DIGIT,
184 CATEGORY_NOT_DIGIT: CATEGORY_NOT_DIGIT,
185 CATEGORY_SPACE: CATEGORY_SPACE,
186 CATEGORY_NOT_SPACE: CATEGORY_NOT_SPACE,
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000187 CATEGORY_WORD: CATEGORY_LOC_WORD,
188 CATEGORY_NOT_WORD: CATEGORY_LOC_NOT_WORD,
Fredrik Lundh436c3d582000-06-29 08:58:44 +0000189 CATEGORY_LINEBREAK: CATEGORY_LINEBREAK,
190 CATEGORY_NOT_LINEBREAK: CATEGORY_NOT_LINEBREAK
191}
192
193CH_UNICODE = {
194 CATEGORY_DIGIT: CATEGORY_UNI_DIGIT,
195 CATEGORY_NOT_DIGIT: CATEGORY_UNI_NOT_DIGIT,
196 CATEGORY_SPACE: CATEGORY_UNI_SPACE,
197 CATEGORY_NOT_SPACE: CATEGORY_UNI_NOT_SPACE,
198 CATEGORY_WORD: CATEGORY_UNI_WORD,
199 CATEGORY_NOT_WORD: CATEGORY_UNI_NOT_WORD,
200 CATEGORY_LINEBREAK: CATEGORY_UNI_LINEBREAK,
201 CATEGORY_NOT_LINEBREAK: CATEGORY_UNI_NOT_LINEBREAK
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000202}
203
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000204# flags
Fredrik Lundh3562f112000-07-02 12:00:07 +0000205SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking)
206SRE_FLAG_IGNORECASE = 2 # case insensitive
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000207SRE_FLAG_LOCALE = 4 # honour system locale
Fredrik Lundh3562f112000-07-02 12:00:07 +0000208SRE_FLAG_MULTILINE = 8 # treat target as multiline string
209SRE_FLAG_DOTALL = 16 # treat target as a single string
210SRE_FLAG_UNICODE = 32 # use unicode locale
211SRE_FLAG_VERBOSE = 64 # ignore whitespace and comments
Fredrik Lundh770617b2001-01-14 15:06:11 +0000212SRE_FLAG_DEBUG = 128 # debugging
Fredrik Lundh3562f112000-07-02 12:00:07 +0000213
214# flags for INFO primitive
215SRE_INFO_PREFIX = 1 # has prefix
216SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix)
217SRE_INFO_CHARSET = 4 # pattern starts with character from given set
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000218
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000219if __name__ == "__main__":
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000220 def dump(f, d, prefix):
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000221 items = d.items()
Raymond Hettinger6b59f5f2003-10-16 05:53:16 +0000222 items.sort(key=lambda a: a[1])
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000223 for k, v in items:
Barry Warsaw8bee7612004-08-25 02:22:30 +0000224 f.write("#define %s_%s %s\n" % (prefix, k.upper(), v))
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000225 f = open("sre_constants.h", "w")
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000226 f.write("""\
227/*
228 * Secret Labs' Regular Expression Engine
229 *
230 * regular expression matching engine
231 *
232 * NOTE: This file is generated by sre_constants.py. If you need
233 * to change anything in here, edit sre_constants.py and run it.
234 *
Fredrik Lundh770617b2001-01-14 15:06:11 +0000235 * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000236 *
237 * See the _sre.c file for information on usage and redistribution.
238 */
239
240""")
241
Fredrik Lundhb35ffc02001-01-15 12:46:09 +0000242 f.write("#define SRE_MAGIC %d\n" % MAGIC)
243
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000244 dump(f, OPCODES, "SRE_OP")
245 dump(f, ATCODES, "SRE")
246 dump(f, CHCODES, "SRE")
Fredrik Lundh3562f112000-07-02 12:00:07 +0000247
Fredrik Lundh436c3d582000-06-29 08:58:44 +0000248 f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE)
249 f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE)
250 f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE)
251 f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE)
252 f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL)
253 f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE)
254 f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE)
Fredrik Lundh3562f112000-07-02 12:00:07 +0000255
256 f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX)
257 f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL)
258 f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET)
259
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000260 f.close()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000261 print("done")