keyword.py 2 KB
Newer Older
1
#! /usr/bin/env python3
2 3 4 5 6 7 8 9

"""Keywords (from "graminit.c")

This file is automatically generated; please don't muck it up!

To update the symbols in this file, 'cd' to the top directory of
the python source tree after building the interpreter and run:

10
    ./python Lib/keyword.py
11
"""
12

13
__all__ = ["iskeyword", "kwlist"]
14

15 16
kwlist = [
#--start keywords--
17 18 19
        'False',
        'None',
        'True',
20
        'and',
Thomas Wouters's avatar
Thomas Wouters committed
21
        'as',
22
        'assert',
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
        'break',
        'class',
        'continue',
        'def',
        'del',
        'elif',
        'else',
        'except',
        'finally',
        'for',
        'from',
        'global',
        'if',
        'import',
        'in',
        'is',
        'lambda',
40
        'nonlocal',
41 42 43 44 45 46 47
        'not',
        'or',
        'pass',
        'raise',
        'return',
        'try',
        'while',
Thomas Wouters's avatar
Thomas Wouters committed
48
        'with',
49
        'yield',
50 51 52
#--end keywords--
        ]

53
iskeyword = frozenset(kwlist).__contains__
54 55

def main():
56
    import sys, re
57 58 59 60 61 62 63

    args = sys.argv[1:]
    iptfile = args and args[0] or "Python/graminit.c"
    if len(args) > 1: optfile = args[1]
    else: optfile = "Lib/keyword.py"

    # scan the source file for keywords
64 65 66 67 68 69 70 71
    with open(iptfile) as fp:
        strprog = re.compile('"([^"]+)"')
        lines = []
        for line in fp:
            if '{1, "' in line:
                match = strprog.search(line)
                if match:
                    lines.append("        '" + match.group(1) + "',\n")
72
    lines.sort()
73 74

    # load the output skeleton from the target
75 76
    with open(optfile) as fp:
        format = fp.readlines()
77

78
    # insert the lines of keywords
79 80 81
    try:
        start = format.index("#--start keywords--\n") + 1
        end = format.index("#--end keywords--\n")
82
        format[start:end] = lines
83 84
    except ValueError:
        sys.stderr.write("target does not contain format markers\n")
85
        sys.exit(1)
86 87

    # write the output file
88
    fp = open(optfile, 'w')
89
    fp.write(''.join(format))
90 91
    fp.close()

92 93
if __name__ == "__main__":
    main()