blob: ba132059abf5ee5d0edb56c0d61057b7126f9e6d [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""Token constants (from "token.h")."""
2
Alexander Belopolskyb9d10d02010-11-11 14:07:41 +00003__all__ = ['tok_name', 'ISTERMINAL', 'ISNONTERMINAL', 'ISEOF']
4
Guido van Rossum47478871996-08-21 14:32:37 +00005# 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#
Éric Araujoe1886bf2011-11-29 16:45:34 +010010# ./python Lib/token.py
Guido van Rossumb31c7f71993-11-11 10:31:23 +000011
Guido van Rossum47478871996-08-21 14:32:37 +000012#--start constants--
Guido van Rossumb31c7f71993-11-11 10:31:23 +000013ENDMARKER = 0
14NAME = 1
15NUMBER = 2
16STRING = 3
17NEWLINE = 4
18INDENT = 5
19DEDENT = 6
20LPAR = 7
21RPAR = 8
22LSQB = 9
23RSQB = 10
24COLON = 11
25COMMA = 12
26SEMI = 13
27PLUS = 14
28MINUS = 15
29STAR = 16
30SLASH = 17
31VBAR = 18
32AMPER = 19
33LESS = 20
34GREATER = 21
35EQUAL = 22
36DOT = 23
37PERCENT = 24
Meador Inge33880602012-01-15 19:15:36 -060038LBRACE = 25
39RBRACE = 26
40EQEQUAL = 27
41NOTEQUAL = 28
42LESSEQUAL = 29
43GREATEREQUAL = 30
44TILDE = 31
45CIRCUMFLEX = 32
46LEFTSHIFT = 33
47RIGHTSHIFT = 34
48DOUBLESTAR = 35
49PLUSEQUAL = 36
50MINEQUAL = 37
51STAREQUAL = 38
52SLASHEQUAL = 39
53PERCENTEQUAL = 40
54AMPEREQUAL = 41
55VBAREQUAL = 42
56CIRCUMFLEXEQUAL = 43
57LEFTSHIFTEQUAL = 44
58RIGHTSHIFTEQUAL = 45
59DOUBLESTAREQUAL = 46
60DOUBLESLASH = 47
61DOUBLESLASHEQUAL = 48
62AT = 49
Benjamin Petersond51374e2014-04-09 23:55:56 -040063ATEQUAL = 50
64RARROW = 51
65ELLIPSIS = 52
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +010066# Don't forget to update the table _PyParser_TokenNames in tokenizer.c!
Benjamin Petersond51374e2014-04-09 23:55:56 -040067OP = 53
Serhiy Storchakad08972f2018-04-11 19:15:51 +030068ERRORTOKEN = 54
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +010069# These aren't used by the C tokenizer but are needed for tokenize.py
Serhiy Storchakad08972f2018-04-11 19:15:51 +030070COMMENT = 55
71NL = 56
72ENCODING = 57
73N_TOKENS = 58
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +010074# Special definitions for cooperation with parser
Guido van Rossum47478871996-08-21 14:32:37 +000075NT_OFFSET = 256
76#--end constants--
Guido van Rossumb31c7f71993-11-11 10:31:23 +000077
Alexander Belopolskyb9d10d02010-11-11 14:07:41 +000078tok_name = {value: name
79 for name, value in globals().items()
Antoine Pitrouea3eb882012-05-17 18:55:59 +020080 if isinstance(value, int) and not name.startswith('_')}
Alexander Belopolskyb9d10d02010-11-11 14:07:41 +000081__all__.extend(tok_name.values())
Guido van Rossumb31c7f71993-11-11 10:31:23 +000082
Guido van Rossum154a5391996-07-21 02:17:52 +000083def ISTERMINAL(x):
84 return x < NT_OFFSET
85
86def ISNONTERMINAL(x):
87 return x >= NT_OFFSET
88
89def ISEOF(x):
90 return x == ENDMARKER
Guido van Rossum47478871996-08-21 14:32:37 +000091
92
Alexander Belopolskyb9d10d02010-11-11 14:07:41 +000093def _main():
Guido van Rossum9694fca1997-10-22 21:00:49 +000094 import re
Guido van Rossum47478871996-08-21 14:32:37 +000095 import sys
96 args = sys.argv[1:]
97 inFileName = args and args[0] or "Include/token.h"
98 outFileName = "Lib/token.py"
99 if len(args) > 1:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000100 outFileName = args[1]
Guido van Rossum47478871996-08-21 14:32:37 +0000101 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000102 fp = open(inFileName)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200103 except OSError as err:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000104 sys.stdout.write("I/O error: %s\n" % str(err))
105 sys.exit(1)
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300106 with fp:
107 lines = fp.read().split("\n")
Guido van Rossum9694fca1997-10-22 21:00:49 +0000108 prog = re.compile(
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +0100109 r"#define[ \t][ \t]*([A-Z0-9][A-Z0-9_]*)[ \t][ \t]*([0-9][0-9]*)",
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000110 re.IGNORECASE)
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +0100111 comment_regex = re.compile(
112 r"^\s*/\*\s*(.+?)\s*\*/\s*$",
113 re.IGNORECASE)
114
Guido van Rossum47478871996-08-21 14:32:37 +0000115 tokens = {}
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +0100116 prev_val = None
Guido van Rossum47478871996-08-21 14:32:37 +0000117 for line in lines:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000118 match = prog.match(line)
119 if match:
120 name, val = match.group(1, 2)
Eric S. Raymond6e025bc2001-02-10 00:22:33 +0000121 val = int(val)
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +0100122 tokens[val] = {'token': name} # reverse so we can sort them...
123 prev_val = val
124 else:
125 comment_match = comment_regex.match(line)
126 if comment_match and prev_val is not None:
127 comment = comment_match.group(1)
128 tokens[prev_val]['comment'] = comment
Guido van Rossumc145ef32007-02-26 14:08:27 +0000129 keys = sorted(tokens.keys())
Guido van Rossum47478871996-08-21 14:32:37 +0000130 # load the output skeleton from the target:
131 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000132 fp = open(outFileName)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200133 except OSError as err:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000134 sys.stderr.write("I/O error: %s\n" % str(err))
135 sys.exit(2)
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300136 with fp:
137 format = fp.read().split("\n")
Guido van Rossum47478871996-08-21 14:32:37 +0000138 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000139 start = format.index("#--start constants--") + 1
140 end = format.index("#--end constants--")
Guido van Rossum47478871996-08-21 14:32:37 +0000141 except ValueError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000142 sys.stderr.write("target does not contain format markers")
143 sys.exit(3)
Guido van Rossum47478871996-08-21 14:32:37 +0000144 lines = []
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +0100145 for key in keys:
146 lines.append("%s = %d" % (tokens[key]["token"], key))
147 if "comment" in tokens[key]:
148 lines.append("# %s" % tokens[key]["comment"])
Guido van Rossum47478871996-08-21 14:32:37 +0000149 format[start:end] = lines
150 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000151 fp = open(outFileName, 'w')
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200152 except OSError as err:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000153 sys.stderr.write("I/O error: %s\n" % str(err))
154 sys.exit(4)
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300155 with fp:
156 fp.write("\n".join(format))
Guido van Rossum47478871996-08-21 14:32:37 +0000157
158
159if __name__ == "__main__":
Alexander Belopolskyb9d10d02010-11-11 14:07:41 +0000160 _main()