1# 2# Secret Labs' Regular Expression Engine 3# 4# various symbols used by the regular expression engine. 5# run this script to update the _sre include files! 6# 7# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved. 8# 9# See the sre.py file for information on usage and redistribution. 10# 11 12"""Internal support module for sre""" 13 14# update when constants are added or removed 15 16MAGIC = 20171005 17 18from _sre import MAXREPEAT, MAXGROUPS 19 20# SRE standard exception (access as sre.error) 21# should this really be here? 22 23class error(Exception): 24 """Exception raised for invalid regular expressions. 25 26 Attributes: 27 28 msg: The unformatted error message 29 pattern: The regular expression pattern 30 pos: The index in the pattern where compilation failed (may be None) 31 lineno: The line corresponding to pos (may be None) 32 colno: The column corresponding to pos (may be None) 33 """ 34 35 __module__ = 're' 36 37 def __init__(self, msg, pattern=None, pos=None): 38 self.msg = msg 39 self.pattern = pattern 40 self.pos = pos 41 if pattern is not None and pos is not None: 42 msg = '%s at position %d' % (msg, pos) 43 if isinstance(pattern, str): 44 newline = '\n' 45 else: 46 newline = b'\n' 47 self.lineno = pattern.count(newline, 0, pos) + 1 48 self.colno = pos - pattern.rfind(newline, 0, pos) 49 if newline in pattern: 50 msg = '%s (line %d, column %d)' % (msg, self.lineno, self.colno) 51 else: 52 self.lineno = self.colno = None 53 super().__init__(msg) 54 55 56class _NamedIntConstant(int): 57 def __new__(cls, value, name): 58 self = super(_NamedIntConstant, cls).__new__(cls, value) 59 self.name = name 60 return self 61 62 def __str__(self): 63 return self.name 64 65 __repr__ = __str__ 66 67MAXREPEAT = _NamedIntConstant(MAXREPEAT, 'MAXREPEAT') 68 69def _makecodes(names): 70 names = names.strip().split() 71 items = [_NamedIntConstant(i, name) for i, name in enumerate(names)] 72 globals().update({item.name: item for item in items}) 73 return items 74 75# operators 76# failure=0 success=1 (just because it looks better that way :-) 77OPCODES = _makecodes(""" 78 FAILURE SUCCESS 79 80 ANY ANY_ALL 81 ASSERT ASSERT_NOT 82 AT 83 BRANCH 84 CALL 85 CATEGORY 86 CHARSET BIGCHARSET 87 GROUPREF GROUPREF_EXISTS 88 IN 89 INFO 90 JUMP 91 LITERAL 92 MARK 93 MAX_UNTIL 94 MIN_UNTIL 95 NOT_LITERAL 96 NEGATE 97 RANGE 98 REPEAT 99 REPEAT_ONE 100 SUBPATTERN 101 MIN_REPEAT_ONE 102 103 GROUPREF_IGNORE 104 IN_IGNORE 105 LITERAL_IGNORE 106 NOT_LITERAL_IGNORE 107 108 GROUPREF_LOC_IGNORE 109 IN_LOC_IGNORE 110 LITERAL_LOC_IGNORE 111 NOT_LITERAL_LOC_IGNORE 112 113 GROUPREF_UNI_IGNORE 114 IN_UNI_IGNORE 115 LITERAL_UNI_IGNORE 116 NOT_LITERAL_UNI_IGNORE 117 RANGE_UNI_IGNORE 118 119 MIN_REPEAT MAX_REPEAT 120""") 121del OPCODES[-2:] # remove MIN_REPEAT and MAX_REPEAT 122 123# positions 124ATCODES = _makecodes(""" 125 AT_BEGINNING AT_BEGINNING_LINE AT_BEGINNING_STRING 126 AT_BOUNDARY AT_NON_BOUNDARY 127 AT_END AT_END_LINE AT_END_STRING 128 129 AT_LOC_BOUNDARY AT_LOC_NON_BOUNDARY 130 131 AT_UNI_BOUNDARY AT_UNI_NON_BOUNDARY 132""") 133 134# categories 135CHCODES = _makecodes(""" 136 CATEGORY_DIGIT CATEGORY_NOT_DIGIT 137 CATEGORY_SPACE CATEGORY_NOT_SPACE 138 CATEGORY_WORD CATEGORY_NOT_WORD 139 CATEGORY_LINEBREAK CATEGORY_NOT_LINEBREAK 140 141 CATEGORY_LOC_WORD CATEGORY_LOC_NOT_WORD 142 143 CATEGORY_UNI_DIGIT CATEGORY_UNI_NOT_DIGIT 144 CATEGORY_UNI_SPACE CATEGORY_UNI_NOT_SPACE 145 CATEGORY_UNI_WORD CATEGORY_UNI_NOT_WORD 146 CATEGORY_UNI_LINEBREAK CATEGORY_UNI_NOT_LINEBREAK 147""") 148 149 150# replacement operations for "ignore case" mode 151OP_IGNORE = { 152 LITERAL: LITERAL_IGNORE, 153 NOT_LITERAL: NOT_LITERAL_IGNORE, 154} 155 156OP_LOCALE_IGNORE = { 157 LITERAL: LITERAL_LOC_IGNORE, 158 NOT_LITERAL: NOT_LITERAL_LOC_IGNORE, 159} 160 161OP_UNICODE_IGNORE = { 162 LITERAL: LITERAL_UNI_IGNORE, 163 NOT_LITERAL: NOT_LITERAL_UNI_IGNORE, 164} 165 166AT_MULTILINE = { 167 AT_BEGINNING: AT_BEGINNING_LINE, 168 AT_END: AT_END_LINE 169} 170 171AT_LOCALE = { 172 AT_BOUNDARY: AT_LOC_BOUNDARY, 173 AT_NON_BOUNDARY: AT_LOC_NON_BOUNDARY 174} 175 176AT_UNICODE = { 177 AT_BOUNDARY: AT_UNI_BOUNDARY, 178 AT_NON_BOUNDARY: AT_UNI_NON_BOUNDARY 179} 180 181CH_LOCALE = { 182 CATEGORY_DIGIT: CATEGORY_DIGIT, 183 CATEGORY_NOT_DIGIT: CATEGORY_NOT_DIGIT, 184 CATEGORY_SPACE: CATEGORY_SPACE, 185 CATEGORY_NOT_SPACE: CATEGORY_NOT_SPACE, 186 CATEGORY_WORD: CATEGORY_LOC_WORD, 187 CATEGORY_NOT_WORD: CATEGORY_LOC_NOT_WORD, 188 CATEGORY_LINEBREAK: CATEGORY_LINEBREAK, 189 CATEGORY_NOT_LINEBREAK: CATEGORY_NOT_LINEBREAK 190} 191 192CH_UNICODE = { 193 CATEGORY_DIGIT: CATEGORY_UNI_DIGIT, 194 CATEGORY_NOT_DIGIT: CATEGORY_UNI_NOT_DIGIT, 195 CATEGORY_SPACE: CATEGORY_UNI_SPACE, 196 CATEGORY_NOT_SPACE: CATEGORY_UNI_NOT_SPACE, 197 CATEGORY_WORD: CATEGORY_UNI_WORD, 198 CATEGORY_NOT_WORD: CATEGORY_UNI_NOT_WORD, 199 CATEGORY_LINEBREAK: CATEGORY_UNI_LINEBREAK, 200 CATEGORY_NOT_LINEBREAK: CATEGORY_UNI_NOT_LINEBREAK 201} 202 203# flags 204SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking) 205SRE_FLAG_IGNORECASE = 2 # case insensitive 206SRE_FLAG_LOCALE = 4 # honour system locale 207SRE_FLAG_MULTILINE = 8 # treat target as multiline string 208SRE_FLAG_DOTALL = 16 # treat target as a single string 209SRE_FLAG_UNICODE = 32 # use unicode "locale" 210SRE_FLAG_VERBOSE = 64 # ignore whitespace and comments 211SRE_FLAG_DEBUG = 128 # debugging 212SRE_FLAG_ASCII = 256 # use ascii "locale" 213 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 218 219if __name__ == "__main__": 220 def dump(f, d, prefix): 221 items = sorted(d) 222 for item in items: 223 f.write("#define %s_%s %d\n" % (prefix, item, item)) 224 with open("sre_constants.h", "w") as f: 225 f.write("""\ 226/* 227 * Secret Labs' Regular Expression Engine 228 * 229 * regular expression matching engine 230 * 231 * NOTE: This file is generated by sre_constants.py. If you need 232 * to change anything in here, edit sre_constants.py and run it. 233 * 234 * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved. 235 * 236 * See the _sre.c file for information on usage and redistribution. 237 */ 238 239""") 240 241 f.write("#define SRE_MAGIC %d\n" % MAGIC) 242 243 dump(f, OPCODES, "SRE_OP") 244 dump(f, ATCODES, "SRE") 245 dump(f, CHCODES, "SRE") 246 247 f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE) 248 f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE) 249 f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE) 250 f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE) 251 f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL) 252 f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE) 253 f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE) 254 f.write("#define SRE_FLAG_DEBUG %d\n" % SRE_FLAG_DEBUG) 255 f.write("#define SRE_FLAG_ASCII %d\n" % SRE_FLAG_ASCII) 256 257 f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX) 258 f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL) 259 f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET) 260 261 print("done") 262