blob: b0b61d91bfccc0ee2cbc5bdcfddb78fd6ef17fcf [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
Fredrik Lundh19af43d2001-07-02 16:58:38 +000016MAGIC = 20010701
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 Lundh436c3d52000-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"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000045IN = "in"
46IN_IGNORE = "in_ignore"
Fredrik Lundh436c3d52000-06-29 08:58:44 +000047INFO = "info"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000048JUMP = "jump"
49LITERAL = "literal"
50LITERAL_IGNORE = "literal_ignore"
51MARK = "mark"
52MAX_REPEAT = "max_repeat"
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000053MAX_UNTIL = "max_until"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000054MIN_REPEAT = "min_repeat"
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000055MIN_UNTIL = "min_until"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000056NEGATE = "negate"
57NOT_LITERAL = "not_literal"
58NOT_LITERAL_IGNORE = "not_literal_ignore"
59RANGE = "range"
60REPEAT = "repeat"
Fredrik Lundh436c3d52000-06-29 08:58:44 +000061REPEAT_ONE = "repeat_one"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000062SUBPATTERN = "subpattern"
63
64# positions
65AT_BEGINNING = "at_beginning"
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000066AT_BEGINNING_LINE = "at_beginning_line"
Fredrik Lundh770617b2001-01-14 15:06:11 +000067AT_BEGINNING_STRING = "at_beginning_string"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000068AT_BOUNDARY = "at_boundary"
69AT_NON_BOUNDARY = "at_non_boundary"
70AT_END = "at_end"
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000071AT_END_LINE = "at_end_line"
Fredrik Lundh770617b2001-01-14 15:06:11 +000072AT_END_STRING = "at_end_string"
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +000073AT_LOC_BOUNDARY = "at_loc_boundary"
74AT_LOC_NON_BOUNDARY = "at_loc_non_boundary"
75AT_UNI_BOUNDARY = "at_uni_boundary"
76AT_UNI_NON_BOUNDARY = "at_uni_non_boundary"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000077
78# categories
Guido van Rossum7627c0d2000-03-31 14:58:54 +000079CATEGORY_DIGIT = "category_digit"
80CATEGORY_NOT_DIGIT = "category_not_digit"
81CATEGORY_SPACE = "category_space"
82CATEGORY_NOT_SPACE = "category_not_space"
83CATEGORY_WORD = "category_word"
84CATEGORY_NOT_WORD = "category_not_word"
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000085CATEGORY_LINEBREAK = "category_linebreak"
86CATEGORY_NOT_LINEBREAK = "category_not_linebreak"
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000087CATEGORY_LOC_WORD = "category_loc_word"
88CATEGORY_LOC_NOT_WORD = "category_loc_not_word"
Fredrik Lundh436c3d52000-06-29 08:58:44 +000089CATEGORY_UNI_DIGIT = "category_uni_digit"
90CATEGORY_UNI_NOT_DIGIT = "category_uni_not_digit"
91CATEGORY_UNI_SPACE = "category_uni_space"
92CATEGORY_UNI_NOT_SPACE = "category_uni_not_space"
93CATEGORY_UNI_WORD = "category_uni_word"
94CATEGORY_UNI_NOT_WORD = "category_uni_not_word"
95CATEGORY_UNI_LINEBREAK = "category_uni_linebreak"
96CATEGORY_UNI_NOT_LINEBREAK = "category_uni_not_linebreak"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000097
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000098OPCODES = [
Guido van Rossum7627c0d2000-03-31 14:58:54 +000099
100 # failure=0 success=1 (just because it looks better that way :-)
101 FAILURE, SUCCESS,
102
Fredrik Lundhe1869832000-08-01 22:47:49 +0000103 ANY, ANY_ALL,
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000104 ASSERT, ASSERT_NOT,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000105 AT,
106 BRANCH,
107 CALL,
108 CATEGORY,
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000109 CHARSET, BIGCHARSET,
Fredrik Lundh72b82ba2000-07-03 21:31:48 +0000110 GROUPREF, GROUPREF_IGNORE,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000111 IN, IN_IGNORE,
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000112 INFO,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000113 JUMP,
114 LITERAL, LITERAL_IGNORE,
115 MARK,
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000116 MAX_UNTIL,
117 MIN_UNTIL,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000118 NOT_LITERAL, NOT_LITERAL_IGNORE,
119 NEGATE,
120 RANGE,
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000121 REPEAT,
122 REPEAT_ONE,
123 SUBPATTERN
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000124
125]
126
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000127ATCODES = [
Fredrik Lundh770617b2001-01-14 15:06:11 +0000128 AT_BEGINNING, AT_BEGINNING_LINE, AT_BEGINNING_STRING, AT_BOUNDARY,
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000129 AT_NON_BOUNDARY, AT_END, AT_END_LINE, AT_END_STRING,
130 AT_LOC_BOUNDARY, AT_LOC_NON_BOUNDARY, AT_UNI_BOUNDARY,
131 AT_UNI_NON_BOUNDARY
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000132]
133
134CHCODES = [
135 CATEGORY_DIGIT, CATEGORY_NOT_DIGIT, CATEGORY_SPACE,
136 CATEGORY_NOT_SPACE, CATEGORY_WORD, CATEGORY_NOT_WORD,
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000137 CATEGORY_LINEBREAK, CATEGORY_NOT_LINEBREAK, CATEGORY_LOC_WORD,
138 CATEGORY_LOC_NOT_WORD, CATEGORY_UNI_DIGIT, CATEGORY_UNI_NOT_DIGIT,
139 CATEGORY_UNI_SPACE, CATEGORY_UNI_NOT_SPACE, CATEGORY_UNI_WORD,
140 CATEGORY_UNI_NOT_WORD, CATEGORY_UNI_LINEBREAK,
141 CATEGORY_UNI_NOT_LINEBREAK
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000142]
143
144def makedict(list):
145 d = {}
146 i = 0
147 for item in list:
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000148 d[item] = i
149 i = i + 1
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000150 return d
151
152OPCODES = makedict(OPCODES)
153ATCODES = makedict(ATCODES)
154CHCODES = makedict(CHCODES)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000155
156# replacement operations for "ignore case" mode
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000157OP_IGNORE = {
Fredrik Lundh72b82ba2000-07-03 21:31:48 +0000158 GROUPREF: GROUPREF_IGNORE,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000159 IN: IN_IGNORE,
160 LITERAL: LITERAL_IGNORE,
161 NOT_LITERAL: NOT_LITERAL_IGNORE
162}
163
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000164AT_MULTILINE = {
165 AT_BEGINNING: AT_BEGINNING_LINE,
166 AT_END: AT_END_LINE
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000167}
168
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000169AT_LOCALE = {
170 AT_BOUNDARY: AT_LOC_BOUNDARY,
171 AT_NON_BOUNDARY: AT_LOC_NON_BOUNDARY
172}
173
174AT_UNICODE = {
175 AT_BOUNDARY: AT_UNI_BOUNDARY,
176 AT_NON_BOUNDARY: AT_UNI_NON_BOUNDARY
177}
178
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000179CH_LOCALE = {
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000180 CATEGORY_DIGIT: CATEGORY_DIGIT,
181 CATEGORY_NOT_DIGIT: CATEGORY_NOT_DIGIT,
182 CATEGORY_SPACE: CATEGORY_SPACE,
183 CATEGORY_NOT_SPACE: CATEGORY_NOT_SPACE,
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000184 CATEGORY_WORD: CATEGORY_LOC_WORD,
185 CATEGORY_NOT_WORD: CATEGORY_LOC_NOT_WORD,
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000186 CATEGORY_LINEBREAK: CATEGORY_LINEBREAK,
187 CATEGORY_NOT_LINEBREAK: CATEGORY_NOT_LINEBREAK
188}
189
190CH_UNICODE = {
191 CATEGORY_DIGIT: CATEGORY_UNI_DIGIT,
192 CATEGORY_NOT_DIGIT: CATEGORY_UNI_NOT_DIGIT,
193 CATEGORY_SPACE: CATEGORY_UNI_SPACE,
194 CATEGORY_NOT_SPACE: CATEGORY_UNI_NOT_SPACE,
195 CATEGORY_WORD: CATEGORY_UNI_WORD,
196 CATEGORY_NOT_WORD: CATEGORY_UNI_NOT_WORD,
197 CATEGORY_LINEBREAK: CATEGORY_UNI_LINEBREAK,
198 CATEGORY_NOT_LINEBREAK: CATEGORY_UNI_NOT_LINEBREAK
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000199}
200
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000201# flags
Fredrik Lundh3562f112000-07-02 12:00:07 +0000202SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking)
203SRE_FLAG_IGNORECASE = 2 # case insensitive
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000204SRE_FLAG_LOCALE = 4 # honour system locale
Fredrik Lundh3562f112000-07-02 12:00:07 +0000205SRE_FLAG_MULTILINE = 8 # treat target as multiline string
206SRE_FLAG_DOTALL = 16 # treat target as a single string
207SRE_FLAG_UNICODE = 32 # use unicode locale
208SRE_FLAG_VERBOSE = 64 # ignore whitespace and comments
Fredrik Lundh770617b2001-01-14 15:06:11 +0000209SRE_FLAG_DEBUG = 128 # debugging
Fredrik Lundh3562f112000-07-02 12:00:07 +0000210
211# flags for INFO primitive
212SRE_INFO_PREFIX = 1 # has prefix
213SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix)
214SRE_INFO_CHARSET = 4 # pattern starts with character from given set
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000215
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000216if __name__ == "__main__":
Fredrik Lundhf2989b22001-02-18 12:05:16 +0000217 import string
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000218 def dump(f, d, prefix):
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000219 items = d.items()
220 items.sort(lambda a, b: cmp(a[1], b[1]))
221 for k, v in items:
Fredrik Lundhf2989b22001-02-18 12:05:16 +0000222 f.write("#define %s_%s %s\n" % (prefix, string.upper(k), v))
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000223 f = open("sre_constants.h", "w")
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000224 f.write("""\
225/*
226 * Secret Labs' Regular Expression Engine
227 *
228 * regular expression matching engine
229 *
230 * NOTE: This file is generated by sre_constants.py. If you need
231 * to change anything in here, edit sre_constants.py and run it.
232 *
Fredrik Lundh770617b2001-01-14 15:06:11 +0000233 * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000234 *
235 * See the _sre.c file for information on usage and redistribution.
236 */
237
238""")
239
Fredrik Lundhb35ffc02001-01-15 12:46:09 +0000240 f.write("#define SRE_MAGIC %d\n" % MAGIC)
241
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000242 dump(f, OPCODES, "SRE_OP")
243 dump(f, ATCODES, "SRE")
244 dump(f, CHCODES, "SRE")
Fredrik Lundh3562f112000-07-02 12:00:07 +0000245
Fredrik Lundh436c3d52000-06-29 08:58:44 +0000246 f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE)
247 f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE)
248 f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE)
249 f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE)
250 f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL)
251 f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE)
252 f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE)
Fredrik Lundh3562f112000-07-02 12:00:07 +0000253
254 f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX)
255 f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL)
256 f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET)
257
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000258 f.close()
259 print "done"