Search in sources :

Example 71 with ST

use of edu.princeton.cs.algs4.ST in project stagen by wiztools.

the class STTemplateExecutor method render.

@Override
public String render(Map<String, Object> config, File templateFile) throws ExecutorException {
    try {
        final File tmplDir = templateFile.getParentFile();
        final String tmplName = Util.getBaseFileName(templateFile.getName());
        STGroup stg = new STRawGroupDir(tmplDir.toURI().toURL(), Charsets.UTF_8.name(), delimiterStartChar, delimiterStopChar);
        ST st = stg.getInstanceOf(tmplName);
        // Populate data:
        config.entrySet().stream().forEach((e) -> {
            st.add(e.getKey(), e.getValue());
        });
        return st.render();
    } catch (MalformedURLException ex) {
        throw new ExecutorException(ex);
    }
}
Also used : ST(org.stringtemplate.v4.ST) MalformedURLException(java.net.MalformedURLException) STGroup(org.stringtemplate.v4.STGroup) STRawGroupDir(org.stringtemplate.v4.STRawGroupDir) File(java.io.File)

Example 72 with ST

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

the class LeftRecursiveRuleAnalyzer method getArtificialOpPrecRule.

// --------- get transformed rules ----------------
public String getArtificialOpPrecRule() {
    ST ruleST = recRuleTemplates.getInstanceOf("recRule");
    ruleST.add("ruleName", ruleName);
    ST ruleArgST = codegenTemplates.getInstanceOf("recRuleArg");
    ruleST.add("argName", ruleArgST);
    ST setResultST = codegenTemplates.getInstanceOf("recRuleSetResultAction");
    ruleST.add("setResultAction", setResultST);
    ruleST.add("userRetvals", retvals);
    LinkedHashMap<Integer, LeftRecursiveRuleAltInfo> opPrecRuleAlts = new LinkedHashMap<Integer, LeftRecursiveRuleAltInfo>();
    opPrecRuleAlts.putAll(binaryAlts);
    opPrecRuleAlts.putAll(ternaryAlts);
    opPrecRuleAlts.putAll(suffixAlts);
    for (int alt : opPrecRuleAlts.keySet()) {
        LeftRecursiveRuleAltInfo altInfo = opPrecRuleAlts.get(alt);
        ST altST = recRuleTemplates.getInstanceOf("recRuleAlt");
        ST predST = codegenTemplates.getInstanceOf("recRuleAltPredicate");
        predST.add("opPrec", precedence(alt));
        predST.add("ruleName", ruleName);
        altST.add("pred", predST);
        altST.add("alt", altInfo);
        altST.add("precOption", LeftRecursiveRuleTransformer.PRECEDENCE_OPTION_NAME);
        altST.add("opPrec", precedence(alt));
        ruleST.add("opAlts", altST);
    }
    ruleST.add("primaryAlts", prefixAndOtherAlts);
    tool.log("left-recursion", ruleST.render());
    return ruleST.render();
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) RuleRefAST(org.antlr.v4.tool.ast.RuleRefAST) ST(org.stringtemplate.v4.ST) AltAST(org.antlr.v4.tool.ast.AltAST) LinkedHashMap(java.util.LinkedHashMap)

Example 73 with ST

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

the class LexerATNFactory method lexerCallCommand.

@Override
public Handle lexerCallCommand(GrammarAST ID, GrammarAST arg) {
    LexerAction lexerAction = createLexerAction(ID, arg);
    if (lexerAction != null) {
        return action(ID, lexerAction);
    }
    if (codegenTemplates == null) {
        // suppress reporting a single missing template when the target couldn't be loaded
        return epsilon(ID);
    }
    // fall back to standard action generation for the command
    ST cmdST = codegenTemplates.getInstanceOf("Lexer" + CharSupport.capitalize(ID.getText()) + "Command");
    if (cmdST == null) {
        g.tool.errMgr.grammarError(ErrorType.INVALID_LEXER_COMMAND, g.fileName, ID.token, ID.getText());
        return epsilon(ID);
    }
    if (cmdST.impl.formalArguments == null || !cmdST.impl.formalArguments.containsKey("arg")) {
        g.tool.errMgr.grammarError(ErrorType.UNWANTED_LEXER_COMMAND_ARGUMENT, g.fileName, ID.token, ID.getText());
        return epsilon(ID);
    }
    cmdST.add("arg", arg.getText());
    cmdST.add("grammar", arg.g);
    return action(cmdST.render());
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ActionAST(org.antlr.v4.tool.ast.ActionAST) TerminalAST(org.antlr.v4.tool.ast.TerminalAST) RangeAST(org.antlr.v4.tool.ast.RangeAST) ST(org.stringtemplate.v4.ST) LexerAction(org.antlr.v4.runtime.atn.LexerAction)

Example 74 with ST

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

the class CodeGenerator method getTokenVocabOutput.

/**
 * Generate a token vocab file with all the token names/types.  For example:
 *  ID=7
 *  FOR=8
 *  'for'=8
 *
 *  This is independent of the target language; used by antlr internally
 */
ST getTokenVocabOutput() {
    ST vocabFileST = new ST(vocabFilePattern);
    Map<String, Integer> tokens = new LinkedHashMap<String, Integer>();
    // make constants for the token names
    for (String t : g.tokenNameToTypeMap.keySet()) {
        int tokenType = g.tokenNameToTypeMap.get(t);
        if (tokenType >= Token.MIN_USER_TOKEN_TYPE) {
            tokens.put(t, tokenType);
        }
    }
    vocabFileST.add("tokens", tokens);
    // now dump the strings
    Map<String, Integer> literals = new LinkedHashMap<String, Integer>();
    for (String literal : g.stringLiteralToTypeMap.keySet()) {
        int tokenType = g.stringLiteralToTypeMap.get(literal);
        if (tokenType >= Token.MIN_USER_TOKEN_TYPE) {
            literals.put(literal, tokenType);
        }
    }
    vocabFileST.add("literals", literals);
    return vocabFileST;
}
Also used : ST(org.stringtemplate.v4.ST) LinkedHashMap(java.util.LinkedHashMap)

Example 75 with ST

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

the class OutputModelController method buildLeftRecursiveRuleFunction.

public void buildLeftRecursiveRuleFunction(LeftRecursiveRule r, LeftRecursiveRuleFunction function) {
    buildNormalRuleFunction(r, function);
    // now inject code to start alts
    Target target = delegate.getTarget();
    STGroup codegenTemplates = target.getTemplates();
    // pick out alt(s) for primaries
    CodeBlockForOuterMostAlt outerAlt = (CodeBlockForOuterMostAlt) function.code.get(0);
    List<CodeBlockForAlt> primaryAltsCode = new ArrayList<CodeBlockForAlt>();
    SrcOp primaryStuff = outerAlt.ops.get(0);
    if (primaryStuff instanceof Choice) {
        Choice primaryAltBlock = (Choice) primaryStuff;
        primaryAltsCode.addAll(primaryAltBlock.alts);
    } else {
        // just a single alt I guess; no block
        primaryAltsCode.add((CodeBlockForAlt) primaryStuff);
    }
    // pick out alt(s) for op alts
    StarBlock opAltStarBlock = (StarBlock) outerAlt.ops.get(1);
    CodeBlockForAlt altForOpAltBlock = opAltStarBlock.alts.get(0);
    List<CodeBlockForAlt> opAltsCode = new ArrayList<CodeBlockForAlt>();
    SrcOp opStuff = altForOpAltBlock.ops.get(0);
    if (opStuff instanceof AltBlock) {
        AltBlock opAltBlock = (AltBlock) opStuff;
        opAltsCode.addAll(opAltBlock.alts);
    } else {
        // just a single alt I guess; no block
        opAltsCode.add((CodeBlockForAlt) opStuff);
    }
    // Insert code in front of each primary alt to create specialized ctx if there was a label
    for (int i = 0; i < primaryAltsCode.size(); i++) {
        LeftRecursiveRuleAltInfo altInfo = r.recPrimaryAlts.get(i);
        if (altInfo.altLabel == null)
            continue;
        ST altActionST = codegenTemplates.getInstanceOf("recRuleReplaceContext");
        altActionST.add("ctxName", Utils.capitalize(altInfo.altLabel));
        Action altAction = new Action(delegate, function.altLabelCtxs.get(altInfo.altLabel), altActionST);
        CodeBlockForAlt alt = primaryAltsCode.get(i);
        alt.insertOp(0, altAction);
    }
    // Insert code to set ctx.stop after primary block and before op * loop
    ST setStopTokenAST = codegenTemplates.getInstanceOf("recRuleSetStopToken");
    Action setStopTokenAction = new Action(delegate, function.ruleCtx, setStopTokenAST);
    outerAlt.insertOp(1, setStopTokenAction);
    // Insert code to set _prevctx at start of * loop
    ST setPrevCtx = codegenTemplates.getInstanceOf("recRuleSetPrevCtx");
    Action setPrevCtxAction = new Action(delegate, function.ruleCtx, setPrevCtx);
    opAltStarBlock.addIterationOp(setPrevCtxAction);
    // Insert code in front of each op alt to create specialized ctx if there was an alt label
    for (int i = 0; i < opAltsCode.size(); i++) {
        ST altActionST;
        LeftRecursiveRuleAltInfo altInfo = r.recOpAlts.getElement(i);
        String templateName;
        if (altInfo.altLabel != null) {
            templateName = "recRuleLabeledAltStartAction";
            altActionST = codegenTemplates.getInstanceOf(templateName);
            altActionST.add("currentAltLabel", altInfo.altLabel);
        } else {
            templateName = "recRuleAltStartAction";
            altActionST = codegenTemplates.getInstanceOf(templateName);
            altActionST.add("ctxName", Utils.capitalize(r.name));
        }
        altActionST.add("ruleName", r.name);
        // add label of any lr ref we deleted
        altActionST.add("label", altInfo.leftRecursiveRuleRefLabel);
        if (altActionST.impl.formalArguments.containsKey("isListLabel")) {
            altActionST.add("isListLabel", altInfo.isListLabel);
        } else if (altInfo.isListLabel) {
            delegate.getGenerator().tool.errMgr.toolError(ErrorType.CODE_TEMPLATE_ARG_ISSUE, templateName, "isListLabel");
        }
        Action altAction = new Action(delegate, function.altLabelCtxs.get(altInfo.altLabel), altActionST);
        CodeBlockForAlt alt = opAltsCode.get(i);
        alt.insertOp(0, altAction);
    }
}
Also used : GrammarAST(org.antlr.v4.tool.ast.GrammarAST) ActionAST(org.antlr.v4.tool.ast.ActionAST) BlockAST(org.antlr.v4.tool.ast.BlockAST) ST(org.stringtemplate.v4.ST) PredAST(org.antlr.v4.tool.ast.PredAST) Action(org.antlr.v4.codegen.model.Action) SrcOp(org.antlr.v4.codegen.model.SrcOp) Choice(org.antlr.v4.codegen.model.Choice) STGroup(org.stringtemplate.v4.STGroup) ArrayList(java.util.ArrayList) StarBlock(org.antlr.v4.codegen.model.StarBlock) CodeBlockForOuterMostAlt(org.antlr.v4.codegen.model.CodeBlockForOuterMostAlt) CodeBlockForAlt(org.antlr.v4.codegen.model.CodeBlockForAlt) AltBlock(org.antlr.v4.codegen.model.AltBlock) LeftRecursiveRuleAltInfo(org.antlr.v4.analysis.LeftRecursiveRuleAltInfo)

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