Search in sources :

Example 1 with ErrorListener

use of org.antlr.works.utils.ErrorListener in project antlrworks by antlr.

the class ANTLRGrammarEngineImpl method analyze.

public GrammarResult analyze() throws Exception {
    // if there is no need to analyze the grammar, return the previous result
    if (!needsToAnalyzeGrammar) {
        GrammarResult r = analyzeCompleted(null);
        if (r.isSuccess()) {
            return r;
        } else {
            needsToAnalyzeGrammar = true;
        }
    }
    // Set the error listener
    ErrorListener el = ErrorListener.getThreadInstance();
    ErrorManager.setErrorListener(el);
    createGrammars();
    Grammar g = getDefaultGrammar();
    if (g == null) {
        return analyzeCompleted(el);
    }
    List rules = g.checkAllRulesForLeftRecursion();
    if (!rules.isEmpty()) {
        printLeftRecursionToConsole(rules);
        markLeftRecursiveRules(rules);
    }
    if (ErrorManager.doNotAttemptAnalysis()) {
        return analyzeCompleted(el);
    }
    try {
        if (g.nfa == null) {
            g.composite.createNFAs();
        }
        g.createLookaheadDFAs();
        if (engine.isCombinedGrammar()) {
            // If the grammar is combined, analyze also the lexer
            if (lexerGrammar != null) {
                lexerGrammar.composite.createNFAs();
                lexerGrammar.createLookaheadDFAs();
            }
        }
        buildNonDeterministicErrors(el);
        markRulesWithWarningsOrErrors();
    } catch (Exception e) {
    // ignore
    }
    return analyzeCompleted(el);
}
Also used : ErrorListener(org.antlr.works.utils.ErrorListener) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) RecognitionException(org.antlr.runtime.RecognitionException) TokenStreamException(antlr.TokenStreamException)

Example 2 with ErrorListener

use of org.antlr.works.utils.ErrorListener in project antlrworks by antlr.

the class CodeGenerate method generate.

public boolean generate() {
    ErrorListener el = ErrorListener.getThreadInstance();
    ErrorManager.setErrorListener(el);
    String[] params;
    if (debug)
        params = new String[] { "-debug", "-o", getOutputPath(), "-lib", window.getFileFolder(), window.getFilePath() };
    else
        params = new String[] { "-o", getOutputPath(), "-lib", window.getFileFolder(), window.getFilePath() };
    new File(getOutputPath()).mkdirs();
    Tool antlr = new Tool(Utils.concat(params, AWPrefs.getANTLR3Options()));
    antlr.process();
    boolean success = !el.hasErrors();
    if (success) {
        dateOfModificationOnDisk = window.getDocument().getDateOfModificationOnDisk();
    }
    lastError = el.getFirstErrorMessage();
    el.clear();
    ErrorManager.removeErrorListener();
    return success;
}
Also used : ErrorListener(org.antlr.works.utils.ErrorListener) File(java.io.File) Tool(org.antlr.Tool)

Example 3 with ErrorListener

use of org.antlr.works.utils.ErrorListener in project antlrworks by antlr.

the class ANTLRGrammarEngineImpl method createGrammars.

public void createGrammars() throws Exception {
    if (!needsToCreateGrammar) {
        if (createGrammarResult.isSuccess()) {
            return;
        } else {
            needsToCreateGrammar = true;
        }
    }
    ErrorListener el = ErrorListener.getThreadInstance();
    ErrorManager.setErrorListener(el);
    parserGrammar = null;
    lexerGrammar = null;
    createGrammarResult.clear();
    try {
        switch(engine.getType()) {
            case ElementGrammarName.COMBINED:
                createCombinedGrammar();
                break;
            case ElementGrammarName.TREEPARSER:
            case ElementGrammarName.PARSER:
                createParserGrammar();
                break;
            case ElementGrammarName.LEXER:
                createLexerGrammar();
                break;
        }
        // if no exception, then assume create grammar was successful
        needsToCreateGrammar = false;
    } finally {
        // store the result of creating the grammars
        createGrammarResult.setErrors(el.errors);
        createGrammarResult.setWarnings(el.warnings);
        el.clear();
        ErrorManager.removeErrorListener();
    }
}
Also used : ErrorListener(org.antlr.works.utils.ErrorListener)

Aggregations

ErrorListener (org.antlr.works.utils.ErrorListener)3 TokenStreamException (antlr.TokenStreamException)1 File (java.io.File)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Tool (org.antlr.Tool)1 RecognitionException (org.antlr.runtime.RecognitionException)1