Search in sources :

Example 11 with ParserATNFactory

use of org.antlr.v4.automata.ParserATNFactory in project antlr4 by tunnelvisionlabs.

the class BaseTest method createATN.

protected ATN createATN(Grammar g, boolean useSerializer) {
    if (g.atn == null) {
        semanticProcess(g);
        assertEquals(0, g.tool.getNumErrors());
        ParserATNFactory f;
        if (g.isLexer()) {
            f = new LexerATNFactory((LexerGrammar) g);
        } else {
            f = new ParserATNFactory(g);
        }
        g.atn = f.createATN();
        assertEquals(0, g.tool.getNumErrors());
    }
    ATN atn = g.atn;
    if (useSerializer) {
        char[] serialized = ATNSerializer.getSerializedAsChars(atn, Arrays.asList(g.getRuleNames()));
        return new ATNDeserializer().deserialize(serialized);
    }
    return atn;
}
Also used : ATNDeserializer(org.antlr.v4.runtime.atn.ATNDeserializer) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATN(org.antlr.v4.runtime.atn.ATN) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory)

Example 12 with ParserATNFactory

use of org.antlr.v4.automata.ParserATNFactory in project antlr4 by tunnelvisionlabs.

the class Tool method processNonCombinedGrammar.

public void processNonCombinedGrammar(Grammar g, boolean gencode) {
    if (g.ast == null || g.ast.hasErrors)
        return;
    if (internalOption_PrintGrammarTree)
        System.out.println(g.ast.toStringTree());
    boolean ruleFail = checkForRuleIssues(g);
    if (ruleFail)
        return;
    int prevErrors = errMgr.getNumErrors();
    // MAKE SURE GRAMMAR IS SEMANTICALLY CORRECT (FILL IN GRAMMAR OBJECT)
    SemanticPipeline sem = new SemanticPipeline(g);
    sem.process();
    if (errMgr.getNumErrors() > prevErrors)
        return;
    // BUILD ATN FROM AST
    ATNFactory factory;
    if (g.isLexer())
        factory = new LexerATNFactory((LexerGrammar) g);
    else
        factory = new ParserATNFactory(g);
    g.atn = factory.createATN();
    if (generate_ATN_dot)
        generateATNs(g);
    if (g.tool.getNumErrors() == 0)
        generateInterpreterData(g);
    // PERFORM GRAMMAR ANALYSIS ON ATN: BUILD DECISION DFAs
    AnalysisPipeline anal = new AnalysisPipeline(g);
    anal.process();
    if (g.tool.getNumErrors() > prevErrors)
        return;
    // GENERATE CODE
    if (gencode) {
        CodeGenPipeline gen = new CodeGenPipeline(g);
        gen.process();
    }
}
Also used : AnalysisPipeline(org.antlr.v4.analysis.AnalysisPipeline) SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) CodeGenPipeline(org.antlr.v4.codegen.CodeGenPipeline) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory)

Example 13 with ParserATNFactory

use of org.antlr.v4.automata.ParserATNFactory in project antlr4 by antlr.

the class Tool method processNonCombinedGrammar.

public void processNonCombinedGrammar(Grammar g, boolean gencode) {
    if (g.ast == null || g.ast.hasErrors)
        return;
    if (internalOption_PrintGrammarTree)
        System.out.println(g.ast.toStringTree());
    boolean ruleFail = checkForRuleIssues(g);
    if (ruleFail)
        return;
    int prevErrors = errMgr.getNumErrors();
    // MAKE SURE GRAMMAR IS SEMANTICALLY CORRECT (FILL IN GRAMMAR OBJECT)
    SemanticPipeline sem = new SemanticPipeline(g);
    sem.process();
    String language = g.getOptionString("language");
    if (!CodeGenerator.targetExists(language)) {
        errMgr.toolError(ErrorType.CANNOT_CREATE_TARGET_GENERATOR, language);
        return;
    }
    if (errMgr.getNumErrors() > prevErrors)
        return;
    // BUILD ATN FROM AST
    ATNFactory factory;
    if (g.isLexer())
        factory = new LexerATNFactory((LexerGrammar) g);
    else
        factory = new ParserATNFactory(g);
    g.atn = factory.createATN();
    if (generate_ATN_dot)
        generateATNs(g);
    if (gencode && g.tool.getNumErrors() == 0)
        generateInterpreterData(g);
    // PERFORM GRAMMAR ANALYSIS ON ATN: BUILD DECISION DFAs
    AnalysisPipeline anal = new AnalysisPipeline(g);
    anal.process();
    if (g.tool.getNumErrors() > prevErrors)
        return;
    // GENERATE CODE
    if (gencode) {
        CodeGenPipeline gen = new CodeGenPipeline(g);
        gen.process();
    }
}
Also used : AnalysisPipeline(org.antlr.v4.analysis.AnalysisPipeline) SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) CodeGenPipeline(org.antlr.v4.codegen.CodeGenPipeline) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory)

Example 14 with ParserATNFactory

use of org.antlr.v4.automata.ParserATNFactory in project antlr4 by antlr.

the class BaseJavaTest method testActions.

public void testActions(String templates, String actionName, String action, String expected) throws org.antlr.runtime.RecognitionException {
    int lp = templates.indexOf('(');
    String name = templates.substring(0, lp);
    STGroup group = new STGroupString(templates);
    ST st = group.getInstanceOf(name);
    st.add(actionName, action);
    String grammar = st.render();
    ErrorQueue equeue = new ErrorQueue();
    Grammar g = new Grammar(grammar, equeue);
    if (g.ast != null && !g.ast.hasErrors) {
        SemanticPipeline sem = new SemanticPipeline(g);
        sem.process();
        ATNFactory factory = new ParserATNFactory(g);
        if (g.isLexer())
            factory = new LexerATNFactory((LexerGrammar) g);
        g.atn = factory.createATN();
        AnalysisPipeline anal = new AnalysisPipeline(g);
        anal.process();
        CodeGenerator gen = new CodeGenerator(g);
        ST outputFileST = gen.generateParser(false);
        String output = outputFileST.render();
        // System.out.println(output);
        String b = "#" + actionName + "#";
        int start = output.indexOf(b);
        String e = "#end-" + actionName + "#";
        int end = output.indexOf(e);
        String snippet = output.substring(start + b.length(), end);
        assertEquals(expected, snippet);
    }
    if (equeue.size() > 0) {
    // System.err.println(equeue.toString());
    }
}
Also used : AnalysisPipeline(org.antlr.v4.analysis.AnalysisPipeline) ST(org.stringtemplate.v4.ST) SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) STGroup(org.stringtemplate.v4.STGroup) STGroupString(org.stringtemplate.v4.STGroupString) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CodeGenerator(org.antlr.v4.codegen.CodeGenerator) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) STGroupString(org.stringtemplate.v4.STGroupString)

Example 15 with ParserATNFactory

use of org.antlr.v4.automata.ParserATNFactory in project antlr4 by antlr.

the class TestCodeGeneration method getEvalInfoForString.

public List<String> getEvalInfoForString(String grammarString, String pattern) throws RecognitionException {
    ErrorQueue equeue = new ErrorQueue();
    Grammar g = new Grammar(grammarString);
    List<String> evals = new ArrayList<String>();
    if (g.ast != null && !g.ast.hasErrors) {
        SemanticPipeline sem = new SemanticPipeline(g);
        sem.process();
        ATNFactory factory = new ParserATNFactory(g);
        if (g.isLexer())
            factory = new LexerATNFactory((LexerGrammar) g);
        g.atn = factory.createATN();
        CodeGenerator gen = new CodeGenerator(g);
        ST outputFileST = gen.generateParser();
        // STViz viz = outputFileST.inspect();
        // try {
        // viz.waitForClose();
        // }
        // catch (Exception e) {
        // e.printStackTrace();
        // }
        boolean debug = false;
        DebugInterpreter interp = new DebugInterpreter(outputFileST.groupThatCreatedThisInstance, outputFileST.impl.nativeGroup.errMgr, debug);
        InstanceScope scope = new InstanceScope(null, outputFileST);
        StringWriter sw = new StringWriter();
        AutoIndentWriter out = new AutoIndentWriter(sw);
        interp.exec(out, scope);
        for (String e : interp.evals) {
            if (e.contains(pattern)) {
                evals.add(e);
            }
        }
    }
    if (equeue.size() > 0) {
        System.err.println(equeue.toString());
    }
    return evals;
}
Also used : SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) ST(org.stringtemplate.v4.ST) ArrayList(java.util.ArrayList) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CodeGenerator(org.antlr.v4.codegen.CodeGenerator) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) AutoIndentWriter(org.stringtemplate.v4.AutoIndentWriter) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) StringWriter(java.io.StringWriter) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) InstanceScope(org.stringtemplate.v4.InstanceScope) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue)

Aggregations

ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)33 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)22 ATN (org.antlr.v4.runtime.atn.ATN)19 LexerGrammar (org.antlr.v4.tool.LexerGrammar)17 ATNFactory (org.antlr.v4.automata.ATNFactory)12 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)12 STGroupString (org.stringtemplate.v4.STGroupString)12 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)10 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)9 ATNState (org.antlr.v4.runtime.atn.ATNState)9 DOTGenerator (org.antlr.v4.tool.DOTGenerator)9 Grammar (org.antlr.v4.tool.Grammar)9 Rule (org.antlr.v4.tool.Rule)9 ST (org.stringtemplate.v4.ST)9 ATNDeserializer (org.antlr.v4.runtime.atn.ATNDeserializer)8 ATNPrinter (org.antlr.v4.automata.ATNPrinter)7 STGroup (org.stringtemplate.v4.STGroup)7 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)6 AnalysisPipeline (org.antlr.v4.analysis.AnalysisPipeline)5 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)4