blob: 83bb9a9046c155468d5ebf7c6355b33d0f459cf8 [file] [log] [blame]
Guido van Rossumeb8c9721997-03-20 19:45:51 +00001#! /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)
Guido van Rossum90d556f1997-03-20 19:44:30 +000014
Guido van Rossumeb8c9721997-03-20 19:45:51 +000015kwlist = [
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 ]
Guido van Rossum90d556f1997-03-20 19:44:30 +000046
Guido van Rossumeb8c9721997-03-20 19:45:51 +000047kwdict = {}
48for keyword in kwlist:
49 kwdict[keyword] = 1
Guido van Rossum90d556f1997-03-20 19:44:30 +000050
Guido van Rossumeb8c9721997-03-20 19:45:51 +000051iskeyword = kwdict.has_key
52
53def 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
115if __name__ == "__main__":
116 main()