Class ErrorListener

java.lang.Object
org.antlr.v4.runtime.BaseErrorListener
org.ek9lang.compiler.common.ErrorListener
All Implemented Interfaces:
Serializable, org.antlr.v4.runtime.ANTLRErrorListener, SemanticErrorReporter

public class ErrorListener extends org.antlr.v4.runtime.BaseErrorListener implements Serializable, SemanticErrorReporter
We need an error listener for the lexing parsing and also our own tree visiting. So our own tree visiting will be able to use additional methods we add on to this; so we can report semantic errors and warnings. Why? Because we need to gather-up all the errors on a per-file processing basis. This is needed because we are going to multi-thread the various stages of compilation based on a thread per source file. So, we would not want the error information being interleaved in the console output. Plus eventually there will be a UI over the compiler, and we will need very focused sets of errors on a per source code basis. This is not designed to be thread safe, only one thread should be parsing/processing a source or one a specific phase.
See Also:
  • Constructor Details

    • ErrorListener

      public ErrorListener(String generalIdentifierOfSource)
      Create new error listener.
  • Method Details

    • getGeneralIdentifierOfSource

      public String getGeneralIdentifierOfSource()
    • setErrorLevel

      public void setErrorLevel(int errorLevel)
      Set the error verbosity level from CompilerFlags. Called before compilation starts.
      Parameters:
      errorLevel - 0=minimal, 1=visual, 2=visual+fuzzy, 3=rich explanations, 4=rich+forAI.json dump
    • getErrorLevel

      public int getErrorLevel()
      Get the current error verbosity level.
      Returns:
      The error level (0-4)
    • setSourceLineProvider

      public void setSourceLineProvider(SourceLineProvider provider)
      Set the source line provider for visual error display. Called before compilation starts when visual errors are enabled.
    • getSourceLineProvider

      public SourceLineProvider getSourceLineProvider()
      Get the current source line provider. May be null if not configured or after deserialization.
    • isFuzzySuggestionsEnabled

      public boolean isFuzzySuggestionsEnabled()
      Check if fuzzy matching suggestions are computed (level >= 2, or forced by the caller). See CompilerFlags.isFuzzySuggestionsEnabled() for why the two are separable.
      Specified by:
      isFuzzySuggestionsEnabled in interface SemanticErrorReporter
    • setSuggestionsForced

      public void setSuggestionsForced(boolean suggestionsForced)
      Compute "did you mean" candidates whatever the error level - for a programmatic consumer that wants the candidates but not the boxed visual rendering level 1+ brings with it.
    • isSuggestionsForced

      public boolean isSuggestionsForced()
      The raw forced flag, as set - NOT the level's contribution. Read when re-creating a listener.
    • getSourceLine

      public String getSourceLine(String fileName, int lineNumber)
      Get a source line for error display.
    • reset

      public void reset()
      Remove all errors and warnings.
    • suppressSyntaxErrors

      public void suppressSyntaxErrors()
      From now on, ignore syntax errors. Called by the lexer when a source is nested beyond its limit (E11011): the lexer ends the input there, so every syntax error the parser then reports is only a consequence of that cut, and would bury the one error that says what is wrong.
    • getShortNameOfSourceFile

      public String getShortNameOfSourceFile(IToken token)
      Get the filename (but not full path) of the source file.
      Specified by:
      getShortNameOfSourceFile in interface SemanticErrorReporter
    • isExceptionOnAmbiguity

      public boolean isExceptionOnAmbiguity()
    • setExceptionOnAmbiguity

      public void setExceptionOnAmbiguity(boolean exceptionOnAmbiguity)
    • isExceptionOnContextSensitive

      public boolean isExceptionOnContextSensitive()
    • setExceptionOnContextSensitive

      public void setExceptionOnContextSensitive(boolean exceptionOnContextSensitive)
    • isExceptionOnFullContext

      public boolean isExceptionOnFullContext()
    • setExceptionOnFullContext

      public void setExceptionOnFullContext(boolean exceptionOnFullContext)
    • getErrors

      public Iterator<ErrorListener.ErrorDetails> getErrors()
      Get all Errors. See other methods (yet to come to get specific types of errors/warnings). Marks duplicate rich messages for suppression (first-occurrence-only per error code).
    • getDirectiveErrors

      public Iterator<ErrorListener.ErrorDetails> getDirectiveErrors()
    • hasDirectiveErrors

      public boolean hasDirectiveErrors()
    • hasErrors

      public boolean hasErrors()
    • isErrorFree

      public boolean isErrorFree()
    • getWarnings

      public Iterator<ErrorListener.ErrorDetails> getWarnings()
    • hasWarnings

      public boolean hasWarnings()
    • isWarningFree

      public boolean isWarningFree()
      true if has no warnings.
    • raiseReturningNotRequired

      public void raiseReturningNotRequired(IToken token, String msg)
      Create new semantic error.
    • raiseReturningRequired

      public void raiseReturningRequired(IToken token, String msg)
      Create new semantic error.
    • semanticError

      public void semanticError(IToken offendingToken, String msg, ErrorListener.SemanticClassification classification, MatchResults results)
      Create new semantic error.
      Specified by:
      semanticError in interface SemanticErrorReporter
    • semanticError

      public void semanticError(IToken offendingToken, String msg, ErrorListener.SemanticClassification classification, List<String> declaredParameterNames)
      A semantic error ABOUT A CALL'S ARGUMENT LIST, carrying the parameter names the called method declares, in order - see ErrorListener.ErrorDetails.getDeclaredParameterNames().
      Specified by:
      semanticError in interface SemanticErrorReporter
    • semanticError

      public void semanticError(IToken offendingToken, String msg, ErrorListener.SemanticClassification classification)
      Create new semantic error.
      Specified by:
      semanticError in interface SemanticErrorReporter
    • semanticError

      public void semanticError(org.antlr.v4.runtime.Token offendingToken, String msg, ErrorListener.SemanticClassification classification)
      Create new semantic error.
      Specified by:
      semanticError in interface SemanticErrorReporter
    • directiveError

      public void directiveError(IToken offendingToken, String msg, ErrorListener.SemanticClassification classification)
      Create a new error based on the directive. This means that if a directive was present or was missing against an actual compiler error - we can raise this specific type of error to indicate something that the EK9 developer was checking for (via a directive) is in error.
    • semanticWarning

      public void semanticWarning(IToken offendingToken, String msg, ErrorListener.SemanticClassification classification)
      Issue a semantic warning.
      Specified by:
      semanticWarning in interface SemanticErrorReporter
    • syntaxError

      public void syntaxError(org.antlr.v4.runtime.Recognizer<?,?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, org.antlr.v4.runtime.RecognitionException e)
      Specified by:
      syntaxError in interface org.antlr.v4.runtime.ANTLRErrorListener
      Overrides:
      syntaxError in class org.antlr.v4.runtime.BaseErrorListener