Search in sources :

Example 6 with CodeGenerator

use of org.antlr.v4.codegen.CodeGenerator in project antlr4 by antlr.

the class OutputModelController method buildLexerOutputModel.

public OutputModelObject buildLexerOutputModel(boolean header) {
    CodeGenerator gen = delegate.getGenerator();
    LexerFile file = lexerFile(gen.getRecognizerFileName(header));
    setRoot(file);
    file.lexer = lexer(file);
    Grammar g = delegate.getGrammar();
    for (Rule r : g.rules.values()) {
        buildLexerRuleActions(file.lexer, r);
    }
    return file;
}
Also used : Grammar(org.antlr.v4.tool.Grammar) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) LexerFile(org.antlr.v4.codegen.model.LexerFile)

Example 7 with CodeGenerator

use of org.antlr.v4.codegen.CodeGenerator in project antlr4 by antlr.

the class OutputModelController method buildParserOutputModel.

/** Build a file with a parser containing rule functions. Use the
	 *  controller as factory in SourceGenTriggers so it triggers codegen
	 *  extensions too, not just the factory functions in this factory.
	 */
public OutputModelObject buildParserOutputModel(boolean header) {
    CodeGenerator gen = delegate.getGenerator();
    ParserFile file = parserFile(gen.getRecognizerFileName(header));
    setRoot(file);
    file.parser = parser(file);
    Grammar g = delegate.getGrammar();
    for (Rule r : g.rules.values()) {
        buildRuleFunction(file.parser, r);
    }
    return file;
}
Also used : Grammar(org.antlr.v4.tool.Grammar) Rule(org.antlr.v4.tool.Rule) LeftRecursiveRule(org.antlr.v4.tool.LeftRecursiveRule) ParserFile(org.antlr.v4.codegen.model.ParserFile)

Example 8 with CodeGenerator

use of org.antlr.v4.codegen.CodeGenerator in project antlr4 by antlr.

the class BaseNodeTest 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 9 with CodeGenerator

use of org.antlr.v4.codegen.CodeGenerator in project antlr4 by antlr.

the class BasePythonTest 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 10 with CodeGenerator

use of org.antlr.v4.codegen.CodeGenerator in project antlr4 by antlr.

the class LeftRecursiveRuleAnalyzer method loadPrecRuleTemplates.

public void loadPrecRuleTemplates() {
    String templateGroupFile = "org/antlr/v4/tool/templates/LeftRecursiveRules.stg";
    recRuleTemplates = new STGroupFile(templateGroupFile);
    if (!recRuleTemplates.isDefined("recRule")) {
        tool.errMgr.toolError(ErrorType.MISSING_CODE_GEN_TEMPLATES, "LeftRecursiveRules");
    }
    // use codegen to get correct language templates; that's it though
    CodeGenerator gen = new CodeGenerator(tool, null, language);
    codegenTemplates = gen.getTemplates();
}
Also used : CodeGenerator(org.antlr.v4.codegen.CodeGenerator) STGroupFile(org.stringtemplate.v4.STGroupFile)

Aggregations

Grammar (org.antlr.v4.tool.Grammar)15 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)13 ST (org.stringtemplate.v4.ST)10 STGroup (org.stringtemplate.v4.STGroup)8 ATNFactory (org.antlr.v4.automata.ATNFactory)7 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)7 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)7 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)7 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)7 LexerGrammar (org.antlr.v4.tool.LexerGrammar)7 STGroupString (org.stringtemplate.v4.STGroupString)6 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)5 ArrayList (java.util.ArrayList)3 Action (org.antlr.v4.codegen.model.Action)3 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)3 SrcOp (org.antlr.v4.codegen.model.SrcOp)2 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)2 Rule (org.antlr.v4.tool.Rule)2 ActionAST (org.antlr.v4.tool.ast.ActionAST)2 PredAST (org.antlr.v4.tool.ast.PredAST)2