blob: 091f80bf191b9a8163fc7ea8cc860aec50338d8f [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
Yury Selivanov75445082015-05-11 22:57:16 -040068AWAIT = 54
69ASYNC = 55
70ERRORTOKEN = 56
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +010071# These aren't used by the C tokenizer but are needed for tokenize.py
72COMMENT = 57
73NL = 58
74ENCODING = 59
75N_TOKENS = 60
76# Special definitions for cooperation with parser
Guido van Rossum47478871996-08-21 14:32:37 +000077NT_OFFSET = 256
78#--end constants--
Guido van Rossumb31c7f71993-11-11 10:31:23 +000079
Alexander Belopolskyb9d10d02010-11-11 14:07:41 +000080tok_name = {value: name
81 for name, value in globals().items()
Antoine Pitrouea3eb882012-05-17 18:55:59 +020082 if isinstance(value, int) and not name.startswith('_')}
Alexander Belopolskyb9d10d02010-11-11 14:07:41 +000083__all__.extend(tok_name.values())
Guido van Rossumb31c7f71993-11-11 10:31:23 +000084
Guido van Rossum154a5391996-07-21 02:17:52 +000085def ISTERMINAL(x):
86 return x < NT_OFFSET
87
88def ISNONTERMINAL(x):
89 return x >= NT_OFFSET
90
91def ISEOF(x):
92 return x == ENDMARKER
Guido van Rossum47478871996-08-21 14:32:37 +000093
94
Alexander Belopolskyb9d10d02010-11-11 14:07:41 +000095def _main():
Guido van Rossum9694fca1997-10-22 21:00:49 +000096 import re
Guido van Rossum47478871996-08-21 14:32:37 +000097 import sys
98 args = sys.argv[1:]
99 inFileName = args and args[0] or "Include/token.h"
100 outFileName = "Lib/token.py"
101 if len(args) > 1:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000102 outFileName = args[1]
Guido van Rossum47478871996-08-21 14:32:37 +0000103 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000104 fp = open(inFileName)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200105 except OSError as err:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000106 sys.stdout.write("I/O error: %s\n" % str(err))
107 sys.exit(1)
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300108 with fp:
109 lines = fp.read().split("\n")
Guido van Rossum9694fca1997-10-22 21:00:49 +0000110 prog = re.compile(
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +0100111 r"#define[ \t][ \t]*([A-Z0-9][A-Z0-9_]*)[ \t][ \t]*([0-9][0-9]*)",
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000112 re.IGNORECASE)
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +0100113 comment_regex = re.compile(
114 r"^\s*/\*\s*(.+?)\s*\*/\s*$",
115 re.IGNORECASE)
116
Guido van Rossum47478871996-08-21 14:32:37 +0000117 tokens = {}
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +0100118 prev_val = None
Guido van Rossum47478871996-08-21 14:32:37 +0000119 for line in lines:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000120 match = prog.match(line)
121 if match:
122 name, val = match.group(1, 2)
Eric S. Raymond6e025bc2001-02-10 00:22:33 +0000123 val = int(val)
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +0100124 tokens[val] = {'token': name} # reverse so we can sort them...
125 prev_val = val
126 else:
127 comment_match = comment_regex.match(line)
128 if comment_match and prev_val is not None:
129 comment = comment_match.group(1)
130 tokens[prev_val]['comment'] = comment
Guido van Rossumc145ef32007-02-26 14:08:27 +0000131 keys = sorted(tokens.keys())
Guido van Rossum47478871996-08-21 14:32:37 +0000132 # load the output skeleton from the target:
133 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000134 fp = open(outFileName)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200135 except OSError as err:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000136 sys.stderr.write("I/O error: %s\n" % str(err))
137 sys.exit(2)
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300138 with fp:
139 format = fp.read().split("\n")
Guido van Rossum47478871996-08-21 14:32:37 +0000140 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000141 start = format.index("#--start constants--") + 1
142 end = format.index("#--end constants--")
Guido van Rossum47478871996-08-21 14:32:37 +0000143 except ValueError:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000144 sys.stderr.write("target does not contain format markers")
145 sys.exit(3)
Guido van Rossum47478871996-08-21 14:32:37 +0000146 lines = []
Albert-Jan Nijburgfc354f02017-05-31 15:00:21 +0100147 for key in keys:
148 lines.append("%s = %d" % (tokens[key]["token"], key))
149 if "comment" in tokens[key]:
150 lines.append("# %s" % tokens[key]["comment"])
Guido van Rossum47478871996-08-21 14:32:37 +0000151 format[start:end] = lines
152 try:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000153 fp = open(outFileName, 'w')
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200154 except OSError as err:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000155 sys.stderr.write("I/O error: %s\n" % str(err))
156 sys.exit(4)
Serhiy Storchaka46ba6c82015-04-04 11:01:02 +0300157 with fp:
158 fp.write("\n".join(format))
Guido van Rossum47478871996-08-21 14:32:37 +0000159
160
161if __name__ == "__main__":
Alexander Belopolskyb9d10d02010-11-11 14:07:41 +0000162 _main()