]> git.madduck.net Git - etc/vim.git/blob - blib2to3/pygram.py

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

7614af73e1c4e90b051bfaeab670c36aaf38bb8a
[etc/vim.git] / blib2to3 / pygram.py
1 # Copyright 2006 Google, Inc. All Rights Reserved.
2 # Licensed to PSF under a Contributor Agreement.
3
4 """Export the Python grammar and symbols."""
5
6 # Python imports
7 import os
8
9 # Local imports
10 from .pgen2 import token
11 from .pgen2 import driver
12
13 # The grammar file
14 _GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "Grammar.txt")
15 _PATTERN_GRAMMAR_FILE = os.path.join(os.path.dirname(__file__), "PatternGrammar.txt")
16
17
18 class Symbols(object):
19     def __init__(self, grammar):
20         """Initializer.
21
22         Creates an attribute for each grammar symbol (nonterminal),
23         whose value is the symbol's type (an int >= 256).
24         """
25         for name, symbol in grammar.symbol2number.items():
26             setattr(self, name, symbol)
27
28
29 def initialize(cache_dir=None):
30     global python_grammar
31     global python_grammar_no_print_statement
32     global python_grammar_no_print_statement_no_exec_statement
33     global python_grammar_no_print_statement_no_exec_statement_async_keywords
34     global python_symbols
35     global pattern_grammar
36     global pattern_symbols
37
38     # Python 2
39     python_grammar = driver.load_packaged_grammar("blib2to3", _GRAMMAR_FILE, cache_dir)
40
41     python_symbols = Symbols(python_grammar)
42
43     # Python 2 + from __future__ import print_function
44     python_grammar_no_print_statement = python_grammar.copy()
45     del python_grammar_no_print_statement.keywords["print"]
46
47     # Python 3.0-3.6
48     python_grammar_no_print_statement_no_exec_statement = python_grammar.copy()
49     del python_grammar_no_print_statement_no_exec_statement.keywords["print"]
50     del python_grammar_no_print_statement_no_exec_statement.keywords["exec"]
51
52     # Python 3.7+
53     python_grammar_no_print_statement_no_exec_statement_async_keywords = (
54         python_grammar_no_print_statement_no_exec_statement.copy()
55     )
56     python_grammar_no_print_statement_no_exec_statement_async_keywords.async_keywords = (
57         True
58     )
59
60     pattern_grammar = driver.load_packaged_grammar(
61         "blib2to3", _PATTERN_GRAMMAR_FILE, cache_dir
62     )
63     pattern_symbols = Symbols(pattern_grammar)