blob: ea649c048293dfc32949387693120911732c7655 [file] [log] [blame]
Guido van Rossumaad67612000-05-08 17:31:04 +00001#
2# Secret Labs' Regular Expression Engine
Guido van Rossumaad67612000-05-08 17:31:04 +00003#
4# various symbols used by the regular expression engine.
5# run this script to update the _sre include files!
6#
7# Copyright (c) 1998-2000 by Secret Labs AB. All rights reserved.
8#
Guido van Rossum8d691c82000-09-01 19:25:51 +00009# See the sre.py file for information on usage and redistribution.
Guido van Rossumaad67612000-05-08 17:31:04 +000010#
11
Guido van Rossume8d2f552000-10-09 22:14:43 +000012MAXREPEAT = 65535
13
Guido van Rossum3e06ab12000-06-29 19:35:29 +000014# should this really be here?
15
16class error(Exception):
17 pass
18
Guido van Rossumaad67612000-05-08 17:31:04 +000019# operators
20
21FAILURE = "failure"
22SUCCESS = "success"
23
24ANY = "any"
Guido van Rossum8d691c82000-09-01 19:25:51 +000025ANY_ALL = "any_all"
Guido van Rossumaad67612000-05-08 17:31:04 +000026ASSERT = "assert"
Guido van Rossum4358b2c2000-06-30 16:13:37 +000027ASSERT_NOT = "assert_not"
Guido van Rossumaad67612000-05-08 17:31:04 +000028AT = "at"
29BRANCH = "branch"
30CALL = "call"
31CATEGORY = "category"
Guido van Rossum8d691c82000-09-01 19:25:51 +000032CHARSET = "charset"
33GROUPREF = "groupref"
34GROUPREF_IGNORE = "groupref_ignore"
Guido van Rossumaad67612000-05-08 17:31:04 +000035IN = "in"
36IN_IGNORE = "in_ignore"
Guido van Rossum3e06ab12000-06-29 19:35:29 +000037INFO = "info"
Guido van Rossumaad67612000-05-08 17:31:04 +000038JUMP = "jump"
39LITERAL = "literal"
40LITERAL_IGNORE = "literal_ignore"
41MARK = "mark"
42MAX_REPEAT = "max_repeat"
Guido van Rossum8d691c82000-09-01 19:25:51 +000043MAX_UNTIL = "max_until"
Guido van Rossumaad67612000-05-08 17:31:04 +000044MIN_REPEAT = "min_repeat"
Guido van Rossum8d691c82000-09-01 19:25:51 +000045MIN_UNTIL = "min_until"
Guido van Rossumaad67612000-05-08 17:31:04 +000046NEGATE = "negate"
47NOT_LITERAL = "not_literal"
48NOT_LITERAL_IGNORE = "not_literal_ignore"
49RANGE = "range"
50REPEAT = "repeat"
Guido van Rossum3e06ab12000-06-29 19:35:29 +000051REPEAT_ONE = "repeat_one"
Guido van Rossumaad67612000-05-08 17:31:04 +000052SUBPATTERN = "subpattern"
53
54# positions
55AT_BEGINNING = "at_beginning"
Guido van Rossum3e06ab12000-06-29 19:35:29 +000056AT_BEGINNING_LINE = "at_beginning_line"
Guido van Rossumaad67612000-05-08 17:31:04 +000057AT_BOUNDARY = "at_boundary"
58AT_NON_BOUNDARY = "at_non_boundary"
59AT_END = "at_end"
Guido van Rossum3e06ab12000-06-29 19:35:29 +000060AT_END_LINE = "at_end_line"
Guido van Rossumaad67612000-05-08 17:31:04 +000061
62# categories
Guido van Rossumaad67612000-05-08 17:31:04 +000063CATEGORY_DIGIT = "category_digit"
64CATEGORY_NOT_DIGIT = "category_not_digit"
65CATEGORY_SPACE = "category_space"
66CATEGORY_NOT_SPACE = "category_not_space"
67CATEGORY_WORD = "category_word"
68CATEGORY_NOT_WORD = "category_not_word"
Guido van Rossum3e06ab12000-06-29 19:35:29 +000069CATEGORY_LINEBREAK = "category_linebreak"
70CATEGORY_NOT_LINEBREAK = "category_not_linebreak"
71CATEGORY_LOC_WORD = "category_loc_word"
72CATEGORY_LOC_NOT_WORD = "category_loc_not_word"
73CATEGORY_UNI_DIGIT = "category_uni_digit"
74CATEGORY_UNI_NOT_DIGIT = "category_uni_not_digit"
75CATEGORY_UNI_SPACE = "category_uni_space"
76CATEGORY_UNI_NOT_SPACE = "category_uni_not_space"
77CATEGORY_UNI_WORD = "category_uni_word"
78CATEGORY_UNI_NOT_WORD = "category_uni_not_word"
79CATEGORY_UNI_LINEBREAK = "category_uni_linebreak"
80CATEGORY_UNI_NOT_LINEBREAK = "category_uni_not_linebreak"
Guido van Rossumaad67612000-05-08 17:31:04 +000081
Guido van Rossum3e06ab12000-06-29 19:35:29 +000082OPCODES = [
Guido van Rossumaad67612000-05-08 17:31:04 +000083
84 # failure=0 success=1 (just because it looks better that way :-)
85 FAILURE, SUCCESS,
86
Guido van Rossum8d691c82000-09-01 19:25:51 +000087 ANY, ANY_ALL,
Guido van Rossum4358b2c2000-06-30 16:13:37 +000088 ASSERT, ASSERT_NOT,
Guido van Rossumaad67612000-05-08 17:31:04 +000089 AT,
90 BRANCH,
91 CALL,
92 CATEGORY,
Guido van Rossum8d691c82000-09-01 19:25:51 +000093 CHARSET,
94 GROUPREF, GROUPREF_IGNORE,
Guido van Rossumaad67612000-05-08 17:31:04 +000095 IN, IN_IGNORE,
Guido van Rossum3e06ab12000-06-29 19:35:29 +000096 INFO,
Guido van Rossumaad67612000-05-08 17:31:04 +000097 JUMP,
98 LITERAL, LITERAL_IGNORE,
99 MARK,
Guido van Rossum8d691c82000-09-01 19:25:51 +0000100 MAX_UNTIL,
101 MIN_UNTIL,
Guido van Rossumaad67612000-05-08 17:31:04 +0000102 NOT_LITERAL, NOT_LITERAL_IGNORE,
103 NEGATE,
104 RANGE,
Guido van Rossum8d691c82000-09-01 19:25:51 +0000105 REPEAT,
106 REPEAT_ONE,
107 SUBPATTERN
Guido van Rossumaad67612000-05-08 17:31:04 +0000108
109]
110
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000111ATCODES = [
112 AT_BEGINNING, AT_BEGINNING_LINE, AT_BOUNDARY,
113 AT_NON_BOUNDARY, AT_END, AT_END_LINE
114]
115
116CHCODES = [
117 CATEGORY_DIGIT, CATEGORY_NOT_DIGIT, CATEGORY_SPACE,
118 CATEGORY_NOT_SPACE, CATEGORY_WORD, CATEGORY_NOT_WORD,
119 CATEGORY_LINEBREAK, CATEGORY_NOT_LINEBREAK, CATEGORY_LOC_WORD,
120 CATEGORY_LOC_NOT_WORD, CATEGORY_UNI_DIGIT, CATEGORY_UNI_NOT_DIGIT,
121 CATEGORY_UNI_SPACE, CATEGORY_UNI_NOT_SPACE, CATEGORY_UNI_WORD,
122 CATEGORY_UNI_NOT_WORD, CATEGORY_UNI_LINEBREAK,
123 CATEGORY_UNI_NOT_LINEBREAK
124]
125
126def makedict(list):
127 d = {}
128 i = 0
129 for item in list:
Guido van Rossum4358b2c2000-06-30 16:13:37 +0000130 d[item] = i
131 i = i + 1
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000132 return d
133
134OPCODES = makedict(OPCODES)
135ATCODES = makedict(ATCODES)
136CHCODES = makedict(CHCODES)
Guido van Rossumaad67612000-05-08 17:31:04 +0000137
138# replacement operations for "ignore case" mode
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000139OP_IGNORE = {
Guido van Rossum8d691c82000-09-01 19:25:51 +0000140 GROUPREF: GROUPREF_IGNORE,
Guido van Rossumaad67612000-05-08 17:31:04 +0000141 IN: IN_IGNORE,
142 LITERAL: LITERAL_IGNORE,
143 NOT_LITERAL: NOT_LITERAL_IGNORE
144}
145
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000146AT_MULTILINE = {
147 AT_BEGINNING: AT_BEGINNING_LINE,
148 AT_END: AT_END_LINE
Guido van Rossumaad67612000-05-08 17:31:04 +0000149}
150
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000151CH_LOCALE = {
152 CATEGORY_DIGIT: CATEGORY_DIGIT,
153 CATEGORY_NOT_DIGIT: CATEGORY_NOT_DIGIT,
154 CATEGORY_SPACE: CATEGORY_SPACE,
155 CATEGORY_NOT_SPACE: CATEGORY_NOT_SPACE,
156 CATEGORY_WORD: CATEGORY_LOC_WORD,
157 CATEGORY_NOT_WORD: CATEGORY_LOC_NOT_WORD,
158 CATEGORY_LINEBREAK: CATEGORY_LINEBREAK,
159 CATEGORY_NOT_LINEBREAK: CATEGORY_NOT_LINEBREAK
Guido van Rossumaad67612000-05-08 17:31:04 +0000160}
161
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000162CH_UNICODE = {
163 CATEGORY_DIGIT: CATEGORY_UNI_DIGIT,
164 CATEGORY_NOT_DIGIT: CATEGORY_UNI_NOT_DIGIT,
165 CATEGORY_SPACE: CATEGORY_UNI_SPACE,
166 CATEGORY_NOT_SPACE: CATEGORY_UNI_NOT_SPACE,
167 CATEGORY_WORD: CATEGORY_UNI_WORD,
168 CATEGORY_NOT_WORD: CATEGORY_UNI_NOT_WORD,
169 CATEGORY_LINEBREAK: CATEGORY_UNI_LINEBREAK,
170 CATEGORY_NOT_LINEBREAK: CATEGORY_UNI_NOT_LINEBREAK
171}
172
173# flags
Guido van Rossum8d691c82000-09-01 19:25:51 +0000174SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking)
175SRE_FLAG_IGNORECASE = 2 # case insensitive
176SRE_FLAG_LOCALE = 4 # honour system locale
177SRE_FLAG_MULTILINE = 8 # treat target as multiline string
178SRE_FLAG_DOTALL = 16 # treat target as a single string
179SRE_FLAG_UNICODE = 32 # use unicode locale
180SRE_FLAG_VERBOSE = 64 # ignore whitespace and comments
181
182# flags for INFO primitive
183SRE_INFO_PREFIX = 1 # has prefix
184SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix)
185SRE_INFO_CHARSET = 4 # pattern starts with character from given set
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000186
Guido van Rossumaad67612000-05-08 17:31:04 +0000187if __name__ == "__main__":
188 import string
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000189 def dump(f, d, prefix):
Guido van Rossum4358b2c2000-06-30 16:13:37 +0000190 items = d.items()
191 items.sort(lambda a, b: cmp(a[1], b[1]))
192 for k, v in items:
193 f.write("#define %s_%s %s\n" % (prefix, string.upper(k), v))
Guido van Rossumaad67612000-05-08 17:31:04 +0000194 f = open("sre_constants.h", "w")
Guido van Rossum4358b2c2000-06-30 16:13:37 +0000195 f.write("""\
196/*
197 * Secret Labs' Regular Expression Engine
198 *
199 * regular expression matching engine
200 *
201 * NOTE: This file is generated by sre_constants.py. If you need
202 * to change anything in here, edit sre_constants.py and run it.
203 *
204 * Copyright (c) 1997-2000 by Secret Labs AB. All rights reserved.
205 *
206 * See the _sre.c file for information on usage and redistribution.
207 */
208
209""")
210
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000211 dump(f, OPCODES, "SRE_OP")
212 dump(f, ATCODES, "SRE")
213 dump(f, CHCODES, "SRE")
Guido van Rossum8d691c82000-09-01 19:25:51 +0000214
Guido van Rossum3e06ab12000-06-29 19:35:29 +0000215 f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE)
216 f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE)
217 f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE)
218 f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE)
219 f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL)
220 f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE)
221 f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE)
Guido van Rossum8d691c82000-09-01 19:25:51 +0000222
223 f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX)
224 f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL)
225 f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET)
226
Guido van Rossumaad67612000-05-08 17:31:04 +0000227 f.close()
228 print "done"