Search in sources :

Example 11 with Tool

use of org.antlr.v4.Tool in project antlr4 by antlr.

the class BaseRuntimeTest method antlrOnString.

/** Run ANTLR on stuff in workdir and error queue back */
public static ErrorQueue antlrOnString(String workdir, String targetName, String grammarFileName, boolean defaultListener, String... extraOptions) {
    final List<String> options = new ArrayList<>();
    Collections.addAll(options, extraOptions);
    if (targetName != null) {
        options.add("-Dlanguage=" + targetName);
    }
    if (!options.contains("-o")) {
        options.add("-o");
        options.add(workdir);
    }
    if (!options.contains("-lib")) {
        options.add("-lib");
        options.add(workdir);
    }
    if (!options.contains("-encoding")) {
        options.add("-encoding");
        options.add("UTF-8");
    }
    options.add(new File(workdir, grammarFileName).toString());
    final String[] optionsA = new String[options.size()];
    options.toArray(optionsA);
    Tool antlr = new Tool(optionsA);
    ErrorQueue equeue = new ErrorQueue(antlr);
    antlr.addListener(equeue);
    if (defaultListener) {
        antlr.addListener(new DefaultToolListener(antlr));
    }
    synchronized (antlrLock) {
        antlr.processGrammarsOnCommandLine();
    }
    List<String> errors = new ArrayList<>();
    if (!defaultListener && !equeue.errors.isEmpty()) {
        for (int i = 0; i < equeue.errors.size(); i++) {
            ANTLRMessage msg = equeue.errors.get(i);
            ST msgST = antlr.errMgr.getMessageTemplate(msg);
            errors.add(msgST.render());
        }
    }
    if (!defaultListener && !equeue.warnings.isEmpty()) {
        for (int i = 0; i < equeue.warnings.size(); i++) {
            ANTLRMessage msg = equeue.warnings.get(i);
        // antlrToolErrors.append(msg); warnings are hushed
        }
    }
    return equeue;
}
Also used : DefaultToolListener(org.antlr.v4.tool.DefaultToolListener) ST(org.stringtemplate.v4.ST) ArrayList(java.util.ArrayList) File(java.io.File) STGroupFile(org.stringtemplate.v4.STGroupFile) ANTLRMessage(org.antlr.v4.tool.ANTLRMessage) Tool(org.antlr.v4.Tool)

Example 12 with Tool

use of org.antlr.v4.Tool in project antlr4 by antlr.

the class ErrorQueue method toString.

public String toString(boolean rendered) {
    if (!rendered) {
        return Utils.join(all.iterator(), "\n");
    }
    if (tool == null) {
        throw new IllegalStateException(String.format("No %s instance is available.", Tool.class.getName()));
    }
    StringBuilder buf = new StringBuilder();
    for (ANTLRMessage m : all) {
        ST st = tool.errMgr.getMessageTemplate(m);
        buf.append(st.render());
        buf.append("\n");
    }
    return buf.toString();
}
Also used : ST(org.stringtemplate.v4.ST) ANTLRMessage(org.antlr.v4.tool.ANTLRMessage)

Example 13 with Tool

use of org.antlr.v4.Tool in project antlr4 by antlr.

the class BaseCppTest method semanticProcess.

protected void semanticProcess(Grammar g) {
    if (g.ast != null && !g.ast.hasErrors) {
        System.out.println(g.ast.toStringTree());
        Tool antlr = new Tool();
        SemanticPipeline sem = new SemanticPipeline(g);
        sem.process();
        if (g.getImportedGrammars() != null) {
            // process imported grammars (if any)
            for (Grammar imp : g.getImportedGrammars()) {
                antlr.processNonCombinedGrammar(imp, false);
            }
        }
    }
}
Also used : SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Tool(org.antlr.v4.Tool)

Example 14 with Tool

use of org.antlr.v4.Tool in project antlr4 by antlr.

the class BaseGoTest method semanticProcess.

protected void semanticProcess(Grammar g) {
    if (g.ast != null && !g.ast.hasErrors) {
        System.out.println(g.ast.toStringTree());
        Tool antlr = new Tool();
        SemanticPipeline sem = new SemanticPipeline(g);
        sem.process();
        if (g.getImportedGrammars() != null) {
            // (if any)
            for (Grammar imp : g.getImportedGrammars()) {
                antlr.processNonCombinedGrammar(imp, false);
            }
        }
    }
}
Also used : SemanticPipeline(org.antlr.v4.semantics.SemanticPipeline) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Tool(org.antlr.v4.Tool)

Example 15 with Tool

use of org.antlr.v4.Tool in project antlr4 by antlr.

the class LeftRecursiveRuleTransformer method translateLeftRecursiveRule.

/** Return true if successful */
public boolean translateLeftRecursiveRule(GrammarRootAST ast, LeftRecursiveRule r, String language) {
    //tool.log("grammar", ruleAST.toStringTree());
    GrammarAST prevRuleAST = r.ast;
    String ruleName = prevRuleAST.getChild(0).getText();
    LeftRecursiveRuleAnalyzer leftRecursiveRuleWalker = new LeftRecursiveRuleAnalyzer(prevRuleAST, tool, ruleName, language);
    boolean isLeftRec;
    try {
        //			System.out.println("TESTING ---------------\n"+
        //							   leftRecursiveRuleWalker.text(ruleAST));
        isLeftRec = leftRecursiveRuleWalker.rec_rule();
    } catch (RecognitionException re) {
        // didn't match; oh well
        isLeftRec = false;
    }
    if (!isLeftRec)
        return false;
    // replace old rule's AST; first create text of altered rule
    GrammarAST RULES = (GrammarAST) ast.getFirstChildWithType(ANTLRParser.RULES);
    String newRuleText = leftRecursiveRuleWalker.getArtificialOpPrecRule();
    //		System.out.println("created: "+newRuleText);
    // now parse within the context of the grammar that originally created
    // the AST we are transforming. This could be an imported grammar so
    // we cannot just reference this.g because the role might come from
    // the imported grammar and not the root grammar (this.g)
    RuleAST t = parseArtificialRule(prevRuleAST.g, newRuleText);
    // reuse the name token from the original AST since it refers to the proper source location in the original grammar
    ((GrammarAST) t.getChild(0)).token = ((GrammarAST) prevRuleAST.getChild(0)).getToken();
    // update grammar AST and set rule's AST.
    RULES.setChild(prevRuleAST.getChildIndex(), t);
    r.ast = t;
    // Reduce sets in newly created rule tree
    GrammarTransformPipeline transform = new GrammarTransformPipeline(g, g.tool);
    transform.reduceBlocksToSets(r.ast);
    transform.expandParameterizedLoops(r.ast);
    // Rerun semantic checks on the new rule
    RuleCollector ruleCollector = new RuleCollector(g);
    ruleCollector.visit(t, "rule");
    BasicSemanticChecks basics = new BasicSemanticChecks(g, ruleCollector);
    // disable the assoc element option checks because they are already
    // handled for the pre-transformed rule.
    basics.checkAssocElementOption = false;
    basics.visit(t, "rule");
    // track recursive alt info for codegen
    r.recPrimaryAlts = new ArrayList<LeftRecursiveRuleAltInfo>();
    r.recPrimaryAlts.addAll(leftRecursiveRuleWalker.prefixAndOtherAlts);
    if (r.recPrimaryAlts.isEmpty()) {
        tool.errMgr.grammarError(ErrorType.NO_NON_LR_ALTS, g.fileName, ((GrammarAST) r.ast.getChild(0)).getToken(), r.name);
    }
    r.recOpAlts = new OrderedHashMap<Integer, LeftRecursiveRuleAltInfo>();
    r.recOpAlts.putAll(leftRecursiveRuleWalker.binaryAlts);
    r.recOpAlts.putAll(leftRecursiveRuleWalker.ternaryAlts);
    r.recOpAlts.putAll(leftRecursiveRuleWalker.suffixAlts);
    // walk alt info records and set their altAST to point to appropriate ALT subtree
    // from freshly created AST
    setAltASTPointers(r, t);
    // update Rule to just one alt and add prec alt
    ActionAST arg = (ActionAST) r.ast.getFirstChildWithType(ANTLRParser.ARG_ACTION);
    if (arg != null) {
        r.args = ScopeParser.parseTypedArgList(arg, arg.getText(), g);
        r.args.type = AttributeDict.DictType.ARG;
        r.args.ast = arg;
        // todo: isn't this Rule or something?
        arg.resolver = r.alt[1];
    }
    // these are so $label in action translation works
    for (Pair<GrammarAST, String> pair : leftRecursiveRuleWalker.leftRecursiveRuleRefLabels) {
        GrammarAST labelNode = pair.a;
        GrammarAST labelOpNode = (GrammarAST) labelNode.getParent();
        GrammarAST elementNode = (GrammarAST) labelOpNode.getChild(1);
        LabelElementPair lp = new LabelElementPair(g, labelNode, elementNode, labelOpNode.getType());
        r.alt[1].labelDefs.map(labelNode.getText(), lp);
    }
    // copy to rule from walker
    r.leftRecursiveRuleRefLabels = leftRecursiveRuleWalker.leftRecursiveRuleRefLabels;
    tool.log("grammar", "added: " + t.toStringTree());
    return true;
}
Also used : RuleAST(org.antlr.v4.tool.ast.RuleAST) RuleCollector(org.antlr.v4.semantics.RuleCollector) GrammarAST(org.antlr.v4.tool.ast.GrammarAST) LabelElementPair(org.antlr.v4.tool.LabelElementPair) ActionAST(org.antlr.v4.tool.ast.ActionAST) GrammarTransformPipeline(org.antlr.v4.tool.GrammarTransformPipeline) BasicSemanticChecks(org.antlr.v4.semantics.BasicSemanticChecks) RecognitionException(org.antlr.runtime.RecognitionException)

Aggregations

Tool (org.antlr.v4.Tool)9 Grammar (org.antlr.v4.tool.Grammar)7 LexerGrammar (org.antlr.v4.tool.LexerGrammar)7 SemanticPipeline (org.antlr.v4.semantics.SemanticPipeline)6 File (java.io.File)5 RecognitionException (org.antlr.runtime.RecognitionException)3 GrammarAST (org.antlr.v4.tool.ast.GrammarAST)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ANTLRMessage (org.antlr.v4.tool.ANTLRMessage)2 GrammarRootAST (org.antlr.v4.tool.ast.GrammarRootAST)2 RuleAST (org.antlr.v4.tool.ast.RuleAST)2 ST (org.stringtemplate.v4.ST)2 STGroupFile (org.stringtemplate.v4.STGroupFile)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 Field (java.lang.reflect.Field)1 LinkedHashMap (java.util.LinkedHashMap)1