from __future__ import absolute_import, division, print_function, unicode_literals
import sys, pythonparser.source, pythonparser.lexer, pythonparser.parser, pythonparser.diagnostic

def parse(source, filename='<unknown>', mode='exec',
          flags=[], version=None, engine=None):
    """
    Parse a string into an abstract syntax tree.
    This is the replacement for the built-in :meth:`..ast.parse`.

    :param source: (string) Source code in the correct encoding
    :param filename: (string) Filename of the source (used in diagnostics)
    :param mode: (string) 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: (list of string) Future flags.
        Equivalent to ``from __future__ import <flags>``.
    :param version: (2-tuple of int) Major and minor version of Python
        syntax to recognize, ``sys.version_info[0:2]`` by default.
    :param engine: (:class:`diagnostic.Engine`) Diagnostic engine,
        a fresh one is created by default
    :return: (:class:`ast.AST`) Abstract syntax tree
    :raise: :class:`diagnostic.Error`
        if the source code is not well-formed
    """
    if version is None:
        version = sys.version_info[0:2]

    if engine is None:
        engine = pythonparser.diagnostic.Engine()

    buffer = pythonparser.source.Buffer(source, filename)

    lexer = pythonparser.lexer.Lexer(buffer, version, engine)
    if mode in ('single', 'eval'):
        lexer.interactive = True

    parser = pythonparser.parser.Parser(lexer, version, engine)
    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()

@@ -175,16 +175,747 @@
 nostic%0A%0A
+def parse_buffer(buffer, mode='exec', flags=%5B%5D, version=None, engine=None):%0A    %22%22%22%0A    Like :meth:%60parse%60, but accepts a :class:%60source.Buffer%60 instead of%0A    source and filename.%22%22%22%0A%0A    if version is None:%0A        version = sys.version_info%5B0:2%5D%0A%0A    if engine is None:%0A        engine = pythonparser.diagnostic.Engine()%0A%0A    lexer = pythonparser.lexer.Lexer(buffer, version, engine)%0A    if mode in ('single', 'eval'):%0A        lexer.interactive = True%0A%0A    parser = pythonparser.parser.Parser(lexer, version, engine)%0A    parser.add_flags(flags)%0A%0A    if mode == 'exec':%0A        return parser.file_input()%0A    elif mode == 'single':%0A        return parser.single_input()%0A    elif mode == 'eval':%0A        return parser.eval_input()%0A%0A
 def pars
@@ -2083,156 +2083,28 @@
     
-if version is None:%0A        version = sys.version_info%5B0:2%5D%0A%0A    if engine is None:%0A        engine = pythonparser.diagnostic.Engine()%0A%0A    
+return parse_
 buffer
- = 
+(
 pyth
@@ -2147,412 +2147,61 @@
 ame)
-%0A%0A    lexer = pythonparser.lexer.Lexer(buffer, version, engine)%0A    if mode in ('single', 'eval'):%0A        lexer.interactive = True%0A%0A    parser = pythonparser.parser.Parser(lexer, version, engine)%0A    parser.add_flags(flags)%0A%0A    if mode == 'exec':%0A        return parser.file_input()%0A    elif mode == 'single':%0A        return parser.single_input()%0A    elif mode == 'eval':%0A        return parser.eval_input()
+,%0A                        mode, flags, version, engine)%0A
 %0A

Add pythonparser.parse_buffer.