Search in sources :

Example 71 with CharStream

use of org.antlr.v4.runtime.CharStream in project claw-compiler by C2SM-RCM.

the class FortranLineBreaksFinder method run.

public List<LineBreakInfo> run(InputStream input) throws FortranSyntaxException, IOException {
    lexer.reset();
    parser.reset();
    lexerErrorListener.reset();
    parserErrorListener.reset();
    CharStream chrStrm = CharStreams.fromStream(input, StandardCharsets.US_ASCII);
    lexer.setInputStream(chrStrm);
    CommonTokenStream tokStrm = new CommonTokenStream(lexer);
    parser.setInputStream(tokStrm);
    parser.setBuildParseTree(true);
    ParseTree tree = null;
    try {
        tree = parser.root();
    } catch (CancellationException e) {
    }
    if (lexerErrorListener.error() != null) {
        throw lexerErrorListener.error();
    }
    if (parserErrorListener.error() != null) {
        throw parserErrorListener.error();
    }
    ParseTreeWalker walker = new ParseTreeWalker();
    Listener listener = new Listener();
    try {
        walker.walk(listener, tree);
    } catch (CancellationException e) {
    }
    if (listener.error() != null) {
        throw listener.error();
    }
    return listener.lineBreaks;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) FortranLineBreaksFilterBaseListener(clawfc.depscan.parser.FortranLineBreaksFilterBaseListener) ConsoleErrorListener(org.antlr.v4.runtime.ConsoleErrorListener) CancellationException(java.util.concurrent.CancellationException) Utils.toCharStream(clawfc.Utils.toCharStream) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 72 with CharStream

use of org.antlr.v4.runtime.CharStream in project claw-compiler by C2SM-RCM.

the class PreprocessorOutputScanner method run.

/**
 * Only absolute paths are recognized, so the preprocessor should be given only
 * absolute paths to include directories and source file.
 */
public Set<Path> run(InputStream input, OutputStream nonPPOutput) throws Exception {
    lexer.reset();
    parser.reset();
    lexerErrorListener.reset();
    parserErrorListener.reset();
    CharStream chrStrm = CharStreams.fromStream(input, StandardCharsets.US_ASCII);
    lexer.setInputStream(chrStrm);
    CommonTokenStream tokStrm = new CommonTokenStream(lexer);
    parser.setInputStream(tokStrm);
    parser.setBuildParseTree(true);
    ParseTree tree = null;
    try {
        tree = parser.root();
    } catch (CancellationException e) {
    }
    if (lexerErrorListener.error() != null) {
        throw lexerErrorListener.error();
    }
    if (parserErrorListener.error() != null) {
        throw parserErrorListener.error();
    }
    ParseTreeWalker walker = new ParseTreeWalker();
    Listener listener = new Listener(nonPPOutput);
    try {
        walker.walk(listener, tree);
    } catch (CancellationException e) {
    }
    if (listener.error() != null) {
        throw listener.error();
    }
    return listener.includes;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) PreprocessorOutputSplitterBaseListener(clawfc.depscan.parser.PreprocessorOutputSplitterBaseListener) ConsoleErrorListener(org.antlr.v4.runtime.ConsoleErrorListener) CancellationException(java.util.concurrent.CancellationException) Utils.toCharStream(clawfc.Utils.toCharStream) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 73 with CharStream

use of org.antlr.v4.runtime.CharStream in project claw-compiler by C2SM-RCM.

the class FortranDepParser method parse.

public List<StatementInfo> parse(InputStream input) throws FortranException, IOException, Exception {
    lexer.reset();
    parser.reset();
    lexerErrorListener.reset();
    parserErrorListener.reset();
    CharStream chrStrm = CharStreams.fromStream(input, StandardCharsets.US_ASCII);
    lexer.setInputStream(chrStrm);
    CommonTokenStream tokStrm = new CommonTokenStream(lexer);
    parser.setInputStream(tokStrm);
    parser.setBuildParseTree(true);
    ParseTree tree = null;
    try {
        tree = parser.root();
    } catch (CancellationException e) {
    }
    if (lexerErrorListener.error() != null) {
        throw lexerErrorListener.error();
    }
    if (parserErrorListener.error() != null) {
        throw parserErrorListener.error();
    }
    ParseTreeWalker walker = new ParseTreeWalker();
    Listener listener = new Listener(statementsParser, procStatementsParser);
    try {
        walker.walk(listener, tree);
    } catch (CancellationException e) {
    }
    if (listener.error() != null) {
        throw listener.error();
    }
    return listener.statements;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ConsoleErrorListener(org.antlr.v4.runtime.ConsoleErrorListener) CancellationException(java.util.concurrent.CancellationException) Utils.toCharStream(clawfc.Utils.toCharStream) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 74 with CharStream

use of org.antlr.v4.runtime.CharStream in project claw-compiler by C2SM-RCM.

the class FortranDepStatementsRecognizer method run.

Listener run(StatementType type, String input) throws IOException, Exception {
    lexer.reset();
    parser.reset();
    lexerErrorListener.reset();
    parserErrorListener.reset();
    CharStream chrStrm = toCharStream(input);
    lexer.setInputStream(chrStrm);
    CommonTokenStream tokStrm = new CommonTokenStream(lexer);
    parser.setInputStream(tokStrm);
    parser.setBuildParseTree(true);
    ParseTree tree = null;
    try {
        switch(type) {
            case MODULE_OPEN:
                tree = parser.module_open_stmt();
                break;
            case MODULE_CLOSE:
                tree = parser.module_close_stmt();
                break;
            case PROGRAM_OPEN:
                tree = parser.program_open_stmt();
                break;
            case PROGRAM_CLOSE:
                tree = parser.program_close_stmt();
                break;
            case BLOCK_DATA_OPEN:
                tree = parser.block_data_open_stmt();
                break;
            case BLOCK_DATA_CLOSE:
                tree = parser.block_data_close_stmt();
                break;
            case USE_MODULE:
                tree = parser.use_stmt();
                break;
            default:
                throw new Exception("Unexpected statement type");
        }
    } catch (CancellationException e) {
    }
    if (lexerErrorListener.error() != null) {
        throw lexerErrorListener.error();
    }
    if (parserErrorListener.error() != null) {
        throw parserErrorListener.error();
    }
    ParseTreeWalker walker = new ParseTreeWalker();
    Listener listener = new Listener(type);
    walker.walk(listener, tree);
    if (listener.error() != null) {
        throw listener.error();
    }
    return listener;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) FortranDepStatementsRecognizerBaseListener(clawfc.depscan.parser.FortranDepStatementsRecognizerBaseListener) ConsoleErrorListener(org.antlr.v4.runtime.ConsoleErrorListener) CancellationException(java.util.concurrent.CancellationException) Utils.toCharStream(clawfc.Utils.toCharStream) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 75 with CharStream

use of org.antlr.v4.runtime.CharStream in project claw-compiler by C2SM-RCM.

the class Utils method toCharStream.

public static CharStream toCharStream(String str) throws IOException {
    InputStream inStrm = toInputStream(str);
    CharStream chrStrm = CharStreams.fromStream(inStrm, StandardCharsets.US_ASCII);
    return chrStrm;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CharStream(org.antlr.v4.runtime.CharStream)

Aggregations

CharStream (org.antlr.v4.runtime.CharStream)187 Test (org.junit.Test)93 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)85 UnbufferedCharStream (org.antlr.v4.runtime.UnbufferedCharStream)46 ParseTree (org.antlr.v4.runtime.tree.ParseTree)38 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)30 IOException (java.io.IOException)28 InputStream (java.io.InputStream)23 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)23 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)22 File (java.io.File)21 TokenStream (org.antlr.v4.runtime.TokenStream)21 StringReader (java.io.StringReader)20 CancellationException (java.util.concurrent.CancellationException)20 ConsoleErrorListener (org.antlr.v4.runtime.ConsoleErrorListener)20 CommonTokenFactory (org.antlr.v4.runtime.CommonTokenFactory)18 Token (org.antlr.v4.runtime.Token)18 LexerGrammar (org.antlr.v4.tool.LexerGrammar)18 Utils.toCharStream (clawfc.Utils.toCharStream)15 Path (java.nio.file.Path)14