]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/values/TreeValue.java
84d36149145c216e0609adc6f354d3e1251b0e11
[simantics/platform.git] / bundles / org.simantics.graph.compiler / src / org / simantics / graph / compiler / internal / values / TreeValue.java
1 package org.simantics.graph.compiler.internal.values;\r
2 \r
3 import java.util.Collection;\r
4 \r
5 import org.antlr.runtime.tree.Tree;\r
6 import org.simantics.databoard.binding.Binding;\r
7 import org.simantics.graph.compiler.internal.parsing.GraphParser;\r
8 import org.simantics.graph.compiler.internal.store.IPreValue;\r
9 import org.simantics.graph.compiler.internal.translation.DataValueTranslator;\r
10 import org.simantics.graph.query.Path;\r
11 import org.simantics.graph.query.Paths;\r
12 import org.simantics.ltk.ISource;\r
13 import org.simantics.ltk.Location;\r
14 import org.simantics.ltk.Problem;\r
15 import org.simantics.ltk.antlr.ANTLRUtils;\r
16 \r
17 public class TreeValue implements IPreValue {\r
18 \r
19         ISource source;\r
20         Tree tree;\r
21         \r
22         public TreeValue(ISource source, Tree tree) {\r
23                 this.source = source;\r
24                 this.tree = tree;\r
25         }\r
26 \r
27         @Override\r
28         public Object toValue(Binding binding, Collection<Problem> problems) {\r
29                 return new DataValueTranslator(source, problems).translate(tree, binding);\r
30         }\r
31 \r
32         public Tree getTree() {\r
33                 return tree;\r
34         }\r
35 \r
36         @Override\r
37         public Path getDefaultType(Paths paths) {\r
38                 Tree b = this.tree;\r
39                 while(b.getType() == GraphParser.TUPLE && b.getChildCount() == 1)\r
40                         b = b.getChild(0);\r
41                 \r
42                 switch(b.getType()) {\r
43                 case GraphParser.INT:     return paths.Integer;\r
44                 case GraphParser.FLOAT:   return paths.Double;\r
45                 case GraphParser.STRING:  return paths.String;\r
46                 case GraphParser.TRUE:\r
47                 case GraphParser.FALSE:   return paths.Boolean;\r
48                 case GraphParser.VARIANT: return paths.Variant;\r
49                 case GraphParser.ARRAY:\r
50                         if(b.getChildCount() > 0) {\r
51                                 switch(b.getChild(0).getType()) {\r
52                                 case GraphParser.INT:\r
53                                 case GraphParser.FLOAT:\r
54                                         for(int i=0;i<b.getChildCount();++i)\r
55                                                 if(b.getChild(i).getType() == GraphParser.FLOAT)\r
56                                                         return paths.DoubleArray;\r
57                                         return paths.IntegerArray;\r
58                                 case GraphParser.STRING:\r
59                                         return paths.StringArray;\r
60                                 case GraphParser.TRUE:\r
61                                 case GraphParser.FALSE:\r
62                                         return paths.BooleanArray;\r
63                                 }\r
64                         }\r
65                 }\r
66                 return null;\r
67         }\r
68 \r
69         @Override\r
70         public Location getLocation() {\r
71                 return ANTLRUtils.location(source, tree);\r
72         }\r
73         \r
74         @Override\r
75         public String toString() {\r
76                 return tree.getText();\r
77         }\r
78 }\r