KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > parse > Name


1 package polyglot.ext.jl.parse;
2
3 import polyglot.ast.*;
4 import polyglot.parse.*;
5 import polyglot.types.*;
6 import polyglot.util.*;
7
8 /**
9  * Represents an ambiguous, possibly qualified, identifier encountered while parsing.
10  */

11 public class Name {
12     public final Name prefix;
13     public final String JavaDoc name;
14     public final Position pos;
15     NodeFactory nf;
16     TypeSystem ts;
17
18     public Name(BaseParser parser, Position pos, String JavaDoc name) {
19         this(parser, pos, null, name);
20     }
21
22     public Name(BaseParser parser, Position pos, Name prefix, String JavaDoc name) {
23         this.nf = parser.nf;
24         this.ts = parser.ts;
25         this.pos = pos;
26         this.prefix = prefix;
27         this.name = name;
28     }
29
30     // expr
31
public Expr toExpr() {
32         if (prefix == null) {
33             return nf.AmbExpr(pos, name);
34         }
35
36         return nf.Field(pos, prefix.toReceiver(), name);
37     }
38
39     // expr or type
40
public Receiver toReceiver() {
41         if (prefix == null) {
42             return nf.AmbReceiver(pos, name);
43         }
44
45         return nf.AmbReceiver(pos, prefix.toPrefix(), name);
46     }
47
48     // expr, type, or package
49
public Prefix toPrefix() {
50         if (prefix == null) {
51             return nf.AmbPrefix(pos, name);
52         }
53
54         return nf.AmbPrefix(pos, prefix.toPrefix(), name);
55     }
56
57     // type or package
58
public QualifierNode toQualifier() {
59         if (prefix == null) {
60             return nf.AmbQualifierNode(pos, name);
61         }
62
63         return nf.AmbQualifierNode(pos, prefix.toQualifier(), name);
64     }
65
66     // package
67
public PackageNode toPackage() {
68                 if (prefix == null) {
69                     return nf.PackageNode(pos, ts.createPackage(null, name));
70                 }
71                 else {
72                     return nf.PackageNode(pos, ts.createPackage(prefix.toPackage().package_(), name));
73                 }
74     }
75
76     // type
77
public TypeNode toType() {
78         if (prefix == null) {
79             return nf.AmbTypeNode(pos, name);
80         }
81
82         return nf.AmbTypeNode(pos, prefix.toQualifier(), name);
83     }
84
85     public String JavaDoc toString() {
86         if (prefix == null) {
87             return name;
88         }
89
90         return prefix.toString() + "." + name;
91     }
92 }
93
Popular Tags