sourceprinter.java

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

JAVA
1,060
字号
    }

    // visitOneNl
    //   new lines are used by parser, but are not created on the AST,
    //   they can be implied by the source code line/column information

    public void visitOptionalDot(GroovySourceAST t,int visit) {
        print(t,visit,"?.",null,null);
    }
    
    public void visitPackageDef(GroovySourceAST t, int visit) {
        print(t,visit,"package ",null,null);
    }

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

    public void visitParameters(GroovySourceAST t,int visit) {
    	if (getParentNode().getType() == GroovyTokenTypes.CLOSABLE_BLOCK) {
    		printUpdatingTabLevel(t,visit,null,","," ");
    	} else {
    		printUpdatingTabLevel(t,visit,"(",", ",") ");
    	}
    }

    public void visitPlus(GroovySourceAST t, int visit) {
        print(t,visit," + ",null,null);
    }
    
    public void visitPlusAssign(GroovySourceAST t, int visit) {
        print(t,visit," += ",null,null);
    }
    public void visitPostDec(GroovySourceAST t, int visit) {
    	print(t,visit,null,null,"--");
    }

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

    public void visitQuestion(GroovySourceAST t, int visit) {
        // ternary operator
        print(t,visit,"?",":",null);
    }

    public void visitRangeExclusive(GroovySourceAST t, int visit) {
        print(t,visit,"..<",null,null);
    }

    public void visitRangeInclusive(GroovySourceAST t, int visit) {
        print(t,visit,"..",null,null);
    }

    // visit rbrack()
    //   token type RBRACK only used inside parser, never visited/created

    // visit rcurly()
    //   token type RCURLY only used inside parser, never visited/created

    // visit RegexpCtorEnd
    // visit RegexpLiteral
    // visit RegexpSymbol
    //    token types REGEXP_CTOR_END, REGEXP_LITERAL, REGEXP_SYMBOL only used inside lexer
    
    public void visitRegexFind(GroovySourceAST t, int visit) {
    	print(t,visit," =~ ",null,null);
    }
    public void visitRegexMatch(GroovySourceAST t, int visit) {
    	print(t,visit," ==~ ",null,null);
    }
    // visit rparen()
    //   token type RPAREN only used inside parser, never visited/created

    public void visitScopeEscape(GroovySourceAST t, int visit) {
    	print(t,visit,"$",null,null);
    }
    public void visitSelectSlot(GroovySourceAST t, int visit) {
    	print(t,visit,"@",null,null);
    }
    
    // visit semi()
    //  SEMI only used inside parser, never visited/created (see visitForCondition(), visitForIterator())
    
    // visit ShComment()
    //  never visited/created by parser
    
    public void visitSl(GroovySourceAST t, int visit) {
    	print(t,visit," << ",null,null);
    }
    public void visitSlAssign(GroovySourceAST t, int visit) {
    	print(t,visit," <<= ",null,null);
    }
    public void visitSlist(GroovySourceAST t,int visit) {
        if (visit == OPENING_VISIT) {
            tabLevel++;
            print(t,visit,"{");
        } else {
            tabLevel--;
            print(t,visit,"}");
        }
    }

    // visit SlComment()
    //   never visited/created by parser
    
    public void visitSpreadArg(GroovySourceAST t,int visit) {
    	print(t,visit,"*",null,null);
    }
    
    public void visitSpreadMapArg(GroovySourceAST t,int visit) {
    	print(t,visit,"*:",null,null);
    }
    
    public void visitSr(GroovySourceAST t, int visit) {
    	print(t,visit," >> ",null,null);
    }
    public void visitSrAssign(GroovySourceAST t, int visit) {
    	print(t,visit," >>= ",null,null);
    }

    public void visitStar(GroovySourceAST t,int visit) {
        print(t,visit,"*",null,null);
    }
    public void visitStarAssign(GroovySourceAST t, int visit) {
    	print(t,visit," *= ",null,null);
    }
    public void visitStarStar(GroovySourceAST t,int visit) {
        print(t,visit,"**",null,null);
    }
    public void visitStarStarAssign(GroovySourceAST t, int visit) {
    	print(t,visit," **= ",null,null);
    }
    
    public void visitStaticInit(GroovySourceAST t, int visit) {
    	print(t,visit,"static ",null,null);
    }
    public void visitStaticImport(GroovySourceAST t,int visit) {
        print(t,visit,"import static ",null,null);
    }
    public void visitStrictfp(GroovySourceAST t,int visit) {
    	print(t,visit,"strictfp ",null,null);
    }

    // visitStringch
    //   String characters only used by lexer, never visited/created directly


    public void visitStringConstructor(GroovySourceAST t,int visit) {
        if (visit == OPENING_VISIT) {
            stringConstructorCounter = 0;
            print(t,visit,"\"");
        }
        if (visit == SUBSEQUENT_VISIT) {
            // every other subsequent visit use an escaping $
            if (stringConstructorCounter % 2 == 0) {
               print(t,visit,"$");
            }
            stringConstructorCounter++;
        }
        if (visit == CLOSING_VISIT) {
            print(t,visit,"\"");
        }
    }

    public void visitStringLiteral(GroovySourceAST t,int visit) {
        if (visit == OPENING_VISIT) {
            String theString = escape(t.getText());
        if (getParentNode().getType() != GroovyTokenTypes.LABELED_ARG &&
            getParentNode().getType() != GroovyTokenTypes.STRING_CONSTRUCTOR) {
                theString = "\"" + theString + "\"";
            }
            print(t,visit,theString);
        }
    }

    private String escape(String literal) {
        literal = literal.replaceAll("\n","\\\\<<REMOVE>>n"); // can't seem to do \n in one go with Java regex
        literal = literal.replaceAll("<<REMOVE>>","");
        return literal;
    }

    public void visitSuperCtorCall(GroovySourceAST t,int visit) {
		printUpdatingTabLevel(t,visit,"super("," ",")");
    }
    
    // visit TripleDot, not used in the AST
    
    public void visitType(GroovySourceAST t,int visit) {
        GroovySourceAST parent = getParentNode();
        GroovySourceAST modifiers = parent.childOfType(GroovyTokenTypes.MODIFIERS);

        // No need to print 'def' if we already have some modifiers
        if (modifiers == null || modifiers.getNumberOfChildren() == 0) {

            if (visit == OPENING_VISIT) {
                if (t.getNumberOfChildren() == 0 && 
                		parent.getType() != GroovyTokenTypes.PARAMETER_DEF) { // no need for 'def' if in a parameter list
                    print(t,visit,"def");
                }
            }
        	if (visit == CLOSING_VISIT) {
        		print(t,visit," ");
            }
        } else {
        	if (visit == CLOSING_VISIT) {
        		if (t.getNumberOfChildren() != 0) {
        			print(t,visit," ");
        		}
        	}
        }
    }
    public void visitTypeArgument(GroovySourceAST t, int visit) {
    	// print nothing
    }

    public void visitTypeArguments(GroovySourceAST t, int visit) {
    	print(t,visit,"<",", ",">");
    }

    public void visitTypecast(GroovySourceAST t,int visit) {
        print(t,visit,"(",null,")");
    }
    public void visitTypeLowerBounds(GroovySourceAST t,int visit) {
        print(t,visit," super "," & ",null);
    }
    public void visitTypeParameter(GroovySourceAST t, int visit) {
    	// print nothing
    }

    public void visitTypeParameters(GroovySourceAST t, int visit) {
    	print(t,visit,"<",", ",">");
    }

    public void visitTypeUpperBounds(GroovySourceAST t,int visit) {
        print(t,visit," extends "," & ",null);
    }
    public void visitUnaryMinus(GroovySourceAST t, int visit) {
    	print(t,visit,"-",null,null);
    }
    public void visitUnaryPlus(GroovySourceAST t, int visit) {
    	print(t,visit,"+",null,null);
    }

    // visit Unused "const", "do", "goto" - unsurprisingly these are unused by the AST.
    
    public void visitVariableDef(GroovySourceAST t,int visit) {
        // do nothing
    }

    // a.k.a. "variable arity parameter" in the JLS
    public void visitVariableParameterDef(GroovySourceAST t,int visit) {
        print(t,visit,null,"... ",null);
    }
    
    // visit Vocab - only used by Lexer
    
    public void visitWildcardType(GroovySourceAST t, int visit) {
    	print(t,visit,"?",null,null);
    }

    // visit WS - only used by lexer
    
    
    
    public void visitDefault(GroovySourceAST t,int visit) {
        if (visit == OPENING_VISIT) {
            print(t,visit,"<" + tokenNames[t.getType()] + ">");
            //out.print("<" + t.getType() + ">");
        } else {
            print(t,visit,"</" + tokenNames[t.getType()] + ">");
            //out.print("</" + t.getType() + ">");
        }
    }

    protected void printUpdatingTabLevel(GroovySourceAST t,int visit,String opening, String subsequent, String closing) {
        if (visit == OPENING_VISIT && opening != null) {
            print(t,visit,opening);
            tabLevel++;
        }
        if (visit == SUBSEQUENT_VISIT && subsequent != null) {
            print(t,visit,subsequent);
        }
        if (visit == CLOSING_VISIT && closing != null) {
            tabLevel--;
            print(t,visit,closing);
        }
    }

    protected void print(GroovySourceAST t,int visit,String opening, String subsequent, String closing) {
        if (visit == OPENING_VISIT && opening != null) {
            print(t,visit,opening);
        }
        if (visit == SUBSEQUENT_VISIT && subsequent != null) {
            print(t,visit,subsequent);
        }
        if (visit == CLOSING_VISIT && closing != null) {
            print(t,visit,closing);
        }
    }
    protected void print(GroovySourceAST t,int visit,String value) {
        if(visit == OPENING_VISIT) {
            printNewlineAndIndent(t, visit);
        }
        if (visit == CLOSING_VISIT) {
            printNewlineAndIndent(t, visit);
        }
        out.print(value);
    }

    protected void printNewlineAndIndent(GroovySourceAST t, int visit) {
        int currentLine = t.getLine();
        if (lastLinePrinted == 0) { lastLinePrinted = currentLine; }
        if (lastLinePrinted != currentLine) {
            if (newLines) {
                if (!(visit == OPENING_VISIT && t.getType() == GroovyTokenTypes.SLIST)) {
                    for (int i=lastLinePrinted;i<currentLine;i++) {
                        out.println();
                    }
                    if (lastLinePrinted > currentLine) {
                        out.println();
                    }
                    if (visit == OPENING_VISIT || (visit == CLOSING_VISIT && lastLinePrinted > currentLine)) {
                        for (int i=0;i<tabLevel;i++) {
                            out.print("    ");
                        }
                    }
                }
            }
            lastLinePrinted = Math.max(currentLine,lastLinePrinted);
        }
    }

    public void push(GroovySourceAST t) {
        stack.push(t);
    }
    public GroovySourceAST pop() {
        if (!stack.empty()) {
            return (GroovySourceAST) stack.pop();
        }
        return null;
    }

    private GroovySourceAST getParentNode() {
        Object currentNode = stack.pop();
        Object parentNode = stack.peek();
        stack.push(currentNode);
        return (GroovySourceAST) parentNode;
    }

}

⌨️ 快捷键说明

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