blob: fc684ae96fd30a67ca129628663d769912e0a9d7 [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
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +020016MAGIC = 20140917
Fredrik Lundhb35ffc02001-01-15 12:46:09 +000017
Serhiy Storchaka9baa5b22014-09-29 22:49:23 +030018from _sre import MAXREPEAT, MAXGROUPS
Serhiy Storchaka9acb9bc2013-02-18 11:14:21 +020019
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):
Serhiy Storchakaad446d52014-11-10 13:49:00 +020024 def __init__(self, msg, pattern=None, pos=None):
25 self.msg = msg
26 self.pattern = pattern
27 self.pos = pos
28 if pattern is not None and pos is not None:
29 msg = '%s at position %d' % (msg, pos)
30 if isinstance(pattern, str):
31 newline = '\n'
32 else:
33 newline = b'\n'
34 self.lineno = pattern.count(newline, 0, pos) + 1
35 self.colno = pos - pattern.rfind(newline, 0, pos)
36 if newline in pattern:
37 msg = '%s (line %d, column %d)' % (msg, self.lineno, self.colno)
38 else:
39 self.lineno = self.colno = None
40 super().__init__(msg)
41
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020042
43class _NamedIntConstant(int):
44 def __new__(cls, value, name):
45 self = super(_NamedIntConstant, cls).__new__(cls, value)
46 self.name = name
47 return self
48
49 def __str__(self):
50 return self.name
51
52 __repr__ = __str__
53
54MAXREPEAT = _NamedIntConstant(MAXREPEAT, 'MAXREPEAT')
55
56def _makecodes(names):
57 names = names.strip().split()
58 items = [_NamedIntConstant(i, name) for i, name in enumerate(names)]
59 globals().update({item.name: item for item in items})
60 return items
61
Guido van Rossum7627c0d2000-03-31 14:58:54 +000062# operators
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020063# failure=0 success=1 (just because it looks better that way :-)
64OPCODES = _makecodes("""
65 FAILURE SUCCESS
Guido van Rossum7627c0d2000-03-31 14:58:54 +000066
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020067 ANY ANY_ALL
68 ASSERT ASSERT_NOT
69 AT
70 BRANCH
71 CALL
72 CATEGORY
73 CHARSET BIGCHARSET
74 GROUPREF GROUPREF_EXISTS GROUPREF_IGNORE
75 IN IN_IGNORE
76 INFO
77 JUMP
78 LITERAL LITERAL_IGNORE
79 MARK
80 MAX_UNTIL
81 MIN_UNTIL
82 NOT_LITERAL NOT_LITERAL_IGNORE
83 NEGATE
84 RANGE
85 REPEAT
86 REPEAT_ONE
87 SUBPATTERN
88 MIN_REPEAT_ONE
89 RANGE_IGNORE
Guido van Rossum7627c0d2000-03-31 14:58:54 +000090
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020091 MIN_REPEAT MAX_REPEAT
92""")
93del OPCODES[-2:] # remove MIN_REPEAT and MAX_REPEAT
Guido van Rossum7627c0d2000-03-31 14:58:54 +000094
95# positions
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020096ATCODES = _makecodes("""
97 AT_BEGINNING AT_BEGINNING_LINE AT_BEGINNING_STRING
98 AT_BOUNDARY AT_NON_BOUNDARY
99 AT_END AT_END_LINE AT_END_STRING
100 AT_LOC_BOUNDARY AT_LOC_NON_BOUNDARY
101 AT_UNI_BOUNDARY AT_UNI_NON_BOUNDARY
102""")
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000103
104# categories
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200105CHCODES = _makecodes("""
106 CATEGORY_DIGIT CATEGORY_NOT_DIGIT
107 CATEGORY_SPACE CATEGORY_NOT_SPACE
108 CATEGORY_WORD CATEGORY_NOT_WORD
109 CATEGORY_LINEBREAK CATEGORY_NOT_LINEBREAK
110 CATEGORY_LOC_WORD CATEGORY_LOC_NOT_WORD
111 CATEGORY_UNI_DIGIT CATEGORY_UNI_NOT_DIGIT
112 CATEGORY_UNI_SPACE CATEGORY_UNI_NOT_SPACE
113 CATEGORY_UNI_WORD CATEGORY_UNI_NOT_WORD
114 CATEGORY_UNI_LINEBREAK CATEGORY_UNI_NOT_LINEBREAK
115""")
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000116
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000117
118# replacement operations for "ignore case" mode
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000119OP_IGNORE = {
Fredrik Lundh72b82ba2000-07-03 21:31:48 +0000120 GROUPREF: GROUPREF_IGNORE,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000121 IN: IN_IGNORE,
122 LITERAL: LITERAL_IGNORE,
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200123 NOT_LITERAL: NOT_LITERAL_IGNORE,
124 RANGE: RANGE_IGNORE,
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000125}
126
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000127AT_MULTILINE = {
128 AT_BEGINNING: AT_BEGINNING_LINE,
129 AT_END: AT_END_LINE
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000130}
131
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000132AT_LOCALE = {
133 AT_BOUNDARY: AT_LOC_BOUNDARY,
134 AT_NON_BOUNDARY: AT_LOC_NON_BOUNDARY
135}
136
137AT_UNICODE = {
138 AT_BOUNDARY: AT_UNI_BOUNDARY,
139 AT_NON_BOUNDARY: AT_UNI_NON_BOUNDARY
140}
141
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000142CH_LOCALE = {
Fredrik Lundh436c3d582000-06-29 08:58:44 +0000143 CATEGORY_DIGIT: CATEGORY_DIGIT,
144 CATEGORY_NOT_DIGIT: CATEGORY_NOT_DIGIT,
145 CATEGORY_SPACE: CATEGORY_SPACE,
146 CATEGORY_NOT_SPACE: CATEGORY_NOT_SPACE,
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000147 CATEGORY_WORD: CATEGORY_LOC_WORD,
148 CATEGORY_NOT_WORD: CATEGORY_LOC_NOT_WORD,
Fredrik Lundh436c3d582000-06-29 08:58:44 +0000149 CATEGORY_LINEBREAK: CATEGORY_LINEBREAK,
150 CATEGORY_NOT_LINEBREAK: CATEGORY_NOT_LINEBREAK
151}
152
153CH_UNICODE = {
154 CATEGORY_DIGIT: CATEGORY_UNI_DIGIT,
155 CATEGORY_NOT_DIGIT: CATEGORY_UNI_NOT_DIGIT,
156 CATEGORY_SPACE: CATEGORY_UNI_SPACE,
157 CATEGORY_NOT_SPACE: CATEGORY_UNI_NOT_SPACE,
158 CATEGORY_WORD: CATEGORY_UNI_WORD,
159 CATEGORY_NOT_WORD: CATEGORY_UNI_NOT_WORD,
160 CATEGORY_LINEBREAK: CATEGORY_UNI_LINEBREAK,
161 CATEGORY_NOT_LINEBREAK: CATEGORY_UNI_NOT_LINEBREAK
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000162}
163
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000164# flags
Fredrik Lundh3562f112000-07-02 12:00:07 +0000165SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking)
166SRE_FLAG_IGNORECASE = 2 # case insensitive
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000167SRE_FLAG_LOCALE = 4 # honour system locale
Fredrik Lundh3562f112000-07-02 12:00:07 +0000168SRE_FLAG_MULTILINE = 8 # treat target as multiline string
169SRE_FLAG_DOTALL = 16 # treat target as a single string
Antoine Pitroufd036452008-08-19 17:56:33 +0000170SRE_FLAG_UNICODE = 32 # use unicode "locale"
Fredrik Lundh3562f112000-07-02 12:00:07 +0000171SRE_FLAG_VERBOSE = 64 # ignore whitespace and comments
Fredrik Lundh770617b2001-01-14 15:06:11 +0000172SRE_FLAG_DEBUG = 128 # debugging
Antoine Pitroufd036452008-08-19 17:56:33 +0000173SRE_FLAG_ASCII = 256 # use ascii "locale"
Fredrik Lundh3562f112000-07-02 12:00:07 +0000174
175# flags for INFO primitive
176SRE_INFO_PREFIX = 1 # has prefix
177SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix)
178SRE_INFO_CHARSET = 4 # pattern starts with character from given set
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000179
Guido van Rossum7627c0d2000-03-31 14:58:54 +0000180if __name__ == "__main__":
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000181 def dump(f, d, prefix):
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200182 items = sorted(d)
183 for item in items:
184 f.write("#define %s_%s %d\n" % (prefix, item, item))
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300185 with open("sre_constants.h", "w") as f:
186 f.write("""\
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000187/*
188 * Secret Labs' Regular Expression Engine
189 *
190 * regular expression matching engine
191 *
192 * NOTE: This file is generated by sre_constants.py. If you need
193 * to change anything in here, edit sre_constants.py and run it.
194 *
Fredrik Lundh770617b2001-01-14 15:06:11 +0000195 * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
Fredrik Lundh43b3b492000-06-30 10:41:31 +0000196 *
197 * See the _sre.c file for information on usage and redistribution.
198 */
199
200""")
201
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300202 f.write("#define SRE_MAGIC %d\n" % MAGIC)
Fredrik Lundhb35ffc02001-01-15 12:46:09 +0000203
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300204 dump(f, OPCODES, "SRE_OP")
205 dump(f, ATCODES, "SRE")
206 dump(f, CHCODES, "SRE")
Fredrik Lundh3562f112000-07-02 12:00:07 +0000207
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300208 f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE)
209 f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE)
210 f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE)
211 f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE)
212 f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL)
213 f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE)
214 f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE)
215 f.write("#define SRE_FLAG_DEBUG %d\n" % SRE_FLAG_DEBUG)
216 f.write("#define SRE_FLAG_ASCII %d\n" % SRE_FLAG_ASCII)
Fredrik Lundh3562f112000-07-02 12:00:07 +0000217
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300218 f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX)
219 f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL)
220 f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET)
Fredrik Lundh3562f112000-07-02 12:00:07 +0000221
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000222 print("done")