/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

  1. #!/usr/bin/env python
  2. import os
  3. from subprocess import call
  4. if not os.path.exists('antlr-3.1.jar'):
  5. #subprocess.
  6. call(['wget', 'http://www.antlr.org/download/antlr-3.1.jar'])
  7. os.environ['CLASSPATH'] = './antlr-3.1.jar:%s' %os.environ.get('CLASSPATH', '')
  8. print "comping grammars..."
  9. lst = os.listdir('grammars')[:]
  10. lst.sort()
  11. #lst = ["grammars/%s" %x for x in lst if x[-2:] == '.g']
  12. for x in lst:
  13. if (x[-2:] == ".g" and x[-4:] != "__.g") or x == '__init__.py' or x == "." or x == "..":
  14. continue
  15. print "delete grammars/%s" %x
  16. os.unlink('grammars/%s' %x)
  17. import re
  18. IM = re.compile('pida-(\w+)\: (.*)\n')
  19. GRAMPATH = os.path.abspath('grammars')
  20. mapping = {}
  21. grammars = {}
  22. ginfo = open('_grammars.py', 'w')
  23. ginfo.write("""
  24. # !!! AUTOGENERATED. DO NOT ALTER !!!
  25. TYPES = """)
  26. for fn in lst:
  27. if fn[-2:] != '.g':
  28. continue
  29. if fn[-4:] == '__.g':
  30. continue
  31. print "-"*10, " %s " %fn, "-"*10
  32. txt = open("grammars/%s" %fn).read()
  33. txt = txt.replace("\r\n", "\n")
  34. info = IM.findall(txt)
  35. rv = call(['java', 'org.antlr.Tool', "-Xconversiontimeout", "15000",
  36. "-fo", GRAMPATH, "grammars/%s" %fn])
  37. #rv = 0
  38. if info:
  39. #'Perl': ('Perl', ('perl', 'pl'), ('*.pl', '*.pm'), ('text/x-perl', 'application/x-perl')),
  40. print info
  41. d = dict(info)
  42. print d
  43. if not d.has_key('internal'):
  44. continue
  45. inter = d['internal']
  46. inter.strip()
  47. mapping[inter] = fn[:-2]
  48. grammars[inter] = (d.get('human', inter).strip(),
  49. tuple([x.strip() for x in d.get('alias', '').split(' ') if x]),
  50. tuple([x.strip() for x in d.get('glob', '').split(' ') if x]),
  51. tuple([x.strip() for x in d.get('mime', '').split(' ') if x])
  52. )
  53. ginfo.write(repr(grammars))
  54. ginfo.write("\n")
  55. ginfo.write("MAPPING = " + repr(mapping) + "\n")
  56. ginfo.close()
  57. print rv