Esempio n. 1
0
def main(codegen=pythoncg):
    args = parse_args()

    if args.whitespace:
        args.whitespace = eval_escapes(args.whitespace)

    outfile = args.outfile
    prepare_for_output(outfile)
    prepare_for_output(args.object_model_outfile)

    grammar = codecs.open(args.filename, encoding='utf-8').read()

    try:
        model = compile(grammar,
                        args.name,
                        trace=args.trace,
                        filename=args.filename,
                        colorize=args.color)
        model.whitespace = args.whitespace
        model.nameguard = args.nameguard
        model.left_recursion = args.left_recursion

        if args.draw:
            from tatsu import diagrams
            diagrams.draw(outfile, model)
        else:
            if args.pretty:
                result = model.pretty()
            elif args.pretty_lean:
                result = model.pretty_lean()
            elif args.object_model:
                result = objectmodel.codegen(model, base_type=args.base_type)
            else:
                result = codegen(model)

            if outfile:
                save(outfile, result)
            else:
                print(result)

        # if requested, always save it
        if args.object_model_outfile:
            save(args.object_model_outfile,
                 objectmodel.codegen(model, base_type=args.base_type))

        print('-' * 72, file=sys.stderr)
        print('{:12,d}  lines in grammar'.format(len(grammar.split())),
              file=sys.stderr)
        print('{:12,d}  rules in grammar'.format(len(model.rules)),
              file=sys.stderr)  # noqa
        print('{:12,d}  nodes in AST'.format(model.nodecount()),
              file=sys.stderr)
    except ParseException as e:
        print(e, file=sys.stderr)
        sys.exit(1)
Esempio n. 2
0
def main(codegen=pythoncg):
    args = parse_args()

    if args.whitespace:
        args.whitespace = eval_escapes(args.whitespace)

    outfile = args.outfile
    prepare_for_output(outfile)
    prepare_for_output(args.object_model_outfile)

    grammar = codecs.open(args.filename, 'r', encoding='utf-8').read()

    try:
        model = compile(
            grammar,
            args.name,
            trace=args.trace,
            filename=args.filename,
            colorize=args.color
        )
        model.whitespace = args.whitespace
        model.nameguard = args.nameguard
        model.left_recursion = args.left_recursion

        if args.draw:
            from tatsu import diagrams
            diagrams.draw(outfile, model)
        else:
            if args.pretty:
                result = model.pretty()
            elif args.pretty_lean:
                result = model.pretty_lean()
            elif args.object_model:
                result = objectmodel.codegen(model)
            else:
                result = codegen(model)

            if outfile:
                save(outfile, result)
            else:
                print(result)

        # if requested, always save it
        if args.object_model_outfile:
            save(args.object_model_outfile, objectmodel.codegen(model))

        print('-' * 72, file=sys.stderr)
        print('{:12,d}  lines in grammar'.format(len(grammar.split())), file=sys.stderr)
        print('{:12,d}  rules in grammar'.format(len(model.rules)), file=sys.stderr)  # noqa
        print('{:12,d}  nodes in AST'.format(model.nodecount()), file=sys.stderr)
    except ParseException as e:
        print(e, file=sys.stderr)
        sys.exit(1)
Esempio n. 3
0
def to_python_model(grammar,
                    name=None,
                    filename=None,
                    base_type=None,
                    **kwargs):
    model = compile(grammar, name=name, filename=filename, **kwargs)
    return objectmodel.codegen(model, base_type=base_type)
Esempio n. 4
0
def to_python_model(grammar,
                    name=None,
                    filename=None,
                    base_type=None,
                    config: ParserConfig = None,
                    **settings):
    model = compile(grammar,
                    name=name,
                    filename=filename,
                    config=config,
                    **settings)
    return objectmodel.codegen(model, base_type=base_type)
Esempio n. 5
0
def to_python_model(grammar, name=None, filename=None, **kwargs):
    model = compile(grammar, name=name, filename=filename, **kwargs)
    return objectmodel.codegen(model)