コード例 #1
0
ファイル: diagram_test.py プロジェクト: brigittebigi/sppas
    def test_dot(self):
        grammar = '''
            start = "foo\\nbar" $;
        '''
        try:
            from grako.diagrams import draw
        except ImportError:
            return

        m = genmodel('Diagram', grammar)
        draw('tmp/diagram.png', m)
コード例 #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 grako 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)
        print('{:12,d}  nodes in AST'.format(model.nodecount()),
              file=sys.stderr)
    except ParseException as e:
        print(e, file=sys.stderr)
        sys.exit(1)
コード例 #3
0
ファイル: diagram_test.py プロジェクト: apalala/grako
    def test_dot(self):
        grammar = '''
            start = "foo\\nbar" $;
        '''
        try:
            from grako.diagrams import draw
        except ImportError:
            return

        m = compile(grammar, 'Diagram')
        draw('tmp/diagram.png', m)
コード例 #4
0
    def test_dot(self):
        grammar = '''
            start = "foo\\nbar" $;
        '''
        try:
            from grako.diagrams import draw
        except ImportError:
            return

        m = genmodel('Diagram', grammar)
        draw('tmp/diagram.png', m)
コード例 #5
0
ファイル: diagram_test.py プロジェクト: waveform80/grako
    def test_dot(self):
        grammar = '''
            start = "foo\\nbar" $;
        '''
        try:
            from grako.diagrams import draw
        except ImportError:
            return

        m = compile(grammar, 'Diagram')
        draw('tmp/diagram.png', m)
コード例 #6
0
ファイル: tool.py プロジェクト: apalala/grako
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 grako 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)
        print('{:12,d}  nodes in AST'.format(model.nodecount()), file=sys.stderr)
    except ParseException as e:
        print(e, file=sys.stderr)
        sys.exit(1)
コード例 #7
0
    def test_bootstrap(self):
        print()

        if os.path.isfile('tmp/00.ast'):
            shutil.rmtree('tmp')
        if not os.path.isdir('tmp'):
            os.mkdir('tmp')
        print('-' * 20, 'phase 00 - parse using the bootstrap grammar')
        with open('etc/grako.ebnf') as f:
            text = str(f.read())
        g = GrakoParser('GrakoBootstrap')
        grammar0 = g.parse(text)
        ast0 = json.dumps(grammar0, indent=2)
        with open('tmp/00.ast', 'w') as f:
            f.write(ast0)

        print('-' * 20, 'phase 01 - parse with parser generator')
        with open('etc/grako.ebnf') as f:
            text = str(f.read())
        g = GrakoGrammarGenerator('GrakoBootstrap')
        g.parse(text, trace=False)

        generated_grammar1 = str(g.ast['grammar'])
        with open('tmp/01.ebnf', 'w') as f:
            f.write(generated_grammar1)

        print('-' * 20, 'phase 02 - parse previous output with the parser generator')
        with open('tmp/01.ebnf', 'r') as f:
            text = str(f.read())
        g = GrakoGrammarGenerator('GrakoBootstrap')
        g.parse(text, trace=False)
        generated_grammar2 = str(g.ast['grammar'])
        with open('tmp/02.ebnf', 'w') as f:
            f.write(generated_grammar2)
        self.assertEqual(generated_grammar2, generated_grammar1)

        print('-' * 20, 'phase 03 - repeat')
        with open('tmp/02.ebnf') as f:
            text = f.read()
        g = GrakoParser('GrakoBootstrap')
        ast3 = g.parse(text)
        with open('tmp/03.ast', 'w') as f:
            f.write(json.dumps(ast3, indent=2))

        print('-' * 20, 'phase 04 - repeat')
        with open('tmp/02.ebnf') as f:
            text = f.read()
        g = GrakoGrammarGenerator('GrakoBootstrap')
        g.parse(text)
        parser = g.ast['grammar']
    #    pprint(parser.first_sets, indent=2, depth=3)
        generated_grammar4 = str(parser)
        with open('tmp/04.ebnf', 'w') as f:
            f.write(generated_grammar4)
        self.assertEqual(generated_grammar4, generated_grammar2)

        print('-' * 20, 'phase 05 - parse using the grammar model')
        with open('tmp/04.ebnf') as f:
            text = f.read()
        ast5 = parser.parse(text)
        with open('tmp/05.ast', 'w') as f:
            f.write(json.dumps(ast5, indent=2))

        print('-' * 20, 'phase 06 - generate parser code')
        gencode6 = codegen(parser)
        with open('tmp/g06.py', 'w') as f:
            f.write(gencode6)

        print('-' * 20, 'phase 07 - import generated code')
        from g06 import GrakoBootstrapParser as GenParser  # @UnresolvedImport

        print('-' * 20, 'phase 08 - compile using generated code')
        parser = GenParser(trace=False)
        result = parser.parse(
            text,
            'grammar',
            comments_re=COMMENTS_RE,
            eol_comments_re=EOL_COMMENTS_RE
        )
        self.assertEqual(result, parser.ast['grammar'])
        ast8 = parser.ast['grammar']
        json8 = json.dumps(ast8, indent=2)
        open('tmp/08.ast', 'w').write(json8)
        self.assertEqual(ast5, ast8)

        print('-' * 20, 'phase 09 - Generate parser with semantics')
        with open('etc/grako.ebnf') as f:
            text = f.read()
        parser = GrakoGrammarGenerator('GrakoBootstrap')
        g9 = parser.parse(text)
        generated_grammar9 = str(g9)
        with open('tmp/09.ebnf', 'w') as f:
            f.write(generated_grammar9)
        self.assertEqual(generated_grammar9, generated_grammar1)

        print('-' * 20, 'phase 10 - Parse with a model using a semantics')
        g10 = g9.parse(
            text,
            start_rule='grammar',
            semantics=GrakoSemantics('GrakoBootstrap')
        )
        generated_grammar10 = str(g10)
        with open('tmp/10.ebnf', 'w') as f:
            f.write(generated_grammar10)
        gencode10 = codegen(g10)
        with open('tmp/g10.py', 'w') as f:
            f.write(gencode10)

        print('-' * 20, 'phase 11 - Pickle the model and try again.')
        with open('tmp/11.grako', 'wb') as f:
            pickle.dump(g10, f, protocol=2)
        with open('tmp/11.grako', 'rb') as f:
            g11 = pickle.load(f)
        r11 = g11.parse(
            text,
            start_rule='grammar',
            semantics=GrakoSemantics('GrakoBootstrap')
        )
        with open('tmp/11.ebnf', 'w') as f:
            f.write(str(g11))
        gencode11 = codegen(r11)
        with open('tmp/g11.py', 'w') as f:
            f.write(gencode11)

        print('-' * 20, 'phase 12 - Walker')

        class PrintNameWalker(DepthFirstWalker):
            def __init__(self):
                self.walked = []

            def walk_default(self, o, children):
                self.walked.append(o.__class__.__name__)

        v = PrintNameWalker()
        v.walk(g11)
        with open('tmp/12.txt', 'w') as f:
            f.write('\n'.join(v.walked))

        print('-' * 20, 'phase 13 - Graphics')
        try:
            from grako.diagrams import draw
        except ImportError:
            print('PyGraphViz not found!')
        else:
            draw('tmp/13.png', g11)
コード例 #8
0
ファイル: tool.py プロジェクト: brigittebigi/sppas
def main(codegen=pythoncg, outer_version=''):
    argparser.add_argument(
        '-v', '--version',
        help='provide version information and exit',
        action='version',
        version=outer_version + __version__
    )
    try:
        args = argparser.parse_args()
    except Exception as e:
        _error(str(e))
        sys.exit(2)

    binary = args.binary
    filename = args.filename
    name = args.name
    nameguard = args.nameguard
    draw = args.draw
    outfile = args.output
    pretty = args.pretty
    trace = args.trace
    whitespace = args.whitespace
    left_recursion = args.left_recursion

    if whitespace:
        whitespace = eval_escapes(args.whitespace)

    if binary and not outfile:
        _error('--binary requires --outfile')
        sys.exit(2)

    if draw and not outfile:
        _error('--draw requires --outfile')
        sys.exit(2)

    if sum((binary, draw, pretty)) > 1:
        _error('either --binary or --draw or --pretty')
        sys.exit(2)

    if name is None:
        name = os.path.splitext(os.path.basename(filename))[0]

    if outfile and os.path.isfile(outfile):
        os.unlink(outfile)

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

    if outfile:
        dirname = os.path.dirname(outfile)
        if dirname and not os.path.isdir(dirname):
            os.makedirs(dirname)

    try:
        model = genmodel(name, grammar, trace=trace, filename=filename)
        model.whitespace = whitespace
        model.nameguard = False if not nameguard else None  # None allows grammar specified or the default of True
        model.left_recursion = left_recursion

        if binary:
            result = pickle.dumps(model, protocol=2)
        elif pretty:
            result = str(model)
        else:
            result = codegen(model)

        if draw:
            from grako import diagrams
            diagrams.draw(outfile, model)
        elif outfile:
            with codecs.open(outfile, 'w', encoding='utf-8') as f:
                f.write(result)
        else:
            print(result)

        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)
        print('{:12,d}  nodes in AST'.format(model.nodecount()), file=sys.stderr)
    except GrakoException as e:
        _error(e)
        sys.exit(1)
コード例 #9
0
ファイル: tool.py プロジェクト: vmuriart/grako
def main(codegen=pythoncg, outer_version=''):
    argparser.add_argument('-v',
                           '--version',
                           help='provide version information and exit',
                           action='version',
                           version=outer_version + __version__)
    try:
        args = argparser.parse_args()
    except Exception as e:
        _error(str(e))
        sys.exit(2)

    colorize = args.color
    filename = args.filename
    name = args.name
    nameguard = args.nameguard
    draw = args.draw
    outfile = args.output
    pretty = args.pretty
    object_model = args.object_model
    trace = args.trace
    whitespace = args.whitespace
    left_recursion = args.left_recursion

    if whitespace:
        whitespace = eval_escapes(args.whitespace)

    if draw and not outfile:
        _error('--draw requires --outfile')
        sys.exit(2)

    if sum([draw, pretty, object_model]) > 1:
        _error('only one of --draw, --pretty, --object_model allowed')
        sys.exit(2)

    # if name is None:
    #    name = os.path.splitext(os.path.basename(filename))[0]

    if outfile and os.path.isfile(outfile):
        os.unlink(outfile)

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

    if outfile:
        dirname = os.path.dirname(outfile)
        if dirname and not os.path.isdir(dirname):
            os.makedirs(dirname)

    try:
        model = genmodel(name,
                         grammar,
                         trace=trace,
                         filename=filename,
                         colorize=colorize)
        model.whitespace = whitespace
        model.nameguard = False if not nameguard else None  # None allows grammar specified or the default of True
        model.left_recursion = left_recursion

        if pretty:
            result = str(model)
        elif object_model:
            result = objectmodel.codegen(model)
        else:
            result = codegen(model)

        if draw:
            from grako import diagrams
            diagrams.draw(outfile, model)
        elif outfile:
            with codecs.open(outfile, 'w', encoding='utf-8') as f:
                f.write(result)
        else:
            print(result)

        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)
        print('{:12,d}  nodes in AST'.format(model.nodecount()),
              file=sys.stderr)
    except GrakoException as e:
        _error(e)
        sys.exit(1)
コード例 #10
0
def main():
    if not os.path.isdir('tmp'):
        os.mkdir('tmp')
    print('-' * 20, 'phase 00 - parse using the bootstrap grammar')
    text = open('etc/grako.ebnf').read()
    g = GrakoParser('Grako', parseinfo=False)
    g.parse(text)
    open('tmp/00.ebnf', 'w').write(text)

    print('-' * 20, 'phase 01 - parse with parser generator')
    text = open('tmp/00.ebnf').read()
    g = GrakoGrammarGenerator('Grako', parseinfo=False)
    g.parse(text)
    generated_grammar1 = str(g.ast['grammar'])
    open('tmp/01.ebnf', 'w').write(generated_grammar1)

    print('-' * 20, 'phase 02 - parse previous output with the parser generator')
    text = open('tmp/01.ebnf').read()
    g = GrakoGrammarGenerator('Grako')
    g.parse(text)
    generated_grammar2 = str(g.ast['grammar'])
    open('tmp/02.ebnf', 'w').write(generated_grammar2)
    assert generated_grammar2 == generated_grammar1

    print('-' * 20, 'phase 03 - repeat')
    text = open('tmp/02.ebnf').read()
    g = GrakoParser('Grako', parseinfo=False)
    ast3 = g.parse(text)
    open('tmp/03.ast', 'w').write(json.dumps(ast3, indent=2))

    print('-' * 20, 'phase 04 - repeat')
    text = open('tmp/02.ebnf').read()
    g = GrakoGrammarGenerator('Grako')
    g.parse(text)
    parser = g.ast['grammar']
#    pprint(parser.first_sets, indent=2, depth=3)
    generated_grammar4 = str(parser)
    open('tmp/04.ebnf', 'w').write(generated_grammar4)
    assert generated_grammar4 == generated_grammar2

    print('-' * 20, 'phase 05 - parse using the grammar model')
    text = open('tmp/04.ebnf').read()
    ast5 = parser.parse(text)
    open('tmp/05.ast', 'w').write(json.dumps(ast5, indent=2))

    print('-' * 20, 'phase 06 - generate parser code')
    gencode6 = parser.codegen()
    open('tmp/g06.py', 'w').write(gencode6)

    print('-' * 20, 'phase 07 - import generated code')
    from g06 import GrakoParser as GenParser  # @UnresolvedImport

    print('-' * 20, 'phase 08 - compile using generated code')
    parser = GenParser(trace=False)
    result = parser.parse(text, 'grammar')
    assert result == parser.ast['grammar']
    ast8 = parser.ast['grammar']
    json8 = json.dumps(ast8, indent=2)
    open('tmp/08.ast', 'w').write(json8)
    assert ast5 == ast8
#    assert json8 == open('etc/check.js').read()

    print('-' * 20, 'phase 09 - Generate parser with semantics')
    text = open('etc/grako.ebnf').read()
    parser = GrakoGrammarGenerator('Grako')
    g9 = parser.parse(text)
    generated_grammar9 = str(g9)
    open('tmp/09.ebnf', 'w').write(generated_grammar9)
    assert generated_grammar9 == generated_grammar1

    print('-' * 20, 'phase 10 - Parse with a model using a semantics')
    g10 = g9.parse(text,
                   start_rule='grammar',
                   semantics=GrakoSemantics('Grako'),
                   comments_re=COMMENTS_RE
                   )
    generated_grammar10 = str(g10)
    open('tmp/10.ebnf', 'w').write(generated_grammar10)
    gencode10 = g10.codegen()
    open('tmp/g10.py', 'w').write(gencode10)

    print('-' * 20, 'phase 11 - Pickle the model and try again.')
    with open('tmp/11.grako', 'wb') as f:
        pickle.dump(g10, f, protocol=2)
    with open('tmp/11.grako', 'rb') as f:
        g11 = pickle.load(f)
    r11 = g11.parse(text,
                    start_rule='grammar',
                    semantics=GrakoSemantics('Grako'),
                    comments_re=COMMENTS_RE
                    )
    open('tmp/11.ebnf', 'w').write(str(110))
    gencode11 = r11.codegen()
    open('tmp/g11.py', 'w').write(gencode11)

    print('-' * 20, 'phase 12 - Visitor')

    class PrintNameVisitor(NodeVisitor):
        def __init__(self):
            self.visited = []

        def visit(self, o):
            self.visited.append(o.__class__.__name__)
            super(PrintNameVisitor, self).visit(o)

    v = PrintNameVisitor()
    g11.accept(v)
    open('tmp/12.txt', 'w').write('\n'.join(v.visited))

    print('-' * 20, 'phase 13 - Graphics')
    try:
        from grako.diagrams import draw
    except:
        print('PyGraphViz not found!')
    else:
        draw('tmp/13.png', g11)