-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
whitequark
committed
May 8, 2015
1 parent
3f61113
commit ab67bb8
Showing
4 changed files
with
67 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
import sys, pyparser.source, pyparser.lexer, pyparser.parser | ||
|
||
def parse(source, filename='<unknown>', mode='exec', | ||
flags=[], version=sys.version_info[0:2]): | ||
""" | ||
Parse a string into an abstract syntax tree. | ||
This is the replacement for the built-in :meth:`..ast.parse`. | ||
:param str source: Source code in the correct encoding | ||
:param filename: Filename of the source (used in diagnostics) | ||
:param mode: Execution mode. Pass ``"exec"`` to parse a module, | ||
``"single"`` to parse a single (interactive) statement, | ||
and ``"eval"`` to parse an expression. In the last two cases, | ||
``source`` must be terminated with an empty line | ||
(i.e. end with ``"\\n\\n"``). | ||
:param flags: Future flags. Equivalent to ``from __future__ import <flags>``. | ||
:param int,int version: A tuple of the major and minor version | ||
of Python syntax to recognize. | ||
:return ast.AST: abstract syntax tree | ||
:raise diagnostic.DiagnosticException: | ||
if the source code is not well-formed | ||
""" | ||
buffer = pyparser.source.Buffer(source, filename) | ||
lexer = pyparser.lexer.Lexer(buffer, version) | ||
parser = pyparser.parser.Parser(lexer) | ||
|
||
parser.add_flags(flags) | ||
|
||
if mode == 'exec': | ||
return parser.file_input() | ||
elif mode == 'single': | ||
return parser.single_input() | ||
elif mode == 'eval': | ||
return parser.eval_input() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from __future__ import absolute_import, division, print_function, unicode_literals | ||
from . import parse, diagnostic | ||
import sys, time, codecs | ||
|
||
for filename in sys.argv[1:]: | ||
with codecs.open(filename, encoding='utf-8') as f: | ||
input = f.read() | ||
try: | ||
start = time.time() | ||
root = parse(input, filename) | ||
interval = time.time() - start | ||
|
||
print(root) | ||
print("elapsed: %.2f (%.2f kb/s)" % (interval, len(input)/interval/1000), | ||
file=sys.stderr) | ||
except diagnostic.DiagnosticException as e: | ||
print("\n".join(e.diagnostic.render()), | ||
file=sys.stderr) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters