Search in sources :

Example 66 with CharStream

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

the class IgnoreDirectiveRecognizer method parse.

public String parse(InputStream input) 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();
    try {
        walker.walk(listener, tree);
    } catch (CancellationException e) {
    }
    if (listener.getError() != null) {
        throw listener.getError();
    }
    return listener.getContents();
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserErrorListener(clawfc.utils.ParserErrorListener) ConsoleErrorListener(org.antlr.v4.runtime.ConsoleErrorListener) CLAWIgnoreLineRecognizerBaseListener(clawfc.parsers.CLAWIgnoreLineRecognizerBaseListener) CancellationException(java.util.concurrent.CancellationException) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 67 with CharStream

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

the class RemoveIgnoreDirectiveFilter method run.

public void run(InputStream input, OutputStream output) 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();
    }
    if (tree == null) {
        throw new Exception("Antlr-generated parser failed to accept input");
    }
    ParseTreeWalker walker = new ParseTreeWalker();
    Listener listener = new Listener(output, directiveRecognizer);
    try {
        walker.walk(listener, tree);
    } catch (CancellationException e) {
    }
    if (listener.getError() != null) {
        throw listener.getError();
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ParserErrorListener(clawfc.utils.ParserErrorListener) CLAWRemoveIgnoreScannerBaseListener(clawfc.parsers.CLAWRemoveIgnoreScannerBaseListener) ConsoleErrorListener(org.antlr.v4.runtime.ConsoleErrorListener) CancellationException(java.util.concurrent.CancellationException) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) CancellationException(java.util.concurrent.CancellationException) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 68 with CharStream

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

the class AddIgnoreDirectiveFilter method run.

public void run(InputStream input, OutputStream output) 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();
    }
    if (tree == null) {
        throw new Exception("Antlr-generated parser failed to accept input");
    }
    ParseTreeWalker walker = new ParseTreeWalker();
    Listener listener = new Listener(output);
    try {
        walker.walk(listener, tree);
    } catch (CancellationException e) {
    }
    if (listener.getError() != null) {
        throw listener.getError();
    }
    if (listener.getIgnoreBlockStartLineNum() != null) {
        final int lineNum = listener.getIgnoreBlockStartLineNum();
        throw new Exception(sprintf("Unclosed ignore directive at line %s", lineNum));
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CLAWAddIgnoreScannerBaseListener(clawfc.parsers.CLAWAddIgnoreScannerBaseListener) ParserErrorListener(clawfc.utils.ParserErrorListener) ConsoleErrorListener(org.antlr.v4.runtime.ConsoleErrorListener) CancellationException(java.util.concurrent.CancellationException) CharStream(org.antlr.v4.runtime.CharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) CancellationException(java.util.concurrent.CancellationException) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

Example 69 with CharStream

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

the class FortranCLAWScannerTest method toTokenStream.

private CommonTokenStream toTokenStream(String str) throws IOException {
    CharStream chrStrm = toCharStream(str);
    lexer.reset();
    lexer.setInputStream(chrStrm);
    CommonTokenStream tokStrm = new CommonTokenStream(lexer);
    return tokStrm;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CharStream(org.antlr.v4.runtime.CharStream)

Example 70 with CharStream

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

the class FortranIncludeStatementRecognizer method run.

public String run(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 {
        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();
    walker.walk(listener, tree);
    return listener.includeString;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) FortranIncludeStatementRecognizerBaseListener(clawfc.depscan.parser.FortranIncludeStatementRecognizerBaseListener) ConsoleErrorListener(org.antlr.v4.runtime.ConsoleErrorListener) CancellationException(java.util.concurrent.CancellationException) CharStream(org.antlr.v4.runtime.CharStream) Utils.toCharStream(clawfc.Utils.toCharStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker)

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