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