X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.graph.compiler%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fcompiler%2Finternal%2Ftranslation%2FDataTypeTranslator.java;fp=bundles%2Forg.simantics.graph.compiler%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fcompiler%2Finternal%2Ftranslation%2FDataTypeTranslator.java;h=640d89ee19d11cd39244e204512051fcf3232c3f;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/translation/DataTypeTranslator.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/translation/DataTypeTranslator.java new file mode 100644 index 000000000..640d89ee1 --- /dev/null +++ b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/translation/DataTypeTranslator.java @@ -0,0 +1,203 @@ +package org.simantics.graph.compiler.internal.translation; + +import gnu.trove.map.hash.THashMap; + +import java.util.Collection; + +import org.antlr.runtime.tree.Tree; +import org.simantics.databoard.Datatypes; +import org.simantics.databoard.type.ArrayType; +import org.simantics.databoard.type.ByteType; +import org.simantics.databoard.type.Component; +import org.simantics.databoard.type.Datatype; +import org.simantics.databoard.type.DoubleType; +import org.simantics.databoard.type.FloatType; +import org.simantics.databoard.type.IntegerType; +import org.simantics.databoard.type.LongType; +import org.simantics.databoard.type.MapType; +import org.simantics.databoard.type.OptionalType; +import org.simantics.databoard.type.RecordType; +import org.simantics.databoard.type.StringType; +import org.simantics.databoard.type.UnionType; +import org.simantics.graph.compiler.internal.parsing.GraphParser; +import org.simantics.ltk.ISource; +import org.simantics.ltk.Problem; +import org.simantics.ltk.antlr.ANTLRUtils; + +public class DataTypeTranslator { + + ISource source; + Collection problems; + + public DataTypeTranslator(ISource source, Collection problems) { + this.source = source; + this.problems = problems; + } + + public Datatype translate(Tree tree) { + switch(tree.getType()) { + case GraphParser.RECORD_TYPE: + return translateRecordType(tree); + case GraphParser.UNION_TYPE: + return translateUnionType(tree); + case GraphParser.TUPLE_TYPE: + return translateTupleType(tree); + case GraphParser.ARRAY_TYPE: + return translateArrayType(tree); + case GraphParser.TYPE_REFERENCE: + return translateTypeReference(tree); + default: + printTree(0, tree); + throw new IllegalArgumentException("The argument is not a data type AST"); + } + } + + private Datatype translateArrayType(Tree tree) { + return new ArrayType(translate(tree.getChild(0))); + } + + private Datatype translateTupleType(Tree tree) { + int count = tree.getChildCount(); + if(count == 1) + return translate(tree.getChild(0)); + Component[] components = new Component[count]; + for(int i=0;i getAnnotations(Tree tree) { + THashMap result = new THashMap(); + for(int i=1;i annotations = getAnnotations(tree); + return new ByteType( + annotations.get("unit"), + annotations.get("range") + ); + } + else if(name.equals("Integer")) { + THashMap annotations = getAnnotations(tree); + return new IntegerType( + annotations.get("unit"), + annotations.get("range") + ); + } + else if(name.equals("Float")) { + THashMap annotations = getAnnotations(tree); + return new FloatType( + annotations.get("unit"), + annotations.get("range") + ); + } + else if(name.equals("Double")) { + THashMap annotations = getAnnotations(tree); + return new DoubleType( + annotations.get("unit"), + annotations.get("range") + ); + } + else if(name.equals("Long")) { + THashMap annotations = getAnnotations(tree); + return new LongType( + annotations.get("unit"), + annotations.get("range") + ); + } + else if(name.equals("String")) { + THashMap annotations = getAnnotations(tree); + return new StringType( + annotations.get("pattern"), + annotations.get("mimeType"), + annotations.get("length") + ); + } + else if(name.equals("Variant")) + return Datatypes.VARIANT; + else if(name.equals("Optional")) + return new OptionalType(translate(tree.getChild(1))); + else if(name.equals("Map")) + return new MapType(translate(tree.getChild(1)), translate(tree.getChild(2))); + else if(name.equals("DataType")) + return Datatypes.getDatatypeUnchecked(Datatype.class); + else { + error(tree, "Invalid data type " + name + "."); + return null; + } + } + + private Datatype translateUnionType(Tree tree) { + Component[] components = new Component[tree.getChildCount()]; + for(int i=0;i 1 ? translate(c.getChild(1)) : RecordType.VOID_TYPE + ); + } + return new UnionType(components); + } + + private Datatype translateRecordType(Tree tree) { + Component[] components = new Component[tree.getChildCount()]; + for(int i=0;i