blob: 6e1e882a91e7006f5699ca9ac6df8292d8b8457f [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#! /usr/bin/env python3
Guido van Rossum54f22ed2000-02-04 15:10:34 +00002
3"""Keywords (from "graminit.c")
4
5This file is automatically generated; please don't muck it up!
6
7To update the symbols in this file, 'cd' to the top directory of
8the python source tree after building the interpreter and run:
9
Éric Araujoe1886bf2011-11-29 16:45:34 +010010 ./python Lib/keyword.py
Guido van Rossum54f22ed2000-02-04 15:10:34 +000011"""
Guido van Rossum90d556f1997-03-20 19:44:30 +000012
Raymond Hettingeredc853e2002-10-30 05:17:22 +000013__all__ = ["iskeyword", "kwlist"]
Skip Montanaro17ab1232001-01-24 06:27:27 +000014
Guido van Rossumeb8c9721997-03-20 19:45:51 +000015kwlist = [
16#--start keywords--
Guido van Rossum486364b2007-06-30 05:01:58 +000017 'False',
18 'None',
19 'True',
Guido van Rossumeb8c9721997-03-20 19:45:51 +000020 'and',
Thomas Wouters1f1c16a2006-02-28 22:50:17 +000021 'as',
Guido van Rossumed57d761997-07-23 18:10:52 +000022 'assert',
Guido van Rossumeb8c9721997-03-20 19:45:51 +000023 'break',
24 'class',
25 'continue',
26 'def',
27 'del',
28 'elif',
29 'else',
30 'except',
Guido van Rossumeb8c9721997-03-20 19:45:51 +000031 'finally',
32 'for',
33 'from',
34 'global',
35 'if',
36 'import',
37 'in',
38 'is',
39 'lambda',
Jack Diederich4b7f3172007-02-28 20:21:30 +000040 'nonlocal',
Guido van Rossumeb8c9721997-03-20 19:45:51 +000041 'not',
42 'or',
43 'pass',
Guido van Rossumeb8c9721997-03-20 19:45:51 +000044 'raise',
45 'return',
46 'try',
47 'while',
Thomas Wouters1f1c16a2006-02-28 22:50:17 +000048 'with',
Tim Peters46376682001-06-19 00:26:25 +000049 'yield',
Guido van Rossumeb8c9721997-03-20 19:45:51 +000050#--end keywords--
51 ]
Guido van Rossum90d556f1997-03-20 19:44:30 +000052
Raymond Hettinger70ef8692003-12-02 07:48:15 +000053iskeyword = frozenset(kwlist).__contains__
Guido van Rossumeb8c9721997-03-20 19:45:51 +000054
55def main():
Eric S. Raymondee5e61d2001-02-09 09:10:35 +000056 import sys, re
Guido van Rossumeb8c9721997-03-20 19:45:51 +000057
58 args = sys.argv[1:]
59 iptfile = args and args[0] or "Python/graminit.c"
60 if len(args) > 1: optfile = args[1]
61 else: optfile = "Lib/keyword.py"
62
R David Murrayf0f7cea2013-04-25 12:01:36 -040063 # load the output skeleton from the target, taking care to preserve its
64 # newline convention.
65 with open(optfile, newline='') as fp:
66 format = fp.readlines()
67 nl = format[0][len(format[0].strip()):] if format else '\n'
68
Guido van Rossumeb8c9721997-03-20 19:45:51 +000069 # scan the source file for keywords
Florent Xicluna7dde7922010-09-03 19:52:03 +000070 with open(iptfile) as fp:
71 strprog = re.compile('"([^"]+)"')
72 lines = []
73 for line in fp:
74 if '{1, "' in line:
75 match = strprog.search(line)
76 if match:
R David Murrayf0f7cea2013-04-25 12:01:36 -040077 lines.append(" '" + match.group(1) + "'," + nl)
Guido van Rossum4d819841997-03-20 20:40:45 +000078 lines.sort()
Guido van Rossumeb8c9721997-03-20 19:45:51 +000079
R David Murrayf0f7cea2013-04-25 12:01:36 -040080 # insert the lines of keywords into the skeleton
Guido van Rossumeb8c9721997-03-20 19:45:51 +000081 try:
R David Murrayf0f7cea2013-04-25 12:01:36 -040082 start = format.index("#--start keywords--" + nl) + 1
83 end = format.index("#--end keywords--" + nl)
Guido van Rossum4d819841997-03-20 20:40:45 +000084 format[start:end] = lines
Guido van Rossumeb8c9721997-03-20 19:45:51 +000085 except ValueError:
86 sys.stderr.write("target does not contain format markers\n")
Guido van Rossum4d819841997-03-20 20:40:45 +000087 sys.exit(1)
Guido van Rossumeb8c9721997-03-20 19:45:51 +000088
89 # write the output file
R David Murrayf0f7cea2013-04-25 12:01:36 -040090 with open(optfile, 'w', newline='') as fp:
91 fp.writelines(format)
Guido van Rossumeb8c9721997-03-20 19:45:51 +000092
Guido van Rossum9694fca1997-10-22 21:00:49 +000093if __name__ == "__main__":
94 main()