KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > coffer > ast > AmbKeySetNode_c


1 package polyglot.ext.coffer.ast;
2
3 import polyglot.ast.*;
4 import polyglot.types.*;
5 import polyglot.visit.*;
6 import polyglot.util.*;
7 import polyglot.ext.jl.ast.*;
8 import polyglot.ext.coffer.types.*;
9 import java.util.*;
10
11 /**
12  * Implementation of an ambiguous key set AST node.
13  */

14 public class AmbKeySetNode_c extends Node_c implements AmbKeySetNode
15 {
16     protected List keys;
17     protected KeySet keySet;
18
19     public AmbKeySetNode_c(Position pos, List keys) {
20         super(pos);
21         this.keys = TypedList.copyAndCheck(keys, KeyNode.class, true);
22     }
23
24     public KeySet keys() {
25         return keySet;
26     }
27
28     public List keyNodes() {
29         return keys;
30     }
31
32     public AmbKeySetNode keyNodes(List keys) {
33         AmbKeySetNode_c n = (AmbKeySetNode_c) copy();
34         n.keys = TypedList.copyAndCheck(keys, KeyNode.class, true);
35         return n;
36     }
37
38     public AmbKeySetNode_c reconstruct(List keys) {
39         if (! CollectionUtil.equals(this.keys, keys)) {
40             AmbKeySetNode_c n = (AmbKeySetNode_c) copy();
41             n.keys = TypedList.copyAndCheck(keys, KeyNode.class, true);
42             return n;
43         }
44
45         return this;
46     }
47
48     public Node visitChildren(NodeVisitor v) {
49         List keys = visitList(this.keys, v);
50         return reconstruct(keys);
51     }
52
53     public Node buildTypes(TypeBuilder tb) throws SemanticException {
54         CofferTypeSystem ts = (CofferTypeSystem) tb.typeSystem();
55
56         KeySet s = ts.emptyKeySet(position());
57
58         for (Iterator i = keys.iterator(); i.hasNext(); ) {
59             KeyNode key = (KeyNode) i.next();
60             s = s.add(key.key());
61         }
62
63         AmbKeySetNode_c n = (AmbKeySetNode_c) copy();
64         n.keys = keys;
65         n.keySet = s;
66         return n;
67     }
68         
69     public Node disambiguate(AmbiguityRemover sc) throws SemanticException {
70         CofferTypeSystem ts = (CofferTypeSystem) sc.typeSystem();
71         CofferNodeFactory nf = (CofferNodeFactory) sc.nodeFactory();
72
73         KeySet s = ts.emptyKeySet(position());
74
75         for (Iterator i = keys.iterator(); i.hasNext(); ) {
76             KeyNode key = (KeyNode) i.next();
77
78             if (key.key().isCanonical()) {
79                 s = s.add(key.key());
80             }
81             else {
82                 // return this;
83
throw new SemanticException("Could not disambiguate " +
84                                             this + ".");
85             }
86         }
87
88         return nf.CanonicalKeySetNode(position(), s);
89     }
90
91     public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
92         w.write("[");
93
94         w.begin(0);
95
96         for (Iterator i = keys.iterator(); i.hasNext(); ) {
97             KeyNode key = (KeyNode) i.next();
98             print(key, w, tr);
99             if (i.hasNext()) {
100                 w.write(",");
101                 w.allowBreak(0, " ");
102             }
103         }
104
105         w.end();
106
107         w.write("]");
108     }
109
110     public void translate(CodeWriter w, Translator tr) {
111     throw new InternalCompilerError(position(),
112         "Cannot translate ambiguous key set " + this + ".");
113     }
114
115     public String JavaDoc toString() {
116         String JavaDoc s = "[";
117
118         for (Iterator i = keys.iterator(); i.hasNext(); ) {
119             KeyNode key = (KeyNode) i.next();
120
121             s += key.toString();
122
123             if (i.hasNext()) {
124                 s += ", ";
125             }
126         }
127
128         s += "]";
129         
130         return s;
131     }
132 }
133
Popular Tags