2008-03-19 01:43:46 -03:00
|
|
|
# Copyright 2004-2005 Elemental Security, Inc. All Rights Reserved.
|
|
|
|
# Licensed to PSF under a Contributor Agreement.
|
|
|
|
|
|
|
|
# Modifications:
|
|
|
|
# Copyright 2006 Google, Inc. All Rights Reserved.
|
|
|
|
# Licensed to PSF under a Contributor Agreement.
|
|
|
|
|
|
|
|
"""Parser driver.
|
|
|
|
|
|
|
|
This provides a high-level interface to parse a file into a syntax tree.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = "Guido van Rossum <guido@python.org>"
|
|
|
|
|
|
|
|
__all__ = ["Driver", "load_grammar"]
|
|
|
|
|
|
|
|
# Python imports
|
2009-05-08 22:01:14 -03:00
|
|
|
import codecs
|
2008-03-19 01:43:46 -03:00
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
|
|
|
# Pgen imports
|
Merged revisions 61602-61723 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r61626 | david.wolever | 2008-03-19 17:19:16 +0100 (Mi, 19 Mär 2008) | 1 line
Added fixer for implicit local imports. See #2414.
........
r61628 | david.wolever | 2008-03-19 17:57:43 +0100 (Mi, 19 Mär 2008) | 1 line
Added a class for tests which should not run if a particular import is found.
........
r61629 | collin.winter | 2008-03-19 17:58:19 +0100 (Mi, 19 Mär 2008) | 1 line
Two more relative import fixes in pgen2.
........
r61635 | david.wolever | 2008-03-19 20:16:03 +0100 (Mi, 19 Mär 2008) | 1 line
Fixed print fixer so it will do the Right Thing when it encounters __future__.print_function. 2to3 gets upset, though, so the tests have been commented out.
........
r61637 | david.wolever | 2008-03-19 21:37:17 +0100 (Mi, 19 Mär 2008) | 3 lines
Added a fixer for itertools imports (from itertools import imap, ifilterfalse --> from itertools import filterfalse)
........
r61645 | david.wolever | 2008-03-19 23:22:35 +0100 (Mi, 19 Mär 2008) | 1 line
SVN is happier when you add the files you create... -_-'
........
r61654 | david.wolever | 2008-03-20 01:09:56 +0100 (Do, 20 Mär 2008) | 1 line
Added an explicit sort order to fixers -- fixes problems like #2427
........
r61664 | david.wolever | 2008-03-20 04:32:40 +0100 (Do, 20 Mär 2008) | 3 lines
Fixes #2428 -- comments are no longer eatten by __future__ fixer.
........
r61673 | david.wolever | 2008-03-20 17:22:40 +0100 (Do, 20 Mär 2008) | 1 line
Added 2to3 node pretty-printer
........
r61679 | david.wolever | 2008-03-20 20:50:42 +0100 (Do, 20 Mär 2008) | 1 line
Made node printing a little bit prettier
........
r61723 | martin.v.loewis | 2008-03-22 00:59:27 +0100 (Sa, 22 Mär 2008) | 2 lines
Fix whitespace.
........
2008-03-21 21:01:12 -03:00
|
|
|
from . import grammar, parse, token, tokenize, pgen
|
2008-03-19 01:43:46 -03:00
|
|
|
|
|
|
|
|
|
|
|
class Driver(object):
|
|
|
|
|
|
|
|
def __init__(self, grammar, convert=None, logger=None):
|
|
|
|
self.grammar = grammar
|
|
|
|
if logger is None:
|
|
|
|
logger = logging.getLogger()
|
|
|
|
self.logger = logger
|
|
|
|
self.convert = convert
|
|
|
|
|
|
|
|
def parse_tokens(self, tokens, debug=False):
|
|
|
|
"""Parse a series of tokens and return the syntax tree."""
|
|
|
|
# XXX Move the prefix computation into a wrapper around tokenize.
|
|
|
|
p = parse.Parser(self.grammar, self.convert)
|
|
|
|
p.setup()
|
|
|
|
lineno = 1
|
|
|
|
column = 0
|
|
|
|
type = value = start = end = line_text = None
|
2009-05-08 22:01:14 -03:00
|
|
|
prefix = u""
|
2008-03-19 01:43:46 -03:00
|
|
|
for quintuple in tokens:
|
|
|
|
type, value, start, end, line_text = quintuple
|
|
|
|
if start != (lineno, column):
|
|
|
|
assert (lineno, column) <= start, ((lineno, column), start)
|
|
|
|
s_lineno, s_column = start
|
|
|
|
if lineno < s_lineno:
|
|
|
|
prefix += "\n" * (s_lineno - lineno)
|
|
|
|
lineno = s_lineno
|
|
|
|
column = 0
|
|
|
|
if column < s_column:
|
|
|
|
prefix += line_text[column:s_column]
|
|
|
|
column = s_column
|
|
|
|
if type in (tokenize.COMMENT, tokenize.NL):
|
|
|
|
prefix += value
|
|
|
|
lineno, column = end
|
|
|
|
if value.endswith("\n"):
|
|
|
|
lineno += 1
|
|
|
|
column = 0
|
|
|
|
continue
|
|
|
|
if type == token.OP:
|
|
|
|
type = grammar.opmap[value]
|
|
|
|
if debug:
|
|
|
|
self.logger.debug("%s %r (prefix=%r)",
|
|
|
|
token.tok_name[type], value, prefix)
|
|
|
|
if p.addtoken(type, value, (prefix, start)):
|
|
|
|
if debug:
|
|
|
|
self.logger.debug("Stop.")
|
|
|
|
break
|
|
|
|
prefix = ""
|
|
|
|
lineno, column = end
|
|
|
|
if value.endswith("\n"):
|
|
|
|
lineno += 1
|
|
|
|
column = 0
|
|
|
|
else:
|
|
|
|
# We never broke out -- EOF is too soon (how can this happen???)
|
Merged revisions 68306-68308,68340,68368,68422 via svnmerge from
svn+ssh://pythondev@svn.python.org/sandbox/trunk/2to3/lib2to3
........
r68306 | benjamin.peterson | 2009-01-04 12:27:19 -0600 (Sun, 04 Jan 2009) | 1 line
fix_urllib: add mappings for the url parsing functions
........
r68307 | benjamin.peterson | 2009-01-04 12:30:01 -0600 (Sun, 04 Jan 2009) | 1 line
remove duplicated function
........
r68308 | benjamin.peterson | 2009-01-04 12:50:34 -0600 (Sun, 04 Jan 2009) | 1 line
turtle is no longer renamed
........
r68340 | georg.brandl | 2009-01-05 02:11:39 -0600 (Mon, 05 Jan 2009) | 2 lines
Fix undefined locals in parse_tokens().
........
r68368 | benjamin.peterson | 2009-01-06 17:56:10 -0600 (Tue, 06 Jan 2009) | 1 line
fix typo (thanks to Robert Lehmann)
........
r68422 | benjamin.peterson | 2009-01-08 20:01:03 -0600 (Thu, 08 Jan 2009) | 1 line
run the imports fixers after fix_import, so fix_import doesn't try to make stdlib renames into relative imports #4876
........
2009-01-08 22:13:34 -04:00
|
|
|
raise parse.ParseError("incomplete input",
|
|
|
|
type, value, (prefix, start))
|
2008-03-19 01:43:46 -03:00
|
|
|
return p.rootnode
|
|
|
|
|
|
|
|
def parse_stream_raw(self, stream, debug=False):
|
|
|
|
"""Parse a stream and return the syntax tree."""
|
|
|
|
tokens = tokenize.generate_tokens(stream.readline)
|
|
|
|
return self.parse_tokens(tokens, debug)
|
|
|
|
|
|
|
|
def parse_stream(self, stream, debug=False):
|
|
|
|
"""Parse a stream and return the syntax tree."""
|
|
|
|
return self.parse_stream_raw(stream, debug)
|
|
|
|
|
2009-05-08 22:01:14 -03:00
|
|
|
def parse_file(self, filename, encoding=None, debug=False):
|
2008-03-19 01:43:46 -03:00
|
|
|
"""Parse a file and return the syntax tree."""
|
2009-05-08 22:01:14 -03:00
|
|
|
stream = codecs.open(filename, "r", encoding)
|
2008-03-19 01:43:46 -03:00
|
|
|
try:
|
|
|
|
return self.parse_stream(stream, debug)
|
|
|
|
finally:
|
|
|
|
stream.close()
|
|
|
|
|
|
|
|
def parse_string(self, text, debug=False):
|
|
|
|
"""Parse a string and return the syntax tree."""
|
|
|
|
tokens = tokenize.generate_tokens(generate_lines(text).next)
|
|
|
|
return self.parse_tokens(tokens, debug)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_lines(text):
|
|
|
|
"""Generator that behaves like readline without using StringIO."""
|
|
|
|
for line in text.splitlines(True):
|
|
|
|
yield line
|
|
|
|
while True:
|
|
|
|
yield ""
|
|
|
|
|
|
|
|
|
|
|
|
def load_grammar(gt="Grammar.txt", gp=None,
|
|
|
|
save=True, force=False, logger=None):
|
|
|
|
"""Load the grammar (maybe from a pickle)."""
|
|
|
|
if logger is None:
|
|
|
|
logger = logging.getLogger()
|
|
|
|
if gp is None:
|
|
|
|
head, tail = os.path.splitext(gt)
|
|
|
|
if tail == ".txt":
|
|
|
|
tail = ""
|
|
|
|
gp = head + tail + ".".join(map(str, sys.version_info)) + ".pickle"
|
|
|
|
if force or not _newer(gp, gt):
|
|
|
|
logger.info("Generating grammar tables from %s", gt)
|
|
|
|
g = pgen.generate_grammar(gt)
|
|
|
|
if save:
|
|
|
|
logger.info("Writing grammar tables to %s", gp)
|
2008-05-25 14:19:22 -03:00
|
|
|
try:
|
|
|
|
g.dump(gp)
|
|
|
|
except IOError, e:
|
|
|
|
logger.info("Writing failed:"+str(e))
|
2008-03-19 01:43:46 -03:00
|
|
|
else:
|
|
|
|
g = grammar.Grammar()
|
|
|
|
g.load(gp)
|
|
|
|
return g
|
|
|
|
|
|
|
|
|
|
|
|
def _newer(a, b):
|
|
|
|
"""Inquire whether file a was written since file b."""
|
|
|
|
if not os.path.exists(a):
|
|
|
|
return False
|
|
|
|
if not os.path.exists(b):
|
|
|
|
return True
|
|
|
|
return os.path.getmtime(a) >= os.path.getmtime(b)
|