001: package org.codehaus.groovy.antlr.java;
002:
003: import org.codehaus.groovy.antlr.GroovySourceAST;
004: import org.codehaus.groovy.antlr.parser.GroovyTokenTypes;
005: import org.codehaus.groovy.antlr.treewalker.VisitorAdapter;
006:
007: public class Java2GroovyConverter extends VisitorAdapter {
008: private String[] tokenNames;
009: private int[] typeMapping;
010:
011: public Java2GroovyConverter(String[] tokenNames) {
012: this .tokenNames = tokenNames;
013: typeMapping = new int[400]; // magic number, much greater than current number of java tokens
014: typeMapping[JavaTokenTypes.ABSTRACT] = GroovyTokenTypes.ABSTRACT;
015:
016: typeMapping[JavaTokenTypes.EOF] = GroovyTokenTypes.EOF;
017: typeMapping[JavaTokenTypes.NULL_TREE_LOOKAHEAD] = GroovyTokenTypes.NULL_TREE_LOOKAHEAD;
018: typeMapping[JavaTokenTypes.BLOCK] = GroovyTokenTypes.BLOCK;
019: typeMapping[JavaTokenTypes.MODIFIERS] = GroovyTokenTypes.MODIFIERS;
020: typeMapping[JavaTokenTypes.OBJBLOCK] = GroovyTokenTypes.OBJBLOCK;
021: typeMapping[JavaTokenTypes.SLIST] = GroovyTokenTypes.SLIST;
022: typeMapping[JavaTokenTypes.METHOD_DEF] = GroovyTokenTypes.METHOD_DEF;
023: typeMapping[JavaTokenTypes.VARIABLE_DEF] = GroovyTokenTypes.VARIABLE_DEF;
024: typeMapping[JavaTokenTypes.INSTANCE_INIT] = GroovyTokenTypes.INSTANCE_INIT;
025: typeMapping[JavaTokenTypes.STATIC_INIT] = GroovyTokenTypes.STATIC_INIT;
026: typeMapping[JavaTokenTypes.TYPE] = GroovyTokenTypes.TYPE;
027: typeMapping[JavaTokenTypes.CLASS_DEF] = GroovyTokenTypes.CLASS_DEF;
028: typeMapping[JavaTokenTypes.INTERFACE_DEF] = GroovyTokenTypes.INTERFACE_DEF;
029: typeMapping[JavaTokenTypes.PACKAGE_DEF] = GroovyTokenTypes.PACKAGE_DEF;
030: typeMapping[JavaTokenTypes.ARRAY_DECLARATOR] = GroovyTokenTypes.ARRAY_DECLARATOR;
031: typeMapping[JavaTokenTypes.EXTENDS_CLAUSE] = GroovyTokenTypes.EXTENDS_CLAUSE;
032: typeMapping[JavaTokenTypes.IMPLEMENTS_CLAUSE] = GroovyTokenTypes.IMPLEMENTS_CLAUSE;
033: typeMapping[JavaTokenTypes.PARAMETERS] = GroovyTokenTypes.PARAMETERS;
034: typeMapping[JavaTokenTypes.PARAMETER_DEF] = GroovyTokenTypes.PARAMETER_DEF;
035: typeMapping[JavaTokenTypes.LABELED_STAT] = GroovyTokenTypes.LABELED_STAT;
036: typeMapping[JavaTokenTypes.TYPECAST] = GroovyTokenTypes.TYPECAST;
037: typeMapping[JavaTokenTypes.INDEX_OP] = GroovyTokenTypes.INDEX_OP;
038: typeMapping[JavaTokenTypes.POST_INC] = GroovyTokenTypes.POST_INC;
039: typeMapping[JavaTokenTypes.POST_DEC] = GroovyTokenTypes.POST_DEC;
040: typeMapping[JavaTokenTypes.METHOD_CALL] = GroovyTokenTypes.METHOD_CALL;
041: typeMapping[JavaTokenTypes.EXPR] = GroovyTokenTypes.EXPR;
042: // typeMapping[JavaTokenTypes.ARRAY_INIT] = GroovyTokenTypes.ARRAY_INIT;
043: typeMapping[JavaTokenTypes.IMPORT] = GroovyTokenTypes.IMPORT;
044: typeMapping[JavaTokenTypes.UNARY_MINUS] = GroovyTokenTypes.UNARY_MINUS;
045: typeMapping[JavaTokenTypes.UNARY_PLUS] = GroovyTokenTypes.UNARY_PLUS;
046: typeMapping[JavaTokenTypes.CASE_GROUP] = GroovyTokenTypes.CASE_GROUP;
047: typeMapping[JavaTokenTypes.ELIST] = GroovyTokenTypes.ELIST;
048: typeMapping[JavaTokenTypes.FOR_INIT] = GroovyTokenTypes.FOR_INIT;
049: typeMapping[JavaTokenTypes.FOR_CONDITION] = GroovyTokenTypes.FOR_CONDITION;
050: typeMapping[JavaTokenTypes.FOR_ITERATOR] = GroovyTokenTypes.FOR_ITERATOR;
051: typeMapping[JavaTokenTypes.EMPTY_STAT] = GroovyTokenTypes.EMPTY_STAT;
052: typeMapping[JavaTokenTypes.FINAL] = GroovyTokenTypes.FINAL;
053: typeMapping[JavaTokenTypes.ABSTRACT] = GroovyTokenTypes.ABSTRACT;
054: typeMapping[JavaTokenTypes.STRICTFP] = GroovyTokenTypes.STRICTFP;
055: typeMapping[JavaTokenTypes.SUPER_CTOR_CALL] = GroovyTokenTypes.SUPER_CTOR_CALL;
056: typeMapping[JavaTokenTypes.CTOR_CALL] = GroovyTokenTypes.CTOR_CALL;
057: typeMapping[JavaTokenTypes.VARIABLE_PARAMETER_DEF] = GroovyTokenTypes.VARIABLE_PARAMETER_DEF;
058: typeMapping[JavaTokenTypes.STATIC_IMPORT] = GroovyTokenTypes.STATIC_IMPORT;
059: typeMapping[JavaTokenTypes.ENUM_DEF] = GroovyTokenTypes.ENUM_DEF;
060: typeMapping[JavaTokenTypes.ENUM_CONSTANT_DEF] = GroovyTokenTypes.ENUM_CONSTANT_DEF;
061: typeMapping[JavaTokenTypes.FOR_EACH_CLAUSE] = GroovyTokenTypes.FOR_EACH_CLAUSE;
062: typeMapping[JavaTokenTypes.ANNOTATION_DEF] = GroovyTokenTypes.ANNOTATION_DEF;
063: typeMapping[JavaTokenTypes.ANNOTATIONS] = GroovyTokenTypes.ANNOTATIONS;
064: typeMapping[JavaTokenTypes.ANNOTATION] = GroovyTokenTypes.ANNOTATION;
065: typeMapping[JavaTokenTypes.ANNOTATION_MEMBER_VALUE_PAIR] = GroovyTokenTypes.ANNOTATION_MEMBER_VALUE_PAIR;
066: typeMapping[JavaTokenTypes.ANNOTATION_FIELD_DEF] = GroovyTokenTypes.ANNOTATION_FIELD_DEF;
067: typeMapping[JavaTokenTypes.ANNOTATION_ARRAY_INIT] = GroovyTokenTypes.ANNOTATION_ARRAY_INIT;
068: typeMapping[JavaTokenTypes.TYPE_ARGUMENTS] = GroovyTokenTypes.TYPE_ARGUMENTS;
069: typeMapping[JavaTokenTypes.TYPE_ARGUMENT] = GroovyTokenTypes.TYPE_ARGUMENT;
070: typeMapping[JavaTokenTypes.TYPE_PARAMETERS] = GroovyTokenTypes.TYPE_PARAMETERS;
071: typeMapping[JavaTokenTypes.TYPE_PARAMETER] = GroovyTokenTypes.TYPE_PARAMETER;
072: typeMapping[JavaTokenTypes.WILDCARD_TYPE] = GroovyTokenTypes.WILDCARD_TYPE;
073: typeMapping[JavaTokenTypes.TYPE_UPPER_BOUNDS] = GroovyTokenTypes.TYPE_UPPER_BOUNDS;
074: typeMapping[JavaTokenTypes.TYPE_LOWER_BOUNDS] = GroovyTokenTypes.TYPE_LOWER_BOUNDS;
075: typeMapping[JavaTokenTypes.LITERAL_package] = GroovyTokenTypes.LITERAL_package;
076: typeMapping[JavaTokenTypes.SEMI] = GroovyTokenTypes.SEMI;
077: typeMapping[JavaTokenTypes.LITERAL_import] = GroovyTokenTypes.LITERAL_import;
078: typeMapping[JavaTokenTypes.LITERAL_static] = GroovyTokenTypes.LITERAL_static;
079: typeMapping[JavaTokenTypes.LBRACK] = GroovyTokenTypes.LBRACK;
080: typeMapping[JavaTokenTypes.RBRACK] = GroovyTokenTypes.RBRACK;
081: typeMapping[JavaTokenTypes.IDENT] = GroovyTokenTypes.IDENT;
082: typeMapping[JavaTokenTypes.DOT] = GroovyTokenTypes.DOT;
083: typeMapping[JavaTokenTypes.QUESTION] = GroovyTokenTypes.QUESTION;
084: typeMapping[JavaTokenTypes.LITERAL_extends] = GroovyTokenTypes.LITERAL_extends;
085: typeMapping[JavaTokenTypes.LITERAL_super ] = GroovyTokenTypes.LITERAL_super ;
086: typeMapping[JavaTokenTypes.LT] = GroovyTokenTypes.LT;
087: typeMapping[JavaTokenTypes.COMMA] = GroovyTokenTypes.COMMA;
088: typeMapping[JavaTokenTypes.GT] = GroovyTokenTypes.GT;
089: typeMapping[JavaTokenTypes.SR] = GroovyTokenTypes.SR;
090: typeMapping[JavaTokenTypes.BSR] = GroovyTokenTypes.BSR;
091: typeMapping[JavaTokenTypes.LITERAL_void] = GroovyTokenTypes.LITERAL_void;
092: typeMapping[JavaTokenTypes.LITERAL_boolean] = GroovyTokenTypes.LITERAL_boolean;
093: typeMapping[JavaTokenTypes.LITERAL_byte] = GroovyTokenTypes.LITERAL_byte;
094: typeMapping[JavaTokenTypes.LITERAL_char] = GroovyTokenTypes.LITERAL_char;
095: typeMapping[JavaTokenTypes.LITERAL_short] = GroovyTokenTypes.LITERAL_short;
096: typeMapping[JavaTokenTypes.LITERAL_int] = GroovyTokenTypes.LITERAL_int;
097: typeMapping[JavaTokenTypes.LITERAL_float] = GroovyTokenTypes.LITERAL_float;
098: typeMapping[JavaTokenTypes.LITERAL_long] = GroovyTokenTypes.LITERAL_long;
099: typeMapping[JavaTokenTypes.LITERAL_double] = GroovyTokenTypes.LITERAL_double;
100: typeMapping[JavaTokenTypes.STAR] = GroovyTokenTypes.STAR;
101: typeMapping[JavaTokenTypes.LITERAL_private] = GroovyTokenTypes.LITERAL_private;
102: typeMapping[JavaTokenTypes.LITERAL_public] = GroovyTokenTypes.LITERAL_public;
103: typeMapping[JavaTokenTypes.LITERAL_protected] = GroovyTokenTypes.LITERAL_protected;
104: typeMapping[JavaTokenTypes.LITERAL_transient] = GroovyTokenTypes.LITERAL_transient;
105: typeMapping[JavaTokenTypes.LITERAL_native] = GroovyTokenTypes.LITERAL_native;
106: typeMapping[JavaTokenTypes.LITERAL_threadsafe] = GroovyTokenTypes.LITERAL_threadsafe;
107: typeMapping[JavaTokenTypes.LITERAL_synchronized] = GroovyTokenTypes.LITERAL_synchronized;
108: typeMapping[JavaTokenTypes.LITERAL_volatile] = GroovyTokenTypes.LITERAL_volatile;
109: typeMapping[JavaTokenTypes.AT] = GroovyTokenTypes.AT;
110: typeMapping[JavaTokenTypes.LPAREN] = GroovyTokenTypes.LPAREN;
111: typeMapping[JavaTokenTypes.RPAREN] = GroovyTokenTypes.RPAREN;
112: typeMapping[JavaTokenTypes.ASSIGN] = GroovyTokenTypes.ASSIGN;
113: typeMapping[JavaTokenTypes.LCURLY] = GroovyTokenTypes.LCURLY;
114: typeMapping[JavaTokenTypes.RCURLY] = GroovyTokenTypes.RCURLY;
115: typeMapping[JavaTokenTypes.LITERAL_class] = GroovyTokenTypes.LITERAL_class;
116: typeMapping[JavaTokenTypes.LITERAL_interface] = GroovyTokenTypes.LITERAL_interface;
117: typeMapping[JavaTokenTypes.LITERAL_enum] = GroovyTokenTypes.LITERAL_enum;
118: typeMapping[JavaTokenTypes.BAND] = GroovyTokenTypes.BAND;
119: typeMapping[JavaTokenTypes.LITERAL_default] = GroovyTokenTypes.LITERAL_default;
120: typeMapping[JavaTokenTypes.LITERAL_implements ] = GroovyTokenTypes.LITERAL_implements ;
121: typeMapping[JavaTokenTypes.LITERAL_this ] = GroovyTokenTypes.LITERAL_this ;
122: typeMapping[JavaTokenTypes.LITERAL_throws] = GroovyTokenTypes.LITERAL_throws;
123: typeMapping[JavaTokenTypes.TRIPLE_DOT] = GroovyTokenTypes.TRIPLE_DOT;
124: typeMapping[JavaTokenTypes.COLON] = GroovyTokenTypes.COLON;
125: typeMapping[JavaTokenTypes.LITERAL_if] = GroovyTokenTypes.LITERAL_if;
126: typeMapping[JavaTokenTypes.LITERAL_else] = GroovyTokenTypes.LITERAL_else;
127: typeMapping[JavaTokenTypes.LITERAL_while] = GroovyTokenTypes.LITERAL_while;
128: typeMapping[JavaTokenTypes.LITERAL_break] = GroovyTokenTypes.LITERAL_break;
129: typeMapping[JavaTokenTypes.LITERAL_continue] = GroovyTokenTypes.LITERAL_continue;
130: typeMapping[JavaTokenTypes.LITERAL_return] = GroovyTokenTypes.LITERAL_return;
131: typeMapping[JavaTokenTypes.LITERAL_switch] = GroovyTokenTypes.LITERAL_switch;
132: typeMapping[JavaTokenTypes.LITERAL_throw] = GroovyTokenTypes.LITERAL_throw;
133: typeMapping[JavaTokenTypes.LITERAL_assert] = GroovyTokenTypes.LITERAL_assert;
134: typeMapping[JavaTokenTypes.LITERAL_for] = GroovyTokenTypes.LITERAL_for;
135: typeMapping[JavaTokenTypes.LITERAL_case] = GroovyTokenTypes.LITERAL_case;
136: typeMapping[JavaTokenTypes.LITERAL_try] = GroovyTokenTypes.LITERAL_try;
137: typeMapping[JavaTokenTypes.LITERAL_finally] = GroovyTokenTypes.LITERAL_finally;
138: typeMapping[JavaTokenTypes.LITERAL_catch] = GroovyTokenTypes.LITERAL_catch;
139: typeMapping[JavaTokenTypes.PLUS_ASSIGN] = GroovyTokenTypes.PLUS_ASSIGN;
140: typeMapping[JavaTokenTypes.MINUS_ASSIGN] = GroovyTokenTypes.MINUS_ASSIGN;
141: typeMapping[JavaTokenTypes.STAR_ASSIGN] = GroovyTokenTypes.STAR_ASSIGN;
142: typeMapping[JavaTokenTypes.DIV_ASSIGN] = GroovyTokenTypes.DIV_ASSIGN;
143: typeMapping[JavaTokenTypes.MOD_ASSIGN] = GroovyTokenTypes.MOD_ASSIGN;
144: typeMapping[JavaTokenTypes.SR_ASSIGN] = GroovyTokenTypes.SR_ASSIGN;
145: typeMapping[JavaTokenTypes.BSR_ASSIGN] = GroovyTokenTypes.BSR_ASSIGN;
146: typeMapping[JavaTokenTypes.SL_ASSIGN] = GroovyTokenTypes.SL_ASSIGN;
147: typeMapping[JavaTokenTypes.BAND_ASSIGN] = GroovyTokenTypes.BAND_ASSIGN;
148: typeMapping[JavaTokenTypes.BXOR_ASSIGN] = GroovyTokenTypes.BXOR_ASSIGN;
149: typeMapping[JavaTokenTypes.BOR_ASSIGN] = GroovyTokenTypes.BOR_ASSIGN;
150: typeMapping[JavaTokenTypes.LOR] = GroovyTokenTypes.LOR;
151: typeMapping[JavaTokenTypes.LAND] = GroovyTokenTypes.LAND;
152: typeMapping[JavaTokenTypes.BOR] = GroovyTokenTypes.BOR;
153: typeMapping[JavaTokenTypes.BXOR] = GroovyTokenTypes.BXOR;
154: typeMapping[JavaTokenTypes.NOT_EQUAL] = GroovyTokenTypes.NOT_EQUAL;
155: typeMapping[JavaTokenTypes.EQUAL] = GroovyTokenTypes.EQUAL;
156: typeMapping[JavaTokenTypes.LE] = GroovyTokenTypes.LE;
157: typeMapping[JavaTokenTypes.GE] = GroovyTokenTypes.GE;
158: typeMapping[JavaTokenTypes.LITERAL_instanceof ] = GroovyTokenTypes.LITERAL_instanceof ;
159: typeMapping[JavaTokenTypes.SL] = GroovyTokenTypes.SL;
160: typeMapping[JavaTokenTypes.PLUS] = GroovyTokenTypes.PLUS;
161: typeMapping[JavaTokenTypes.MINUS] = GroovyTokenTypes.MINUS;
162: typeMapping[JavaTokenTypes.DIV] = GroovyTokenTypes.DIV;
163: typeMapping[JavaTokenTypes.MOD] = GroovyTokenTypes.MOD;
164: typeMapping[JavaTokenTypes.INC] = GroovyTokenTypes.INC;
165: typeMapping[JavaTokenTypes.DEC] = GroovyTokenTypes.DEC;
166: typeMapping[JavaTokenTypes.BNOT] = GroovyTokenTypes.BNOT;
167: typeMapping[JavaTokenTypes.LNOT] = GroovyTokenTypes.LNOT;
168: typeMapping[JavaTokenTypes.LITERAL_true] = GroovyTokenTypes.LITERAL_true;
169: typeMapping[JavaTokenTypes.LITERAL_false] = GroovyTokenTypes.LITERAL_false;
170: typeMapping[JavaTokenTypes.LITERAL_null] = GroovyTokenTypes.LITERAL_null;
171: typeMapping[JavaTokenTypes.LITERAL_new] = GroovyTokenTypes.LITERAL_new;
172: typeMapping[JavaTokenTypes.NUM_INT] = GroovyTokenTypes.NUM_INT;
173: typeMapping[JavaTokenTypes.STRING_LITERAL] = GroovyTokenTypes.STRING_LITERAL;
174: typeMapping[JavaTokenTypes.NUM_FLOAT] = GroovyTokenTypes.NUM_FLOAT;
175: typeMapping[JavaTokenTypes.NUM_LONG] = GroovyTokenTypes.NUM_LONG;
176: typeMapping[JavaTokenTypes.NUM_DOUBLE] = GroovyTokenTypes.NUM_DOUBLE;
177: typeMapping[JavaTokenTypes.WS] = GroovyTokenTypes.WS;
178: typeMapping[JavaTokenTypes.SL_COMMENT] = GroovyTokenTypes.SL_COMMENT;
179: typeMapping[JavaTokenTypes.ML_COMMENT] = GroovyTokenTypes.ML_COMMENT;
180: typeMapping[JavaTokenTypes.ESC] = GroovyTokenTypes.ESC;
181: typeMapping[JavaTokenTypes.HEX_DIGIT] = GroovyTokenTypes.HEX_DIGIT;
182: typeMapping[JavaTokenTypes.VOCAB] = GroovyTokenTypes.VOCAB;
183: typeMapping[JavaTokenTypes.EXPONENT] = GroovyTokenTypes.EXPONENT;
184: typeMapping[JavaTokenTypes.FLOAT_SUFFIX] = GroovyTokenTypes.FLOAT_SUFFIX;
185: }
186:
187: public void visitDefault(GroovySourceAST t, int visit) {
188: if (visit == OPENING_VISIT) {
189: // only want to do this once per node...
190: t.setType(typeMapping[t.getType()]);
191: // ----
192:
193: // need to remove double quotes in string literals
194: // as groovy AST doesn't expect to have them
195: if (t.getType() == GroovyTokenTypes.STRING_LITERAL) {
196: String text = t.getText();
197: if (isDoubleQuoted(text)) {
198: t.setText(text.substring(1, text.length() - 1)); // chop off the double quotes at start and end
199: }
200: }
201: }
202: }
203:
204: private boolean isDoubleQuoted(String text) {
205: return text != null && text.length() > 2
206: && text.charAt(0) == '"'
207: && text.charAt(text.length() - 1) == '"';
208: }
209: }
|