Browse Source

Added files for a very minimal metadepth-like front-end

Yentl Van Tendeloo 9 years ago
parent
commit
a7c1be419f

+ 44 - 0
interface/HUTN/grammars/modelling.g

@@ -0,0 +1,44 @@
+grammar{
+    start: (import | model | NEWLINE)+;
+
+    import: IMPORT MV_URL AS MODEL_ID;
+
+    model: MODEL_ID MODEL_ID NEWLINE? LCURLY NEWLINE? model_element* NEWLINE? RCURLY;
+
+    model_element: MODEL_ID MODEL_ID inheritance? (LPAR MODEL_ID COMMA MODEL_ID RPAR)? NEWLINE? LCURLY NEWLINE? model_attribute* NEWLINE? RCURLY;
+
+    inheritance: COLON MODEL_ID (COMMA MODEL_ID)*;
+
+    model_attribute
+        : (MODEL_ID COLON MODEL_ID NEWLINE?)
+        | (MODEL_ID ASSIGN value NEWLINE?)
+        | (DOLLAR STRVALUE DOLLAR NEWLINE?);
+
+    value
+        : DEC_NUMBER
+        | FLOAT_NUMBER
+        | TRUE
+        | FALSE
+        | STRVALUE;
+
+    tokens{
+        IMPORT: 'import';
+        AS: 'as';
+        MODEL_ID: '[a-zA-Z_][a-zA-Z_0-9]*';
+        MV_URL: '[a-zA-Z_0-9/]*';
+        LCURLY: '{';
+        RCURLY: '}';
+        NEWLINE: '(\r?\n)+';
+        DEC_NUMBER: '[+-]?(0|[1-9]\d*[lL]?)';
+        FLOAT_NUMBER: '[+-]?((\d+\.\d*|\.\d+)([eE][-+]?\d+)?|\d+[eE][-+]?\d+)';
+        STRVALUE: 'u?r?("(?!"").*?(?<!\\)(\\\\)*?"|\'(?!\'\').*?(?<!\\)(\\\\)*?\')';
+        TRUE: 'True';
+        FALSE: 'False';
+        ASSIGN: '=';
+        DOLLAR: '$';
+        WS: '[ ]+' @Impl;
+        COLON : ':';
+        LPAR: '\(';
+        RPAR: '\)';
+    }
+}

+ 64 - 0
interface/HUTN/hutn_compiler/model_visitor.py

@@ -0,0 +1,64 @@
+from visitor import Visitor
+
+def jsonstr(s):
+    return '"%s"' % s
+
+class ModelVisitor(Visitor):
+    def __init__(self, args):
+        Visitor.__init__(self, args)
+        self.constructors = []
+        self.free_id = 0
+        self.name_maps = {}
+        self.current_model = None
+        self.current_element = None
+
+    def dump(self):
+        return self.constructors
+
+    def visit_start(self, tree):
+        for t in tree.get_tail():
+            self.visit(t)
+
+    def visit_import(self, tree):
+        url = tree.get_children("MV_URL")[0]
+        target = tree.get_children("MODEL_ID")[0]
+        self.constructors.extend(['"import_node"', jsonstr(url.get_text()), jsonstr(target.get_text())])
+
+    def visit_model(self, tree):
+        children = tree.get_children("MODEL_ID")
+        model_type = children[0].get_text()
+        model_name = children[1].get_text()
+        self.constructors.extend(['"instantiate_model"', jsonstr(model_type), jsonstr(model_name)])
+        self.current_model = model_name
+        for element in tree.get_children("model_element"):
+            self.visit(element)
+
+    def visit_model_element(self, tree):
+        children = tree.get_children("MODEL_ID")
+        element_type = children[0].get_text()
+        element_name = children[1].get_text()
+
+        if len(children) == 4:
+            source_name = children[2].get_text()
+            target_name = children[3].get_text()
+            self.constructors.extend(['"instantiate_link"', jsonstr(self.current_model), jsonstr(element_type), jsonstr(element_name), jsonstr(source_name), jsonstr(target_name)])
+        else:
+            self.constructors.extend(['"instantiate_node"', jsonstr(self.current_model), jsonstr(element_type), jsonstr(element_name)])
+        self.current_element = element_name
+            
+        for attr in tree.get_children("model_attribute"):
+            self.visit(attr)
+
+    def visit_model_attribute(self, tree):
+        children = tree.get_children("MODEL_ID")
+        is_definition = bool(tree.get_children("COLON"))
+
+        if is_definition:
+            attr_name = children[0].get_text()
+            attr_type = children[1].get_text()
+            self.constructors.extend(['"instantiate_link"', jsonstr("Association"), jsonstr(self.current_element + "_" + attr_name), jsonstr(self.current_element), jsonstr(attr_type)])
+            self.constructors.extend(['"instantiate_attribute"', jsonstr(self.current_element + "_" + attr_name), jsonstr("name"), jsonstr(attr_name)])
+        else:
+            attr_name = children[0].get_text()
+            attr_value = children[1]
+            self.constructors.extend(['"instantiate_attribute"', jsonstr(self.current_element), jsonstr(attr_name), jsonstr(attr_value.get_text()) if attr_value.head == "STRVALUE" else attr_value.get_text()])

+ 20 - 0
interface/HUTN/test/modelling_language/test_compile.py

@@ -0,0 +1,20 @@
+import unittest
+import util
+
+from hutn_compiler.compiler import main
+import json
+
+def compile_file(obj, filename):
+    result = main(util.get_code_path(filename), "grammars/modelling.g", "M", [])
+    try:
+        expected = json.loads(open(util.get_expected_path(filename)).read())
+    except:
+        #f = open(util.get_expected_path(filename), 'w')
+        #f.write(json.dumps(result))
+        #f.close()
+        pass
+    assert result == expected
+
+class TestCompile(unittest.TestCase):
+    def test_PetriNets(self):
+        compile_file(self, "petrinets.mvc")