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