sourceprinter.java

来自「Groovy动态语言 运行在JVM中的动态语言 可以方便的处理业务逻辑变化大的业」· Java 代码 · 共 1,060 行 · 第 1/3 页

JAVA
1,060
字号
/**
 *
 * Copyright 2005 Jeremy Rayner
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **/
package org.codehaus.groovy.antlr.treewalker;

import java.io.PrintStream;
import java.util.Stack;

import org.codehaus.groovy.antlr.GroovySourceAST;
import org.codehaus.groovy.antlr.parser.GroovyTokenTypes;

/**
 * An antlr AST visitor that prints groovy source code for each visited node
 * to the supplied PrintStream.
 *
 * @author <a href="mailto:groovy@ross-rayner.com">Jeremy Rayner</a>
 * @version $Revision: 4538 $
 */

public class SourcePrinter extends VisitorAdapter {
    private String[] tokenNames;
    private int tabLevel;
    private int lastLinePrinted;
    private boolean newLines;
    protected PrintStream out;
    private String className;
    private Stack stack;
    private int stringConstructorCounter;

    /**
     * A visitor that prints groovy source code for each node visited.
     * @param out where to print the source code to
     * @param tokenNames an array of token names from antlr
     */
    public SourcePrinter(PrintStream out,String[] tokenNames) {
        this(out,tokenNames,true);
    }

    /**
     * A visitor that prints groovy source code for each node visited.
     * @param out where to print the source code to
     * @param tokenNames an array of token names from antlr
     * @param newLines output newline character
     */
    public SourcePrinter(PrintStream out,String[] tokenNames, boolean newLines) {
        this.tokenNames = tokenNames;
        tabLevel = 0;
        lastLinePrinted = 0;
        this.out = out;
        this.newLines = newLines;
        this.stack = new Stack();
    }
    

	public void visitAbstract(GroovySourceAST t, int visit) {
		print(t,visit,"abstract ",null,null);
	}

	public void visitAnnotation(GroovySourceAST t, int visit) {
		if (visit == OPENING_VISIT) {
			print(t,visit,"@");
		}
		if (visit == SECOND_VISIT) {
			print(t,visit,"(");
		}
		if (visit == SUBSEQUENT_VISIT) {
			print(t,visit,", ");
		}
		if (visit == CLOSING_VISIT) {
			if (t.getNumberOfChildren() > 1) {
				print(t,visit,") ");
			} else {
				print(t,visit," ");
			}
		}

    }

    public void visitAnnotations(GroovySourceAST t, int visit) {
    	// do nothing
    }

    public void visitAnnotationDef(GroovySourceAST t,int visit) {
        print(t,visit,"@interface ",null,null);
    }

	public void visitAnnotationFieldDef(GroovySourceAST t, int visit) {
    	print(t,visit,"() ","default ",null);
	}

	public void visitAnnotationMemberValuePair(GroovySourceAST t, int visit) {
		print(t,visit," = ",null,null);
	}

	public void visitArrayDeclarator(GroovySourceAST t, int visit) {
		//<ARRAY_DECLARATOR>int</ARRAY_DECLARATOR> primes = new int(<ARRAY_DECLARATOR>5</ARRAY_DECLARATOR>)
		if (getParentNode().getType() == GroovyTokenTypes.TYPE ||
				getParentNode().getType() == GroovyTokenTypes.TYPECAST) { // ugly hack
			// type defintion, i.e.   int[] x;
			print(t,visit,null,null,"[]");
		} else {
			// usually in new, i.e.   def y = new int[5];
			print(t,visit,"[",null,"]");
		}
	}

	public void visitAssign(GroovySourceAST t,int visit) {
        print(t,visit," = ",null,null);
    }
	
    // visitAt() ...
    //   token type 'AT' should never be visited, as annotation definitions and usage, and
    //   direct field access should have all moved this token out of the way. No test needed.

	//   one of the BAND tokens is actually replaced by TYPE_UPPER_BOUNDS (e.g. class Foo<T extends C & I> {T t} )
    public void visitBand(GroovySourceAST t, int visit) {
        print(t,visit," & ",null,null);
    }

	public void visitBandAssign(GroovySourceAST t,int visit) {
        print(t,visit," &= ",null,null);
    }
	
    // visitBigSuffix() ...
	//   token type BIG_SUFFIX never created/visited, NUM_BIG_INT, NUM_BIG_DECIMAL instead...    
    
	// visitBlock() ...
	//   token type BLOCK never created/visited, see CLOSABLE_BLOCK etc...
	
	public void visitBnot(GroovySourceAST t, int visit) {
		print(t,visit,"~",null,null);
	}
	
	// Note: old closure syntax using BOR is deprecated, and also never creates/visits a BOR node
    public void visitBor(GroovySourceAST t, int visit) {
        print(t,visit," | ",null,null);
    }
	
	public void visitBorAssign(GroovySourceAST t,int visit) {
        print(t,visit," |= ",null,null);
    }
	
    public void visitBsr(GroovySourceAST t, int visit) {
        print(t,visit," >>> ",null,null);
    }
	
	public void visitBsrAssign(GroovySourceAST t,int visit) {
        print(t,visit," >>>= ",null,null);
    }
	
    public void visitBxor(GroovySourceAST t, int visit) {
        print(t,visit," ^ ",null,null);
    }
	
	public void visitBxorAssign(GroovySourceAST t,int visit) {
        print(t,visit," ^= ",null,null);
    }
	
    public void visitCaseGroup(GroovySourceAST t, int visit) {
        if (visit == OPENING_VISIT) {
            tabLevel++;
        }
        if (visit == CLOSING_VISIT) {
            tabLevel--;
        }
    }

    public void visitClassDef(GroovySourceAST t,int visit) {
        print(t,visit,"class ",null,null);

        if (visit == OPENING_VISIT) {
            // store name of class away for use in constructor ident
            className = t.childOfType(GroovyTokenTypes.IDENT).getText();
        }
    }

    public void visitClosedBlock(GroovySourceAST t, int visit) {
        printUpdatingTabLevel(t,visit,"{","-> ","}");
    }
    
    // visitClosureOp ...
	//   token type CLOSABLE_BLOCK_OP never created/visited, see CLOSABLE_BLOCK...
	

    // visitColon ...
    //   token type COLON never created/visited, see LABELED_STAT, FOR_IN_ITERABLE, 
    //   ASSERT, CASE, QUESTION, MAP_CONSTRUCTOR, LABELED_ARG, SPREAD_MAP_ARG

    // visitComma ...
    //   token type COMMA never created/visited,
    //   see TYPE_ARGUMENTS, ANNOTATION, many others ...
    
    public void visitCompareTo(GroovySourceAST t,int visit) {
        print(t,visit," <=> ",null,null);
    }

    public void visitCtorCall(GroovySourceAST t,int visit) {
        printUpdatingTabLevel(t,visit,"this("," ",")");
    }

    public void visitCtorIdent(GroovySourceAST t, int visit) {
        // use name of class for constructor from the class definition
        print(t,visit,className,null,null);
    }

    public void visitDec(GroovySourceAST t, int visit) {
    	print(t,visit,"--",null,null);
    }
    
    // visitDigit ...
    //    never created/visited
    
    public void visitDiv(GroovySourceAST t, int visit) {
        print(t,visit," / ",null,null);
    }

	public void visitDivAssign(GroovySourceAST t,int visit) {
        print(t,visit," /= ",null,null);
    }
	
    // visitDollar ...
    //   token type DOLLAR never created/visited, see SCOPE_ESCAPE instead
    
    public void visitDot(GroovySourceAST t,int visit) {
        print(t,visit,".",null,null);
    }
    
    public void visitDynamicMember(GroovySourceAST t, int visit) {
    	if (t.childOfType(GroovyTokenTypes.STRING_CONSTRUCTOR) == null) {
    		printUpdatingTabLevel(t,visit,"(",null,")");
    	}
    }
    
    public void visitElist(GroovySourceAST t,int visit) {
    	if (getParentNode().getType() == GroovyTokenTypes.ENUM_CONSTANT_DEF) {
    		print(t,visit,"(",", ",")");
    	} else {
    		print(t,visit,null,", ",null);
    	}
    }

    // visitEmptyStat ...
    //   token type EMPTY_STAT obsolete and should be removed, never visited/created
    
    public void visitEnumConstantDef(GroovySourceAST t,int visit) {
    	GroovySourceAST sibling = (GroovySourceAST)t.getNextSibling();
    	if (sibling != null && sibling.getType() == GroovyTokenTypes.ENUM_CONSTANT_DEF) {
    		print(t,visit,null,null,", ");
    	}
    }

    public void visitEnumDef(GroovySourceAST t,int visit) {
        print(t,visit,"enum ",null,null);
    }

    // visitEof ...
    //   token type EOF never visited/created

    public void visitEqual(GroovySourceAST t,int visit) {
        print(t,visit," == ",null,null);
    }

    // visitExponent ...
    //   token type EXPONENT only used by lexer, never visited/created
    
    public void visitExpr(GroovySourceAST t,int visit) {
    	// do nothing
    }

    public void visitExtendsClause(GroovySourceAST t,int visit) {
        if (visit == OPENING_VISIT) {
            if (t.getNumberOfChildren() != 0) {
                print(t,visit," extends ");
            }
        }
    }
    
	public void visitFinal(GroovySourceAST t, int visit) {
        print(t,visit,"final ",null,null);
	}

	// visitFloatSuffix ... never visited/created see NUM_DOUBLE or NUM_FLOAT instead
	
	public void visitForCondition(GroovySourceAST t, int visit) {
    	print(t,visit," ; ",null,null);
    }
	
	// visitForEachClause ... 
	//   FOR_EACH_CLAUSE obsolete and should be removed, never visited/created

    public void visitForInit(GroovySourceAST t, int visit) {
    	print(t,visit,"(",null,null);
    }
    
    public void visitForInIterable(GroovySourceAST t, int visit) {
        printUpdatingTabLevel(t,visit,"("," in ",") ");
    }

    public void visitForIterator(GroovySourceAST t, int visit) {
    	print(t,visit," ; ",null,")");
    }
    
    public void visitGe(GroovySourceAST t, int visit) {
    	print(t,visit," >= ",null,null);
    }
    
    public void visitGt(GroovySourceAST t, int visit) {
        print(t,visit," > ",null,null);
    }

    public void visitIdent(GroovySourceAST t,int visit) {
        print(t,visit,t.getText(),null,null);
    }
    public void visitImplementsClause(GroovySourceAST t,int visit) {
        if (visit == OPENING_VISIT) {
            if (t.getNumberOfChildren() != 0) {
                print(t,visit," implements ");
            }
        }
        if (visit == CLOSING_VISIT) {
            //space between classdef and objblock
            print(t,visit," ");
        }
    }

    public void visitImplicitParameters(GroovySourceAST t, int visit) {
    	// do nothing
    }

    public void visitImport(GroovySourceAST t,int visit) {
        print(t,visit,"import ",null,null);
    }

    public void visitInc(GroovySourceAST t, int visit) {
    	print(t,visit,"++",null,null);
    }

    public void visitIndexOp(GroovySourceAST t, int visit) {
        printUpdatingTabLevel(t,visit,"[",null,"]");
    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?