Search in sources :

Example 11 with SemanticPipeline

use of org.antlr.v4.semantics.SemanticPipeline 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);
    // 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 12 with SemanticPipeline

use of org.antlr.v4.semantics.SemanticPipeline in project antlr4 by antlr.

the class BaseCppTest 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();
        CodeGenerator gen = new CodeGenerator(g);
        ST outputFileST = gen.generateParser();
        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 : ST(org.stringtemplate.v4.ST) SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) STGroup(org.stringtemplate.v4.STGroup) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CodeGenerator(org.antlr.v4.codegen.CodeGenerator) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) STGroupString(org.stringtemplate.v4.STGroupString)

Example 13 with SemanticPipeline

use of org.antlr.v4.semantics.SemanticPipeline in project antlr4 by antlr.

the class BaseCppTest method semanticProcess.

protected void semanticProcess(Grammar g) {
    if (g.ast != null && !g.ast.hasErrors) {
        System.out.println(g.ast.toStringTree());
        Tool antlr = new Tool();
        SemanticPipeline sem = new SemanticPipeline(g);
        sem.process();
        if (g.getImportedGrammars() != null) {
            // process imported grammars (if any)
            for (Grammar imp : g.getImportedGrammars()) {
                antlr.processNonCombinedGrammar(imp, false);
            }
        }
    }
}
Also used : SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Tool(org.antlr.v4.Tool)

Example 14 with SemanticPipeline

use of org.antlr.v4.semantics.SemanticPipeline in project antlr4 by antlr.

the class BaseGoTest method semanticProcess.

protected void semanticProcess(Grammar g) {
    if (g.ast != null && !g.ast.hasErrors) {
        System.out.println(g.ast.toStringTree());
        Tool antlr = new Tool();
        SemanticPipeline sem = new SemanticPipeline(g);
        sem.process();
        if (g.getImportedGrammars() != null) {
            // (if any)
            for (Grammar imp : g.getImportedGrammars()) {
                antlr.processNonCombinedGrammar(imp, false);
            }
        }
    }
}
Also used : SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Tool(org.antlr.v4.Tool)

Example 15 with SemanticPipeline

use of org.antlr.v4.semantics.SemanticPipeline in project antlr4 by antlr.

the class BaseBrowserTest 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();
        CodeGenerator gen = new CodeGenerator(g);
        ST outputFileST = gen.generateParser();
        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 : ST(org.stringtemplate.v4.ST) SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) STGroup(org.stringtemplate.v4.STGroup) ParserATNFactory(org.antlr.v4.automata.ParserATNFactory) ATNFactory(org.antlr.v4.automata.ATNFactory) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) STGroupString(org.stringtemplate.v4.STGroupString) BaseRuntimeTest.antlrOnString(org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) CodeGenerator(org.antlr.v4.codegen.CodeGenerator) LexerATNFactory(org.antlr.v4.automata.LexerATNFactory) STGroupString(org.stringtemplate.v4.STGroupString)

Aggregations

SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)14 Grammar (org.antlr.v4.tool.Grammar)13 LexerGrammar (org.antlr.v4.tool.LexerGrammar)13 ATNFactory (org.antlr.v4.automata.ATNFactory)8 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)8 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)8 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)7 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)7 ST (org.stringtemplate.v4.ST)7 Tool (org.antlr.v4.Tool)6 STGroup (org.stringtemplate.v4.STGroup)6 STGroupString (org.stringtemplate.v4.STGroupString)6 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)5 AnalysisPipeline (org.antlr.v4.analysis.AnalysisPipeline)2 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 CodeGenPipeline (org.antlr.v4.codegen.CodeGenPipeline)1 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)1 Rule (org.antlr.v4.tool.Rule)1 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)1