/antlr/compile.py
https://bitbucket.org/poelzi/pida-poelzi-antlr · Python · 71 lines · 52 code · 14 blank · 5 comment · 18 complexity · a436bce268a82409d6bf291558acd781 MD5 · raw file
- #!/usr/bin/env python
- import os
- from subprocess import call
- if not os.path.exists('antlr-3.1.jar'):
- #subprocess.
- call(['wget', 'http://www.antlr.org/download/antlr-3.1.jar'])
- os.environ['CLASSPATH'] = './antlr-3.1.jar:%s' %os.environ.get('CLASSPATH', '')
- print "comping grammars..."
- lst = os.listdir('grammars')[:]
- lst.sort()
- #lst = ["grammars/%s" %x for x in lst if x[-2:] == '.g']
- for x in lst:
- if (x[-2:] == ".g" and x[-4:] != "__.g") or x == '__init__.py' or x == "." or x == "..":
- continue
- print "delete grammars/%s" %x
- os.unlink('grammars/%s' %x)
- import re
- IM = re.compile('pida-(\w+)\: (.*)\n')
- GRAMPATH = os.path.abspath('grammars')
- mapping = {}
- grammars = {}
- ginfo = open('_grammars.py', 'w')
- ginfo.write("""
- # !!! AUTOGENERATED. DO NOT ALTER !!!
- TYPES = """)
- for fn in lst:
- if fn[-2:] != '.g':
- continue
- if fn[-4:] == '__.g':
- continue
- print "-"*10, " %s " %fn, "-"*10
- txt = open("grammars/%s" %fn).read()
- txt = txt.replace("\r\n", "\n")
- info = IM.findall(txt)
- rv = call(['java', 'org.antlr.Tool', "-Xconversiontimeout", "15000",
- "-fo", GRAMPATH, "grammars/%s" %fn])
- #rv = 0
- if info:
- #'Perl': ('Perl', ('perl', 'pl'), ('*.pl', '*.pm'), ('text/x-perl', 'application/x-perl')),
- print info
- d = dict(info)
- print d
- if not d.has_key('internal'):
- continue
- inter = d['internal']
- inter.strip()
- mapping[inter] = fn[:-2]
- grammars[inter] = (d.get('human', inter).strip(),
- tuple([x.strip() for x in d.get('alias', '').split(' ') if x]),
- tuple([x.strip() for x in d.get('glob', '').split(' ') if x]),
- tuple([x.strip() for x in d.get('mime', '').split(' ') if x])
- )
- ginfo.write(repr(grammars))
- ginfo.write("\n")
- ginfo.write("MAPPING = " + repr(mapping) + "\n")
- ginfo.close()
- print rv