Search in sources :

Example 1 with DefaultToolListener

use of org.antlr.v4.tool.DefaultToolListener in project antlr4 by tunnelvisionlabs.

the class BaseTest method antlr.

protected ErrorQueue antlr(String grammarFileName, boolean defaultListener, String... extraOptions) {
    final List<String> options = new ArrayList<String>();
    Collections.addAll(options, extraOptions);
    if (!options.contains("-o")) {
        options.add("-o");
        options.add(tmpdir);
    }
    if (!options.contains("-lib")) {
        options.add("-lib");
        options.add(tmpdir);
    }
    if (!options.contains("-encoding")) {
        options.add("-encoding");
        options.add("UTF-8");
    }
    options.add(new File(tmpdir, grammarFileName).toString());
    final String[] optionsA = new String[options.size()];
    options.toArray(optionsA);
    Tool antlr = newTool(optionsA);
    ErrorQueue equeue = new ErrorQueue(antlr);
    antlr.addListener(equeue);
    if (defaultListener) {
        antlr.addListener(new DefaultToolListener(antlr));
    }
    antlr.processGrammarsOnCommandLine();
    if (!defaultListener && !equeue.errors.isEmpty()) {
        System.err.println("antlr reports errors from " + options);
        for (int i = 0; i < equeue.errors.size(); i++) {
            ANTLRMessage msg = equeue.errors.get(i);
            System.err.println(msg);
        }
        System.out.println("!!!\ngrammar:");
        try {
            System.out.println(new String(Utils.readFile(tmpdir + "/" + grammarFileName)));
        } catch (IOException ioe) {
            System.err.println(ioe.toString());
        }
        System.out.println("###");
    }
    if (!defaultListener && !equeue.warnings.isEmpty()) {
        System.err.println("antlr reports warnings from " + options);
        for (int i = 0; i < equeue.warnings.size(); i++) {
            ANTLRMessage msg = equeue.warnings.get(i);
            System.err.println(msg);
        }
    }
    if (!defaultListener && !equeue.warnings.isEmpty()) {
        System.err.println("antlr reports warnings from " + options);
        for (int i = 0; i < equeue.warnings.size(); i++) {
            ANTLRMessage msg = equeue.warnings.get(i);
            System.err.println(msg);
        }
    }
    return equeue;
}
Also used : DefaultToolListener(org.antlr.v4.tool.DefaultToolListener) ArrayList(java.util.ArrayList) STGroupString(org.stringtemplate.v4.STGroupString) IOException(java.io.IOException) File(java.io.File) ANTLRMessage(org.antlr.v4.tool.ANTLRMessage) Tool(org.antlr.v4.Tool)

Example 2 with DefaultToolListener

use of org.antlr.v4.tool.DefaultToolListener 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)

Aggregations

File (java.io.File)2 ArrayList (java.util.ArrayList)2 Tool (org.antlr.v4.Tool)2 ANTLRMessage (org.antlr.v4.tool.ANTLRMessage)2 DefaultToolListener (org.antlr.v4.tool.DefaultToolListener)2 IOException (java.io.IOException)1 ST (org.stringtemplate.v4.ST)1 STGroupFile (org.stringtemplate.v4.STGroupFile)1 STGroupString (org.stringtemplate.v4.STGroupString)1