blob: bbe7880a1d55f023cad702ca151ec7ba8ae25c36 [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
Fredrik Lundhb35ffc02001-01-15 12:46:09 +000012# update when constants are added or removed
13
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +000014MAGIC = 20010320
Fredrik Lundhb35ffc02001-01-15 12:46:09 +000015
16# max code word in this release
17
Fredrik Lundh13ac9922000-10-07 17:38:23 +000018MAXREPEAT = 65535
19
Fredrik Lundhb35ffc02001-01-15 12:46:09 +000020# SRE standard exception (access as sre.error)
Fredrik Lundh436c3d582000-06-29 08:58:44 +000021# should this really be here?
22
23class error(Exception):
24 pass
25
Guido van Rossum7627c0d2000-03-31 14:58:54 +000026# operators
27
28FAILURE = "failure"
29SUCCESS = "success"
30
31ANY = "any"
Fredrik Lundhe1869832000-08-01 22:47:49 +000032ANY_ALL = "any_all"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000033ASSERT = "assert"
Fredrik Lundh43b3b492000-06-30 10:41:31 +000034ASSERT_NOT = "assert_not"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000035AT = "at"
36BRANCH = "branch"
37CALL = "call"
38CATEGORY = "category"
Fredrik Lundh3562f112000-07-02 12:00:07 +000039CHARSET = "charset"
Fredrik Lundh72b82ba2000-07-03 21:31:48 +000040GROUPREF = "groupref"
41GROUPREF_IGNORE = "groupref_ignore"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000042IN = "in"
43IN_IGNORE = "in_ignore"
Fredrik Lundh436c3d582000-06-29 08:58:44 +000044INFO = "info"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000045JUMP = "jump"
46LITERAL = "literal"
47LITERAL_IGNORE = "literal_ignore"
48MARK = "mark"
49MAX_REPEAT = "max_repeat"
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000050MAX_UNTIL = "max_until"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000051MIN_REPEAT = "min_repeat"
Fredrik Lundh29c4ba92000-08-01 18:20:07 +000052MIN_UNTIL = "min_until"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000053NEGATE = "negate"
54NOT_LITERAL = "not_literal"
55NOT_LITERAL_IGNORE = "not_literal_ignore"
56RANGE = "range"
57REPEAT = "repeat"
Fredrik Lundh436c3d582000-06-29 08:58:44 +000058REPEAT_ONE = "repeat_one"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000059SUBPATTERN = "subpattern"
60
61# positions
62AT_BEGINNING = "at_beginning"
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000063AT_BEGINNING_LINE = "at_beginning_line"
Fredrik Lundh770617b2001-01-14 15:06:11 +000064AT_BEGINNING_STRING = "at_beginning_string"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000065AT_BOUNDARY = "at_boundary"
66AT_NON_BOUNDARY = "at_non_boundary"
67AT_END = "at_end"
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000068AT_END_LINE = "at_end_line"
Fredrik Lundh770617b2001-01-14 15:06:11 +000069AT_END_STRING = "at_end_string"
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +000070AT_LOC_BOUNDARY = "at_loc_boundary"
71AT_LOC_NON_BOUNDARY = "at_loc_non_boundary"
72AT_UNI_BOUNDARY = "at_uni_boundary"
73AT_UNI_NON_BOUNDARY = "at_uni_non_boundary"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000074
75# categories
Guido van Rossum7627c0d2000-03-31 14:58:54 +000076CATEGORY_DIGIT = "category_digit"
77CATEGORY_NOT_DIGIT = "category_not_digit"
78CATEGORY_SPACE = "category_space"
79CATEGORY_NOT_SPACE = "category_not_space"
80CATEGORY_WORD = "category_word"
81CATEGORY_NOT_WORD = "category_not_word"
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000082CATEGORY_LINEBREAK = "category_linebreak"
83CATEGORY_NOT_LINEBREAK = "category_not_linebreak"
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000084CATEGORY_LOC_WORD = "category_loc_word"
85CATEGORY_LOC_NOT_WORD = "category_loc_not_word"
Fredrik Lundh436c3d582000-06-29 08:58:44 +000086CATEGORY_UNI_DIGIT = "category_uni_digit"
87CATEGORY_UNI_NOT_DIGIT = "category_uni_not_digit"
88CATEGORY_UNI_SPACE = "category_uni_space"
89CATEGORY_UNI_NOT_SPACE = "category_uni_not_space"
90CATEGORY_UNI_WORD = "category_uni_word"
91CATEGORY_UNI_NOT_WORD = "category_uni_not_word"
92CATEGORY_UNI_LINEBREAK = "category_uni_linebreak"
93CATEGORY_UNI_NOT_LINEBREAK = "category_uni_not_linebreak"
Guido van Rossum7627c0d2000-03-31 14:58:54 +000094
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +000095OPCODES = [
Guido van Rossum7627c0d2000-03-31 14:58:54 +000096
97 # failure=0 success=1 (just because it looks better that way :-)
98 FAILURE, SUCCESS,
99
Fredrik Lundhe1869832000-08-01 22:47:49 +0000100 ANY, ANY_ALL,
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000101 ASSERT, ASSERT_NOT,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000102 AT,
103 BRANCH,
104 CALL,
105 CATEGORY,
Fredrik Lundh3562f112000-07-02 12:00:07 +0000106 CHARSET,
Fredrik Lundh72b82ba2000-07-03 21:31:48 +0000107 GROUPREF, GROUPREF_IGNORE,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000108 IN, IN_IGNORE,
Fredrik Lundh436c3d582000-06-29 08:58:44 +0000109 INFO,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000110 JUMP,
111 LITERAL, LITERAL_IGNORE,
112 MARK,
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000113 MAX_UNTIL,
114 MIN_UNTIL,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000115 NOT_LITERAL, NOT_LITERAL_IGNORE,
116 NEGATE,
117 RANGE,
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000118 REPEAT,
119 REPEAT_ONE,
120 SUBPATTERN
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000121
122]
123
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000124ATCODES = [
Fredrik Lundh770617b2001-01-14 15:06:11 +0000125 AT_BEGINNING, AT_BEGINNING_LINE, AT_BEGINNING_STRING, AT_BOUNDARY,
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000126 AT_NON_BOUNDARY, AT_END, AT_END_LINE, AT_END_STRING,
127 AT_LOC_BOUNDARY, AT_LOC_NON_BOUNDARY, AT_UNI_BOUNDARY,
128 AT_UNI_NON_BOUNDARY
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000129]
130
131CHCODES = [
132 CATEGORY_DIGIT, CATEGORY_NOT_DIGIT, CATEGORY_SPACE,
133 CATEGORY_NOT_SPACE, CATEGORY_WORD, CATEGORY_NOT_WORD,
Fredrik Lundh436c3d582000-06-29 08:58:44 +0000134 CATEGORY_LINEBREAK, CATEGORY_NOT_LINEBREAK, CATEGORY_LOC_WORD,
135 CATEGORY_LOC_NOT_WORD, CATEGORY_UNI_DIGIT, CATEGORY_UNI_NOT_DIGIT,
136 CATEGORY_UNI_SPACE, CATEGORY_UNI_NOT_SPACE, CATEGORY_UNI_WORD,
137 CATEGORY_UNI_NOT_WORD, CATEGORY_UNI_LINEBREAK,
138 CATEGORY_UNI_NOT_LINEBREAK
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000139]
140
141def makedict(list):
142 d = {}
143 i = 0
144 for item in list:
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000145 d[item] = i
146 i = i + 1
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000147 return d
148
149OPCODES = makedict(OPCODES)
150ATCODES = makedict(ATCODES)
151CHCODES = makedict(CHCODES)
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000152
153# replacement operations for "ignore case" mode
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000154OP_IGNORE = {
Fredrik Lundh72b82ba2000-07-03 21:31:48 +0000155 GROUPREF: GROUPREF_IGNORE,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000156 IN: IN_IGNORE,
157 LITERAL: LITERAL_IGNORE,
158 NOT_LITERAL: NOT_LITERAL_IGNORE
159}
160
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000161AT_MULTILINE = {
162 AT_BEGINNING: AT_BEGINNING_LINE,
163 AT_END: AT_END_LINE
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000164}
165
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000166AT_LOCALE = {
167 AT_BOUNDARY: AT_LOC_BOUNDARY,
168 AT_NON_BOUNDARY: AT_LOC_NON_BOUNDARY
169}
170
171AT_UNICODE = {
172 AT_BOUNDARY: AT_UNI_BOUNDARY,
173 AT_NON_BOUNDARY: AT_UNI_NON_BOUNDARY
174}
175
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000176CH_LOCALE = {
Fredrik Lundh436c3d582000-06-29 08:58:44 +0000177 CATEGORY_DIGIT: CATEGORY_DIGIT,
178 CATEGORY_NOT_DIGIT: CATEGORY_NOT_DIGIT,
179 CATEGORY_SPACE: CATEGORY_SPACE,
180 CATEGORY_NOT_SPACE: CATEGORY_NOT_SPACE,
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000181 CATEGORY_WORD: CATEGORY_LOC_WORD,
182 CATEGORY_NOT_WORD: CATEGORY_LOC_NOT_WORD,
Fredrik Lundh436c3d582000-06-29 08:58:44 +0000183 CATEGORY_LINEBREAK: CATEGORY_LINEBREAK,
184 CATEGORY_NOT_LINEBREAK: CATEGORY_NOT_LINEBREAK
185}
186
187CH_UNICODE = {
188 CATEGORY_DIGIT: CATEGORY_UNI_DIGIT,
189 CATEGORY_NOT_DIGIT: CATEGORY_UNI_NOT_DIGIT,
190 CATEGORY_SPACE: CATEGORY_UNI_SPACE,
191 CATEGORY_NOT_SPACE: CATEGORY_UNI_NOT_SPACE,
192 CATEGORY_WORD: CATEGORY_UNI_WORD,
193 CATEGORY_NOT_WORD: CATEGORY_UNI_NOT_WORD,
194 CATEGORY_LINEBREAK: CATEGORY_UNI_LINEBREAK,
195 CATEGORY_NOT_LINEBREAK: CATEGORY_UNI_NOT_LINEBREAK
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000196}
197
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000198# flags
Fredrik Lundh3562f112000-07-02 12:00:07 +0000199SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking)
200SRE_FLAG_IGNORECASE = 2 # case insensitive
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000201SRE_FLAG_LOCALE = 4 # honour system locale
Fredrik Lundh3562f112000-07-02 12:00:07 +0000202SRE_FLAG_MULTILINE = 8 # treat target as multiline string
203SRE_FLAG_DOTALL = 16 # treat target as a single string
204SRE_FLAG_UNICODE = 32 # use unicode locale
205SRE_FLAG_VERBOSE = 64 # ignore whitespace and comments
Fredrik Lundh770617b2001-01-14 15:06:11 +0000206SRE_FLAG_DEBUG = 128 # debugging
Fredrik Lundh3562f112000-07-02 12:00:07 +0000207
208# flags for INFO primitive
209SRE_INFO_PREFIX = 1 # has prefix
210SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix)
211SRE_INFO_CHARSET = 4 # pattern starts with character from given set
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000212
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000213if __name__ == "__main__":
Fredrik Lundhf2989b22001-02-18 12:05:16 +0000214 import string
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000215 def dump(f, d, prefix):
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000216 items = d.items()
217 items.sort(lambda a, b: cmp(a[1], b[1]))
218 for k, v in items:
Fredrik Lundhf2989b22001-02-18 12:05:16 +0000219 f.write("#define %s_%s %s\n" % (prefix, string.upper(k), v))
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000220 f = open("sre_constants.h", "w")
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000221 f.write("""\
222/*
223 * Secret Labs' Regular Expression Engine
224 *
225 * regular expression matching engine
226 *
227 * NOTE: This file is generated by sre_constants.py. If you need
228 * to change anything in here, edit sre_constants.py and run it.
229 *
Fredrik Lundh770617b2001-01-14 15:06:11 +0000230 * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000231 *
232 * See the _sre.c file for information on usage and redistribution.
233 */
234
235""")
236
Fredrik Lundhb35ffc02001-01-15 12:46:09 +0000237 f.write("#define SRE_MAGIC %d\n" % MAGIC)
238
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000239 dump(f, OPCODES, "SRE_OP")
240 dump(f, ATCODES, "SRE")
241 dump(f, CHCODES, "SRE")
Fredrik Lundh3562f112000-07-02 12:00:07 +0000242
Fredrik Lundh436c3d582000-06-29 08:58:44 +0000243 f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE)
244 f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE)
245 f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE)
246 f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE)
247 f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL)
248 f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE)
249 f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE)
Fredrik Lundh3562f112000-07-02 12:00:07 +0000250
251 f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX)
252 f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL)
253 f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET)
254
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000255 f.close()
256 print "done"