File: generate_parser

package info (click to toggle)
python-parsley 1.3-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: python: 9,897; makefile: 127
file content (33 lines) | stat: -rwxr-xr-x 939 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python
# -*- mode: python -*-

import sys
from ometa.runtime import ParseError
from ometa.grammar import OMeta
from ometa.builder import writePython
from ometa.vm_builder import writeBytecode, bytecodeToPython

if len(sys.argv) != 3:
 print "Usage: generate_grammar grammar-filename python-filename"
 sys.exit(1)


def findObject(name):
    classSplit = name.split('.')
    modname = '.'.join(classSplit[:-1])
    topLevel = __import__(modname)
    packages = modname.split(".")[1:]
    module = topLevel
    for p in packages:
        module = getattr(module, p)
    return getattr(module, classSplit[-1])

grammar = findObject(sys.argv[1])
pythonFile =  open(sys.argv[2], 'w')
g = OMeta(grammar)
tree = g.parseGrammar("Parser")
# source = writePython(tree)
bytecode = writeBytecode(tree)
source = bytecodeToPython(bytecode)
pythonFile.write("from ometa.runtime import OMetaBase as GrammarBase\n")
pythonFile.write(source)