001: package java_cup.runtime;
002:
003: /**
004: * Default Implementation for SymbolFactory, creates
005: * plain old Symbols
006: *
007: * @version last updated 27-03-2006
008: * @author Michael Petter
009: */
010:
011: /* *************************************************
012: class DefaultSymbolFactory
013:
014: interface for creating new symbols
015: ***************************************************/
016: public class ComplexSymbolFactory implements SymbolFactory {
017: public static class Location {
018: private String unit = "unknown";
019: private int line, column;
020:
021: public Location(String unit, int line, int column) {
022: this .unit = unit;
023: this .line = line;
024: this .column = column;
025: }
026:
027: public Location(int line, int column) {
028: this .line = line;
029: this .column = column;
030: }
031:
032: public String toString() {
033: return unit + ":" + line + "/" + column;
034: }
035:
036: public int getColumn() {
037: return column;
038: }
039:
040: public int getLine() {
041: return line;
042: }
043:
044: public String getUnit() {
045: return unit;
046: }
047: }
048:
049: /**
050: * ComplexSymbol with detailed Location Informations and a Name
051: */
052: public static class ComplexSymbol extends Symbol {
053: protected String name;
054: protected Location xleft, xright;
055:
056: public ComplexSymbol(String name, int id) {
057: super (id);
058: this .name = name;
059: }
060:
061: public ComplexSymbol(String name, int id, Object value) {
062: super (id, value);
063: this .name = name;
064: }
065:
066: public String toString() {
067: if (xleft == null || xright == null)
068: return "Symbol: " + name;
069: return "Symbol: " + name + " (" + xleft + " - " + xright
070: + ")";
071: }
072:
073: public ComplexSymbol(String name, int id, int state) {
074: super (id, state);
075: this .name = name;
076: }
077:
078: public ComplexSymbol(String name, int id, Symbol left,
079: Symbol right) {
080: super (id, left, right);
081: this .name = name;
082: if (left != null)
083: this .xleft = ((ComplexSymbol) left).xleft;
084: if (right != null)
085: this .xright = ((ComplexSymbol) right).xright;
086: }
087:
088: public ComplexSymbol(String name, int id, Location left,
089: Location right) {
090: super (id);
091: this .name = name;
092: this .xleft = left;
093: this .xright = right;
094: }
095:
096: public ComplexSymbol(String name, int id, Symbol left,
097: Symbol right, Object value) {
098: super (id, value);
099: this .name = name;
100: if (left != null)
101: this .xleft = ((ComplexSymbol) left).xleft;
102: if (right != null)
103: this .xright = ((ComplexSymbol) right).xright;
104: }
105:
106: public ComplexSymbol(String name, int id, Location left,
107: Location right, Object value) {
108: super (id, value);
109: this .name = name;
110: this .xleft = left;
111: this .xright = right;
112: }
113:
114: public Location getLeft() {
115: return xleft;
116: }
117:
118: public Location getRight() {
119: return xright;
120: }
121: }
122:
123: // Factory methods
124: public Symbol newSymbol(String name, int id, Location left,
125: Location right, Object value) {
126: return new ComplexSymbol(name, id, left, right, value);
127: }
128:
129: public Symbol newSymbol(String name, int id, Location left,
130: Location right) {
131: return new ComplexSymbol(name, id, left, right);
132: }
133:
134: public Symbol newSymbol(String name, int id, Symbol left,
135: Symbol right, Object value) {
136: return new ComplexSymbol(name, id, left, right, value);
137: }
138:
139: public Symbol newSymbol(String name, int id, Symbol left,
140: Symbol right) {
141: return new ComplexSymbol(name, id, left, right);
142: }
143:
144: public Symbol newSymbol(String name, int id) {
145: return new ComplexSymbol(name, id);
146: }
147:
148: public Symbol newSymbol(String name, int id, Object value) {
149: return new ComplexSymbol(name, id, value);
150: }
151:
152: public Symbol startSymbol(String name, int id, int state) {
153: return new ComplexSymbol(name, id, state);
154: }
155: }
|