Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 1 | # |
| 2 | # Secret Labs' Regular Expression Engine |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 3 | # |
| 4 | # various symbols used by the regular expression engine. |
| 5 | # run this script to update the _sre include files! |
| 6 | # |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 7 | # Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved. |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 8 | # |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 9 | # See the sre.py file for information on usage and redistribution. |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 10 | # |
| 11 | |
Fred Drake | b8f2274 | 2001-09-04 19:10:20 +0000 | [diff] [blame] | 12 | """Internal support module for sre""" |
| 13 | |
Fredrik Lundh | b35ffc0 | 2001-01-15 12:46:09 +0000 | [diff] [blame] | 14 | # update when constants are added or removed |
| 15 | |
Fredrik Lundh | 19af43d | 2001-07-02 16:58:38 +0000 | [diff] [blame] | 16 | MAGIC = 20010701 |
Fredrik Lundh | b35ffc0 | 2001-01-15 12:46:09 +0000 | [diff] [blame] | 17 | |
| 18 | # max code word in this release |
| 19 | |
Fredrik Lundh | 13ac992 | 2000-10-07 17:38:23 +0000 | [diff] [blame] | 20 | MAXREPEAT = 65535 |
| 21 | |
Fredrik Lundh | b35ffc0 | 2001-01-15 12:46:09 +0000 | [diff] [blame] | 22 | # SRE standard exception (access as sre.error) |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 23 | # should this really be here? |
| 24 | |
| 25 | class error(Exception): |
| 26 | pass |
| 27 | |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 28 | # operators |
| 29 | |
| 30 | FAILURE = "failure" |
| 31 | SUCCESS = "success" |
| 32 | |
| 33 | ANY = "any" |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 34 | ANY_ALL = "any_all" |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 35 | ASSERT = "assert" |
Fredrik Lundh | 43b3b49 | 2000-06-30 10:41:31 +0000 | [diff] [blame] | 36 | ASSERT_NOT = "assert_not" |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 37 | AT = "at" |
Fredrik Lundh | 19af43d | 2001-07-02 16:58:38 +0000 | [diff] [blame] | 38 | BIGCHARSET = "bigcharset" |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 39 | BRANCH = "branch" |
| 40 | CALL = "call" |
| 41 | CATEGORY = "category" |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 42 | CHARSET = "charset" |
Fredrik Lundh | 72b82ba | 2000-07-03 21:31:48 +0000 | [diff] [blame] | 43 | GROUPREF = "groupref" |
| 44 | GROUPREF_IGNORE = "groupref_ignore" |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 45 | IN = "in" |
| 46 | IN_IGNORE = "in_ignore" |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 47 | INFO = "info" |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 48 | JUMP = "jump" |
| 49 | LITERAL = "literal" |
| 50 | LITERAL_IGNORE = "literal_ignore" |
| 51 | MARK = "mark" |
| 52 | MAX_REPEAT = "max_repeat" |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 53 | MAX_UNTIL = "max_until" |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 54 | MIN_REPEAT = "min_repeat" |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 55 | MIN_UNTIL = "min_until" |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 56 | NEGATE = "negate" |
| 57 | NOT_LITERAL = "not_literal" |
| 58 | NOT_LITERAL_IGNORE = "not_literal_ignore" |
| 59 | RANGE = "range" |
| 60 | REPEAT = "repeat" |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 61 | REPEAT_ONE = "repeat_one" |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 62 | SUBPATTERN = "subpattern" |
| 63 | |
| 64 | # positions |
| 65 | AT_BEGINNING = "at_beginning" |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 66 | AT_BEGINNING_LINE = "at_beginning_line" |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 67 | AT_BEGINNING_STRING = "at_beginning_string" |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 68 | AT_BOUNDARY = "at_boundary" |
| 69 | AT_NON_BOUNDARY = "at_non_boundary" |
| 70 | AT_END = "at_end" |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 71 | AT_END_LINE = "at_end_line" |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 72 | AT_END_STRING = "at_end_string" |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 73 | AT_LOC_BOUNDARY = "at_loc_boundary" |
| 74 | AT_LOC_NON_BOUNDARY = "at_loc_non_boundary" |
| 75 | AT_UNI_BOUNDARY = "at_uni_boundary" |
| 76 | AT_UNI_NON_BOUNDARY = "at_uni_non_boundary" |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 77 | |
| 78 | # categories |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 79 | CATEGORY_DIGIT = "category_digit" |
| 80 | CATEGORY_NOT_DIGIT = "category_not_digit" |
| 81 | CATEGORY_SPACE = "category_space" |
| 82 | CATEGORY_NOT_SPACE = "category_not_space" |
| 83 | CATEGORY_WORD = "category_word" |
| 84 | CATEGORY_NOT_WORD = "category_not_word" |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 85 | CATEGORY_LINEBREAK = "category_linebreak" |
| 86 | CATEGORY_NOT_LINEBREAK = "category_not_linebreak" |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 87 | CATEGORY_LOC_WORD = "category_loc_word" |
| 88 | CATEGORY_LOC_NOT_WORD = "category_loc_not_word" |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 89 | CATEGORY_UNI_DIGIT = "category_uni_digit" |
| 90 | CATEGORY_UNI_NOT_DIGIT = "category_uni_not_digit" |
| 91 | CATEGORY_UNI_SPACE = "category_uni_space" |
| 92 | CATEGORY_UNI_NOT_SPACE = "category_uni_not_space" |
| 93 | CATEGORY_UNI_WORD = "category_uni_word" |
| 94 | CATEGORY_UNI_NOT_WORD = "category_uni_not_word" |
| 95 | CATEGORY_UNI_LINEBREAK = "category_uni_linebreak" |
| 96 | CATEGORY_UNI_NOT_LINEBREAK = "category_uni_not_linebreak" |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 97 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 98 | OPCODES = [ |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 99 | |
| 100 | # failure=0 success=1 (just because it looks better that way :-) |
| 101 | FAILURE, SUCCESS, |
| 102 | |
Fredrik Lundh | e186983 | 2000-08-01 22:47:49 +0000 | [diff] [blame] | 103 | ANY, ANY_ALL, |
Fredrik Lundh | 43b3b49 | 2000-06-30 10:41:31 +0000 | [diff] [blame] | 104 | ASSERT, ASSERT_NOT, |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 105 | AT, |
| 106 | BRANCH, |
| 107 | CALL, |
| 108 | CATEGORY, |
Fredrik Lundh | 19af43d | 2001-07-02 16:58:38 +0000 | [diff] [blame] | 109 | CHARSET, BIGCHARSET, |
Fredrik Lundh | 72b82ba | 2000-07-03 21:31:48 +0000 | [diff] [blame] | 110 | GROUPREF, GROUPREF_IGNORE, |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 111 | IN, IN_IGNORE, |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 112 | INFO, |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 113 | JUMP, |
| 114 | LITERAL, LITERAL_IGNORE, |
| 115 | MARK, |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 116 | MAX_UNTIL, |
| 117 | MIN_UNTIL, |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 118 | NOT_LITERAL, NOT_LITERAL_IGNORE, |
| 119 | NEGATE, |
| 120 | RANGE, |
Fredrik Lundh | 29c4ba9 | 2000-08-01 18:20:07 +0000 | [diff] [blame] | 121 | REPEAT, |
| 122 | REPEAT_ONE, |
| 123 | SUBPATTERN |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 124 | |
| 125 | ] |
| 126 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 127 | ATCODES = [ |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 128 | AT_BEGINNING, AT_BEGINNING_LINE, AT_BEGINNING_STRING, AT_BOUNDARY, |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 129 | AT_NON_BOUNDARY, AT_END, AT_END_LINE, AT_END_STRING, |
| 130 | AT_LOC_BOUNDARY, AT_LOC_NON_BOUNDARY, AT_UNI_BOUNDARY, |
| 131 | AT_UNI_NON_BOUNDARY |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 132 | ] |
| 133 | |
| 134 | CHCODES = [ |
| 135 | CATEGORY_DIGIT, CATEGORY_NOT_DIGIT, CATEGORY_SPACE, |
| 136 | CATEGORY_NOT_SPACE, CATEGORY_WORD, CATEGORY_NOT_WORD, |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 137 | CATEGORY_LINEBREAK, CATEGORY_NOT_LINEBREAK, CATEGORY_LOC_WORD, |
| 138 | CATEGORY_LOC_NOT_WORD, CATEGORY_UNI_DIGIT, CATEGORY_UNI_NOT_DIGIT, |
| 139 | CATEGORY_UNI_SPACE, CATEGORY_UNI_NOT_SPACE, CATEGORY_UNI_WORD, |
| 140 | CATEGORY_UNI_NOT_WORD, CATEGORY_UNI_LINEBREAK, |
| 141 | CATEGORY_UNI_NOT_LINEBREAK |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 142 | ] |
| 143 | |
| 144 | def makedict(list): |
| 145 | d = {} |
| 146 | i = 0 |
| 147 | for item in list: |
Fredrik Lundh | 43b3b49 | 2000-06-30 10:41:31 +0000 | [diff] [blame] | 148 | d[item] = i |
| 149 | i = i + 1 |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 150 | return d |
| 151 | |
| 152 | OPCODES = makedict(OPCODES) |
| 153 | ATCODES = makedict(ATCODES) |
| 154 | CHCODES = makedict(CHCODES) |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 155 | |
| 156 | # replacement operations for "ignore case" mode |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 157 | OP_IGNORE = { |
Fredrik Lundh | 72b82ba | 2000-07-03 21:31:48 +0000 | [diff] [blame] | 158 | GROUPREF: GROUPREF_IGNORE, |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 159 | IN: IN_IGNORE, |
| 160 | LITERAL: LITERAL_IGNORE, |
| 161 | NOT_LITERAL: NOT_LITERAL_IGNORE |
| 162 | } |
| 163 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 164 | AT_MULTILINE = { |
| 165 | AT_BEGINNING: AT_BEGINNING_LINE, |
| 166 | AT_END: AT_END_LINE |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Fredrik Lundh | b25e1ad | 2001-03-22 15:50:10 +0000 | [diff] [blame] | 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 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 179 | CH_LOCALE = { |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 180 | CATEGORY_DIGIT: CATEGORY_DIGIT, |
| 181 | CATEGORY_NOT_DIGIT: CATEGORY_NOT_DIGIT, |
| 182 | CATEGORY_SPACE: CATEGORY_SPACE, |
| 183 | CATEGORY_NOT_SPACE: CATEGORY_NOT_SPACE, |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 184 | CATEGORY_WORD: CATEGORY_LOC_WORD, |
| 185 | CATEGORY_NOT_WORD: CATEGORY_LOC_NOT_WORD, |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 201 | # flags |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 202 | SRE_FLAG_TEMPLATE = 1 # template mode (disable backtracking) |
| 203 | SRE_FLAG_IGNORECASE = 2 # case insensitive |
Fredrik Lundh | 8a3ebf8 | 2000-07-23 21:46:17 +0000 | [diff] [blame] | 204 | SRE_FLAG_LOCALE = 4 # honour system locale |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 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 |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 209 | SRE_FLAG_DEBUG = 128 # debugging |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 210 | |
| 211 | # flags for INFO primitive |
| 212 | SRE_INFO_PREFIX = 1 # has prefix |
| 213 | SRE_INFO_LITERAL = 2 # entire pattern is literal (given by prefix) |
| 214 | SRE_INFO_CHARSET = 4 # pattern starts with character from given set |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 215 | |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 216 | if __name__ == "__main__": |
Fredrik Lundh | f2989b2 | 2001-02-18 12:05:16 +0000 | [diff] [blame] | 217 | import string |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 218 | def dump(f, d, prefix): |
Fredrik Lundh | 43b3b49 | 2000-06-30 10:41:31 +0000 | [diff] [blame] | 219 | items = d.items() |
| 220 | items.sort(lambda a, b: cmp(a[1], b[1])) |
| 221 | for k, v in items: |
Fredrik Lundh | f2989b2 | 2001-02-18 12:05:16 +0000 | [diff] [blame] | 222 | f.write("#define %s_%s %s\n" % (prefix, string.upper(k), v)) |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 223 | f = open("sre_constants.h", "w") |
Fredrik Lundh | 43b3b49 | 2000-06-30 10:41:31 +0000 | [diff] [blame] | 224 | f.write("""\ |
| 225 | /* |
| 226 | * Secret Labs' Regular Expression Engine |
| 227 | * |
| 228 | * regular expression matching engine |
| 229 | * |
| 230 | * NOTE: This file is generated by sre_constants.py. If you need |
| 231 | * to change anything in here, edit sre_constants.py and run it. |
| 232 | * |
Fredrik Lundh | 770617b | 2001-01-14 15:06:11 +0000 | [diff] [blame] | 233 | * Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved. |
Fredrik Lundh | 43b3b49 | 2000-06-30 10:41:31 +0000 | [diff] [blame] | 234 | * |
| 235 | * See the _sre.c file for information on usage and redistribution. |
| 236 | */ |
| 237 | |
| 238 | """) |
| 239 | |
Fredrik Lundh | b35ffc0 | 2001-01-15 12:46:09 +0000 | [diff] [blame] | 240 | f.write("#define SRE_MAGIC %d\n" % MAGIC) |
| 241 | |
Jeremy Hylton | b1aa195 | 2000-06-01 17:39:12 +0000 | [diff] [blame] | 242 | dump(f, OPCODES, "SRE_OP") |
| 243 | dump(f, ATCODES, "SRE") |
| 244 | dump(f, CHCODES, "SRE") |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 245 | |
Fredrik Lundh | 436c3d5 | 2000-06-29 08:58:44 +0000 | [diff] [blame] | 246 | f.write("#define SRE_FLAG_TEMPLATE %d\n" % SRE_FLAG_TEMPLATE) |
| 247 | f.write("#define SRE_FLAG_IGNORECASE %d\n" % SRE_FLAG_IGNORECASE) |
| 248 | f.write("#define SRE_FLAG_LOCALE %d\n" % SRE_FLAG_LOCALE) |
| 249 | f.write("#define SRE_FLAG_MULTILINE %d\n" % SRE_FLAG_MULTILINE) |
| 250 | f.write("#define SRE_FLAG_DOTALL %d\n" % SRE_FLAG_DOTALL) |
| 251 | f.write("#define SRE_FLAG_UNICODE %d\n" % SRE_FLAG_UNICODE) |
| 252 | f.write("#define SRE_FLAG_VERBOSE %d\n" % SRE_FLAG_VERBOSE) |
Fredrik Lundh | 3562f11 | 2000-07-02 12:00:07 +0000 | [diff] [blame] | 253 | |
| 254 | f.write("#define SRE_INFO_PREFIX %d\n" % SRE_INFO_PREFIX) |
| 255 | f.write("#define SRE_INFO_LITERAL %d\n" % SRE_INFO_LITERAL) |
| 256 | f.write("#define SRE_INFO_CHARSET %d\n" % SRE_INFO_CHARSET) |
| 257 | |
Guido van Rossum | 7627c0d | 2000-03-31 14:58:54 +0000 | [diff] [blame] | 258 | f.close() |
| 259 | print "done" |