Python lib2to3.pytree.convert() Examples
The following are 2
code examples of lib2to3.pytree.convert().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
lib2to3.pytree
, or try the search function
.
Example #1
Source File: __init__.py From retype with MIT License | 6 votes |
def lib2to3_parse(src_txt): """Given a string with source, return the lib2to3 Node.""" grammar = pygram.python_grammar_no_print_statement drv = driver.Driver(grammar, pytree.convert) if src_txt[-1] != "\n": nl = "\r\n" if "\r\n" in src_txt[:1024] else "\n" src_txt += nl try: result = drv.parse_string(src_txt, True) except ParseError as pe: lineno, column = pe.context[1] lines = src_txt.splitlines() try: faulty_line = lines[lineno - 1] except IndexError: faulty_line = "<line number missing in source>" raise ValueError(f"Cannot parse: {lineno}:{column}: {faulty_line}") from None if isinstance(result, Leaf): result = Node(syms.file_input, [result]) return result
Example #2
Source File: pytree_utils.py From yapf with Apache License 2.0 | 5 votes |
def ParseCodeToTree(code): """Parse the given code to a lib2to3 pytree. Arguments: code: a string with the code to parse. Raises: SyntaxError if the code is invalid syntax. parse.ParseError if some other parsing failure. Returns: The root node of the parsed tree. """ # This function is tiny, but the incantation for invoking the parser correctly # is sufficiently magical to be worth abstracting away. try: # Try to parse using a Python 3 grammar, which is more permissive (print and # exec are not keywords). parser_driver = driver.Driver(_GRAMMAR_FOR_PY3, convert=pytree.convert) tree = parser_driver.parse_string(code, debug=False) except parse.ParseError: # Now try to parse using a Python 2 grammar; If this fails, then # there's something else wrong with the code. try: parser_driver = driver.Driver(_GRAMMAR_FOR_PY2, convert=pytree.convert) tree = parser_driver.parse_string(code, debug=False) except parse.ParseError: # Raise a syntax error if the code is invalid python syntax. try: ast.parse(code) except SyntaxError as e: raise e else: raise return _WrapEndMarker(tree)