summaryrefslogtreecommitdiff
path: root/src/main/java/io/devnulllabs/openjava/ptree/Leaf.java
blob: c4fdf16aa5a803305cb306c6b8ce111a2512ee37 (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
/*
 * Leaf.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
 *
 * @see io.devnulllabs.openjava.ptree.ParseTree
 * @see io.devnulllabs.openjava.ptree.ParseTreeObject
 * @version 1.0 last updated: Jun 11, 1997
 * @author  Michiaki Tatsubori
 */
package io.devnulllabs.openjava.ptree;

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

/**
 * The Leaf class is a token-node in the parse tree of OpenJava.
 * Any object of this class or subclasses must be immutable.
 *
 * @see io.devnulllabs.openjava.ptree.ParseTree
 * @see io.devnulllabs.openjava.ptree.NonLeaf
 */
public class Leaf extends ParseTreeObject implements ParseTree {
    protected void replaceChildWith(ParseTree dist, ParseTree replacement)
        throws ParseTreeException {
        throw new ParseTreeException("no child");
    }

    /** textString is the text of this token. */
    private String textString = null;

    /** tokenID is the identifer number of this token */
    private int tokenID = 0;

    /** line is the number of the line at which this token is. */
    public int line = -1;

    /** charBegin is the number of the character at which this token is. */
    public int charBegin = -1;

    /**
     * Allocates a new leaf(token) with its text.
     *
     * @param  term_num  dummy parameter.
     * @param  str  its text.
     */
    public Leaf(String str) {
        this(-1, str, -1, -1);
    }

    /**
     * Allocates a new leaf(token) with its text.
     *
     * @param  term_num  dummy parameter.
     * @param  str  its text.
     */
    public Leaf(int term_num, String str) {
        this(term_num, str, -1, -1);
    }

    /**
     * Allocates a new leaf(token) with its text and where this is.
     *
     * @param  term_num  dummy parameter.
     * @param  str  its text.
     */
    public Leaf(int term_num, String str, int line, int charBegin) {
        tokenID = term_num;
        textString = str;
        this.line = line;
        this.charBegin = charBegin;
    }

    /**
     * Overrides to return its text as the string of this instance.
     *
     * @return  the text of this token.
     */
    public String toString() {
        return textString;
    }

    /**
     * Makes a new copy of this leaf-node.
     * This method equals to makeCopy().
     *
     * @return  the copy of this nonleaf-node as a ptree-node.
     */
    public ParseTree makeRecursiveCopy() {
        return (ParseTree) this.clone();
    }

    /**
     * Makes a new copy of this leaf-node.
     *
     * @return  the copy of this nonleaf-node as a ptree-node.
     */
    public ParseTree makeCopy() {
        return (ParseTree) this.clone();
    }

    /**
     * Tests if the specified ptree-node equals to this leaf-node.
     *
     * @param  p  the ptree-node to be tested.
     * @return  true if p equals to this leaf-node
     */
    public boolean equals(ParseTree p) {
        if (p == null || !(p instanceof Leaf))
            return false;
        if (this == p)
            return true;
        return this.toString().equals(p.toString());
    }

    /**
     * Tests if the specified string equals to this leaf-node's text.
     *
     * @param  p  the ptree-node to be tested.
     * @return  true if p equals to this leaf-node
     */
    public boolean equals(String str) {
        if (str == null)
            return false;
        return this.toString().equals(str);
    }

    /**
     * Returns the identifer-number of this token.
     *
     * @return  the identifer-number of this token.
     */
    public int getTokenID() {
        return tokenID;
    }

    /**
     * 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 void childrenAccept(ParseTreeVisitor visitor) {
        return;
    }

    public void accept(ParseTreeVisitor v) throws ParseTreeException {
        v.visit(this);
    }

}