📄 antlrtreeprinter.java
字号:
// $ANTLR 2.7.7 (2006-01-29): "antlr.print.g" -> "ANTLRTreePrinter.java"$/* [The "BSD licence"] Copyright (c) 2005-2006 Terence Parr All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ package org.antlr.tool; import java.util.*;import antlr.TreeParser;import antlr.Token;import antlr.collections.AST;import antlr.RecognitionException;import antlr.ANTLRException;import antlr.NoViableAltException;import antlr.MismatchedTokenException;import antlr.SemanticException;import antlr.collections.impl.BitSet;import antlr.ASTPair;import antlr.collections.impl.ASTArray;/** Print out a grammar (no pretty printing). * * Terence Parr * University of San Francisco * August 19, 2003 */public class ANTLRTreePrinter extends antlr.TreeParser implements ANTLRTreePrinterTokenTypes { protected Grammar grammar; protected boolean showActions; protected StringBuffer buf = new StringBuffer(300); public void out(String s) { buf.append(s); } public void reportError(RecognitionException ex) { Token token = null; if ( ex instanceof MismatchedTokenException ) { token = ((MismatchedTokenException)ex).token; } else if ( ex instanceof NoViableAltException ) { token = ((NoViableAltException)ex).token; } ErrorManager.syntaxError( ErrorManager.MSG_SYNTAX_ERROR, grammar, token, "antlr.print: "+ex.toString(), ex); } /** Normalize a grammar print out by removing all double spaces * and trailing/beginning stuff. FOr example, convert * * ( A | B | C )* * * to * * ( A | B | C )* */ public static String normalize(String g) { StringTokenizer st = new StringTokenizer(g, " ", false); StringBuffer buf = new StringBuffer(); while ( st.hasMoreTokens() ) { String w = st.nextToken(); buf.append(w); buf.append(" "); } return buf.toString().trim(); }public ANTLRTreePrinter() { tokenNames = _tokenNames;}/** Call this to figure out how to print */ public final String toString(AST _t, Grammar g, boolean showActions ) throws RecognitionException { String s=null; GrammarAST toString_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t; grammar = g; this.showActions = showActions; try { // for error handling { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case LEXER_GRAMMAR: case PARSER_GRAMMAR: case TREE_GRAMMAR: case COMBINED_GRAMMAR: { grammar(_t); _t = _retTree; break; } case RULE: { rule(_t); _t = _retTree; break; } case ALT: { alternative(_t); _t = _retTree; break; } case BLOCK: case OPTIONAL: case CLOSURE: case POSITIVE_CLOSURE: case SYNPRED: case RANGE: case CHAR_RANGE: case EPSILON: case SET: case LABEL: case GATED_SEMPRED: case SYN_SEMPRED: case ACTION: case ASSIGN: case STRING_LITERAL: case CHAR_LITERAL: case TOKEN_REF: case PLUS_ASSIGN: case SEMPRED: case NOT: case TREE_BEGIN: case RULE_REF: case WILDCARD: { element(_t); _t = _retTree; break; } case REWRITE: { single_rewrite(_t); _t = _retTree; break; } case EOR: { GrammarAST tmp1_AST_in = (GrammarAST)_t; match(_t,EOR); _t = _t.getNextSibling(); s="EOR"; break; } default: { throw new NoViableAltException(_t); } } } return normalize(buf.toString()); } catch (RecognitionException ex) { reportError(ex); if (_t!=null) {_t = _t.getNextSibling();} } _retTree = _t; return s; } public final void grammar(AST _t) throws RecognitionException { GrammarAST grammar_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t; try { // for error handling { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case LEXER_GRAMMAR: { AST __t5 = _t; GrammarAST tmp2_AST_in = (GrammarAST)_t; match(_t,LEXER_GRAMMAR); _t = _t.getFirstChild(); grammarSpec(_t,"lexer " ); _t = _retTree; _t = __t5; _t = _t.getNextSibling(); break; } case PARSER_GRAMMAR: { AST __t6 = _t; GrammarAST tmp3_AST_in = (GrammarAST)_t; match(_t,PARSER_GRAMMAR); _t = _t.getFirstChild(); grammarSpec(_t,"parser "); _t = _retTree; _t = __t6; _t = _t.getNextSibling(); break; } case TREE_GRAMMAR: { AST __t7 = _t; GrammarAST tmp4_AST_in = (GrammarAST)_t; match(_t,TREE_GRAMMAR); _t = _t.getFirstChild(); grammarSpec(_t,"tree "); _t = _retTree; _t = __t7; _t = _t.getNextSibling(); break; } case COMBINED_GRAMMAR: { AST __t8 = _t; GrammarAST tmp5_AST_in = (GrammarAST)_t; match(_t,COMBINED_GRAMMAR); _t = _t.getFirstChild(); grammarSpec(_t,""); _t = _retTree; _t = __t8; _t = _t.getNextSibling(); break; } default: { throw new NoViableAltException(_t); } } } } catch (RecognitionException ex) { reportError(ex); if (_t!=null) {_t = _t.getNextSibling();} } _retTree = _t; } public final void rule(AST _t) throws RecognitionException { GrammarAST rule_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t; GrammarAST id = null; GrammarAST arg = null; GrammarAST ret = null; GrammarAST b = null; try { // for error handling AST __t42 = _t; GrammarAST tmp6_AST_in = (GrammarAST)_t; match(_t,RULE); _t = _t.getFirstChild(); id = (GrammarAST)_t; match(_t,ID); _t = _t.getNextSibling(); { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case FRAGMENT: case LITERAL_protected: case LITERAL_public: case LITERAL_private: { modifier(_t); _t = _retTree; break; } case ARG: { break; } default: { throw new NoViableAltException(_t); } } } out(id.getText()); AST __t44 = _t; GrammarAST tmp7_AST_in = (GrammarAST)_t; match(_t,ARG); _t = _t.getFirstChild(); { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case ARG_ACTION: { arg = (GrammarAST)_t; match(_t,ARG_ACTION); _t = _t.getNextSibling(); out("["+arg.getText()+"]"); break; } case 3: { break; } default: { throw new NoViableAltException(_t); } } } _t = __t44; _t = _t.getNextSibling(); AST __t46 = _t; GrammarAST tmp8_AST_in = (GrammarAST)_t; match(_t,RET); _t = _t.getFirstChild(); { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case ARG_ACTION: { ret = (GrammarAST)_t; match(_t,ARG_ACTION); _t = _t.getNextSibling(); out(" returns ["+ret.getText()+"]"); break; } case 3: { break; } default: { throw new NoViableAltException(_t); } } } _t = __t46; _t = _t.getNextSibling(); { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case OPTIONS: { optionsSpec(_t); _t = _retTree; break; } case BLOCK: case SCOPE: case AMPERSAND: { break; } default: { throw new NoViableAltException(_t); } } } { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case SCOPE: { ruleScopeSpec(_t); _t = _retTree; break; } case BLOCK: case AMPERSAND: { break; } default: { throw new NoViableAltException(_t); } } } { _loop51: do { if (_t==null) _t=ASTNULL; if ((_t.getType()==AMPERSAND)) { ruleAction(_t); _t = _retTree; } else { break _loop51; } } while (true); } out(" : "); b = _t==ASTNULL ? null : (GrammarAST)_t; block(_t,false); _t = _retTree; { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case LITERAL_exception: { exceptionGroup(_t); _t = _retTree; break; } case EOR: { break; } default: { throw new NoViableAltException(_t); } } } GrammarAST tmp9_AST_in = (GrammarAST)_t; match(_t,EOR); _t = _t.getNextSibling(); out(";\n"); _t = __t42; _t = _t.getNextSibling(); } catch (RecognitionException ex) { reportError(ex); if (_t!=null) {_t = _t.getNextSibling();} } _retTree = _t; } public final void alternative(AST _t) throws RecognitionException { GrammarAST alternative_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t; try { // for error handling AST __t74 = _t; GrammarAST tmp10_AST_in = (GrammarAST)_t; match(_t,ALT); _t = _t.getFirstChild(); { int _cnt76=0; _loop76: do { if (_t==null) _t=ASTNULL; if ((_t.getType()==BLOCK||_t.getType()==OPTIONAL||_t.getType()==CLOSURE||_t.getType()==POSITIVE_CLOSURE||_t.getType()==SYNPRED||_t.getType()==RANGE||_t.getType()==CHAR_RANGE||_t.getType()==EPSILON||_t.getType()==SET||_t.getType()==LABEL||_t.getType()==GATED_SEMPRED||_t.getType()==SYN_SEMPRED||_t.getType()==ACTION||_t.getType()==ASSIGN||_t.getType()==STRING_LITERAL||_t.getType()==CHAR_LITERAL||_t.getType()==TOKEN_REF||_t.getType()==PLUS_ASSIGN||_t.getType()==SEMPRED||_t.getType()==NOT||_t.getType()==TREE_BEGIN||_t.getType()==RULE_REF||_t.getType()==WILDCARD)) { element(_t); _t = _retTree; } else { if ( _cnt76>=1 ) { break _loop76; } else {throw new NoViableAltException(_t);} } _cnt76++; } while (true); } GrammarAST tmp11_AST_in = (GrammarAST)_t; match(_t,EOA); _t = _t.getNextSibling(); _t = __t74; _t = _t.getNextSibling(); } catch (RecognitionException ex) { reportError(ex); if (_t!=null) {_t = _t.getNextSibling();} } _retTree = _t; } public final void element(AST _t) throws RecognitionException { GrammarAST element_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t; GrammarAST id = null; GrammarAST id2 = null; GrammarAST a = null; GrammarAST pred = null; GrammarAST spred = null; GrammarAST gpred = null; try { // for error handling if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case SET: case LABEL: case STRING_LITERAL: case CHAR_LITERAL: case TOKEN_REF: case RULE_REF: case WILDCARD: { atom(_t); _t = _retTree; break; } case NOT: { AST __t103 = _t; GrammarAST tmp12_AST_in = (GrammarAST)_t; match(_t,NOT); _t = _t.getFirstChild(); out("~"); atom(_t); _t = _retTree; _t = __t103; _t = _t.getNextSibling(); break; } case RANGE: { AST __t104 = _t; GrammarAST tmp13_AST_in = (GrammarAST)_t; match(_t,RANGE); _t = _t.getFirstChild(); atom(_t); _t = _retTree; out(".."); atom(_t); _t = _retTree; _t = __t104; _t = _t.getNextSibling(); break; } case CHAR_RANGE: { AST __t105 = _t; GrammarAST tmp14_AST_in = (GrammarAST)_t; match(_t,CHAR_RANGE); _t = _t.getFirstChild(); atom(_t); _t = _retTree; out(".."); atom(_t); _t = _retTree; _t = __t105; _t = _t.getNextSibling(); break; } case ASSIGN: { AST __t106 = _t; GrammarAST tmp15_AST_in = (GrammarAST)_t; match(_t,ASSIGN); _t = _t.getFirstChild(); id = (GrammarAST)_t; match(_t,ID); _t = _t.getNextSibling(); out(id.getText()+"="); { if (_t==null) _t=ASTNULL; switch ( _t.getType()) { case NOT: { AST __t108 = _t; GrammarAST tmp16_AST_in = (GrammarAST)_t; match(_t,NOT); _t = _t.getFirstChild(); out("~"); atom(_t); _t = _retTree; _t = __t108; _t = _t.getNextSibling(); break; } case SET: case LABEL: case STRING_LITERAL: case CHAR_LITERAL: case TOKEN_REF: case RULE_REF: case WILDCARD: { atom(_t); _t = _retTree; break; } default: { throw new NoViableAltException(_t); } } } _t = __t106; _t = _t.getNextSibling(); break; } case PLUS_ASSIGN: { AST __t109 = _t; GrammarAST tmp17_AST_in = (GrammarAST)_t; match(_t,PLUS_ASSIGN); _t = _t.getFirstChild(); id2 = (GrammarAST)_t; match(_t,ID); _t = _t.getNextSibling();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -