compiler.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. try:
  2. import cPickle as pickle
  3. except ImportError:
  4. import pickle as pickle
  5. import os
  6. import sys
  7. import time
  8. from hutn_compiler.grammar_compiler_visitor import GrammarCompilerVisitor
  9. from hutn_compiler.hutnparser import Parser, Tree
  10. from hutn_compiler.meta_grammar import Grammar
  11. from hutn_compiler.cached_exception import CachedException
  12. import hashlib
  13. global parsers
  14. parsers = {}
  15. sys.setrecursionlimit(200000)
  16. def read(filename):
  17. with open(filename, 'r') as f:
  18. return f.read()
  19. def md5digest(data):
  20. hasher = hashlib.md5()
  21. try:
  22. data = data.encode('utf-8')
  23. except UnicodeDecodeError:
  24. pass
  25. except AttributeError:
  26. pass
  27. hasher.update(data)
  28. return hasher.hexdigest()
  29. def fetch_cached(data):
  30. try:
  31. md5 = md5digest(data)
  32. cache_folder = os.path.abspath("%s/../caches/" % (os.path.dirname(os.path.abspath(__file__))))
  33. picklefile = cache_folder + "/%s.pickle" % md5
  34. with open(picklefile, "rb") as f:
  35. return pickle.load(f)
  36. except:
  37. return None
  38. def make_cached(original_data, data, mode=None):
  39. md5 = md5digest(original_data)
  40. cache_folder = os.path.abspath("%s/../caches/" % (os.path.dirname(os.path.abspath(__file__))))
  41. if mode is None:
  42. picklefile = cache_folder + "/%s.pickle" % md5
  43. else:
  44. picklefile = cache_folder + "/%s_%s.pickle" % (mode, md5)
  45. with open(picklefile, "wb") as f:
  46. pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
  47. def do_parse(inputfile, grammarfile):
  48. if grammarfile not in parsers:
  49. grammar = fetch_cached(read(grammarfile))
  50. if grammar is None:
  51. result = parser = Parser(Grammar(), hide_implicit = True).parse(read(grammarfile))
  52. if result['status'] != Parser.Constants.Success:
  53. print('not a valid grammar!')
  54. print(result)
  55. tree = result['tree']
  56. visitor = GrammarCompilerVisitor()
  57. structure = visitor.visit(tree)
  58. grammar = Grammar()
  59. grammar.rules = structure['rules']
  60. grammar.tokens = structure['tokens']
  61. make_cached(read(grammarfile), grammar)
  62. parsers[grammarfile] = grammar
  63. else:
  64. grammar = parsers[grammarfile]
  65. result = fetch_cached(read(inputfile))
  66. if result is None:
  67. result = Parser(grammar, line_position = True).parse(read(inputfile))
  68. if result['status'] != Parser.Constants.Success:
  69. lines = open(inputfile, 'r').readlines()
  70. begin_line = max(result["line"] - 3, 0)
  71. end_line = min(result["line"] + 3, len(lines))
  72. lines = lines[begin_line:end_line]
  73. lines = ["%s: %s" % (begin_line + i + 1, line) for i, line in enumerate(lines)]
  74. lines = "".join(lines)
  75. msg = "%s:%s:%s: %s\nContext:\n%s" % (inputfile, result["line"], result["column"], result["text"], lines)
  76. raise Exception(msg)
  77. make_cached(read(inputfile), result)
  78. return result
  79. def find_file(filename, include_paths):
  80. import os.path
  81. include_paths = ["."] + \
  82. [os.path.abspath(os.path.dirname(working_file))] + \
  83. [os.path.abspath("%s/../includes/" % (os.path.dirname(os.path.abspath(__file__))))] + \
  84. include_paths + \
  85. []
  86. attempts = []
  87. for include in include_paths:
  88. testfile = include + os.sep + filename
  89. if os.path.isfile(os.path.abspath(testfile)):
  90. return os.path.abspath(testfile)
  91. else:
  92. attempts.append(os.path.abspath(testfile))
  93. else:
  94. raise Exception("Could not resolve file %s. Tried: %s" % (filename, attempts))
  95. def do_compile(inputfile, grammarfile, visitors=[], include_paths = [], mode=""):
  96. import os.path
  97. global working_file
  98. working_file = os.path.abspath(inputfile)
  99. result = do_parse(inputfile, grammarfile)
  100. error = result["status"] != Parser.Constants.Success
  101. if error:
  102. lines = open(working_file, 'r').readlines()
  103. begin_line = max(result["line"] - 3, 0)
  104. end_line = max(result["line"] + 3, len(lines))
  105. lines = lines[begin_line:end_line]
  106. lines = ["%s: %s" % (begin_line + i + 1, line) for i, line in enumerate(lines)]
  107. lines = "".join(lines)
  108. msg = "%s:%s:%s: %s\nContext:\n%s" % (inputfile, result["line"], result["column"], result["text"], lines)
  109. raise Exception(msg)
  110. else:
  111. for child in result["tree"]['tail']:
  112. child['inputfile'] = inputfile
  113. included = set()
  114. while True:
  115. for i, v in enumerate(result["tree"]['tail']):
  116. if v['head'] == "include":
  117. # Expand this node
  118. for j in v['tail']:
  119. if j['head'] == "STRVALUE":
  120. f = str(j['tail'][0])[1:-1]
  121. if f in included:
  122. subtree = []
  123. else:
  124. name = str(j['tail'][0])[1:-1]
  125. subtree = do_parse(find_file(name, include_paths), grammarfile)["tree"]['tail']
  126. if subtree is None:
  127. raise Exception("Parsing error for included file %s" % find_file(name, include_paths))
  128. for t in subtree:
  129. t['inputfile'] = name
  130. included.add(f)
  131. # Found the string value, so break from the inner for ("searching for element")
  132. break
  133. # Merge all nodes in
  134. before = result["tree"]['tail'][:i]
  135. after = result["tree"]['tail'][i+1:]
  136. result["tree"]['tail'] = before + subtree + after
  137. # Found an include node, but to prevent corruption of the tree, we need to start over again, so break from the outer for loop
  138. break
  139. else:
  140. # The outer for finally finished, so there were no includes remaining, thus terminate the infinite while loop
  141. break
  142. Tree.fix_tracability(result['tree'], inputfile)
  143. for visitor in visitors:
  144. visitor.visit(result["tree"])
  145. if visitors:
  146. result = visitors[-1].dump()
  147. return result
  148. def main(input_file, grammar_file, mode, args=[]):
  149. from hutn_compiler.prettyprint_visitor import PrettyPrintVisitor
  150. from hutn_compiler.prettyprint_visitor import PrintVisitor
  151. from hutn_compiler.semantics_visitor import SemanticsVisitor
  152. from hutn_compiler.bootstrap_visitor import BootstrapVisitor
  153. from hutn_compiler.constructors_visitor import ConstructorsVisitor
  154. from hutn_compiler.model_visitor import ModelVisitor
  155. from hutn_compiler.model_bootstrap_visitor import ModelBootstrapVisitor
  156. modes = {
  157. "N" : [],
  158. "P" : [PrintVisitor],
  159. "PP" : [PrettyPrintVisitor],
  160. "BS" : [SemanticsVisitor, BootstrapVisitor],
  161. "CS" : [SemanticsVisitor, ConstructorsVisitor],
  162. "M" : [ModelVisitor],
  163. "MB" : [ModelBootstrapVisitor],
  164. }
  165. try:
  166. visitors = [v(args) for v in modes[mode]]
  167. result = do_compile(input_file, grammar_file, visitors, mode=mode)
  168. except CachedException:
  169. return True
  170. return result
  171. if __name__ == "__main__":
  172. if len(sys.argv) <= 2:
  173. print("Invocation: ")
  174. print(" %s input_file grammar_file mode [mode_params]*" % sys.argv[0])
  175. sys.exit(1)
  176. else:
  177. value = main(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4:])
  178. if value is not None:
  179. print(value)