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();
}
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();
}
}
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));
}
}
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;
}
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;
}
Aggregations