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

📄 errormanager.java

📁 antlr最新版本V3源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/* [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 antlr.Token;import org.antlr.Tool;import org.antlr.misc.BitSet;import org.antlr.analysis.DFAState;import org.antlr.analysis.DecisionProbe;import org.antlr.stringtemplate.StringTemplate;import org.antlr.stringtemplate.StringTemplateErrorListener;import org.antlr.stringtemplate.StringTemplateGroup;import org.antlr.stringtemplate.language.AngleBracketTemplateLexer;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.util.*;/** Defines all the errors ANTLR can generator for both the tool and for *  issues with a grammar. * *  Here is a list of language names: * *  http://ftp.ics.uci.edu/pub/ietf/http/related/iso639.txt * *  Here is a list of country names: * *  http://www.chemie.fu-berlin.de/diverse/doc/ISO_3166.html * *  I use constants not strings to identify messages as the compiler will *  find any errors/mismatches rather than leaving a mistyped string in *  the code to be found randomly in the future.  Further, Intellij can *  do field name expansion to save me some typing.  I have to map *  int constants to template names, however, which could introduce a mismatch. *  Someone could provide a .stg file that had a template name wrong.  When *  I load the group, then, I must verify that all messages are there. * *  This is essentially the functionality of the resource bundle stuff Java *  has, but I don't want to load a property file--I want to load a template *  group file and this is so simple, why mess with their junk. * *  I use the default Locale as defined by java to compute a group file name *  in the org/antlr/tool/templates/messages dir called en_US.stg and so on. * *  Normally we want to use the default locale, but often a message file will *  not exist for it so we must fall back on the US local. * *  During initialization of this class, all errors go straight to System.err. *  There is no way around this.  If I have not set up the error system, how *  can I do errors properly?  For example, if the string template group file *  full of messages has an error, how could I print to anything but System.err? * *  TODO: how to map locale to a file encoding for the stringtemplate group file? *  StringTemplate knows how to pay attention to the default encoding so it *  should probably just work unless a GUI sets the local to some chinese *  variation but System.getProperty("file.encoding") is US.  Hmm... * *  TODO: get antlr.g etc.. parsing errors to come here. */public class ErrorManager {	// TOOL ERRORS	// file errors	public static final int MSG_CANNOT_WRITE_FILE = 1;	public static final int MSG_CANNOT_CLOSE_FILE = 2;	public static final int MSG_CANNOT_FIND_TOKENS_FILE = 3;	public static final int MSG_ERROR_READING_TOKENS_FILE = 4;	public static final int MSG_DIR_NOT_FOUND = 5;	public static final int MSG_OUTPUT_DIR_IS_FILE = 6;	public static final int MSG_CANNOT_OPEN_FILE = 7;	public static final int MSG_FILE_AND_GRAMMAR_NAME_DIFFER = 8;	public static final int MSG_FILENAME_EXTENSION_ERROR = 9;	public static final int MSG_INTERNAL_ERROR = 10;	public static final int MSG_INTERNAL_WARNING = 11;	public static final int MSG_ERROR_CREATING_ARTIFICIAL_RULE = 12;	public static final int MSG_TOKENS_FILE_SYNTAX_ERROR = 13;	public static final int MSG_CANNOT_GEN_DOT_FILE = 14;	public static final int MSG_BAD_AST_STRUCTURE = 15;	public static final int MSG_BAD_ACTION_AST_STRUCTURE = 16;	// code gen errors	public static final int MSG_MISSING_CODE_GEN_TEMPLATES = 20;	public static final int MSG_MISSING_CYCLIC_DFA_CODE_GEN_TEMPLATES = 21;	public static final int MSG_CODE_GEN_TEMPLATES_INCOMPLETE = 22;	public static final int MSG_CANNOT_CREATE_TARGET_GENERATOR = 23;	//public static final int MSG_CANNOT_COMPUTE_SAMPLE_INPUT_SEQ = 24;	// GRAMMAR ERRORS	public static final int MSG_SYNTAX_ERROR = 100;	public static final int MSG_RULE_REDEFINITION = 101;	public static final int MSG_LEXER_RULES_NOT_ALLOWED = 102;	public static final int MSG_PARSER_RULES_NOT_ALLOWED = 103;	public static final int MSG_CANNOT_FIND_ATTRIBUTE_NAME_IN_DECL = 104;	public static final int MSG_NO_TOKEN_DEFINITION = 105;	public static final int MSG_UNDEFINED_RULE_REF = 106;	public static final int MSG_LITERAL_NOT_ASSOCIATED_WITH_LEXER_RULE = 107;	public static final int MSG_CANNOT_ALIAS_TOKENS_IN_LEXER = 108;	public static final int MSG_ATTRIBUTE_REF_NOT_IN_RULE = 111;	public static final int MSG_INVALID_RULE_SCOPE_ATTRIBUTE_REF = 112;	public static final int MSG_UNKNOWN_ATTRIBUTE_IN_SCOPE = 113;	public static final int MSG_UNKNOWN_SIMPLE_ATTRIBUTE = 114;	public static final int MSG_INVALID_RULE_PARAMETER_REF = 115;	public static final int MSG_UNKNOWN_RULE_ATTRIBUTE = 116;	public static final int MSG_ISOLATED_RULE_SCOPE = 117;	public static final int MSG_SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE = 118;	public static final int MSG_LABEL_CONFLICTS_WITH_RULE = 119;	public static final int MSG_LABEL_CONFLICTS_WITH_TOKEN = 120;	public static final int MSG_LABEL_CONFLICTS_WITH_RULE_SCOPE_ATTRIBUTE = 121;	public static final int MSG_LABEL_CONFLICTS_WITH_RULE_ARG_RETVAL = 122;	public static final int MSG_ATTRIBUTE_CONFLICTS_WITH_RULE = 123;	public static final int MSG_ATTRIBUTE_CONFLICTS_WITH_RULE_ARG_RETVAL = 124;	public static final int MSG_LABEL_TYPE_CONFLICT = 125;	public static final int MSG_ARG_RETVAL_CONFLICT = 126;	public static final int MSG_NONUNIQUE_REF = 127;	public static final int MSG_FORWARD_ELEMENT_REF = 128;	public static final int MSG_MISSING_RULE_ARGS = 129;	public static final int MSG_RULE_HAS_NO_ARGS = 130;	public static final int MSG_ARGS_ON_TOKEN_REF = 131;	public static final int MSG_AMBIGUOUS_RULE_SCOPE = 132;	public static final int MSG_ILLEGAL_OPTION = 133;	public static final int MSG_LIST_LABEL_INVALID_UNLESS_RETVAL_STRUCT = 134;	public static final int MSG_UNDEFINED_TOKEN_REF_IN_REWRITE = 135;	public static final int MSG_REWRITE_ELEMENT_NOT_PRESENT_ON_LHS = 136;	public static final int MSG_UNDEFINED_LABEL_REF_IN_REWRITE = 137;	public static final int MSG_NO_GRAMMAR_START_RULE = 138;	public static final int MSG_EMPTY_COMPLEMENT = 139;	public static final int MSG_UNKNOWN_DYNAMIC_SCOPE = 140;	public static final int MSG_UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE = 141;	public static final int MSG_ISOLATED_RULE_ATTRIBUTE = 142;	public static final int MSG_INVALID_ACTION_SCOPE = 143;	public static final int MSG_ACTION_REDEFINITION = 144;	public static final int MSG_DOUBLE_QUOTES_ILLEGAL = 145;	public static final int MSG_INVALID_TEMPLATE_ACTION = 146;	public static final int MSG_MISSING_ATTRIBUTE_NAME = 147;	public static final int MSG_ARG_INIT_VALUES_ILLEGAL = 148;	public static final int MSG_REWRITE_OR_OP_WITH_NO_OUTPUT_OPTION = 149;	public static final int MSG_NO_RULES = 150;	public static final int MSG_WRITE_TO_READONLY_ATTR = 151;	public static final int MSG_MISSING_AST_TYPE_IN_TREE_GRAMMAR = 152;	public static final int MSG_REWRITE_FOR_MULTI_ELEMENT_ALT = 153;	public static final int MSG_RULE_INVALID_SET = 154;	// GRAMMAR WARNINGS	public static final int MSG_GRAMMAR_NONDETERMINISM = 200; // A predicts alts 1,2	public static final int MSG_UNREACHABLE_ALTS = 201;       // nothing predicts alt i	public static final int MSG_DANGLING_STATE = 202;        // no edges out of state	public static final int MSG_INSUFFICIENT_PREDICATES = 203;	public static final int MSG_DUPLICATE_SET_ENTRY = 204;    // (A|A)	public static final int MSG_ANALYSIS_ABORTED = 205;	public static final int MSG_RECURSION_OVERLOW = 206;	public static final int MSG_LEFT_RECURSION = 207;	public static final int MSG_UNREACHABLE_TOKENS = 208; // nothing predicts token	public static final int MSG_TOKEN_NONDETERMINISM = 209; // alts of Tokens rule	public static final int MSG_LEFT_RECURSION_CYCLES = 210;	public static final int MSG_NONREGULAR_DECISION = 211;	public static final int MAX_MESSAGE_NUMBER = 211;	/** Do not do perform analysis and code gen if one of these happens */	public static final BitSet ERRORS_FORCING_NO_ANALYSIS = new BitSet() {		{			add(MSG_RULE_REDEFINITION);			add(MSG_UNDEFINED_RULE_REF);			add(MSG_LEFT_RECURSION_CYCLES);			add(MSG_REWRITE_OR_OP_WITH_NO_OUTPUT_OPTION);			add(MSG_NO_RULES);			// TODO: ...		}	};	/** Do not do perform analysis and code gen if one of these happens */	public static final BitSet ERRORS_FORCING_NO_CODEGEN = new BitSet() {		{			add(MSG_NONREGULAR_DECISION);			add(MSG_FILE_AND_GRAMMAR_NAME_DIFFER);			// TODO: ...		}	};	/** Only one error can be emitted for any entry in this table.	 *  Map<String,Set> where the key is a method name like danglingState.	 *  The set is whatever that method accepts or derives like a DFA.	 */	public static final Map emitSingleError = new HashMap() {		{			put("danglingState", new HashSet());		}	};	/** Messages should be sensitive to the locale. */	private static Locale locale;	private static String formatName;	/** Each thread might need it's own error listener; e.g., a GUI with	 *  multiple window frames holding multiple grammars.	 */	private static Map threadToListenerMap = new HashMap();	static class ErrorState {		public int errors;		public int warnings;		public int infos;		/** Track all msgIDs; we use to abort later if necessary		 *  also used in Message to find out what type of message it is via getMessageType()		 */		public BitSet errorMsgIDs = new BitSet();		public BitSet warningMsgIDs = new BitSet();		// TODO: figure out how to do info messages. these do not have IDs...kr		//public BitSet infoMsgIDs = new BitSet();	}	/** Track the number of errors regardless of the listener but track	 *  per thread.	 */	private static Map threadToErrorCountMap = new HashMap();	/** Each thread has its own ptr to a Tool object, which knows how	 *  to panic, for example.  In a GUI, the thread might just throw an Error	 *  to exit rather than the suicide System.exit.	 */	private static Map threadToToolMap = new HashMap();	/** The group of templates that represent all possible ANTLR errors. */	private static StringTemplateGroup messages;	/** The group of templates that represent the current message format. */	private static StringTemplateGroup format;	/** From a msgID how can I get the name of the template that describes	 *  the error or warning?	 */	private static String[] idToMessageTemplateName = new String[MAX_MESSAGE_NUMBER+1];	static ANTLRErrorListener theDefaultErrorListener = new ANTLRErrorListener() {		public void info(String msg) {			if (formatWantsSingleLineMessage()) {				msg = msg.replaceAll("\n", " ");			}			System.err.println(msg);		}		public void error(Message msg) {			String outputMsg = msg.toString();			if (formatWantsSingleLineMessage()) {				outputMsg = outputMsg.replaceAll("\n", " ");			}			System.err.println(outputMsg);		}		public void warning(Message msg) {			String outputMsg = msg.toString();			if (formatWantsSingleLineMessage()) {				outputMsg = outputMsg.replaceAll("\n", " ");			}			System.err.println(outputMsg);		}		public void error(ToolMessage msg) {			String outputMsg = msg.toString();			if (formatWantsSingleLineMessage()) {				outputMsg = outputMsg.replaceAll("\n", " ");			}			System.err.println(outputMsg);		}	};	/** Handle all ST error listeners here (code gen, Grammar, and this class	 *  use templates.	 */	static StringTemplateErrorListener initSTListener =		new StringTemplateErrorListener() {			public void error(String s, Throwable e) {				System.err.println("ErrorManager init error: "+s);				if ( e!=null ) {					System.err.println("exception: "+e);				}				/*

⌨️ 快捷键说明

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