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