Search in sources :

Example 21 with ST

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

the class ANTLRMessage method getMessageTemplate.

public ST getMessageTemplate(boolean verbose) {
    ST messageST = new ST(getErrorType().msg);
    messageST.impl.name = errorType.name();
    messageST.add("verbose", verbose);
    Object[] args = getArgs();
    for (int i = 0; i < args.length; i++) {
        String attr = "arg";
        if (i > 0)
            attr += i + 1;
        messageST.add(attr, args[i]);
    }
    // some messages ref arg2
    if (args.length < 2)
        messageST.add("arg2", null);
    Throwable cause = getCause();
    if (cause != null) {
        messageST.add("exception", cause);
        messageST.add("stackTrace", cause.getStackTrace());
    } else {
        // avoid ST error msg
        messageST.add("exception", null);
        messageST.add("stackTrace", null);
    }
    return messageST;
}
Also used : ST(org.stringtemplate.v4.ST)

Example 22 with ST

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

the class BuildDependencyGenerator method getGeneratedFileList.

/** From T.g return a list of File objects that
     *  name files ANTLR will emit from T.g.
     */
public List<File> getGeneratedFileList() {
    List<File> files = new ArrayList<File>();
    // add generated recognizer; e.g., TParser.java
    if (generator.getTarget().needsHeader()) {
        files.add(getOutputFile(generator.getRecognizerFileName(true)));
    }
    files.add(getOutputFile(generator.getRecognizerFileName(false)));
    // add output vocab file; e.g., T.tokens. This is always generated to
    // the base output directory, which will be just . if there is no -o option
    //
    files.add(getOutputFile(generator.getVocabFileName()));
    // are we generating a .h file?
    ST headerExtST = null;
    ST extST = generator.getTemplates().getInstanceOf("codeFileExtension");
    if (generator.getTemplates().isDefined("headerFile")) {
        headerExtST = generator.getTemplates().getInstanceOf("headerFileExtension");
        String suffix = Grammar.getGrammarTypeToFileNameSuffix(g.getType());
        String fileName = g.name + suffix + headerExtST.render();
        files.add(getOutputFile(fileName));
    }
    if (g.isCombined()) {
        // add autogenerated lexer; e.g., TLexer.java TLexer.h TLexer.tokens
        String suffix = Grammar.getGrammarTypeToFileNameSuffix(ANTLRParser.LEXER);
        String lexer = g.name + suffix + extST.render();
        files.add(getOutputFile(lexer));
        String lexerTokens = g.name + suffix + CodeGenerator.VOCAB_FILE_EXTENSION;
        files.add(getOutputFile(lexerTokens));
        // TLexer.h
        if (headerExtST != null) {
            String header = g.name + suffix + headerExtST.render();
            files.add(getOutputFile(header));
        }
    }
    if (g.tool.gen_listener) {
        // add generated listener; e.g., TListener.java
        if (generator.getTarget().needsHeader()) {
            files.add(getOutputFile(generator.getListenerFileName(true)));
        }
        files.add(getOutputFile(generator.getListenerFileName(false)));
        // add generated base listener; e.g., TBaseListener.java
        if (generator.getTarget().needsHeader()) {
            files.add(getOutputFile(generator.getBaseListenerFileName(true)));
        }
        files.add(getOutputFile(generator.getBaseListenerFileName(false)));
    }
    if (g.tool.gen_visitor) {
        // add generated visitor; e.g., TVisitor.java
        if (generator.getTarget().needsHeader()) {
            files.add(getOutputFile(generator.getVisitorFileName(true)));
        }
        files.add(getOutputFile(generator.getVisitorFileName(false)));
        // add generated base visitor; e.g., TBaseVisitor.java
        if (generator.getTarget().needsHeader()) {
            files.add(getOutputFile(generator.getBaseVisitorFileName(true)));
        }
        files.add(getOutputFile(generator.getBaseVisitorFileName(false)));
    }
    // handle generated files for imported grammars
    List<Grammar> imports = g.getAllImportedGrammars();
    if (imports != null) {
        for (Grammar g : imports) {
            //				File outputDir = tool.getOutputDirectory(g.fileName);
            //				String fname = groomQualifiedFileName(outputDir.toString(), g.getRecognizerName() + extST.render());
            //				files.add(new File(outputDir, fname));
            files.add(getOutputFile(g.fileName));
        }
    }
    if (files.isEmpty()) {
        return null;
    }
    return files;
}
Also used : ST(org.stringtemplate.v4.ST) ArrayList(java.util.ArrayList) STGroupFile(org.stringtemplate.v4.STGroupFile) File(java.io.File)

Example 23 with ST

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

the class DOTGenerator method getDOT.

/** Return a String containing a DOT description that, when displayed,
     *  will show the incoming state machine visually.  All nodes reachable
     *  from startState will be included.
     */
public String getDOT(ATNState startState, String[] ruleNames, boolean isLexer) {
    if (startState == null)
        return null;
    // The output DOT graph for visualization
    Set<ATNState> markedStates = new HashSet<ATNState>();
    ST dot = stlib.getInstanceOf("atn");
    dot.add("startState", startState.stateNumber);
    dot.add("rankdir", rankdir);
    List<ATNState> work = new LinkedList<ATNState>();
    work.add(startState);
    while (!work.isEmpty()) {
        ATNState s = work.get(0);
        if (markedStates.contains(s)) {
            work.remove(0);
            continue;
        }
        markedStates.add(s);
        // don't go past end of rule node to the follow states
        if (s instanceof RuleStopState)
            continue;
        // special case: if decision point, then line up the alt start states
        // unless it's an end of block
        //			if ( s instanceof BlockStartState ) {
        //				ST rankST = stlib.getInstanceOf("decision-rank");
        //				DecisionState alt = (DecisionState)s;
        //				for (int i=0; i<alt.getNumberOfTransitions(); i++) {
        //					ATNState target = alt.transition(i).target;
        //					if ( target!=null ) {
        //						rankST.add("states", target.stateNumber);
        //					}
        //				}
        //				dot.add("decisionRanks", rankST);
        //			}
        // make a DOT edge for each transition
        ST edgeST;
        for (int i = 0; i < s.getNumberOfTransitions(); i++) {
            Transition edge = s.transition(i);
            if (edge instanceof RuleTransition) {
                RuleTransition rr = ((RuleTransition) edge);
                // don't jump to other rules, but display edge to follow node
                edgeST = stlib.getInstanceOf("edge");
                String label = "<" + ruleNames[rr.ruleIndex];
                if (((RuleStartState) rr.target).isLeftRecursiveRule) {
                    label += "[" + rr.precedence + "]";
                }
                label += ">";
                edgeST.add("label", label);
                edgeST.add("src", "s" + s.stateNumber);
                edgeST.add("target", "s" + rr.followState.stateNumber);
                edgeST.add("arrowhead", arrowhead);
                dot.add("edges", edgeST);
                work.add(rr.followState);
                continue;
            }
            if (edge instanceof ActionTransition) {
                edgeST = stlib.getInstanceOf("action-edge");
                edgeST.add("label", getEdgeLabel(edge.toString()));
            } else if (edge instanceof AbstractPredicateTransition) {
                edgeST = stlib.getInstanceOf("edge");
                edgeST.add("label", getEdgeLabel(edge.toString()));
            } else if (edge.isEpsilon()) {
                edgeST = stlib.getInstanceOf("epsilon-edge");
                edgeST.add("label", getEdgeLabel(edge.toString()));
                boolean loopback = false;
                if (edge.target instanceof PlusBlockStartState) {
                    loopback = s.equals(((PlusBlockStartState) edge.target).loopBackState);
                } else if (edge.target instanceof StarLoopEntryState) {
                    loopback = s.equals(((StarLoopEntryState) edge.target).loopBackState);
                }
                edgeST.add("loopback", loopback);
            } else if (edge instanceof AtomTransition) {
                edgeST = stlib.getInstanceOf("edge");
                AtomTransition atom = (AtomTransition) edge;
                String label = String.valueOf(atom.label);
                if (isLexer)
                    label = "'" + getEdgeLabel(new StringBuilder().appendCodePoint(atom.label).toString()) + "'";
                else if (grammar != null)
                    label = grammar.getTokenDisplayName(atom.label);
                edgeST.add("label", getEdgeLabel(label));
            } else if (edge instanceof SetTransition) {
                edgeST = stlib.getInstanceOf("edge");
                SetTransition set = (SetTransition) edge;
                String label = set.label().toString();
                if (isLexer)
                    label = set.label().toString(true);
                else if (grammar != null)
                    label = set.label().toString(grammar.getVocabulary());
                if (edge instanceof NotSetTransition)
                    label = "~" + label;
                edgeST.add("label", getEdgeLabel(label));
            } else if (edge instanceof RangeTransition) {
                edgeST = stlib.getInstanceOf("edge");
                RangeTransition range = (RangeTransition) edge;
                String label = range.label().toString();
                if (isLexer)
                    label = range.toString();
                else if (grammar != null)
                    label = range.label().toString(grammar.getVocabulary());
                edgeST.add("label", getEdgeLabel(label));
            } else {
                edgeST = stlib.getInstanceOf("edge");
                edgeST.add("label", getEdgeLabel(edge.toString()));
            }
            edgeST.add("src", "s" + s.stateNumber);
            edgeST.add("target", "s" + edge.target.stateNumber);
            edgeST.add("arrowhead", arrowhead);
            if (s.getNumberOfTransitions() > 1) {
                edgeST.add("transitionIndex", i);
            } else {
                edgeST.add("transitionIndex", false);
            }
            dot.add("edges", edgeST);
            work.add(edge.target);
        }
    }
    //		}
    for (ATNState s : markedStates) {
        if (!(s instanceof RuleStopState))
            continue;
        ST st = stlib.getInstanceOf("stopstate");
        st.add("name", "s" + s.stateNumber);
        st.add("label", getStateLabel(s));
        dot.add("states", st);
    }
    for (ATNState s : markedStates) {
        if (s instanceof RuleStopState)
            continue;
        ST st = stlib.getInstanceOf("state");
        st.add("name", "s" + s.stateNumber);
        st.add("label", getStateLabel(s));
        st.add("transitions", s.getTransitions());
        dot.add("states", st);
    }
    return dot.render();
}
Also used : ST(org.stringtemplate.v4.ST) RuleStopState(org.antlr.v4.runtime.atn.RuleStopState) AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) SetTransition(org.antlr.v4.runtime.atn.SetTransition) RangeTransition(org.antlr.v4.runtime.atn.RangeTransition) LinkedList(java.util.LinkedList) ATNState(org.antlr.v4.runtime.atn.ATNState) PlusBlockStartState(org.antlr.v4.runtime.atn.PlusBlockStartState) ActionTransition(org.antlr.v4.runtime.atn.ActionTransition) StarLoopEntryState(org.antlr.v4.runtime.atn.StarLoopEntryState) RuleStartState(org.antlr.v4.runtime.atn.RuleStartState) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) NotSetTransition(org.antlr.v4.runtime.atn.NotSetTransition) RuleTransition(org.antlr.v4.runtime.atn.RuleTransition) ActionTransition(org.antlr.v4.runtime.atn.ActionTransition) Transition(org.antlr.v4.runtime.atn.Transition) AbstractPredicateTransition(org.antlr.v4.runtime.atn.AbstractPredicateTransition) AtomTransition(org.antlr.v4.runtime.atn.AtomTransition) SetTransition(org.antlr.v4.runtime.atn.SetTransition) RangeTransition(org.antlr.v4.runtime.atn.RangeTransition) AbstractPredicateTransition(org.antlr.v4.runtime.atn.AbstractPredicateTransition) HashSet(java.util.HashSet)

Example 24 with ST

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

the class DefaultToolListener method warning.

@Override
public void warning(ANTLRMessage msg) {
    ST msgST = tool.errMgr.getMessageTemplate(msg);
    String outputMsg = msgST.render();
    if (tool.errMgr.formatWantsSingleLineMessage()) {
        outputMsg = outputMsg.replace('\n', ' ');
    }
    System.err.println(outputMsg);
}
Also used : ST(org.stringtemplate.v4.ST)

Example 25 with ST

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

the class ErrorManager method panic.

public void panic(ErrorType errorType, Object... args) {
    ToolMessage msg = new ToolMessage(errorType, args);
    ST msgST = getMessageTemplate(msg);
    String outputMsg = msgST.render();
    if (formatWantsSingleLineMessage()) {
        outputMsg = outputMsg.replace('\n', ' ');
    }
    panic(outputMsg);
}
Also used : ST(org.stringtemplate.v4.ST)

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