Search in sources :

Example 11 with ST

use of edu.princeton.cs.algs4.ST in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class StringTemplate method prepareTemplate.

/**
   * Prepares the template by adding the variables.
   * @param prototype The template prototype
   * @param domainObject The domain object
   * @param contentInfo The reference information and the encoded content hashes
   * @return The prepared template instance
   */
protected ST prepareTemplate(ST prototype, D domainObject, Map<String, ContentInfo> contentInfo) {
    ST template = new ST(prototype);
    template.add(MODEL_VARIABLE, domainObject);
    template.add(CONTENT_VARIABLE, contentInfo);
    return template;
}
Also used : ST(org.stringtemplate.v4.ST)

Example 12 with ST

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

the class BaseGoTest method writeParserTestFile.

protected void writeParserTestFile(String parserName, String lexerName, String listenerName, String visitorName, String parserStartRuleName, boolean debug) {
    ST outputFileST = new ST("package main\n" + "import (\n" + "	\"github.com/antlr/antlr4/runtime/Go/antlr\"\n" + "	\"./parser\"\n" + "	\"os\"\n" + ")\n" + "\n" + "type TreeShapeListener struct {\n" + "	*parser.Base<listenerName>\n" + "}\n" + "\n" + "func NewTreeShapeListener() *TreeShapeListener {\n" + "	return new(TreeShapeListener)\n" + "}\n" + "\n" + "func (this *TreeShapeListener) EnterEveryRule(ctx antlr.ParserRuleContext) {\n" + "	for i := 0; i\\<ctx.GetChildCount(); i++ {\n" + "		child := ctx.GetChild(i)\n" + "		parentR,ok := child.GetParent().(antlr.RuleNode)\n" + "		if !ok || parentR.GetBaseRuleContext() != ctx.GetBaseRuleContext() {\n" + "			panic(\"Invalid parse tree shape detected.\")\n" + "		}\n" + "	}\n" + "}\n" + "\n" + "func main() {\n" + "	input := antlr.NewFileStream(os.Args[1])\n" + "	lexer := parser.New<lexerName>(input)\n" + "	stream := antlr.NewCommonTokenStream(lexer,0)\n" + "<createParser>" + "	p.BuildParseTrees = true\n" + "	tree := p.<parserStartRuleName>()\n" + "	antlr.ParseTreeWalkerDefault.Walk(NewTreeShapeListener(), tree)\n" + "}\n");
    ST createParserST = new ST("	p := parser.New<parserName>(stream)\n");
    if (debug) {
        createParserST = new ST("	p := parser.New<parserName>(stream)\n" + "	p.AddErrorListener(antlr.NewDiagnosticErrorListener(true))\n");
    }
    outputFileST.add("createParser", createParserST);
    outputFileST.add("parserName", parserName);
    outputFileST.add("lexerName", lexerName);
    outputFileST.add("listenerName", listenerName);
    outputFileST.add("visitorName", visitorName);
    outputFileST.add("parserStartRuleName", parserStartRuleName.substring(0, 1).toUpperCase() + parserStartRuleName.substring(1));
    writeFile(overall_tmpdir.toString(), "Test.go", outputFileST.render());
}
Also used : ST(org.stringtemplate.v4.ST)

Example 13 with ST

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

the class BaseGoTest method writeLexerTestFile.

protected void writeLexerTestFile(String lexerName, boolean showDFA) {
    ST outputFileST = new ST("package main\n" + "import (\n" + "	\"github.com/antlr/antlr4/runtime/Go/antlr\"\n" + "	\"./parser\"\n" + "	\"os\"\n" + "	\"fmt\"\n" + ")\n" + "\n" + "func main() {\n" + "	input := antlr.NewFileStream(os.Args[1])\n" + "	lexer := parser.New<lexerName>(input)\n" + "	stream := antlr.NewCommonTokenStream(lexer,0)\n" + "	stream.Fill()\n" + "	for _, t := range stream.GetAllTokens() {\n" + "		fmt.Println(t)\n" + "	}\n" + (showDFA ? "fmt.Print(lexer.GetInterpreter().DecisionToDFA()[antlr.LexerDefaultMode].ToLexerString())\n" : "") + "}\n" + "\n");
    outputFileST.add("lexerName", lexerName);
    writeFile(overall_tmpdir.toString(), "Test.go", outputFileST.render());
}
Also used : ST(org.stringtemplate.v4.ST)

Example 14 with ST

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

the class BaseGoTest 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 15 with ST

use of edu.princeton.cs.algs4.ST 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) ErrorQueue(org.antlr.v4.test.runtime.ErrorQueue) STGroupString(org.stringtemplate.v4.STGroupString)

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