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