View Javadoc

    1   /***
    2    *
    3    * Copyright 2005 Jeremy Rayner
    4    *
    5    * Licensed under the Apache License, Version 2.0 (the "License");
    6    * you may not use this file except in compliance with the License.
    7    * You may obtain a copy of the License at
    8    *
    9    * http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   **/
    18  package org.codehaus.groovy.antlr.treewalker;
    19
    20  import java.io.PrintStream;
    21  import java.util.Stack;
    22
    23  import org.codehaus.groovy.antlr.GroovySourceAST;
    24  import org.codehaus.groovy.antlr.parser.GroovyTokenTypes;
    25
    26  /***
    27   * A visitor that prints a html tags of each node to the supplied PrintStream
    28   *
    29   * @author <a href="mailto:groovy@ross-rayner.com">Jeremy Rayner</a>
    30   * @version $Revision: 4526 $
    31   */
    32
    33  public class NodeAsHTMLPrinter extends VisitorAdapter {
    34      private String[] tokenNames;
    35      private PrintStream out;
    36      private Stack stack;
    37
    38      /***
    39       * A visitor that prints a html tags, for each node, to the supplied PrintStream.
    40       * @param out supplied PrintStream to output nodes to
    41       * @param tokenNames an array of token names to use
    42       */
    43      public NodeAsHTMLPrinter(PrintStream out,String[] tokenNames) {
    44          this.tokenNames = tokenNames;
    45          this.out = out;
    46          this.stack = new Stack();
    47      }
    48
    49      public void setUp() {
    50          out.println("<html><head></head><body><pre>");
    51      }
    52
    53      public void visitDefault(GroovySourceAST t,int visit) {
    54          if (visit == OPENING_VISIT) {
    55              out.print("<code title='" + tokenNames[t.getType()] + "'><font color='#" + colour(t) + "'>");
    56          } else {
    57              out.print("</font></code>");
    58          }
    59      }
    60
    61      public void tearDown() {
    62          out.println("</pre></body></html>");
    63      }
    64
    65      private String colour(GroovySourceAST t) {
    66          String black = "000000";
    67          String blue = "17178B";
    68          String green = "008000";
    69          //String purple = "7C308D";
    70          String colour = black;
    71          switch (t.getType()) {
    72              case GroovyTokenTypes.ABSTRACT                      :
    73              case GroovyTokenTypes.ANNOTATION                    :
    74              case GroovyTokenTypes.ANNOTATIONS                   :
    75              case GroovyTokenTypes.ANNOTATION_ARRAY_INIT         :
    76              case GroovyTokenTypes.ANNOTATION_DEF                :
    77              case GroovyTokenTypes.ANNOTATION_FIELD_DEF          :
    78              case GroovyTokenTypes.ANNOTATION_MEMBER_VALUE_PAIR  :
    79              case GroovyTokenTypes.ARRAY_DECLARATOR              :
    80              case GroovyTokenTypes.ASSIGN                        :
    81              case GroovyTokenTypes.AT                            :
    82              case GroovyTokenTypes.BAND                          :
    83              case GroovyTokenTypes.BAND_ASSIGN                   :
    84              case GroovyTokenTypes.BIG_SUFFIX                    :
    85              case GroovyTokenTypes.BLOCK                         :
    86              case GroovyTokenTypes.BNOT                          :
    87              case GroovyTokenTypes.BOR                           :
    88              case GroovyTokenTypes.BOR_ASSIGN                    :
    89              case GroovyTokenTypes.BSR                           :
    90              case GroovyTokenTypes.BSR_ASSIGN                    :
    91              case GroovyTokenTypes.BXOR                          :
    92              case GroovyTokenTypes.BXOR_ASSIGN                   :
    93              case GroovyTokenTypes.CASE_GROUP                    :
    94              case GroovyTokenTypes.CLOSABLE_BLOCK                :
    95              case GroovyTokenTypes.CLOSABLE_BLOCK_OP             :
    96              case GroovyTokenTypes.COLON                         :
    97              case GroovyTokenTypes.COMMA                         :
    98              case GroovyTokenTypes.COMPARE_TO                    :
    99              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     public GroovySourceAST pop() {
    309         if (!stack.empty()) {
    310             return (GroovySourceAST) stack.pop();
    311         }
    312         return null;
    313     }
    314 }