Search in sources :

Example 21 with CodeGenerator

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

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 22 with CodeGenerator

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

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)

Example 23 with CodeGenerator

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

the class GoTarget method getVisitorFileName.

/**
 * A given grammar T, return the visitor name such as
 *  TVisitor.java, if we're using the Java target.
 */
public String getVisitorFileName(boolean header) {
    CodeGenerator gen = getCodeGenerator();
    Grammar g = gen.g;
    assert g.name != null;
    return g.name.toLowerCase() + "_visitor.go";
}
Also used : CodeGenerator(org.antlr.v4.codegen.CodeGenerator) Grammar(org.antlr.v4.tool.Grammar)

Example 24 with CodeGenerator

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

the class GoTarget method getRecognizerFileName.

public String getRecognizerFileName(boolean header) {
    CodeGenerator gen = getCodeGenerator();
    Grammar g = gen.g;
    assert g != null;
    String name;
    switch(g.getType()) {
        case ANTLRParser.PARSER:
            name = g.name.endsWith("Parser") ? g.name.substring(0, g.name.length() - 6) : g.name;
            return name.toLowerCase() + "_parser.go";
        case ANTLRParser.LEXER:
            // trim off "lexer"
            name = g.name.endsWith("Lexer") ? g.name.substring(0, g.name.length() - 5) : g.name;
            return name.toLowerCase() + "_lexer.go";
        case ANTLRParser.COMBINED:
            return g.name.toLowerCase() + "_parser.go";
        default:
            return "INVALID_FILE_NAME";
    }
}
Also used : CodeGenerator(org.antlr.v4.codegen.CodeGenerator) Grammar(org.antlr.v4.tool.Grammar)

Example 25 with CodeGenerator

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

the class GoTarget method getListenerFileName.

/**
 * A given grammar T, return the listener name such as
 *  TListener.java, if we're using the Java target.
 */
public String getListenerFileName(boolean header) {
    CodeGenerator gen = getCodeGenerator();
    Grammar g = gen.g;
    assert g.name != null;
    return g.name.toLowerCase() + "_listener.go";
}
Also used : CodeGenerator(org.antlr.v4.codegen.CodeGenerator) Grammar(org.antlr.v4.tool.Grammar)

Aggregations

Grammar (org.antlr.v4.tool.Grammar)21 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)17 ST (org.stringtemplate.v4.ST)15 STGroup (org.stringtemplate.v4.STGroup)11 LexerGrammar (org.antlr.v4.tool.LexerGrammar)10 ATNFactory (org.antlr.v4.automata.ATNFactory)9 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)9 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)9 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)9 STGroupString (org.stringtemplate.v4.STGroupString)7 ErrorQueue (org.antlr.v4.test.runtime.ErrorQueue)6 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)6 ArrayList (java.util.ArrayList)5 Action (org.antlr.v4.codegen.model.Action)5 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)5 LeftRecursiveRule (org.antlr.v4.tool.LeftRecursiveRule)4 Rule (org.antlr.v4.tool.Rule)4 SrcOp (org.antlr.v4.codegen.model.SrcOp)3 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)3 ActionAST (org.antlr.v4.tool.ast.ActionAST)3