Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 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 | |
| 16 | MAGIC = 20171005 |
| 17 | |
| 18 | from _sre import MAXREPEAT, MAXGROUPS |
| 19 | |
| 20 | # SRE standard exception (access as sre.error) |
| 21 | # should this really be here? |
| 22 | |
| 23 | class 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 | |
| 56 | class _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 __repr__(self): |
| 63 | return self.name |
| 64 | |
| 65 | MAXREPEAT = _NamedIntConstant(MAXREPEAT, 'MAXREPEAT') |
| 66 | |
| 67 | def _makecodes(names): |
| 68 | names = names.strip().split() |
| 69 | items = [_NamedIntConstant(i, name) for i, name in enumerate(names)] |
| 70 | globals().update({item.name: item for item in items}) |
| 71 | return items |
| 72 | |
| 73 | # operators |
| 74 | # failure=0 success=1 (just because it looks better that way :-) |
| 75 | OPCODES = _makecodes(""" |
| 76 | FAILURE SUCCESS |
| 77 | |
| 78 | ANY ANY_ALL |
| 79 | ASSERT ASSERT_NOT |
| 80 | AT |
| 81 | BRANCH |
| 82 | CALL |
| 83 | CATEGORY |
| 84 | CHARSET BIGCHARSET |
| 85 | GROUPREF GROUPREF_EXISTS |
| 86 | IN |
| 87 | INFO |
| 88 | JUMP |
| 89 | LITERAL |
| 90 | MARK |
| 91 | MAX_UNTIL |
| 92 | MIN_UNTIL |
| 93 | NOT_LITERAL |
| 94 | NEGATE |
| 95 | RANGE |
| 96 | REPEAT |
| 97 | REPEAT_ONE |
| 98 | SUBPATTERN |
| 99 | MIN_REPEAT_ONE |
| 100 | |
| 101 | GROUPREF_IGNORE |
| 102 | IN_IGNORE |
| 103 | LITERAL_IGNORE |
| 104 | NOT_LITERAL_IGNORE |
| 105 | |
| 106 | GROUPREF_LOC_IGNORE |
| 107 | IN_LOC_IGNORE |
| 108 | LITERAL_LOC_IGNORE |
| 109 | NOT_LITERAL_LOC_IGNORE |
| 110 | |
| 111 | GROUPREF_UNI_IGNORE |
| 112 | IN_UNI_IGNORE |
| 113 | LITERAL_UNI_IGNORE |
| 114 | NOT_LITERAL_UNI_IGNORE |
| 115 | RANGE_UNI_IGNORE |
| 116 | |
| 117 | MIN_REPEAT MAX_REPEAT |
| 118 | """) |
| 119 | del OPCODES[-2:] # remove MIN_REPEAT and MAX_REPEAT |
| 120 | |
| 121 | # positions |
| 122 | ATCODES = _makecodes(""" |
| 123 | AT_BEGINNING AT_BEGINNING_LINE AT_BEGINNING_STRING |
| 124 | AT_BOUNDARY AT_NON_BOUNDARY |
| 125 | AT_END AT_END_LINE AT_END_STRING |
| 126 | |
| 127 | AT_LOC_BOUNDARY AT_LOC_NON_BOUNDARY |
| 128 | |
| 129 | AT_UNI_BOUNDARY AT_UNI_NON_BOUNDARY |
| 130 | """) |
| 131 | |
| 132 | # categories |
| 133 | CHCODES = _makecodes(""" |
| 134 | CATEGORY_DIGIT CATEGORY_NOT_DIGIT |
| 135 | CATEGORY_SPACE CATEGORY_NOT_SPACE |
| 136 | CATEGORY_WORD CATEGORY_NOT_WORD |
| 137 | CATEGORY_LINEBREAK CATEGORY_NOT_LINEBREAK |
| 138 | |
| 139 | CATEGORY_LOC_WORD CATEGORY_LOC_NOT_WORD |
| 140 | |
| 141 | CATEGORY_UNI_DIGIT CATEGORY_UNI_NOT_DIGIT |
| 142 | CATEGORY_UNI_SPACE CATEGORY_UNI_NOT_SPACE |
| 143 | CATEGORY_UNI_WORD CATEGORY_UNI_NOT_WORD |
| 144 | CATEGORY_UNI_LINEBREAK CATEGORY_UNI_NOT_LINEBREAK |
| 145 | """) |
| 146 | |
| 147 | |
| 148 | # replacement operations for "ignore case" mode |
| 149 | OP_IGNORE = { |
| 150 | LITERAL: LITERAL_IGNORE, |
| 151 | NOT_LITERAL: NOT_LITERAL_IGNORE, |
| 152 | } |
| 153 | |
| 154 | OP_LOCALE_IGNORE = { |
| 155 | LITERAL: LITERAL_LOC_IGNORE, |
| 156 | NOT_LITERAL: NOT_LITERAL_LOC_IGNORE, |
| 157 | } |
| 158 | |
| 159 | OP_UNICODE_IGNORE = { |
| 160 | LITERAL: LITERAL_UNI_IGNORE, |
| 161 | NOT_LITERAL: NOT_LITERAL_UNI_IGNORE, |
| 162 | } |
| 163 | |
| 164 | AT_MULTILINE = { |
| 165 | AT_BEGINNING: AT_BEGINNING_LINE, |
| 166 | AT_END: AT_END_LINE |
| 167 | } |
| 168 | |
| 169 | AT_LOCALE = { |
| 170 | AT_BOUNDARY: AT_LOC_BOUNDARY, |
| 171 | AT_NON_BOUNDARY: AT_LOC_NON_BOUNDARY |
| 172 | } |
| 173 | |
| 174 | AT_UNICODE = { |
| 175 | AT_BOUNDARY: AT_UNI_BOUNDARY, |
| 176 | AT_NON_BOUNDARY: AT_UNI_NON_BOUNDARY |
| 177 | } |
| 178 | |
| 179 | CH_LOCALE = { |
| 180 | CATEGORY_DIGIT: CATEGORY_DIGIT, |
| 181 | CATEGORY_NOT_DIGIT: CATEGORY_NOT_DIGIT, |
| 182 | CATEGORY_SPACE: CATEGORY_SPACE, |
| 183 | CATEGORY_NOT_SPACE: CATEGORY_NOT_SPACE, |
| 184 | CATEGORY_WORD: CATEGORY_LOC_WORD, |
| 185 | CATEGORY_NOT_WORD: CATEGORY_LOC_NOT_WORD, |
| 186 | CATEGORY_LINEBREAK: CATEGORY_LINEBREAK, |
| 187 | CATEGORY_NOT_LINEBREAK: CATEGORY_NOT_LINEBREAK |
| 188 | } |
| 189 | |
| 190 | CH_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 |
| 199 | } |
| 200 | |
| 201 | # flags |
| 202 | SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking) |
| 203 | SRE_FLAG_IGNORECASE = 2 # case insensitive |
| 204 | SRE_FLAG_LOCALE = 4 # honour system locale |
| 205 | SRE_FLAG_MULTILINE = 8 # treat target as multiline string |
| 206 | SRE_FLAG_DOTALL = 16 # treat target as a single string |
| 207 | SRE_FLAG_UNICODE = 32 # use unicode "locale" |
| 208 | SRE_FLAG_VERBOSE = 64 # ignore whitespace and comments |
| 209 | SRE_FLAG_DEBUG = 128 # debugging |
| 210 | SRE_FLAG_ASCII = 256 # use ascii "locale" |
| 211 | |
| 212 | # flags for INFO primitive |
| 213 | SRE_INFO_PREFIX = 1 # has prefix |
| 214 | SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix) |
| 215 | SRE_INFO_CHARSET = 4 # pattern starts with character from given set |
| 216 | |
| 217 | if __name__ == "__main__": |
| 218 | def dump(f, d, prefix): |
| 219 | items = sorted(d) |
| 220 | for item in items: |
| 221 | f.write("#define %s_%s %d\n" % (prefix, item, item)) |
| 222 | with open("sre_constants.h", "w") as f: |
| 223 | f.write("""\ |
| 224 | /* |
| 225 | * Secret Labs' Regular Expression Engine |
| 226 | * |
| 227 | * regular expression matching engine |
| 228 | * |
| 229 | * NOTE: This file is generated by sre_constants.py. If you need |
| 230 | * to change anything in here, edit sre_constants.py and run it. |
| 231 | * |
| 232 | * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved. |
| 233 | * |
| 234 | * See the _sre.c file for information on usage and redistribution. |
| 235 | */ |
| 236 | |
| 237 | """) |
| 238 | |
| 239 | f.write("#define SRE_MAGIC %d\n" % MAGIC) |
| 240 | |
| 241 | dump(f, OPCODES, "SRE_OP") |
| 242 | dump(f, ATCODES, "SRE") |
| 243 | dump(f, CHCODES, "SRE") |
| 244 | |
| 245 | f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE) |
| 246 | f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE) |
| 247 | f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE) |
| 248 | f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE) |
| 249 | f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL) |
| 250 | f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE) |
| 251 | f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE) |
| 252 | f.write("#define SRE_FLAG_DEBUG %d\n" % SRE_FLAG_DEBUG) |
| 253 | f.write("#define SRE_FLAG_ASCII %d\n" % SRE_FLAG_ASCII) |
| 254 | |
| 255 | f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX) |
| 256 | f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL) |
| 257 | f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET) |
| 258 | |
| 259 | print("done") |