model_visitor.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. from visitor import Visitor
  2. from compiler import main as do_compile
  3. import os
  4. def empty(s):
  5. return None
  6. class ModelVisitor(Visitor):
  7. def __init__(self, args):
  8. Visitor.__init__(self, args)
  9. self.constructors = []
  10. self.free_id = 0
  11. self.names = set()
  12. self.current_model = None
  13. self.current_element = []
  14. self.includes = []
  15. def dump(self):
  16. return self.constructors
  17. def __getattr__(self, attr):
  18. if attr.startswith("visit_"):
  19. return empty
  20. else:
  21. raise AttributeError()
  22. def visit_start(self, tree):
  23. for t in tree.get_tail():
  24. self.visit(t)
  25. def visit_include_files(self, tree):
  26. self.includes.append('include %s' % tree.get_children("STRVALUE")[0].get_text())
  27. def visit_import(self, tree):
  28. url = tree.get_children("MV_URL")[0]
  29. target = tree.get_children("MODEL_ID")[0]
  30. self.constructors.extend(["import_node", url.get_text(), target.get_text()])
  31. def visit_export(self, tree):
  32. url = tree.get_children("MV_URL")[0]
  33. target = tree.get_children("MODEL_ID")[0]
  34. self.constructors.extend(["export_node", target.get_text(), url.get_text()])
  35. def visit_model(self, tree):
  36. children = tree.get_children("MODEL_ID")
  37. model_type = children[0].get_text()
  38. model_name = children[-1].get_text()
  39. self.constructors.extend(["instantiate_model", model_type, model_name])
  40. self.current_model = model_name
  41. for element in tree.get_children("model_element"):
  42. self.visit(element)
  43. def visit_model_element(self, tree):
  44. children = tree.get_children("MODEL_ID")
  45. element_type = children[0].get_text()
  46. if len(children) == 2 or len(children) == 4:
  47. element_name = children[1].get_text()
  48. else:
  49. element_name = "__%s" % self.free_id
  50. self.free_id += 1
  51. if element_name in self.names:
  52. raise Exception("Redefinition of element %s" % element_name)
  53. if len(children) > 2:
  54. # So we have a source and target; but aren't sure which is which, because the name is optional!
  55. source_name = children[-2].get_text()
  56. target_name = children[-1].get_text()
  57. if source_name not in self.names:
  58. raise Exception("Source of link %s unknown: %s" % (element_name, source_name))
  59. if target_name not in self.names:
  60. raise Exception("Target of link %s unknown: %s" % (element_name, target_name))
  61. self.constructors.extend(["instantiate_link", self.current_model, element_type, element_name, source_name, target_name])
  62. else:
  63. self.constructors.extend(["instantiate_node", self.current_model, element_type, element_name])
  64. self.names.add(element_name)
  65. self.current_element.append(element_name)
  66. if tree.get_children("inheritance"):
  67. self.visit(tree.get_children("inheritance")[0])
  68. for attr in tree.get_children("model_attribute"):
  69. self.visit(attr)
  70. self.current_element.pop()
  71. return element_name
  72. def visit_inheritance(self, tree):
  73. for token in tree.get_children("MODEL_ID"):
  74. superclass = token.get_text()
  75. if superclass not in self.names:
  76. raise Exception("Superclass %s is undefined" % superclass)
  77. self.constructors.extend(["instantiate_link", self.current_model, "Inheritance", "%s_inherits_from_%s" % (self.current_element[-1], superclass), self.current_element[-1], superclass])
  78. self.names.add("%s_inherits_from_%s" % (self.current_element[-1], superclass))
  79. def visit_model_attribute(self, tree):
  80. children = tree.get_children("MODEL_ID")
  81. is_definition = bool(tree.get_children("COLON"))
  82. is_assign = bool(tree.get_children("model_attr_instance"))
  83. is_nested = bool(tree.get_children("model_element"))
  84. if is_definition:
  85. attr_name = children[0].get_text()
  86. attr_optional = len(tree.get_children("OPTIONAL")) > 0
  87. attr_type = children[1].get_text()
  88. self.constructors.extend(["model_define_attribute", self.current_model, self.current_element[-1], attr_name, attr_optional, attr_type])
  89. full_attribute_name = self.current_element[-1] + "_" + attr_name
  90. if is_assign:
  91. # There are also some attributes to set!
  92. self.current_element.append(full_attribute_name)
  93. for f in tree.get_children("model_attr_instance"):
  94. self.visit(f)
  95. self.current_element.pop()
  96. elif is_assign:
  97. self.visit(tree.get_children("model_attr_instance")[0])
  98. elif is_nested:
  99. if tree.get_children("MODEL_ID"):
  100. contains_link = tree.get_children("MODEL_ID")[0].get_text()
  101. else:
  102. contains_link = ""
  103. entry = self.visit(tree.get_children("model_element")[0])
  104. self.constructors.extend(["instantiate_link", self.current_model, contains_link, "__%s" % self.free_id, self.current_element[-1], entry])
  105. self.names.add("__%s" % self.free_id)
  106. self.free_id += 1
  107. def visit_model_attr_instance(self, tree):
  108. def constructors_compile(code):
  109. code_fragments = code.split("\n")
  110. code_fragments = [i for i in code_fragments if i.strip() != ""]
  111. code_fragments = [i.replace(" ", "\t") for i in code_fragments]
  112. initial_tabs = min([len(i) - len(i.lstrip("\t")) for i in code_fragments])
  113. code_fragments = self.includes + [i[initial_tabs:] for i in code_fragments]
  114. code = "\n".join(code_fragments)
  115. code += "\n"
  116. with open(".code.alc", 'w') as f:
  117. f.write(code)
  118. f.flush()
  119. directory = os.path.realpath(__file__).rsplit(os.sep, 1)[0]
  120. compiled = do_compile(".code.alc", directory + "/../grammars/actionlanguage.g", "CS")
  121. return compiled
  122. children = tree.get_children("MODEL_ID")
  123. attr_name = children[0].get_text()
  124. if tree.get_children("value"):
  125. # Value attribute
  126. attr_value = tree.get_children("value")[0].get_tail()[0]
  127. if attr_value.head == "STRVALUE":
  128. attr_value = attr_value.get_text()[1:-1]
  129. elif attr_value.head == "TRUE":
  130. attr_value = True
  131. elif attr_value.head == "FALSE":
  132. attr_value = False
  133. elif attr_value.head == "DEC_NUMBER":
  134. attr_value = int(attr_value.get_text())
  135. elif attr_value.head == "FLOAT_NUMBER":
  136. attr_value = float(attr_value.get_text())
  137. else:
  138. raise Exception(attr_value.head)
  139. self.constructors.extend(["instantiate_attribute", self.current_model, self.current_element[-1], attr_name, attr_value])
  140. elif tree.get_children("DOLLAR"):
  141. # Coded attribute
  142. self.constructors.extend(["instantiate_attribute_code", self.current_model, self.current_element[-1], attr_name])
  143. self.constructors.extend(constructors_compile(tree.get_children("ANYTHING_EXCEPT_DOLLAR")[0].get_text()))