Search in sources :

Example 16 with ST

use of edu.princeton.cs.algs4.ST 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)

Example 17 with ST

use of edu.princeton.cs.algs4.ST in project antlr4 by antlr.

the class TestBasicSemanticErrors method testArgumentRetvalLocalConflicts.

@Test
public void testArgumentRetvalLocalConflicts() throws Exception {
    String grammarTemplate = "grammar T;\n" + "ss<if(args)>[<args>]<endif> <if(retvals)>returns [<retvals>]<endif>\n" + "<if(locals)>locals [<locals>]<endif>\n" + "  : <body> EOF;\n" + "expr : '=';\n";
    String expected = "error(" + ErrorType.ARG_CONFLICTS_WITH_RULE.code + "): T.g4:2:7: parameter expr conflicts with rule with same name\n" + "error(" + ErrorType.RETVAL_CONFLICTS_WITH_RULE.code + "): T.g4:2:26: return value expr conflicts with rule with same name\n" + "error(" + ErrorType.LOCAL_CONFLICTS_WITH_RULE.code + "): T.g4:3:12: local expr conflicts with rule with same name\n" + "error(" + ErrorType.RETVAL_CONFLICTS_WITH_ARG.code + "): T.g4:2:26: return value expr conflicts with parameter with same name\n" + "error(" + ErrorType.LOCAL_CONFLICTS_WITH_ARG.code + "): T.g4:3:12: local expr conflicts with parameter with same name\n" + "error(" + ErrorType.LOCAL_CONFLICTS_WITH_RETVAL.code + "): T.g4:3:12: local expr conflicts with return value with same name\n" + "error(" + ErrorType.LABEL_CONFLICTS_WITH_RULE.code + "): T.g4:4:4: label expr conflicts with rule with same name\n" + "error(" + ErrorType.LABEL_CONFLICTS_WITH_ARG.code + "): T.g4:4:4: label expr conflicts with parameter with same name\n" + "error(" + ErrorType.LABEL_CONFLICTS_WITH_RETVAL.code + "): T.g4:4:4: label expr conflicts with return value with same name\n" + "error(" + ErrorType.LABEL_CONFLICTS_WITH_LOCAL.code + "): T.g4:4:4: label expr conflicts with local with same name\n";
    ST grammarST = new ST(grammarTemplate);
    grammarST.add("args", "int expr");
    grammarST.add("retvals", "int expr");
    grammarST.add("locals", "int expr");
    grammarST.add("body", "expr=expr");
    testErrors(new String[] { grammarST.render(), expected }, false);
}
Also used : ST(org.stringtemplate.v4.ST) Test(org.junit.Test)

Example 18 with ST

use of edu.princeton.cs.algs4.ST in project antlr4 by antlr.

the class Antlr4ErrorLog method error.

/**
     * {@inheritDoc}
     * <p>
     * This implementation passes the message to the Maven log.
     * </p>
     * @param message The message to send to Maven.
     */
@Override
public void error(ANTLRMessage message) {
    ST msgST = tool.errMgr.getMessageTemplate(message);
    String outputMsg = msgST.render();
    if (tool.errMgr.formatWantsSingleLineMessage()) {
        outputMsg = outputMsg.replace('\n', ' ');
    }
    log.error(outputMsg);
    if (message.fileName != null) {
        String text = message.getMessageTemplate(false).render();
        buildContext.addMessage(new File(message.fileName), message.line, message.charPosition, text, BuildContext.SEVERITY_ERROR, message.getCause());
    }
}
Also used : ST(org.stringtemplate.v4.ST) File(java.io.File)

Example 19 with ST

use of edu.princeton.cs.algs4.ST in project antlr4 by antlr.

the class Antlr4ErrorLog method warning.

/**
     * {@inheritDoc}
     * <p>
     * This implementation passes the message to the Maven log.
     * </p>
     * @param message
     */
@Override
public void warning(ANTLRMessage message) {
    ST msgST = tool.errMgr.getMessageTemplate(message);
    String outputMsg = msgST.render();
    if (tool.errMgr.formatWantsSingleLineMessage()) {
        outputMsg = outputMsg.replace('\n', ' ');
    }
    log.warn(outputMsg);
    if (message.fileName != null) {
        String text = message.getMessageTemplate(false).render();
        buildContext.addMessage(new File(message.fileName), message.line, message.charPosition, text, BuildContext.SEVERITY_WARNING, message.getCause());
    }
}
Also used : ST(org.stringtemplate.v4.ST) File(java.io.File)

Example 20 with ST

use of edu.princeton.cs.algs4.ST in project antlr4 by antlr.

the class SwiftTarget method getLexerOrParserATNJson.

private String getLexerOrParserATNJson(Grammar g, String fileName) {
    ST extST = getTemplates().getInstanceOf("codeFileExtension");
    String className = fileName.substring(0, fileName.lastIndexOf(extST.render()));
    String JSON = "class " + className + "ATN {\n" + "    let jsonString: String = \"" + serializeTojson(g.atn).replaceAll("\"", "\\\\\"") + //.replaceAll("\"", "\\\\\"");
    "\"\n}";
    return JSON;
}
Also used : ST(org.stringtemplate.v4.ST) GrammarAST(org.antlr.v4.tool.ast.GrammarAST)

Aggregations

ST (org.stringtemplate.v4.ST)197 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)37 STGroup (org.stringtemplate.v4.STGroup)24 File (java.io.File)19 ArrayList (java.util.ArrayList)16 IOException (java.io.IOException)12 STGroupFile (org.stringtemplate.v4.STGroupFile)12 Path (java.nio.file.Path)10 Test (org.junit.Test)10 ATNFactory (org.antlr.v4.automata.ATNFactory)9 LexerATNFactory (org.antlr.v4.automata.LexerATNFactory)9 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)9 CodeGenerator (org.antlr.v4.codegen.CodeGenerator)9 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)9 Grammar (org.antlr.v4.tool.Grammar)9 LexerGrammar (org.antlr.v4.tool.LexerGrammar)9 STGroupString (org.stringtemplate.v4.STGroupString)9 LinkedHashMap (java.util.LinkedHashMap)7 URL (java.net.URL)6 Map (java.util.Map)6