Example #1
0
def parse(file):
    encoding = detect_encoding(file)
    file = codecs.lookup(encoding).streamreader(file)
    source = file.read()
    source += u'\n'  # necessary to fix weird parsing error
    features = _detect_future_features(source)
    if u'print_function' in features:
        grammar = pygram.python_grammar_no_print_statement
    else:
        grammar = pygram.python_grammar
    driver = Driver(grammar, convert=pytree.convert)
    return driver.parse_string(source)
Example #2
0
def parse(source):
    """String -> AST

    Parse the string and return its AST representation. May raise
    a ParseError exception.
    """
    # Modified from
    # https://gist.github.com/FZambia/876b724c329e864b6642adc52b577cdb
    drv = Driver(pygram.python_grammar, pytree.convert)
    result = drv.parse_string(source + "\n", True)
    if isinstance(result, Leaf):  # Always return a Node, not a Leaf.
        result = Node(pygram.python_symbols.file_input, [result])
    # Could track whether str() needs to remove the newline, but not worth it.
    return result
Example #3
0
def suite(text):
    d = Driver(g)
    return d.parse_string(text)
Example #4
0
File: parser.py Project: Afey/pyjs
def suite(text):
    d = Driver(g )
    return d.parse_string(text)
Example #5
0
def parse_py(src_txt):
    drv = Driver(grammar, pytree.convert)
    result = drv.parse_string(src_txt, True)
    if isinstance(result, Leaf):
        result = Node(pygram.python_symbols.file_input, [result])
    return result