001: /**
002: *
003: * Copyright 2005 Jeremy Rayner
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: **/package org.codehaus.groovy.antlr.treewalker;
018:
019: import java.io.PrintStream;
020: import java.util.Stack;
021:
022: import org.codehaus.groovy.antlr.GroovySourceAST;
023: import org.codehaus.groovy.antlr.parser.GroovyTokenTypes;
024:
025: /**
026: * A visitor that prints a html tags of each node to the supplied PrintStream
027: *
028: * @author <a href="mailto:groovy@ross-rayner.com">Jeremy Rayner</a>
029: * @version $Revision: 4526 $
030: */
031:
032: public class NodeAsHTMLPrinter extends VisitorAdapter {
033: private String[] tokenNames;
034: private PrintStream out;
035: private Stack stack;
036:
037: /**
038: * A visitor that prints a html tags, for each node, to the supplied PrintStream.
039: * @param out supplied PrintStream to output nodes to
040: * @param tokenNames an array of token names to use
041: */
042: public NodeAsHTMLPrinter(PrintStream out, String[] tokenNames) {
043: this .tokenNames = tokenNames;
044: this .out = out;
045: this .stack = new Stack();
046: }
047:
048: public void setUp() {
049: out.println("<html><head></head><body><pre>");
050: }
051:
052: public void visitDefault(GroovySourceAST t, int visit) {
053: if (visit == OPENING_VISIT) {
054: out.print("<code title='" + tokenNames[t.getType()]
055: + "'><font color='#" + colour(t) + "'>");
056: } else {
057: out.print("</font></code>");
058: }
059: }
060:
061: public void tearDown() {
062: out.println("</pre></body></html>");
063: }
064:
065: private String colour(GroovySourceAST t) {
066: String black = "000000";
067: String blue = "17178B";
068: String green = "008000";
069: //String purple = "7C308D";
070: String colour = black;
071: switch (t.getType()) {
072: case GroovyTokenTypes.ABSTRACT:
073: case GroovyTokenTypes.ANNOTATION:
074: case GroovyTokenTypes.ANNOTATIONS:
075: case GroovyTokenTypes.ANNOTATION_ARRAY_INIT:
076: case GroovyTokenTypes.ANNOTATION_DEF:
077: case GroovyTokenTypes.ANNOTATION_FIELD_DEF:
078: case GroovyTokenTypes.ANNOTATION_MEMBER_VALUE_PAIR:
079: case GroovyTokenTypes.ARRAY_DECLARATOR:
080: case GroovyTokenTypes.ASSIGN:
081: case GroovyTokenTypes.AT:
082: case GroovyTokenTypes.BAND:
083: case GroovyTokenTypes.BAND_ASSIGN:
084: case GroovyTokenTypes.BIG_SUFFIX:
085: case GroovyTokenTypes.BLOCK:
086: case GroovyTokenTypes.BNOT:
087: case GroovyTokenTypes.BOR:
088: case GroovyTokenTypes.BOR_ASSIGN:
089: case GroovyTokenTypes.BSR:
090: case GroovyTokenTypes.BSR_ASSIGN:
091: case GroovyTokenTypes.BXOR:
092: case GroovyTokenTypes.BXOR_ASSIGN:
093: case GroovyTokenTypes.CASE_GROUP:
094: case GroovyTokenTypes.CLOSABLE_BLOCK:
095: case GroovyTokenTypes.CLOSABLE_BLOCK_OP:
096: case GroovyTokenTypes.COLON:
097: case GroovyTokenTypes.COMMA:
098: case GroovyTokenTypes.COMPARE_TO:
099: case GroovyTokenTypes.CTOR_CALL:
100: case GroovyTokenTypes.CTOR_IDENT:
101: case GroovyTokenTypes.DEC:
102: case GroovyTokenTypes.DIGIT:
103: case GroovyTokenTypes.DIV:
104: case GroovyTokenTypes.DIV_ASSIGN:
105: case GroovyTokenTypes.DOLLAR:
106: case GroovyTokenTypes.DOT:
107: case GroovyTokenTypes.DYNAMIC_MEMBER:
108: case GroovyTokenTypes.ELIST:
109: case GroovyTokenTypes.EMPTY_STAT:
110: case GroovyTokenTypes.ENUM_CONSTANT_DEF:
111: case GroovyTokenTypes.ENUM_DEF:
112: case GroovyTokenTypes.EOF:
113: case GroovyTokenTypes.EQUAL:
114: case GroovyTokenTypes.ESC:
115: case GroovyTokenTypes.EXPONENT:
116: case GroovyTokenTypes.EXPR:
117: case GroovyTokenTypes.FINAL:
118: case GroovyTokenTypes.FLOAT_SUFFIX:
119: case GroovyTokenTypes.FOR_CONDITION:
120: case GroovyTokenTypes.FOR_EACH_CLAUSE:
121: case GroovyTokenTypes.FOR_INIT:
122: case GroovyTokenTypes.FOR_IN_ITERABLE:
123: case GroovyTokenTypes.FOR_ITERATOR:
124: case GroovyTokenTypes.GE:
125: case GroovyTokenTypes.GT:
126: case GroovyTokenTypes.HEX_DIGIT:
127: case GroovyTokenTypes.IDENT:
128: case GroovyTokenTypes.IMPLICIT_PARAMETERS:
129: case GroovyTokenTypes.INC:
130: case GroovyTokenTypes.INDEX_OP:
131: case GroovyTokenTypes.INSTANCE_INIT:
132: case GroovyTokenTypes.INTERFACE_DEF:
133: case GroovyTokenTypes.LABELED_ARG:
134: case GroovyTokenTypes.LABELED_STAT:
135: case GroovyTokenTypes.LAND:
136: case GroovyTokenTypes.LBRACK:
137: case GroovyTokenTypes.LCURLY:
138: case GroovyTokenTypes.LE:
139: case GroovyTokenTypes.LETTER:
140: case GroovyTokenTypes.LIST_CONSTRUCTOR:
141: case GroovyTokenTypes.LNOT:
142: case GroovyTokenTypes.LOR:
143: case GroovyTokenTypes.LPAREN:
144: case GroovyTokenTypes.LT:
145: case GroovyTokenTypes.MAP_CONSTRUCTOR:
146: case GroovyTokenTypes.MEMBER_POINTER:
147: case GroovyTokenTypes.METHOD_CALL:
148: case GroovyTokenTypes.METHOD_DEF:
149: case GroovyTokenTypes.MINUS:
150: case GroovyTokenTypes.MINUS_ASSIGN:
151: case GroovyTokenTypes.ML_COMMENT:
152: case GroovyTokenTypes.MOD:
153: case GroovyTokenTypes.MODIFIERS:
154: case GroovyTokenTypes.MOD_ASSIGN:
155: case GroovyTokenTypes.NLS:
156: case GroovyTokenTypes.NOT_EQUAL:
157: case GroovyTokenTypes.NULL_TREE_LOOKAHEAD:
158: case GroovyTokenTypes.NUM_BIG_DECIMAL:
159: case GroovyTokenTypes.NUM_BIG_INT:
160: case GroovyTokenTypes.NUM_DOUBLE:
161: case GroovyTokenTypes.NUM_FLOAT:
162: case GroovyTokenTypes.NUM_INT:
163: case GroovyTokenTypes.NUM_LONG:
164: case GroovyTokenTypes.OBJBLOCK:
165: case GroovyTokenTypes.ONE_NL:
166: case GroovyTokenTypes.OPTIONAL_DOT:
167: case GroovyTokenTypes.PARAMETERS:
168: case GroovyTokenTypes.PARAMETER_DEF:
169: case GroovyTokenTypes.PLUS:
170: case GroovyTokenTypes.PLUS_ASSIGN:
171: case GroovyTokenTypes.POST_DEC:
172: case GroovyTokenTypes.POST_INC:
173: case GroovyTokenTypes.QUESTION:
174: case GroovyTokenTypes.RANGE_EXCLUSIVE:
175: case GroovyTokenTypes.RANGE_INCLUSIVE:
176: case GroovyTokenTypes.RBRACK:
177: case GroovyTokenTypes.RCURLY:
178: case GroovyTokenTypes.REGEXP_CTOR_END:
179: case GroovyTokenTypes.REGEXP_SYMBOL:
180: case GroovyTokenTypes.REGEX_FIND:
181: case GroovyTokenTypes.REGEX_MATCH:
182: case GroovyTokenTypes.RPAREN:
183: case GroovyTokenTypes.SCOPE_ESCAPE:
184: case GroovyTokenTypes.SELECT_SLOT:
185: case GroovyTokenTypes.SEMI:
186: case GroovyTokenTypes.SH_COMMENT:
187: case GroovyTokenTypes.SL:
188: case GroovyTokenTypes.SLIST:
189: case GroovyTokenTypes.SL_ASSIGN:
190: case GroovyTokenTypes.SL_COMMENT:
191: case GroovyTokenTypes.SPREAD_ARG:
192: case GroovyTokenTypes.SPREAD_DOT:
193: case GroovyTokenTypes.SPREAD_MAP_ARG:
194: case GroovyTokenTypes.SR:
195: case GroovyTokenTypes.SR_ASSIGN:
196: case GroovyTokenTypes.STAR:
197: case GroovyTokenTypes.STAR_ASSIGN:
198: case GroovyTokenTypes.STAR_STAR:
199: case GroovyTokenTypes.STAR_STAR_ASSIGN:
200: case GroovyTokenTypes.STATIC_IMPORT:
201: case GroovyTokenTypes.STATIC_INIT:
202: case GroovyTokenTypes.STRICTFP:
203: case GroovyTokenTypes.STRING_CH:
204: case GroovyTokenTypes.STRING_CONSTRUCTOR:
205: case GroovyTokenTypes.STRING_CTOR_END:
206: case GroovyTokenTypes.STRING_CTOR_MIDDLE:
207: case GroovyTokenTypes.STRING_CTOR_START:
208: case GroovyTokenTypes.STRING_NL:
209: case GroovyTokenTypes.SUPER_CTOR_CALL:
210: case GroovyTokenTypes.TRIPLE_DOT:
211: case GroovyTokenTypes.TYPECAST:
212: case GroovyTokenTypes.TYPE_ARGUMENT:
213: case GroovyTokenTypes.TYPE_ARGUMENTS:
214: case GroovyTokenTypes.TYPE_LOWER_BOUNDS:
215: case GroovyTokenTypes.TYPE_PARAMETER:
216: case GroovyTokenTypes.TYPE_PARAMETERS:
217: case GroovyTokenTypes.TYPE_UPPER_BOUNDS:
218: case GroovyTokenTypes.UNARY_MINUS:
219: case GroovyTokenTypes.UNARY_PLUS:
220: case GroovyTokenTypes.UNUSED_CONST:
221: case GroovyTokenTypes.UNUSED_DO:
222: case GroovyTokenTypes.UNUSED_GOTO:
223: case GroovyTokenTypes.VARIABLE_DEF:
224: case GroovyTokenTypes.VARIABLE_PARAMETER_DEF:
225: case GroovyTokenTypes.VOCAB:
226: case GroovyTokenTypes.WILDCARD_TYPE:
227: case GroovyTokenTypes.WS:
228: colour = black;
229: break;
230:
231: case GroovyTokenTypes.STRING_LITERAL:
232: case GroovyTokenTypes.REGEXP_LITERAL:
233: colour = green;
234: break;
235:
236: case GroovyTokenTypes.CLASS_DEF:
237: case GroovyTokenTypes.EXTENDS_CLAUSE:
238: case GroovyTokenTypes.IMPLEMENTS_CLAUSE:
239: case GroovyTokenTypes.IMPORT:
240: case GroovyTokenTypes.LITERAL_any:
241: case GroovyTokenTypes.LITERAL_as:
242: case GroovyTokenTypes.LITERAL_assert:
243: case GroovyTokenTypes.LITERAL_boolean:
244: case GroovyTokenTypes.LITERAL_break:
245: case GroovyTokenTypes.LITERAL_byte:
246: case GroovyTokenTypes.LITERAL_case:
247: case GroovyTokenTypes.LITERAL_catch:
248: case GroovyTokenTypes.LITERAL_char:
249: case GroovyTokenTypes.LITERAL_class:
250: case GroovyTokenTypes.LITERAL_continue:
251: case GroovyTokenTypes.LITERAL_def:
252: case GroovyTokenTypes.LITERAL_default:
253: case GroovyTokenTypes.LITERAL_double:
254: case GroovyTokenTypes.LITERAL_else:
255: case GroovyTokenTypes.LITERAL_enum:
256: case GroovyTokenTypes.LITERAL_extends:
257: case GroovyTokenTypes.LITERAL_false:
258: case GroovyTokenTypes.LITERAL_finally:
259: case GroovyTokenTypes.LITERAL_float:
260: case GroovyTokenTypes.LITERAL_for:
261: case GroovyTokenTypes.LITERAL_if:
262: case GroovyTokenTypes.LITERAL_implements :
263: case GroovyTokenTypes.LITERAL_import:
264: case GroovyTokenTypes.LITERAL_in:
265: case GroovyTokenTypes.LITERAL_instanceof :
266: case GroovyTokenTypes.LITERAL_int:
267: case GroovyTokenTypes.LITERAL_interface:
268: case GroovyTokenTypes.LITERAL_long:
269: case GroovyTokenTypes.LITERAL_native:
270: case GroovyTokenTypes.LITERAL_new:
271: case GroovyTokenTypes.LITERAL_null:
272: case GroovyTokenTypes.LITERAL_package:
273: case GroovyTokenTypes.LITERAL_private:
274: case GroovyTokenTypes.LITERAL_protected:
275: case GroovyTokenTypes.LITERAL_public:
276: case GroovyTokenTypes.LITERAL_return:
277: case GroovyTokenTypes.LITERAL_short:
278: case GroovyTokenTypes.LITERAL_static:
279: case GroovyTokenTypes.LITERAL_super :
280: case GroovyTokenTypes.LITERAL_switch:
281: case GroovyTokenTypes.LITERAL_synchronized:
282: case GroovyTokenTypes.LITERAL_this :
283: case GroovyTokenTypes.LITERAL_threadsafe:
284: case GroovyTokenTypes.LITERAL_throw:
285: case GroovyTokenTypes.LITERAL_throws:
286: case GroovyTokenTypes.LITERAL_transient:
287: case GroovyTokenTypes.LITERAL_true:
288: case GroovyTokenTypes.LITERAL_try:
289: case GroovyTokenTypes.LITERAL_void:
290: case GroovyTokenTypes.LITERAL_volatile:
291: case GroovyTokenTypes.LITERAL_while:
292: case GroovyTokenTypes.LITERAL_with:
293: case GroovyTokenTypes.PACKAGE_DEF:
294: case GroovyTokenTypes.TYPE:
295: colour = blue;
296: break;
297:
298: default:
299: colour = black;
300: break;
301: }
302: return colour;
303: }
304:
305: public void push(GroovySourceAST t) {
306: stack.push(t);
307: }
308:
309: public GroovySourceAST pop() {
310: if (!stack.empty()) {
311: return (GroovySourceAST) stack.pop();
312: }
313: return null;
314: }
315: }
|