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