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

"""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:

    python Lib/keyword.py
"""
12

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

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

51
iskeyword = frozenset(kwlist).__contains__
52 53

def main():
54
    import sys, re
55 56 57 58 59 60 61

    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
62
    fp = open(iptfile)
63
    strprog = re.compile('"([^"]+)"')
64
    lines = []
65
    for line in fp:
66
        if '{1, "' in line:
67 68 69
            match = strprog.search(line)
            if match:
                lines.append("        '" + match.group(1) + "',\n")
70
    fp.close()
71
    lines.sort()
72 73

    # load the output skeleton from the target
74 75 76
    fp = open(optfile)
    format = fp.readlines()
    fp.close()
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()