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.
1 # Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.
2 # Licensed to PSF under a Contributor Agreement.
5 # Copyright 2006 Google, Inc. All Rights Reserved.
6 # Licensed to PSF under a Contributor Agreement.
10 This provides a high-level interface to parse a file into a syntax tree.
14 __author__ = "Guido van Rossum <guido@python.org>"
16 __all__ = ["Driver", "load_grammar"]
27 from . import grammar, parse, token, tokenize, pgen
32 def __init__(self, grammar, convert=None, logger=None):
33 self.grammar = grammar
35 logger = logging.getLogger()
37 self.convert = convert
39 def parse_tokens(self, tokens, debug=False):
40 """Parse a series of tokens and return the syntax tree."""
41 # XXX Move the prefix computation into a wrapper around tokenize.
42 p = parse.Parser(self.grammar, self.convert)
46 type = value = start = end = line_text = None
48 for quintuple in tokens:
49 type, value, start, end, line_text = quintuple
50 if start != (lineno, column):
51 assert (lineno, column) <= start, ((lineno, column), start)
52 s_lineno, s_column = start
54 prefix += "\n" * (s_lineno - lineno)
58 prefix += line_text[column:s_column]
60 if type in (tokenize.COMMENT, tokenize.NL):
63 if value.endswith("\n"):
68 type = grammar.opmap[value]
70 self.logger.debug("%s %r (prefix=%r)",
71 token.tok_name[type], value, prefix)
72 if type in {token.INDENT, token.DEDENT}:
75 if p.addtoken(type, value, (prefix, start)):
77 self.logger.debug("Stop.")
80 if type == token.INDENT:
81 if _prefix.startswith(value):
82 # Don't double-indent. Since we're delaying the prefix that
83 # would normally belong to INDENT, we need to put the value
84 # at the end versus at the beginning.
85 _prefix = _prefix[len(value):] + value
86 if type in {token.INDENT, token.DEDENT}:
89 if value.endswith("\n"):
93 # We never broke out -- EOF is too soon (how can this happen???)
94 raise parse.ParseError("incomplete input",
95 type, value, (prefix, start))
98 def parse_stream_raw(self, stream, debug=False):
99 """Parse a stream and return the syntax tree."""
100 tokens = tokenize.generate_tokens(stream.readline)
101 return self.parse_tokens(tokens, debug)
103 def parse_stream(self, stream, debug=False):
104 """Parse a stream and return the syntax tree."""
105 return self.parse_stream_raw(stream, debug)
107 def parse_file(self, filename, encoding=None, debug=False):
108 """Parse a file and return the syntax tree."""
109 with io.open(filename, "r", encoding=encoding) as stream:
110 return self.parse_stream(stream, debug)
112 def parse_string(self, text, debug=False):
113 """Parse a string and return the syntax tree."""
114 tokens = tokenize.generate_tokens(io.StringIO(text).readline)
115 return self.parse_tokens(tokens, debug)
118 def _generate_pickle_name(gt):
119 head, tail = os.path.splitext(gt)
122 return head + tail + ".".join(map(str, sys.version_info)) + ".pickle"
125 def load_grammar(gt="Grammar.txt", gp=None,
126 save=True, force=False, logger=None):
127 """Load the grammar (maybe from a pickle)."""
129 logger = logging.getLogger()
130 gp = _generate_pickle_name(gt) if gp is None else gp
131 if force or not _newer(gp, gt):
132 logger.info("Generating grammar tables from %s", gt)
133 g = pgen.generate_grammar(gt)
135 logger.info("Writing grammar tables to %s", gp)
139 logger.info("Writing failed: %s", e)
141 g = grammar.Grammar()
147 """Inquire whether file a was written since file b."""
148 if not os.path.exists(a):
150 if not os.path.exists(b):
152 return os.path.getmtime(a) >= os.path.getmtime(b)
155 def load_packaged_grammar(package, grammar_source):
156 """Normally, loads a pickled grammar by doing
157 pkgutil.get_data(package, pickled_grammar)
158 where *pickled_grammar* is computed from *grammar_source* by adding the
159 Python version and using a ``.pickle`` extension.
161 However, if *grammar_source* is an extant file, load_grammar(grammar_source)
162 is called instead. This facilitates using a packaged grammar file when needed
163 but preserves load_grammar's automatic regeneration behavior when possible.
166 if os.path.isfile(grammar_source):
167 return load_grammar(grammar_source)
168 pickled_name = _generate_pickle_name(os.path.basename(grammar_source))
169 data = pkgutil.get_data(package, pickled_name)
170 g = grammar.Grammar()
176 """Main program, when run as a script: produce grammar pickle files.
178 Calls load_grammar for each argument, a path to a grammar text file.
182 logging.basicConfig(level=logging.INFO, stream=sys.stdout,
183 format='%(message)s')
185 load_grammar(gt, save=True, force=True)
188 if __name__ == "__main__":
189 sys.exit(int(not main()))