Example usage for org.antlr.v4.runtime CharStreams fromReader

List of usage examples for org.antlr.v4.runtime CharStreams fromReader

Introduction

In this page you can find the example usage for org.antlr.v4.runtime CharStreams fromReader.

Prototype

public static CodePointCharStream fromReader(Reader r) throws IOException 

Source Link

Document

Creates a CharStream given a Reader .

Usage

From source file:groovy.ui.text.SmartDocumentFilter.java

License:Apache License

private GroovyLangLexer createLexer(String text) throws IOException {
    CharStream charStream = CharStreams.fromReader(new StringReader(text));
    GroovyLangLexer lexer = new GroovyLangLexer(charStream);

    lexer.removeErrorListener(ConsoleErrorListener.INSTANCE);

    return lexer;
}

From source file:no.nb.nna.broprox.robotsparser.RobotsTxtParser.java

License:Apache License

public RobotsTxt parse(Reader robotsReader) throws IOException {
    return parse(CharStreams.fromReader(robotsReader));
}

From source file:org.btrplace.safeplace.spec.SpecScanner.java

License:Open Source License

private static CommonTokenStream getTokens(String source) throws IOException {
    CharStream is = CharStreams.fromReader(new StringReader(source));
    CstrSpecLexer lexer = new CstrSpecLexer(is);
    lexer.removeErrorListeners();/*from   ww w. j a  v  a2s .c o  m*/
    lexer.addErrorListener(new DiagnosticErrorListener());
    return new CommonTokenStream(lexer);
}

From source file:org.jgrapht.io.CSVImporter.java

License:LGPL

private void read(Graph<V, E> graph, Reader input, CSVBaseListener listener) throws ImportException {
    try {// ww w .  java 2s.c  o  m
        ThrowingErrorListener errorListener = new ThrowingErrorListener();

        // create lexer
        CSVLexer lexer = new CSVLexer(CharStreams.fromReader(input));
        lexer.setSep(delimiter);
        lexer.removeErrorListeners();
        lexer.addErrorListener(errorListener);

        // create parser
        CSVParser parser = new CSVParser(new CommonTokenStream(lexer));
        parser.removeErrorListeners();
        parser.addErrorListener(errorListener);

        // Specify our entry point
        CSVParser.FileContext graphContext = parser.file();

        // Walk it and attach our listener
        ParseTreeWalker walker = new ParseTreeWalker();
        walker.walk(listener, graphContext);
    } catch (IOException e) {
        throw new ImportException("Failed to import CSV graph: " + e.getMessage(), e);
    } catch (ParseCancellationException pe) {
        throw new ImportException("Failed to import CSV graph: " + pe.getMessage(), pe);
    } catch (IllegalArgumentException iae) {
        throw new ImportException("Failed to import CSV graph: " + iae.getMessage(), iae);
    }
}

From source file:org.jgrapht.io.GmlImporter.java

License:LGPL

/**
 * Import a graph./*  ww  w  . j  a  va 2s . c o m*/
 * 
 * <p>
 * The provided graph must be able to support the features of the graph that is read. For
 * example if the gml file contains self-loops then the graph provided must also support
 * self-loops. The same for multiple edges.
 * 
 * <p>
 * If the provided graph is a weighted graph, the importer also reads edge weights. Otherwise
 * edge weights are ignored.
 * 
 * @param graph the output graph
 * @param input the input reader
 * @throws ImportException in case an error occurs, such as I/O or parse error
 */
@Override
public void importGraph(Graph<V, E> graph, Reader input) throws ImportException {
    try {
        ThrowingErrorListener errorListener = new ThrowingErrorListener();

        // create lexer
        GmlLexer lexer = new GmlLexer(CharStreams.fromReader(input));
        lexer.removeErrorListeners();
        lexer.addErrorListener(errorListener);

        // create parser
        GmlParser parser = new GmlParser(new CommonTokenStream(lexer));
        parser.removeErrorListeners();
        parser.addErrorListener(errorListener);

        // Specify our entry point
        GmlContext graphContext = parser.gml();

        // Walk it and attach our listener
        ParseTreeWalker walker = new ParseTreeWalker();
        CreateGraphGmlListener listener = new CreateGraphGmlListener();
        walker.walk(listener, graphContext);

        // update graph
        listener.updateGraph(graph);
    } catch (IOException e) {
        throw new ImportException("Failed to import gml graph: " + e.getMessage(), e);
    } catch (ParseCancellationException pe) {
        throw new ImportException("Failed to import gml graph: " + pe.getMessage(), pe);
    } catch (IllegalArgumentException iae) {
        throw new ImportException("Failed to import gml graph: " + iae.getMessage(), iae);
    }
}

From source file:org.jgrapht.io.JSONImporter.java

License:Open Source License

/**
 * Import a graph./*  w w  w  .  ja va 2 s  . c  o m*/
 * 
 * <p>
 * The provided graph must be able to support the features of the graph that is read. For
 * example if the JSON file contains self-loops then the graph provided must also support
 * self-loops. The same for multiple edges.
 * 
 * <p>
 * If the provided graph is a weighted graph, the importer also reads edge weights. Otherwise
 * edge weights are ignored.
 * 
 * @param graph the output graph
 * @param input the input reader
 * @throws ImportException in case an error occurs, such as I/O or parse error
 */
@Override
public void importGraph(Graph<V, E> graph, Reader input) throws ImportException {
    try {
        ThrowingErrorListener errorListener = new ThrowingErrorListener();

        // create lexer
        JsonLexer lexer = new JsonLexer(CharStreams.fromReader(input));
        lexer.removeErrorListeners();
        lexer.addErrorListener(errorListener);

        // create parser
        JsonParser parser = new JsonParser(new CommonTokenStream(lexer));
        parser.removeErrorListeners();
        parser.addErrorListener(errorListener);

        // Specify our entry point
        JsonContext graphContext = parser.json();

        // Walk it and attach our listener
        ParseTreeWalker walker = new ParseTreeWalker();
        CreateGraphJsonListener listener = new CreateGraphJsonListener();
        walker.walk(listener, graphContext);

        // update graph
        listener.updateGraph(graph);
    } catch (IOException e) {
        throw new ImportException("Failed to import json graph: " + e.getMessage(), e);
    } catch (ParseCancellationException pe) {
        throw new ImportException("Failed to import json graph: " + pe.getMessage(), pe);
    } catch (IllegalArgumentException iae) {
        throw new ImportException("Failed to import json graph: " + iae.getMessage(), iae);
    }
}