📄 errormanager.java
字号:
if ( e!=null ) { e.printStackTrace(System.err); } */ } public void warning(String s) { System.err.println("ErrorManager init warning: "+s); } public void debug(String s) {} }; /** During verification of the messages group file, don't gen errors. * I'll handle them here. This is used only after file has loaded ok * and only for the messages STG. */ static StringTemplateErrorListener blankSTListener = new StringTemplateErrorListener() { public void error(String s, Throwable e) {} public void warning(String s) {} public void debug(String s) {} }; /** Errors during initialization related to ST must all go to System.err. */ static StringTemplateErrorListener theDefaultSTListener = new StringTemplateErrorListener() { public void error(String s, Throwable e) { if ( e instanceof InvocationTargetException ) { e = ((InvocationTargetException)e).getTargetException(); } ErrorManager.error(ErrorManager.MSG_INTERNAL_ERROR, s, e); } public void warning(String s) { ErrorManager.warning(ErrorManager.MSG_INTERNAL_WARNING, s); } public void debug(String s) { } }; // make sure that this class is ready to use after loading static { initIdToMessageNameMapping(); // it is inefficient to set the default locale here if another // piece of code is going to set the locale, but that would // require that a user call an init() function or something. I prefer // that this class be ready to go when loaded as I'm absentminded ;) setLocale(Locale.getDefault()); // try to load the message format group // the user might have specified one on the command line // if not, or if the user has given an illegal value, we will fall back to "antlr" setFormat("antlr"); } public static StringTemplateErrorListener getStringTemplateErrorListener() { return theDefaultSTListener; } /** We really only need a single locale for entire running ANTLR code * in a single VM. Only pay attention to the language, not the country * so that French Canadians and French Frenchies all get the same * template file, fr.stg. Just easier this way. */ public static void setLocale(Locale locale) { ErrorManager.locale = locale; String language = locale.getLanguage(); String fileName = "org/antlr/tool/templates/messages/languages/"+language+".stg"; ClassLoader cl = Thread.currentThread().getContextClassLoader(); InputStream is = cl.getResourceAsStream(fileName); if ( is==null ) { cl = ErrorManager.class.getClassLoader(); is = cl.getResourceAsStream(fileName); } if ( is==null && language.equals(Locale.US.getLanguage()) ) { rawError("ANTLR installation corrupted; cannot find English messages file "+fileName); panic(); } else if ( is==null ) { //rawError("no such locale file "+fileName+" retrying with English locale"); setLocale(Locale.US); // recurse on this rule, trying the US locale return; } BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(is)); messages = new StringTemplateGroup(br, AngleBracketTemplateLexer.class, initSTListener); br.close(); } catch (IOException ioe) { rawError("error reading message file "+fileName, ioe); } finally { if ( br!=null ) { try { br.close(); } catch (IOException ioe) { rawError("cannot close message file "+fileName, ioe); } } } messages.setErrorListener(blankSTListener); boolean messagesOK = verifyMessages(); if ( !messagesOK && language.equals(Locale.US.getLanguage()) ) { rawError("ANTLR installation corrupted; English messages file "+language+".stg incomplete"); panic(); } else if ( !messagesOK ) { setLocale(Locale.US); // try US to see if that will work } } /** The format gets reset either from the Tool if the user supplied a command line option to that effect * Otherwise we just use the default "antlr". */ public static void setFormat(String formatName) { ErrorManager.formatName = formatName; String fileName = "org/antlr/tool/templates/messages/formats/"+formatName+".stg"; ClassLoader cl = Thread.currentThread().getContextClassLoader(); InputStream is = cl.getResourceAsStream(fileName); if ( is==null ) { cl = ErrorManager.class.getClassLoader(); is = cl.getResourceAsStream(fileName); } if ( is==null && formatName.equals("antlr") ) { rawError("ANTLR installation corrupted; cannot find ANTLR messages format file "+fileName); panic(); } else if ( is==null ) { rawError("no such message format file "+fileName+" retrying with default ANTLR format"); setFormat("antlr"); // recurse on this rule, trying the default message format return; } BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(is)); format = new StringTemplateGroup(br, AngleBracketTemplateLexer.class, initSTListener); } finally { try { if ( br!=null ) { br.close(); } } catch (IOException ioe) { rawError("cannot close message format file "+fileName, ioe); } } format.setErrorListener(blankSTListener); boolean formatOK = verifyFormat(); if ( !formatOK && formatName.equals("antlr") ) { rawError("ANTLR installation corrupted; ANTLR messages format file "+formatName+".stg incomplete"); panic(); } else if ( !formatOK ) { setFormat("antlr"); // recurse on this rule, trying the default message format } } /** Encodes the error handling found in setLocale, but does not trigger * panics, which would make GUI tools die if ANTLR's installation was * a bit screwy. Duplicated code...ick. public static Locale getLocaleForValidMessages(Locale locale) { ErrorManager.locale = locale; String language = locale.getLanguage(); String fileName = "org/antlr/tool/templates/messages/"+language+".stg"; ClassLoader cl = Thread.currentThread().getContextClassLoader(); InputStream is = cl.getResourceAsStream(fileName); if ( is==null && language.equals(Locale.US.getLanguage()) ) { return null; } else if ( is==null ) { return getLocaleForValidMessages(Locale.US); // recurse on this rule, trying the US locale } boolean messagesOK = verifyMessages(); if ( !messagesOK && language.equals(Locale.US.getLanguage()) ) { return null; } else if ( !messagesOK ) { return getLocaleForValidMessages(Locale.US); // try US to see if that will work } return true; } */ /** In general, you'll want all errors to go to a single spot. * However, in a GUI, you might have two frames up with two * different grammars. Two threads might launch to process the * grammars--you would want errors to go to different objects * depending on the thread. I store a single listener per * thread. */ public static void setErrorListener(ANTLRErrorListener listener) { threadToListenerMap.put(Thread.currentThread(), listener); } public static void setTool(Tool tool) { threadToToolMap.put(Thread.currentThread(), tool); } /** Given a message ID, return a StringTemplate that somebody can fill * with data. We need to convert the int ID to the name of a template * in the messages ST group. */ public static StringTemplate getMessage(int msgID) { String msgName = idToMessageTemplateName[msgID]; return messages.getInstanceOf(msgName); } public static String getMessageType(int msgID) { if (getErrorState().warningMsgIDs.member(msgID)) { return messages.getInstanceOf("warning").toString(); } else if (getErrorState().errorMsgIDs.member(msgID)) { return messages.getInstanceOf("error").toString(); } assertTrue(false, "Assertion failed! Message ID " + msgID + " created but is not present in errorMsgIDs or warningMsgIDs."); return ""; } /** Return a StringTemplate that refers to the current format used for * emitting messages. */ public static StringTemplate getLocationFormat() { return format.getInstanceOf("location"); } public static StringTemplate getReportFormat() { return format.getInstanceOf("report"); } public static StringTemplate getMessageFormat() { return format.getInstanceOf("message"); } public static boolean formatWantsSingleLineMessage() { return format.getInstanceOf("wantsSingleLineMessage").toString().equals("true"); } public static ANTLRErrorListener getErrorListener() { ANTLRErrorListener el = (ANTLRErrorListener)threadToListenerMap.get(Thread.currentThread()); if ( el==null ) { return theDefaultErrorListener; } return el; } public static ErrorState getErrorState() { ErrorState ec = (ErrorState)threadToErrorCountMap.get(Thread.currentThread()); if ( ec==null ) { ec = new ErrorState(); threadToErrorCountMap.put(Thread.currentThread(), ec); } return ec; } public static void resetErrorState() { ErrorState ec = new ErrorState(); threadToErrorCountMap.put(Thread.currentThread(), ec); } public static void info(String msg) { getErrorState().infos++; getErrorListener().info(msg); } public static void error(int msgID) { getErrorState().errors++; getErrorState().errorMsgIDs.add(msgID); getErrorListener().error(new ToolMessage(msgID)); } public static void error(int msgID, Throwable e) { getErrorState().errors++; getErrorState().errorMsgIDs.add(msgID); getErrorListener().error(new ToolMessage(msgID,e)); } public static void error(int msgID, Object arg) { getErrorState().errors++; getErrorState().errorMsgIDs.add(msgID); getErrorListener().error(new ToolMessage(msgID, arg)); } public static void error(int msgID, Object arg, Object arg2) { getErrorState().errors++; getErrorState().errorMsgIDs.add(msgID); getErrorListener().error(new ToolMessage(msgID, arg, arg2)); } public static void error(int msgID, Object arg, Throwable e) { getErrorState().errors++; getErrorState().errorMsgIDs.add(msgID); getErrorListener().error(new ToolMessage(msgID, arg, e)); } public static void warning(int msgID, Object arg) { getErrorState().warnings++; getErrorState().warningMsgIDs.add(msgID); getErrorListener().warning(new ToolMessage(msgID, arg)); } public static void nondeterminism(DecisionProbe probe, DFAState d)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -