1 package net.sourceforge.pmd.lang.vm.ast;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.PrintWriter;
23 import java.io.Writer;
24
25 import net.sourceforge.pmd.lang.ast.AbstractNode;
26
27 import org.apache.commons.lang3.text.StrBuilder;
28
29 /**
30 *
31 */
32 public class AbstractVmNode extends AbstractNode implements VmNode {
33
34 /** */
35 // TODO - It seems that this field is only valid when parsing, and should not be kept around.
36 protected VmParser parser;
37
38 /** */
39 protected int info; // added
40
41 /** */
42 public boolean state;
43
44 /** */
45 protected boolean invalid = false;
46
47 /** */
48 protected Token first;
49
50 /** */
51 protected Token last;
52
53 protected String templateName;
54
55 /**
56 * @param i
57 */
58 public AbstractVmNode(final int i) {
59 super(i);
60 }
61
62 /**
63 * @param p
64 * @param i
65 */
66 public AbstractVmNode(final VmParser p, final int i) {
67 this(i);
68 parser = p;
69 templateName = parser.currentTemplateName;
70 }
71
72 /**
73 * @see org.apache.velocity.runtime.parser.node.Node#jjtOpen()
74 */
75 @Override
76 public void jjtOpen() {
77 first = parser.getToken(1); // added
78 if (beginLine == -1 && parser.token.next != null) {
79 beginLine = parser.token.next.beginLine;
80 beginColumn = parser.token.next.beginColumn;
81 }
82 }
83
84 /**
85 * @see org.apache.velocity.runtime.parser.node.Node#jjtClose()
86 */
87 @Override
88 public void jjtClose() {
89 last = parser.getToken(0); // added
90 if (beginLine == -1 && (children == null || children.length == 0)) {
91 beginColumn = parser.token.beginColumn;
92 }
93 if (beginLine == -1) {
94 beginLine = parser.token.beginLine;
95 }
96 endLine = parser.token.endLine;
97 endColumn = parser.token.endColumn;
98 }
99
100 /**
101 * @param t
102 */
103 public void setFirstToken(final Token t) {
104 this.first = t;
105 }
106
107 /**
108 * @see org.apache.velocity.runtime.parser.node.Node#getFirstToken()
109 */
110 public Token getFirstToken() {
111 return first;
112 }
113
114 /**
115 * @see org.apache.velocity.runtime.parser.node.Node#getLastToken()
116 */
117 public Token getLastToken() {
118 return last;
119 }
120
121 /**
122 * @see org.apache.velocity.runtime.parser.node.Node#jjtAccept(org.apache.velocity.runtime.parser.node.VmParserVisitor,
123 * java.lang.Object)
124 */
125 public Object jjtAccept(final VmParserVisitor visitor, final Object data) {
126 return visitor.visit(this, data);
127 }
128
129 /**
130 * @see org.apache.velocity.runtime.parser.node.Node#childrenAccept(org.apache.velocity.runtime.parser.node.VmParserVisitor,
131 * java.lang.Object)
132 */
133 public Object childrenAccept(final VmParserVisitor visitor, final Object data) {
134 if (children != null) {
135 for (int i = 0; i < children.length; ++i) {
136 ((VmNode) children[i]).jjtAccept(visitor, data);
137 }
138 }
139 return data;
140 }
141
142 /*
143 * You can override these two methods in subclasses of SimpleNode to customize the way the node appears when the
144 * tree is dumped. If your output uses more than one line you should override toString(String), otherwise overriding
145 * toString() is probably all you need to do.
146 */
147
148 public String toString() {
149 return VmParserTreeConstants.jjtNodeName[id];
150 }
151 /**
152 * @param prefix
153 * @return String representation of this node.
154 */
155 public String toString(final String prefix) {
156 return prefix + toString();
157 }
158
159 /**
160 * Override this method if you want to customize how the node dumps out its children.
161 *
162 * @param prefix
163 */
164 public void dump(final String prefix, final boolean recurse, final Writer writer) {
165 final PrintWriter printWriter = writer instanceof PrintWriter ? (PrintWriter) writer
166 : new PrintWriter(writer);
167 printWriter.println(toString(prefix));
168 if (children != null && recurse) {
169 for (int i = 0; i < children.length; ++i) {
170 final AbstractVmNode n = (AbstractVmNode) children[i];
171 if (n != null) {
172 n.dump(prefix + " ", recurse, printWriter);
173 }
174 }
175 }
176 }
177
178 // All additional methods
179
180 /**
181 * @see org.apache.velocity.runtime.parser.node.Node#literal()
182 */
183 public String literal() {
184 // if we have only one string, just return it and avoid
185 // buffer allocation. VELOCITY-606
186 if (first != null && first.equals(last)) {
187 return NodeUtils.tokenLiteral(first);
188 }
189
190 Token t = first;
191 final StrBuilder sb = new StrBuilder(NodeUtils.tokenLiteral(t));
192 while (t != null && !t.equals(last)) {
193 t = t.next;
194 sb.append(NodeUtils.tokenLiteral(t));
195 }
196 return sb.toString();
197 }
198
199 /**
200 * @see org.apache.velocity.runtime.parser.node.Node#getType()
201 */
202 public int getType() {
203 return id;
204 }
205
206 /**
207 * @see org.apache.velocity.runtime.parser.node.Node#setInfo(int)
208 */
209 public void setInfo(final int info) {
210 this.info = info;
211 }
212
213 /**
214 * @see org.apache.velocity.runtime.parser.node.Node#getInfo()
215 */
216 public int getInfo() {
217 return info;
218 }
219
220 /**
221 * @see org.apache.velocity.runtime.parser.node.Node#setInvalid()
222 */
223 public void setInvalid() {
224 invalid = true;
225 }
226
227 /**
228 * @see org.apache.velocity.runtime.parser.node.Node#isInvalid()
229 */
230 public boolean isInvalid() {
231 return invalid;
232 }
233
234 /**
235 * @see org.apache.velocity.runtime.parser.node.Node#getLine()
236 */
237 public int getLine() {
238 return first.beginLine;
239 }
240
241 /**
242 * @see org.apache.velocity.runtime.parser.node.Node#getColumn()
243 */
244 public int getColumn() {
245 return first.beginColumn;
246 }
247
248 public String getTemplateName() {
249 return templateName;
250 }
251 }