summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ptree/ParseTreeObject.java
blob: f52dec0dcb2bdf0c3cb6004f81f950b13ab6dc8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * ParseTreeObject.java 1.0
 *
 * This subclass of symbol represents (at least) terminal symbols returned
 * by the scanner and placed on the parse stack.  At present, this
 * class does nothing more than its super class.
 *
 * Jun 11, 1997
 * Sep 5, 1997
 *
 * @see java_cup.runtime.symbol
 * @version 1.0 last updated: Sep 5, 1997
 * @author  Michiaki Tatsubori
 */
package io.devnulllabs.openjava.ptree;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.StringTokenizer;

import io.devnulllabs.openjava.ptree.util.ParseTreeVisitor;
import io.devnulllabs.openjava.ptree.util.SourceCodeWriter;

/**
 * The ParseTree class presents for the node of parse tree.
 * This may be a token node, Leaf, or may be a nonterminal node, NonLeaf.
 *
 * @see io.devnulllabs.openjava.ptree.Leaf
 * @see io.devnulllabs.openjava.ptree.NonLeaf
 */
public abstract class ParseTreeObject
    extends Object
    implements ParseTree, Cloneable {
    /**
     * Why this modifier is not final ?
     * - Because of javac bug in excuting it with -O option.
     */
    protected static String LN;
    static {
        StringWriter strw = new StringWriter();
        PrintWriter pw = new PrintWriter(strw);
        pw.println();
        pw.close();
        LN = strw.toString();
    }

    private ParseTreeObject parent;
    /*************/
    public /*private*/
    final ParseTreeObject getParent() {
        return parent;
    }
    protected final void setParent(ParseTreeObject parent) {
        this.parent = parent;
    }
    public final void replace(ParseTree replacement)
        throws ParseTreeException {
        ParseTreeObject p = getParent();
        if (p == null)
            throw new ParseTreeException("no parent");
        p.replaceChildWith(this, replacement);
    }
    protected abstract void replaceChildWith(
        ParseTree dist,
        ParseTree replacement)
        throws ParseTreeException;

    /**
     * Arrocates new parse-tree object and set identifier number
     * on the object.
     *
     */
    public ParseTreeObject() {
        setObjectID();
    }

    /**
     * clone() is fixed as a shallow copy.
     *
     * @return  the copy of this ptree-node.
     */
    protected final Object clone() {
        try {
            ParseTreeObject result = (ParseTreeObject) super.clone();
            result.setObjectID();
            return result;
        } catch (CloneNotSupportedException e) {
            return null;
        }
    }

    /**
     * shallow copy
     */
    public ParseTree makeCopy() {
        return (ParseTree) clone();
    }

    /**
     * deep copy
     */
    public abstract ParseTree makeRecursiveCopy();

    /**
     * Tests if this parse-tree-node's value equals to the specified
     * ptree-node's.
     *
     * @return  true if two values are same.
     */
    public abstract boolean equals(ParseTree p);

    public int hashCode() {
        return getObjectID();
    }

    /**
     * Generates a string object of regular Java source code
     * representing this parse-tree.
     *
     * @return string which represents this parse-tree
     */
    public String toString() {
        StringWriter strwriter = new StringWriter();
        PrintWriter out = new PrintWriter(strwriter);
        SourceCodeWriter writer = new SourceCodeWriter(out);
        try {
            this.accept(writer);
        } catch (ParseTreeException e) {
            System.err.println("fail in toString()");
        }
        out.close();
        return strwriter.toString();
    }

    /**
     * May return true if two ptree-nodes don't refer to not the
     * same objects but their contents are equivalent.
     *
     * @param p the node
     * @param q the node to compare
     * @return true if p equals c
     */
    public static final boolean equal(ParseTree p, ParseTree q) {
        if (p == null && q == null)
            return true;
        if (p == null || q == null)
            return false;
        if (p == q)
            return true;
        return p.equals(q);
    }

    /**
     * The object ID is object identifier number in order to determine
     * two ptree variables refer to the same object.
     */
    private int objectID = -1;

    /** The idCount counts up generated ptree objects */
    private static int idCount = 0;

    public static final int lastObjectID() {
        return idCount;
    }

    /**
     * Increments idCount class variable and set the instance's
     * object identifier number to the number of idCount variable.
     *
     */
    private synchronized final void setObjectID() {
        idCount++;
        objectID = idCount;
    }

    /**
     * Returns
     *
     * @param id ID
     * @return Name the ID represents
     */
    public final int getObjectID() {
        return objectID;
    }

    /**
     * Generates the string expression from this node.  Returned
     * string doesn't have '"' without cancel ( \" ) and doesn't have
     * newline.<p>
     *
     * This method is useful to embed ptree objects as a string literal
     * in source code.
     *
     * @return the flatten string which this node represents
     */
    public String toFlattenString() {
        StringBuffer ret = null;

        /* cancel double quotes and back slashs */
        java.util.StringTokenizer canceller =
            new StringTokenizer(this.toString(), "\\\"", true);
        ret = new StringBuffer();

        while (canceller.hasMoreTokens()) {
            String buf = canceller.nextToken();
            if (buf.equals("\\") || buf.equals("\"")) {
                ret.append("\\");
            }
            ret.append(buf);
        }

        /* remove new line chars */
        java.util.StringTokenizer lnremover =
            new StringTokenizer(ret.toString(), "\n\r", false);
        ret = new StringBuffer();

        while (lnremover.hasMoreTokens()) {
            ret.append(" " + lnremover.nextToken());
        }

        return ret.toString();
    }

    /**
     * Accepts a <code>ParseTreeVisitor</code> object as the role of a
     * Visitor in the Visitor pattern, as the role of an Element in
     * the Visitor pattern.<p>
     *
     * This invoke an appropriate <code>visit()</code> method on the
     * accepted visitor.
     *
     * @param visitor a visitor
     */
    public abstract void accept(ParseTreeVisitor visitor)
        throws ParseTreeException;

    /*
    public void accept( ParseTreeVisitor visitor ) throws ParseTreeException {
    Class active_type = this.getClass();
    Class visitor_clazz = visitor.getClass();
    try {
        Method visit_method
        = visitor_clazz.getMethod( "visit",
                       new Class[]{ active_type } );
        visit_method.invoke( visitor, new Object[]{ this } );
    } catch ( Exception e ) {
        System.err.println( e.toString() );
        System.err.println( "\telement\t-" + active_type.toString() );
        System.err.println( "\tvisitor\t-" + visitor_clazz.toString() );
    }
    }
    */

    /**
     * Accepts a <code>ParseTreeVisitor</code> object as the role of a
     * Visitor in the Visitor pattern, as the role of an Element in the
     * Visitor pattern.<p>
     *
     * This invoke an appropriate <code>visit()</code> method on each
     * child <code>ParseTree</code> object with this visitor.
     *
     * @param visitor a visitor
     **/
    public abstract void childrenAccept(ParseTreeVisitor visitor)
        throws ParseTreeException;

}