# -*- coding: utf-8 -*-
"""
Parse and translate an EBNF grammar into a Python parser for
the described language.
"""
from __future__ import print_function, division, absolute_import, unicode_literals
import sys
import os
import argparse
from .buffering import Buffer
from .parsing import Parser
from .bootstrap import GrakoGrammarGenerator
from .exceptions import GrakoException

DESCRIPTION = ('Grako (for grammar compiler) takes grammars'
               ' in a variation of EBNF as input, and outputs a memoizing'
               ' PEG parser in Python.'
               )
argparser = argparse.ArgumentParser(prog='grako',
                                    description=DESCRIPTION
                                    )

argparser.add_argument('filename',
                       metavar='grammar',
                       help='The filename of the grammar to generate a parser for'
                       )
argparser.add_argument('-m', '--name',
                       nargs=1,
                       metavar='name',
                       help="An optional name for the grammar. It defaults to the basename of the grammar file's name"
                       )
argparser.add_argument('-o', '--outfile',
                       metavar='outfile',
                       help='specify where the output should go (default is stdout)'
                       )
argparser.add_argument('-t', '--trace',
                       help='produce verbose parsing output',
                       action='store_true'
                       )


def main():
    try:
        args = argparser.parse_args()
    except Exception as e:
        print(e)
        sys.exit(1)
    filename = args.filename
    outfile = args.outfile
    name = args.name
    if name is None:
        name = os.path.splitext(os.path.basename(filename))[0]
    if outfile and os.path.isfile(outfile):
        os.unlink(outfile)
    text = open(filename, 'r').read()
    try:
        parser = GrakoGrammarGenerator(name, text, trace=args.trace)
        grammar = parser.parse()
        text = grammar.render()
        if outfile:
            open(outfile, 'w').write(text)
        else:
            print(text)
    except GrakoException as e:
        print(e)

if __name__ == '__main__':
    main()

@@ -2076,16 +2076,143 @@
 utfile:%0A
+            dirname = os.path.dirname(outfile)%0A            if not os.path.isdir(dirname):%0A                os.makedirs(dirname)%0A
         

Create the outfile directory if it doesn't exist.