⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 definegrammaritemswalker.java

📁 ANTLR(ANother Tool for Language Recognition)它是这样的一种工具
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
// $ANTLR 2.7.7 (2006-01-29): "define.g" -> "DefineGrammarItemsWalker.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 org.antlr.misc.*;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;public class DefineGrammarItemsWalker extends antlr.TreeParser       implements DefineGrammarItemsWalkerTokenTypes {protected Grammar grammar;protected GrammarAST root;protected String currentRuleName;protected int outerAltNum = 0;protected int blockLevel = 0;    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,            "define: "+ex.toString(),            ex);    }	protected void finish() {		trimGrammar();	}	/** Remove any lexer rules from a COMBINED; already passed to lexer */	protected void trimGrammar() {		if ( grammar.type!=Grammar.COMBINED ) {			return;		}		// form is (header ... ) ( grammar ID (scope ...) ... ( rule ... ) ( rule ... ) ... )		GrammarAST p = root;		// find the grammar spec		while ( !p.getText().equals("grammar") ) {			p = (GrammarAST)p.getNextSibling();		}		p = (GrammarAST)p.getFirstChild(); // jump down to first child of grammar		// look for first RULE def		GrammarAST prev = p; // points to the ID (grammar name)		while ( p.getType()!=RULE ) {			prev = p;			p = (GrammarAST)p.getNextSibling();		}		// prev points at last node before first rule subtree at this point		while ( p!=null ) {			String ruleName = p.getFirstChild().getText();			//System.out.println("rule "+ruleName+" prev="+prev.getText());			if ( Character.isUpperCase(ruleName.charAt(0)) ) {				// remove lexer rule				prev.setNextSibling(p.getNextSibling());			}			else {				prev = p; // non-lexer rule; move on			}			p = (GrammarAST)p.getNextSibling();		}		//System.out.println("root after removal is: "+root.toStringList());	}public DefineGrammarItemsWalker() {	tokenNames = _tokenNames;}	public final void grammar(AST _t,		Grammar g	) throws RecognitionException {				GrammarAST grammar_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;				grammar = g;		root = grammar_AST_in;						try {      // for error handling			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case LEXER_GRAMMAR:			{				AST __t3 = _t;				GrammarAST tmp1_AST_in = (GrammarAST)_t;				match(_t,LEXER_GRAMMAR);				_t = _t.getFirstChild();				if ( inputState.guessing==0 ) {					grammar.type = Grammar.LEXER;				}				grammarSpec(_t);				_t = _retTree;				_t = __t3;				_t = _t.getNextSibling();				break;			}			case PARSER_GRAMMAR:			{				AST __t4 = _t;				GrammarAST tmp2_AST_in = (GrammarAST)_t;				match(_t,PARSER_GRAMMAR);				_t = _t.getFirstChild();				if ( inputState.guessing==0 ) {					grammar.type = Grammar.PARSER;				}				grammarSpec(_t);				_t = _retTree;				_t = __t4;				_t = _t.getNextSibling();				break;			}			case TREE_GRAMMAR:			{				AST __t5 = _t;				GrammarAST tmp3_AST_in = (GrammarAST)_t;				match(_t,TREE_GRAMMAR);				_t = _t.getFirstChild();				if ( inputState.guessing==0 ) {					grammar.type = Grammar.TREE_PARSER;				}				grammarSpec(_t);				_t = _retTree;				_t = __t5;				_t = _t.getNextSibling();				break;			}			case COMBINED_GRAMMAR:			{				AST __t6 = _t;				GrammarAST tmp4_AST_in = (GrammarAST)_t;				match(_t,COMBINED_GRAMMAR);				_t = _t.getFirstChild();				if ( inputState.guessing==0 ) {					grammar.type = Grammar.COMBINED;				}				grammarSpec(_t);				_t = _retTree;				_t = __t6;				_t = _t.getNextSibling();				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			if ( inputState.guessing==0 ) {				finish();			}		}		catch (RecognitionException ex) {			if (inputState.guessing==0) {				reportError(ex);				if (_t!=null) {_t = _t.getNextSibling();}			} else {			  throw ex;			}		}		_retTree = _t;	}		public final void grammarSpec(AST _t) throws RecognitionException {				GrammarAST grammarSpec_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;		GrammarAST id = null;		GrammarAST cmt = null;				Map opts=null;		Token optionsStartToken=null;						try {      // for error handling			id = (GrammarAST)_t;			match(_t,ID);			_t = _t.getNextSibling();			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case DOC_COMMENT:			{				cmt = (GrammarAST)_t;				match(_t,DOC_COMMENT);				_t = _t.getNextSibling();				break;			}			case OPTIONS:			case TOKENS:			case RULE:			case SCOPE:			case AMPERSAND:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case OPTIONS:			{				if ( inputState.guessing==0 ) {					optionsStartToken=((GrammarAST)_t).getToken();				}				optionsSpec(_t);				_t = _retTree;				break;			}			case TOKENS:			case RULE:			case SCOPE:			case AMPERSAND:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case TOKENS:			{				tokensSpec(_t);				_t = _retTree;				break;			}			case RULE:			case SCOPE:			case AMPERSAND:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			{			_loop14:			do {				if (_t==null) _t=ASTNULL;				if ((_t.getType()==SCOPE)) {					attrScope(_t);					_t = _retTree;				}				else {					break _loop14;				}							} while (true);			}			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case AMPERSAND:			{				actions(_t);				_t = _retTree;				break;			}			case RULE:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			rules(_t);			_t = _retTree;		}		catch (RecognitionException ex) {			if (inputState.guessing==0) {				reportError(ex);				if (_t!=null) {_t = _t.getNextSibling();}			} else {			  throw ex;			}		}		_retTree = _t;	}		public final void attrScope(AST _t) throws RecognitionException {				GrammarAST attrScope_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;		GrammarAST name = null;		GrammarAST attrs = null;				try {      // for error handling			AST __t8 = _t;			GrammarAST tmp5_AST_in = (GrammarAST)_t;			match(_t,SCOPE);			_t = _t.getFirstChild();			name = (GrammarAST)_t;			match(_t,ID);			_t = _t.getNextSibling();			attrs = (GrammarAST)_t;			match(_t,ACTION);			_t = _t.getNextSibling();			_t = __t8;			_t = _t.getNextSibling();			if ( inputState.guessing==0 ) {										AttributeScope scope = grammar.defineGlobalScope(name.getText(),attrs.token);						scope.isDynamicGlobalScope = true;						scope.addAttributes(attrs.getText(), ";");									}		}		catch (RecognitionException ex) {			if (inputState.guessing==0) {				reportError(ex);				if (_t!=null) {_t = _t.getNextSibling();}			} else {			  throw ex;			}		}		_retTree = _t;	}		public final void optionsSpec(AST _t) throws RecognitionException {				GrammarAST optionsSpec_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;				try {      // for error handling			GrammarAST tmp6_AST_in = (GrammarAST)_t;			match(_t,OPTIONS);			_t = _t.getNextSibling();		}		catch (RecognitionException ex) {			if (inputState.guessing==0) {				reportError(ex);				if (_t!=null) {_t = _t.getNextSibling();}			} else {			  throw ex;			}		}		_retTree = _t;	}		public final void tokensSpec(AST _t) throws RecognitionException {				GrammarAST tokensSpec_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;				try {      // for error handling			AST __t24 = _t;			GrammarAST tmp7_AST_in = (GrammarAST)_t;			match(_t,TOKENS);			_t = _t.getFirstChild();			{			int _cnt26=0;			_loop26:			do {				if (_t==null) _t=ASTNULL;				if ((_t.getType()==ASSIGN||_t.getType()==TOKEN_REF)) {					tokenSpec(_t);					_t = _retTree;				}				else {					if ( _cnt26>=1 ) { break _loop26; } else {throw new NoViableAltException(_t);}				}								_cnt26++;			} while (true);			}			_t = __t24;			_t = _t.getNextSibling();		}		catch (RecognitionException ex) {			if (inputState.guessing==0) {				reportError(ex);				if (_t!=null) {_t = _t.getNextSibling();}			} else {			  throw ex;			}		}		_retTree = _t;	}		public final void actions(AST _t) throws RecognitionException {				GrammarAST actions_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;				try {      // for error handling			{			int _cnt18=0;			_loop18:			do {				if (_t==null) _t=ASTNULL;				if ((_t.getType()==AMPERSAND)) {					action(_t);					_t = _retTree;				}				else {					if ( _cnt18>=1 ) { break _loop18; } else {throw new NoViableAltException(_t);}				}								_cnt18++;			} while (true);			}		}		catch (RecognitionException ex) {			if (inputState.guessing==0) {				reportError(ex);				if (_t!=null) {_t = _t.getNextSibling();}			} else {			  throw ex;			}		}		_retTree = _t;	}		public final void rules(AST _t) throws RecognitionException {				GrammarAST rules_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;				try {      // for error handling			{			int _cnt32=0;			_loop32:			do {				if (_t==null) _t=ASTNULL;				if ((_t.getType()==RULE)) {					rule(_t);					_t = _retTree;				}				else {					if ( _cnt32>=1 ) { break _loop32; } else {throw new NoViableAltException(_t);}				}								_cnt32++;			} while (true);			}		}		catch (RecognitionException ex) {			if (inputState.guessing==0) {				reportError(ex);				if (_t!=null) {_t = _t.getNextSibling();}			} else {			  throw ex;			}		}		_retTree = _t;	}		public final void action(AST _t) throws RecognitionException {				GrammarAST action_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;		GrammarAST amp = null;		GrammarAST id1 = null;		GrammarAST id2 = null;		GrammarAST a1 = null;		GrammarAST a2 = null;				String scope=null;		GrammarAST nameAST=null, actionAST=null;						try {      // for error handling			AST __t20 = _t;			amp = _t==ASTNULL ? null :(GrammarAST)_t;			match(_t,AMPERSAND);			_t = _t.getFirstChild();			id1 = (GrammarAST)_t;			match(_t,ID);			_t = _t.getNextSibling();			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case ID:			{				id2 = (GrammarAST)_t;				match(_t,ID);				_t = _t.getNextSibling();				a1 = (GrammarAST)_t;				match(_t,ACTION);				_t = _t.getNextSibling();				if ( inputState.guessing==0 ) {					scope=id1.getText(); nameAST=id2; actionAST=a1;				}				break;			}			case ACTION:			{				a2 = (GrammarAST)_t;				match(_t,ACTION);				_t = _t.getNextSibling();				if ( inputState.guessing==0 ) {					scope=null; nameAST=id1; actionAST=a2;				}				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			_t = __t20;			_t = _t.getNextSibling();			if ( inputState.guessing==0 ) {										 grammar.defineAction(amp,scope,nameAST,actionAST);									}		}		catch (RecognitionException ex) {			if (inputState.guessing==0) {				reportError(ex);				if (_t!=null) {_t = _t.getNextSibling();}			} else {			  throw ex;			}		}		_retTree = _t;	}		public final void tokenSpec(AST _t) throws RecognitionException {				GrammarAST tokenSpec_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;		GrammarAST t = null;		GrammarAST t2 = null;		GrammarAST s = null;		GrammarAST c = null;				try {      // for error handling			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case TOKEN_REF:			{				t = (GrammarAST)_t;				match(_t,TOKEN_REF);				_t = _t.getNextSibling();				break;			}			case ASSIGN:			{				AST __t28 = _t;				GrammarAST tmp8_AST_in = (GrammarAST)_t;				match(_t,ASSIGN);				_t = _t.getFirstChild();				t2 = (GrammarAST)_t;				match(_t,TOKEN_REF);				_t = _t.getNextSibling();				{				if (_t==null) _t=ASTNULL;				switch ( _t.getType()) {				case STRING_LITERAL:				{					s = (GrammarAST)_t;					match(_t,STRING_LITERAL);					_t = _t.getNextSibling();					break;				}				case CHAR_LITERAL:				{					c = (GrammarAST)_t;					match(_t,CHAR_LITERAL);					_t = _t.getNextSibling();					break;				}				default:

⌨️ 快捷键说明

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