]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/values/TreeValue.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / internal / values / TreeValue.java
diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/values/TreeValue.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/values/TreeValue.java
new file mode 100644 (file)
index 0000000..84d3614
--- /dev/null
@@ -0,0 +1,78 @@
+package org.simantics.graph.compiler.internal.values;\r
+\r
+import java.util.Collection;\r
+\r
+import org.antlr.runtime.tree.Tree;\r
+import org.simantics.databoard.binding.Binding;\r
+import org.simantics.graph.compiler.internal.parsing.GraphParser;\r
+import org.simantics.graph.compiler.internal.store.IPreValue;\r
+import org.simantics.graph.compiler.internal.translation.DataValueTranslator;\r
+import org.simantics.graph.query.Path;\r
+import org.simantics.graph.query.Paths;\r
+import org.simantics.ltk.ISource;\r
+import org.simantics.ltk.Location;\r
+import org.simantics.ltk.Problem;\r
+import org.simantics.ltk.antlr.ANTLRUtils;\r
+\r
+public class TreeValue implements IPreValue {\r
+\r
+       ISource source;\r
+       Tree tree;\r
+       \r
+       public TreeValue(ISource source, Tree tree) {\r
+               this.source = source;\r
+               this.tree = tree;\r
+       }\r
+\r
+       @Override\r
+       public Object toValue(Binding binding, Collection<Problem> problems) {\r
+               return new DataValueTranslator(source, problems).translate(tree, binding);\r
+       }\r
+\r
+       public Tree getTree() {\r
+               return tree;\r
+       }\r
+\r
+       @Override\r
+       public Path getDefaultType(Paths paths) {\r
+               Tree b = this.tree;\r
+               while(b.getType() == GraphParser.TUPLE && b.getChildCount() == 1)\r
+                       b = b.getChild(0);\r
+               \r
+               switch(b.getType()) {\r
+               case GraphParser.INT:     return paths.Integer;\r
+               case GraphParser.FLOAT:   return paths.Double;\r
+               case GraphParser.STRING:  return paths.String;\r
+               case GraphParser.TRUE:\r
+               case GraphParser.FALSE:   return paths.Boolean;\r
+               case GraphParser.VARIANT: return paths.Variant;\r
+               case GraphParser.ARRAY:\r
+                       if(b.getChildCount() > 0) {\r
+                               switch(b.getChild(0).getType()) {\r
+                               case GraphParser.INT:\r
+                               case GraphParser.FLOAT:\r
+                                       for(int i=0;i<b.getChildCount();++i)\r
+                                               if(b.getChild(i).getType() == GraphParser.FLOAT)\r
+                                                       return paths.DoubleArray;\r
+                                       return paths.IntegerArray;\r
+                               case GraphParser.STRING:\r
+                                       return paths.StringArray;\r
+                               case GraphParser.TRUE:\r
+                               case GraphParser.FALSE:\r
+                                       return paths.BooleanArray;\r
+                               }\r
+                       }\r
+               }\r
+               return null;\r
+       }\r
+\r
+       @Override\r
+       public Location getLocation() {\r
+               return ANTLRUtils.location(source, tree);\r
+       }\r
+       \r
+       @Override\r
+       public String toString() {\r
+               return tree.getText();\r
+       }\r
+}\r