Class ErrorManager

java.lang.Object
org.antlr.tool.ErrorManager

public class ErrorManager extends Object
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.
  • Field Details

    • MSG_CANNOT_WRITE_FILE

      public static final int MSG_CANNOT_WRITE_FILE
      See Also:
    • MSG_CANNOT_CLOSE_FILE

      public static final int MSG_CANNOT_CLOSE_FILE
      See Also:
    • MSG_CANNOT_FIND_TOKENS_FILE

      public static final int MSG_CANNOT_FIND_TOKENS_FILE
      See Also:
    • MSG_ERROR_READING_TOKENS_FILE

      public static final int MSG_ERROR_READING_TOKENS_FILE
      See Also:
    • MSG_DIR_NOT_FOUND

      public static final int MSG_DIR_NOT_FOUND
      See Also:
    • MSG_OUTPUT_DIR_IS_FILE

      public static final int MSG_OUTPUT_DIR_IS_FILE
      See Also:
    • MSG_CANNOT_OPEN_FILE

      public static final int MSG_CANNOT_OPEN_FILE
      See Also:
    • MSG_FILE_AND_GRAMMAR_NAME_DIFFER

      public static final int MSG_FILE_AND_GRAMMAR_NAME_DIFFER
      See Also:
    • MSG_FILENAME_EXTENSION_ERROR

      public static final int MSG_FILENAME_EXTENSION_ERROR
      See Also:
    • MSG_INTERNAL_ERROR

      public static final int MSG_INTERNAL_ERROR
      See Also:
    • MSG_INTERNAL_WARNING

      public static final int MSG_INTERNAL_WARNING
      See Also:
    • MSG_ERROR_CREATING_ARTIFICIAL_RULE

      public static final int MSG_ERROR_CREATING_ARTIFICIAL_RULE
      See Also:
    • MSG_TOKENS_FILE_SYNTAX_ERROR

      public static final int MSG_TOKENS_FILE_SYNTAX_ERROR
      See Also:
    • MSG_CANNOT_GEN_DOT_FILE

      public static final int MSG_CANNOT_GEN_DOT_FILE
      See Also:
    • MSG_BAD_AST_STRUCTURE

      public static final int MSG_BAD_AST_STRUCTURE
      See Also:
    • MSG_BAD_ACTION_AST_STRUCTURE

      public static final int MSG_BAD_ACTION_AST_STRUCTURE
      See Also:
    • MSG_MISSING_CODE_GEN_TEMPLATES

      public static final int MSG_MISSING_CODE_GEN_TEMPLATES
      See Also:
    • MSG_MISSING_CYCLIC_DFA_CODE_GEN_TEMPLATES

      public static final int MSG_MISSING_CYCLIC_DFA_CODE_GEN_TEMPLATES
      See Also:
    • MSG_CODE_GEN_TEMPLATES_INCOMPLETE

      public static final int MSG_CODE_GEN_TEMPLATES_INCOMPLETE
      See Also:
    • MSG_CANNOT_CREATE_TARGET_GENERATOR

      public static final int MSG_CANNOT_CREATE_TARGET_GENERATOR
      See Also:
    • MSG_SYNTAX_ERROR

      public static final int MSG_SYNTAX_ERROR
      See Also:
    • MSG_RULE_REDEFINITION

      public static final int MSG_RULE_REDEFINITION
      See Also:
    • MSG_LEXER_RULES_NOT_ALLOWED

      public static final int MSG_LEXER_RULES_NOT_ALLOWED
      See Also:
    • MSG_PARSER_RULES_NOT_ALLOWED

      public static final int MSG_PARSER_RULES_NOT_ALLOWED
      See Also:
    • MSG_CANNOT_FIND_ATTRIBUTE_NAME_IN_DECL

      public static final int MSG_CANNOT_FIND_ATTRIBUTE_NAME_IN_DECL
      See Also:
    • MSG_NO_TOKEN_DEFINITION

      public static final int MSG_NO_TOKEN_DEFINITION
      See Also:
    • MSG_UNDEFINED_RULE_REF

      public static final int MSG_UNDEFINED_RULE_REF
      See Also:
    • MSG_LITERAL_NOT_ASSOCIATED_WITH_LEXER_RULE

      public static final int MSG_LITERAL_NOT_ASSOCIATED_WITH_LEXER_RULE
      See Also:
    • MSG_CANNOT_ALIAS_TOKENS_IN_LEXER

      public static final int MSG_CANNOT_ALIAS_TOKENS_IN_LEXER
      See Also:
    • MSG_ATTRIBUTE_REF_NOT_IN_RULE

      public static final int MSG_ATTRIBUTE_REF_NOT_IN_RULE
      See Also:
    • MSG_INVALID_RULE_SCOPE_ATTRIBUTE_REF

      public static final int MSG_INVALID_RULE_SCOPE_ATTRIBUTE_REF
      See Also:
    • MSG_UNKNOWN_ATTRIBUTE_IN_SCOPE

      public static final int MSG_UNKNOWN_ATTRIBUTE_IN_SCOPE
      See Also:
    • MSG_UNKNOWN_SIMPLE_ATTRIBUTE

      public static final int MSG_UNKNOWN_SIMPLE_ATTRIBUTE
      See Also:
    • MSG_INVALID_RULE_PARAMETER_REF

      public static final int MSG_INVALID_RULE_PARAMETER_REF
      See Also:
    • MSG_UNKNOWN_RULE_ATTRIBUTE

      public static final int MSG_UNKNOWN_RULE_ATTRIBUTE
      See Also:
    • MSG_ISOLATED_RULE_SCOPE

      public static final int MSG_ISOLATED_RULE_SCOPE
      See Also:
    • MSG_SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE

      public static final int MSG_SYMBOL_CONFLICTS_WITH_GLOBAL_SCOPE
      See Also:
    • MSG_LABEL_CONFLICTS_WITH_RULE

      public static final int MSG_LABEL_CONFLICTS_WITH_RULE
      See Also:
    • MSG_LABEL_CONFLICTS_WITH_TOKEN

      public static final int MSG_LABEL_CONFLICTS_WITH_TOKEN
      See Also:
    • MSG_LABEL_CONFLICTS_WITH_RULE_SCOPE_ATTRIBUTE

      public static final int MSG_LABEL_CONFLICTS_WITH_RULE_SCOPE_ATTRIBUTE
      See Also:
    • MSG_LABEL_CONFLICTS_WITH_RULE_ARG_RETVAL

      public static final int MSG_LABEL_CONFLICTS_WITH_RULE_ARG_RETVAL
      See Also:
    • MSG_ATTRIBUTE_CONFLICTS_WITH_RULE

      public static final int MSG_ATTRIBUTE_CONFLICTS_WITH_RULE
      See Also:
    • MSG_ATTRIBUTE_CONFLICTS_WITH_RULE_ARG_RETVAL

      public static final int MSG_ATTRIBUTE_CONFLICTS_WITH_RULE_ARG_RETVAL
      See Also:
    • MSG_LABEL_TYPE_CONFLICT

      public static final int MSG_LABEL_TYPE_CONFLICT
      See Also:
    • MSG_ARG_RETVAL_CONFLICT

      public static final int MSG_ARG_RETVAL_CONFLICT
      See Also:
    • MSG_NONUNIQUE_REF

      public static final int MSG_NONUNIQUE_REF
      See Also:
    • MSG_FORWARD_ELEMENT_REF

      public static final int MSG_FORWARD_ELEMENT_REF
      See Also:
    • MSG_MISSING_RULE_ARGS

      public static final int MSG_MISSING_RULE_ARGS
      See Also:
    • MSG_RULE_HAS_NO_ARGS

      public static final int MSG_RULE_HAS_NO_ARGS
      See Also:
    • MSG_ARGS_ON_TOKEN_REF

      public static final int MSG_ARGS_ON_TOKEN_REF
      See Also:
    • MSG_RULE_REF_AMBIG_WITH_RULE_IN_ALT

      public static final int MSG_RULE_REF_AMBIG_WITH_RULE_IN_ALT
      See Also:
    • MSG_ILLEGAL_OPTION

      public static final int MSG_ILLEGAL_OPTION
      See Also:
    • MSG_LIST_LABEL_INVALID_UNLESS_RETVAL_STRUCT

      public static final int MSG_LIST_LABEL_INVALID_UNLESS_RETVAL_STRUCT
      See Also:
    • MSG_UNDEFINED_TOKEN_REF_IN_REWRITE

      public static final int MSG_UNDEFINED_TOKEN_REF_IN_REWRITE
      See Also:
    • MSG_REWRITE_ELEMENT_NOT_PRESENT_ON_LHS

      public static final int MSG_REWRITE_ELEMENT_NOT_PRESENT_ON_LHS
      See Also:
    • MSG_UNDEFINED_LABEL_REF_IN_REWRITE

      public static final int MSG_UNDEFINED_LABEL_REF_IN_REWRITE
      See Also:
    • MSG_NO_GRAMMAR_START_RULE

      public static final int MSG_NO_GRAMMAR_START_RULE
      See Also:
    • MSG_EMPTY_COMPLEMENT

      public static final int MSG_EMPTY_COMPLEMENT
      See Also:
    • MSG_UNKNOWN_DYNAMIC_SCOPE

      public static final int MSG_UNKNOWN_DYNAMIC_SCOPE
      See Also:
    • MSG_UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE

      public static final int MSG_UNKNOWN_DYNAMIC_SCOPE_ATTRIBUTE
      See Also:
    • MSG_ISOLATED_RULE_ATTRIBUTE

      public static final int MSG_ISOLATED_RULE_ATTRIBUTE
      See Also:
    • MSG_INVALID_ACTION_SCOPE

      public static final int MSG_INVALID_ACTION_SCOPE
      See Also:
    • MSG_ACTION_REDEFINITION

      public static final int MSG_ACTION_REDEFINITION
      See Also:
    • MSG_DOUBLE_QUOTES_ILLEGAL

      public static final int MSG_DOUBLE_QUOTES_ILLEGAL
      See Also:
    • MSG_INVALID_TEMPLATE_ACTION

      public static final int MSG_INVALID_TEMPLATE_ACTION
      See Also:
    • MSG_MISSING_ATTRIBUTE_NAME

      public static final int MSG_MISSING_ATTRIBUTE_NAME
      See Also:
    • MSG_ARG_INIT_VALUES_ILLEGAL

      public static final int MSG_ARG_INIT_VALUES_ILLEGAL
      See Also:
    • MSG_REWRITE_OR_OP_WITH_NO_OUTPUT_OPTION

      public static final int MSG_REWRITE_OR_OP_WITH_NO_OUTPUT_OPTION
      See Also:
    • MSG_NO_RULES

      public static final int MSG_NO_RULES
      See Also:
    • MSG_WRITE_TO_READONLY_ATTR

      public static final int MSG_WRITE_TO_READONLY_ATTR
      See Also:
    • MSG_MISSING_AST_TYPE_IN_TREE_GRAMMAR

      public static final int MSG_MISSING_AST_TYPE_IN_TREE_GRAMMAR
      See Also:
    • MSG_REWRITE_FOR_MULTI_ELEMENT_ALT

      public static final int MSG_REWRITE_FOR_MULTI_ELEMENT_ALT
      See Also:
    • MSG_RULE_INVALID_SET

      public static final int MSG_RULE_INVALID_SET
      See Also:
    • MSG_HETERO_ILLEGAL_IN_REWRITE_ALT

      public static final int MSG_HETERO_ILLEGAL_IN_REWRITE_ALT
      See Also:
    • MSG_NO_SUCH_GRAMMAR_SCOPE

      public static final int MSG_NO_SUCH_GRAMMAR_SCOPE
      See Also:
    • MSG_NO_SUCH_RULE_IN_SCOPE

      public static final int MSG_NO_SUCH_RULE_IN_SCOPE
      See Also:
    • MSG_TOKEN_ALIAS_CONFLICT

      public static final int MSG_TOKEN_ALIAS_CONFLICT
      See Also:
    • MSG_TOKEN_ALIAS_REASSIGNMENT

      public static final int MSG_TOKEN_ALIAS_REASSIGNMENT
      See Also:
    • MSG_TOKEN_VOCAB_IN_DELEGATE

      public static final int MSG_TOKEN_VOCAB_IN_DELEGATE
      See Also:
    • MSG_INVALID_IMPORT

      public static final int MSG_INVALID_IMPORT
      See Also:
    • MSG_IMPORTED_TOKENS_RULE_EMPTY

      public static final int MSG_IMPORTED_TOKENS_RULE_EMPTY
      See Also:
    • MSG_IMPORT_NAME_CLASH

      public static final int MSG_IMPORT_NAME_CLASH
      See Also:
    • MSG_AST_OP_WITH_NON_AST_OUTPUT_OPTION

      public static final int MSG_AST_OP_WITH_NON_AST_OUTPUT_OPTION
      See Also:
    • MSG_AST_OP_IN_ALT_WITH_REWRITE

      public static final int MSG_AST_OP_IN_ALT_WITH_REWRITE
      See Also:
    • MSG_WILDCARD_AS_ROOT

      public static final int MSG_WILDCARD_AS_ROOT
      See Also:
    • MSG_CONFLICTING_OPTION_IN_TREE_FILTER

      public static final int MSG_CONFLICTING_OPTION_IN_TREE_FILTER
      See Also:
    • MSG_GRAMMAR_NONDETERMINISM

      public static final int MSG_GRAMMAR_NONDETERMINISM
      See Also:
    • MSG_UNREACHABLE_ALTS

      public static final int MSG_UNREACHABLE_ALTS
      See Also:
    • MSG_DANGLING_STATE

      public static final int MSG_DANGLING_STATE
      See Also:
    • MSG_INSUFFICIENT_PREDICATES

      public static final int MSG_INSUFFICIENT_PREDICATES
      See Also:
    • MSG_DUPLICATE_SET_ENTRY

      public static final int MSG_DUPLICATE_SET_ENTRY
      See Also:
    • MSG_ANALYSIS_ABORTED

      public static final int MSG_ANALYSIS_ABORTED
      See Also:
    • MSG_RECURSION_OVERLOW

      public static final int MSG_RECURSION_OVERLOW
      See Also:
    • MSG_LEFT_RECURSION

      public static final int MSG_LEFT_RECURSION
      See Also:
    • MSG_UNREACHABLE_TOKENS

      public static final int MSG_UNREACHABLE_TOKENS
      See Also:
    • MSG_TOKEN_NONDETERMINISM

      public static final int MSG_TOKEN_NONDETERMINISM
      See Also:
    • MSG_LEFT_RECURSION_CYCLES

      public static final int MSG_LEFT_RECURSION_CYCLES
      See Also:
    • MSG_NONREGULAR_DECISION

      public static final int MSG_NONREGULAR_DECISION
      See Also:
    • MSG_CIRCULAR_DEPENDENCY

      public static final int MSG_CIRCULAR_DEPENDENCY
      See Also:
    • MAX_MESSAGE_NUMBER

      public static final int MAX_MESSAGE_NUMBER
      See Also:
    • ERRORS_FORCING_NO_ANALYSIS

      public static final BitSet ERRORS_FORCING_NO_ANALYSIS
      Do not do perform analysis if one of these happens
    • ERRORS_FORCING_NO_CODEGEN

      public static final BitSet ERRORS_FORCING_NO_CODEGEN
      Do not do code gen if one of these happens
    • emitSingleError

      public static final Map emitSingleError
      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.
    • locale

      private static Locale locale
      Messages should be sensitive to the locale.
    • formatName

      private static String formatName
    • threadToListenerMap

      private static Map threadToListenerMap
      Each thread might need it's own error listener; e.g., a GUI with multiple window frames holding multiple grammars.
    • threadToErrorStateMap

      private static Map threadToErrorStateMap
      Track the number of errors regardless of the listener but track per thread.
    • threadToToolMap

      private static Map threadToToolMap
      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.
    • messages

      private static org.antlr.stringtemplate.StringTemplateGroup messages
      The group of templates that represent all possible ANTLR errors.
    • format

      private static org.antlr.stringtemplate.StringTemplateGroup format
      The group of templates that represent the current message format.
    • idToMessageTemplateName

      private static String[] idToMessageTemplateName
      From a msgID how can I get the name of the template that describes the error or warning?
    • theDefaultErrorListener

      static ANTLRErrorListener theDefaultErrorListener
    • initSTListener

      static org.antlr.stringtemplate.StringTemplateErrorListener initSTListener
      Handle all ST error listeners here (code gen, Grammar, and this class use templates.
    • blankSTListener

      static org.antlr.stringtemplate.StringTemplateErrorListener blankSTListener
      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.
    • theDefaultSTListener

      static org.antlr.stringtemplate.StringTemplateErrorListener theDefaultSTListener
      Errors during initialization related to ST must all go to System.err.
  • Constructor Details

    • ErrorManager

      public ErrorManager()
  • Method Details

    • getStringTemplateErrorListener

      public static org.antlr.stringtemplate.StringTemplateErrorListener getStringTemplateErrorListener()
    • setLocale

      public static void setLocale(Locale locale)
      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.
    • setFormat

      public static void setFormat(String formatName)
      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".
    • setErrorListener

      public static void setErrorListener(ANTLRErrorListener listener)
      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.
    • removeErrorListener

      public static void removeErrorListener()
    • setTool

      public static void setTool(Tool tool)
    • getMessage

      public static org.antlr.stringtemplate.StringTemplate getMessage(int msgID)
      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.
    • getMessageType

      public static String getMessageType(int msgID)
    • getLocationFormat

      public static org.antlr.stringtemplate.StringTemplate getLocationFormat()
      Return a StringTemplate that refers to the current format used for emitting messages.
    • getReportFormat

      public static org.antlr.stringtemplate.StringTemplate getReportFormat()
    • getMessageFormat

      public static org.antlr.stringtemplate.StringTemplate getMessageFormat()
    • formatWantsSingleLineMessage

      public static boolean formatWantsSingleLineMessage()
    • getErrorListener

      public static ANTLRErrorListener getErrorListener()
    • getErrorState

      public static ErrorManager.ErrorState getErrorState()
    • getNumErrors

      public static int getNumErrors()
    • resetErrorState

      public static void resetErrorState()
    • info

      public static void info(String msg)
    • error

      public static void error(int msgID)
    • error

      public static void error(int msgID, Throwable e)
    • error

      public static void error(int msgID, Object arg)
    • error

      public static void error(int msgID, Object arg, Object arg2)
    • error

      public static void error(int msgID, Object arg, Throwable e)
    • warning

      public static void warning(int msgID, Object arg)
    • nondeterminism

      public static void nondeterminism(DecisionProbe probe, DFAState d)
    • danglingState

      public static void danglingState(DecisionProbe probe, DFAState d)
    • analysisAborted

      public static void analysisAborted(DecisionProbe probe)
    • unreachableAlts

      public static void unreachableAlts(DecisionProbe probe, List alts)
    • insufficientPredicates

      public static void insufficientPredicates(DecisionProbe probe, DFAState d, Map<Integer,Set<antlr.Token>> altToUncoveredLocations)
    • nonLLStarDecision

      public static void nonLLStarDecision(DecisionProbe probe)
    • recursionOverflow

      public static void recursionOverflow(DecisionProbe probe, DFAState sampleBadState, int alt, Collection targetRules, Collection callSiteStates)
    • leftRecursionCycles

      public static void leftRecursionCycles(Collection cycles)
    • grammarError

      public static void grammarError(int msgID, Grammar g, antlr.Token token, Object arg, Object arg2)
    • grammarError

      public static void grammarError(int msgID, Grammar g, antlr.Token token, Object arg)
    • grammarError

      public static void grammarError(int msgID, Grammar g, antlr.Token token)
    • grammarWarning

      public static void grammarWarning(int msgID, Grammar g, antlr.Token token, Object arg, Object arg2)
    • grammarWarning

      public static void grammarWarning(int msgID, Grammar g, antlr.Token token, Object arg)
    • grammarWarning

      public static void grammarWarning(int msgID, Grammar g, antlr.Token token)
    • syntaxError

      public static void syntaxError(int msgID, Grammar grammar, antlr.Token token, Object arg, antlr.RecognitionException re)
    • internalError

      public static void internalError(Object error, Throwable e)
    • internalError

      public static void internalError(Object error)
    • doNotAttemptAnalysis

      public static boolean doNotAttemptAnalysis()
    • doNotAttemptCodeGen

      public static boolean doNotAttemptCodeGen()
    • getLastNonErrorManagerCodeLocation

      private static StackTraceElement getLastNonErrorManagerCodeLocation(Throwable e)
      Return first non ErrorManager code location for generating messages
    • assertTrue

      public static void assertTrue(boolean condition, String message)
    • initIdToMessageNameMapping

      protected static boolean initIdToMessageNameMapping()
    • verifyMessages

      protected static boolean verifyMessages()
      Use reflection to find list of MSG_ fields and then verify a template exists for each one from the locale's group.
    • verifyFormat

      protected static boolean verifyFormat()
      Verify the message format template group
    • rawError

      static void rawError(String msg)
      If there are errors during ErrorManager init, we have no choice but to go to System.err.
    • rawError

      static void rawError(String msg, Throwable e)
    • panic

      public static void panic()
      I *think* this will allow Tool subclasses to exit gracefully for GUIs etc...