Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eb8c972

Browse files
committed
Ka-Ping Yee's version is better:
Here's a "keyword" module which, in the spirit of "token.py", updates the list of keywords automatically from a source file (in this case, "graminit.c" seemed like a reasonable choice, easier than "Grammar/Grammar"). You get "kwlist", a sorted list of keywords; "kwdict", a dictionary mapping each keyword to 1; and "iskeyword", a function which tells you if a given string happens to be a keyword.
1 parent 90d556f commit eb8c972

1 file changed

Lines changed: 116 additions & 37 deletions

File tree

Lib/keyword.py

Lines changed: 116 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,116 @@
1-
"""Export the list of Python keywords (reserved words)."""
2-
3-
# grep '{1, "' ../Python/graminit.c | sed 's/.*"\(.*\)".*/ "\1",/' | sort
4-
5-
keywords = [
6-
"__assert__",
7-
"and",
8-
"break",
9-
"class",
10-
"continue",
11-
"def",
12-
"del",
13-
"elif",
14-
"else",
15-
"except",
16-
"exec",
17-
"finally",
18-
"for",
19-
"from",
20-
"global",
21-
"if",
22-
"import",
23-
"in",
24-
"is",
25-
"lambda",
26-
"not",
27-
"or",
28-
"pass",
29-
"print",
30-
"raise",
31-
"return",
32-
"try",
33-
"while",
34-
]
35-
36-
if __name__ == '__main__':
37-
for k in keywords: print k
1+
#! /usr/bin/env python
2+
#
3+
# Keywords (from "graminit.c")
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/keyword.py
11+
#
12+
# (this path allows the import of string.py and regexmodule.so
13+
# for a site with no installation in place)
14+
15+
kwlist = [
16+
#--start keywords--
17+
'and',
18+
'break',
19+
'class',
20+
'continue',
21+
'def',
22+
'del',
23+
'elif',
24+
'else',
25+
'except',
26+
'exec',
27+
'finally',
28+
'for',
29+
'from',
30+
'global',
31+
'if',
32+
'import',
33+
'in',
34+
'is',
35+
'lambda',
36+
'not',
37+
'or',
38+
'pass',
39+
'print',
40+
'raise',
41+
'return',
42+
'try',
43+
'while',
44+
#--end keywords--
45+
]
46+
47+
kwdict = {}
48+
for keyword in kwlist:
49+
kwdict[keyword] = 1
50+
51+
iskeyword = kwdict.has_key
52+
53+
def main():
54+
import sys, regex, string
55+
56+
args = sys.argv[1:]
57+
iptfile = args and args[0] or "Python/graminit.c"
58+
if len(args) > 1: optfile = args[1]
59+
else: optfile = "Lib/keyword.py"
60+
61+
# scan the source file for keywords
62+
try:
63+
fp = open(iptfile)
64+
except IOError, err:
65+
sys.stderr.write("I/O error reading from %s: %s\n" % (optfile, err))
66+
sys.exit(1)
67+
68+
strprog = regex.compile('"\([^"]+\)"')
69+
labelprog = regex.compile('static[ \t]+label.*=[ \t]+{')
70+
keywordlist = []
71+
while 1:
72+
line = fp.readline()
73+
if not line: break
74+
if labelprog.search(line) > -1: break
75+
while line:
76+
line = fp.readline()
77+
if string.find(line, ';') > -1: break
78+
if strprog.search(line) > -1: keywordlist.append(strprog.group(1))
79+
fp.close()
80+
81+
keywordlist.sort()
82+
keywordlist.remove("EMPTY")
83+
84+
# load the output skeleton from the target
85+
try:
86+
fp = open(optfile)
87+
format = fp.readlines()
88+
fp.close()
89+
except IOError, err:
90+
sys.stderr.write("I/O error reading from %s: %s\n" % (optfile, err))
91+
sys.exit(2)
92+
93+
try:
94+
start = format.index("#--start keywords--\n") + 1
95+
end = format.index("#--end keywords--\n")
96+
except ValueError:
97+
sys.stderr.write("target does not contain format markers\n")
98+
sys.exit(3)
99+
100+
# insert the lines of keywords
101+
lines = []
102+
for keyword in keywordlist:
103+
lines.append(" '" + keyword + "',\n")
104+
format[start:end] = lines
105+
106+
# write the output file
107+
try:
108+
fp = open(optfile, 'w')
109+
except IOError, err:
110+
sys.stderr.write("I/O error writing to %s: %s\n" % (optfile, err))
111+
sys.exit(4)
112+
fp.write(string.join(format, ''))
113+
fp.close()
114+
115+
if __name__ == "__main__":
116+
main()

0 commit comments

Comments
 (0)