001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2007 New York University
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public License
007: * version 2.1 as published by the Free Software Foundation.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
017: * USA.
018: */
019: package xtc.typical;
020:
021: import java.util.Iterator;
022:
023: import xtc.util.Pair;
024: import xtc.util.SymbolTable;
025:
026: /**
027: * The base class of all NameSpace names.
028: *
029: * @author Anh Le
030: * @version $Revision: 1.6 $
031: */
032: public abstract class Name<T extends Tuple> extends Variant<T> {
033:
034: /** The tags for subclasses. */
035: public static enum Tag {
036: SimpleNameT, QualifiedNameT
037: }
038:
039: /** Create a new name. */
040: protected Name() {
041: //empty
042: }
043:
044: /**
045: * Get the tag of this class
046: *
047: * @return The tag
048: */
049: public abstract Tag tag();
050:
051: /**
052: * Tests if this name is simple.
053: *
054: * @return <code>true</code> if simple, false otherwise.
055: */
056: public boolean isSimpleName() {
057: return false;
058: }
059:
060: /**
061: * Tests if this name is qualified.
062: *
063: * @return <code>true</code> if simple, false otherwise.
064: */
065: public boolean isQualifiedName() {
066: return false;
067: }
068:
069: /**
070: * Convert the name into a name suitable for symbol table access.
071: *
072: * @param ns The namespace.
073: */
074: public abstract String mangle(String ns);
075:
076: /** The simple namespace name. */
077: public static class SimpleName extends Name<Tuple.T1<String>> {
078:
079: /**
080: * Create a new simple name.
081: *
082: * @param member1 The simple name.
083: */
084: public SimpleName(String member1) {
085: tuple = new Tuple.T1<String>(member1);
086: }
087:
088: public final Tag tag() {
089: return Tag.SimpleNameT;
090: }
091:
092: public boolean isSimpleName() {
093: return true;
094: }
095:
096: public String getName() {
097: return "SimpleName";
098: }
099:
100: public String toString() {
101: return "SimpleName of " + tuple.toString();
102: }
103:
104: public String mangle(String ns) {
105: if ("default".equals(ns))
106: return tuple.get1();
107: else
108: return SymbolTable.toNameSpace(tuple.get1(), ns);
109: }
110:
111: }
112:
113: /** The qualified namespace name. */
114: public static class QualifiedName extends
115: Name<Tuple.T1<Pair<String>>> {
116:
117: /**
118: * Create a new Qualified name.
119: *
120: * @param member1 The qualifier.
121: */
122: public QualifiedName(Pair<String> member1) {
123: tuple = new Tuple.T1<Pair<String>>(member1);
124: }
125:
126: public final Tag tag() {
127: return Tag.QualifiedNameT;
128: }
129:
130: public boolean isQualifiedName() {
131: return true;
132: }
133:
134: public String getName() {
135: return "QualifiedName";
136: }
137:
138: public String toString() {
139: return "QualifiedName of " + tuple.toString();
140: }
141:
142: public String mangle(String ns) {
143: StringBuilder buf = new StringBuilder();
144:
145: for (Iterator<String> iter = tuple.get1().iterator(); iter
146: .hasNext();) {
147: if (iter.hasNext()) {
148: buf.append(iter.next());
149: buf.append('.');
150: } else {
151: if (!"default".equals(ns)) {
152: buf.append(SymbolTable.toNameSpace(iter.next(),
153: ns));
154: } else {
155: buf.append(iter.next());
156: }
157: }
158: }
159:
160: return buf.toString();
161: }
162:
163: }
164:
165: }
|