Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | # |
| 3 | # Tokens (from "token.h") |
| 4 | # |
| 5 | # This file is automatically generated; please don't muck it up! |
| 6 | # |
| 7 | # To update the symbols in this file, 'cd' to the top directory of |
| 8 | # the python source tree after building the interpreter and run: |
| 9 | # |
| 10 | # PYTHONPATH=./Lib ./python Lib/token.py |
| 11 | # |
| 12 | # (this path allows the import of string.py and regexmodule.so |
| 13 | # for a site with no installation in place) |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 15 | #--start constants-- |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 16 | ENDMARKER = 0 |
| 17 | NAME = 1 |
| 18 | NUMBER = 2 |
| 19 | STRING = 3 |
| 20 | NEWLINE = 4 |
| 21 | INDENT = 5 |
| 22 | DEDENT = 6 |
| 23 | LPAR = 7 |
| 24 | RPAR = 8 |
| 25 | LSQB = 9 |
| 26 | RSQB = 10 |
| 27 | COLON = 11 |
| 28 | COMMA = 12 |
| 29 | SEMI = 13 |
| 30 | PLUS = 14 |
| 31 | MINUS = 15 |
| 32 | STAR = 16 |
| 33 | SLASH = 17 |
| 34 | VBAR = 18 |
| 35 | AMPER = 19 |
| 36 | LESS = 20 |
| 37 | GREATER = 21 |
| 38 | EQUAL = 22 |
| 39 | DOT = 23 |
| 40 | PERCENT = 24 |
| 41 | BACKQUOTE = 25 |
| 42 | LBRACE = 26 |
| 43 | RBRACE = 27 |
| 44 | EQEQUAL = 28 |
| 45 | NOTEQUAL = 29 |
| 46 | LESSEQUAL = 30 |
| 47 | GREATEREQUAL = 31 |
| 48 | TILDE = 32 |
| 49 | CIRCUMFLEX = 33 |
| 50 | LEFTSHIFT = 34 |
| 51 | RIGHTSHIFT = 35 |
Guido van Rossum | 154a539 | 1996-07-21 02:17:52 +0000 | [diff] [blame] | 52 | DOUBLESTAR = 36 |
| 53 | OP = 37 |
| 54 | ERRORTOKEN = 38 |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 55 | N_TOKENS = 39 |
| 56 | NT_OFFSET = 256 |
| 57 | #--end constants-- |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 58 | |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 59 | tok_name = {} |
Fred Drake | e3dbc7e | 1997-10-06 21:28:04 +0000 | [diff] [blame] | 60 | for _name, _value in globals().items(): |
| 61 | if type(_value) is type(0): |
| 62 | tok_name[_value] = _name |
| 63 | |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 154a539 | 1996-07-21 02:17:52 +0000 | [diff] [blame] | 65 | def ISTERMINAL(x): |
| 66 | return x < NT_OFFSET |
| 67 | |
| 68 | def ISNONTERMINAL(x): |
| 69 | return x >= NT_OFFSET |
| 70 | |
| 71 | def ISEOF(x): |
| 72 | return x == ENDMARKER |
Guido van Rossum | 4747887 | 1996-08-21 14:32:37 +0000 | [diff] [blame] | 73 | |
| 74 | |
| 75 | def main(): |
| 76 | import regex |
| 77 | import string |
| 78 | import sys |
| 79 | args = sys.argv[1:] |
| 80 | inFileName = args and args[0] or "Include/token.h" |
| 81 | outFileName = "Lib/token.py" |
| 82 | if len(args) > 1: |
| 83 | outFileName = args[1] |
| 84 | try: |
| 85 | fp = open(inFileName) |
| 86 | except IOError, err: |
| 87 | sys.stdout.write("I/O error: %s\n" % str(err)) |
| 88 | sys.exit(1) |
| 89 | lines = string.splitfields(fp.read(), "\n") |
| 90 | fp.close() |
| 91 | re = regex.compile( |
| 92 | "#define[ \t][ \t]*\([A-Z][A-Z_]*\)[ \t][ \t]*\([0-9][0-9]*\)", |
| 93 | regex.casefold) |
| 94 | tokens = {} |
| 95 | for line in lines: |
| 96 | if re.match(line) > -1: |
| 97 | name, val = re.group(1, 2) |
| 98 | val = string.atoi(val) |
| 99 | tokens[val] = name # reverse so we can sort them... |
| 100 | keys = tokens.keys() |
| 101 | keys.sort() |
| 102 | # load the output skeleton from the target: |
| 103 | try: |
| 104 | fp = open(outFileName) |
| 105 | except IOError, err: |
| 106 | sys.stderr.write("I/O error: %s\n" % str(err)) |
| 107 | sys.exit(2) |
| 108 | format = string.splitfields(fp.read(), "\n") |
| 109 | fp.close() |
| 110 | try: |
| 111 | start = format.index("#--start constants--") + 1 |
| 112 | end = format.index("#--end constants--") |
| 113 | except ValueError: |
| 114 | sys.stderr.write("target does not contain format markers") |
| 115 | sys.exit(3) |
| 116 | lines = [] |
| 117 | for val in keys: |
| 118 | lines.append("%s = %d" % (tokens[val], val)) |
| 119 | format[start:end] = lines |
| 120 | try: |
| 121 | fp = open(outFileName, 'w') |
| 122 | except IOError, err: |
| 123 | sys.stderr.write("I/O error: %s\n" % str(err)) |
| 124 | sys.exit(4) |
| 125 | fp.write(string.joinfields(format, "\n")) |
| 126 | fp.close() |
| 127 | |
| 128 | |
| 129 | if __name__ == "__main__": |
| 130 | main() |