use of org.abs_models.frontend.antlr.parser.SyntaxErrorCollector in project abstools by abstools.
the class Main method parseUnit.
/**
* Parse the content of `reader` into a CompilationUnit.
*
* @param file The filename of the input stream, or null
* @param reader The stream to parse
* @param raiseExceptions Raise parse errors as exceptions if true
* @return The parsed content of `reader`, or an empty CompilationUnit with parse error information
* @throws IOException
*/
private static CompilationUnit parseUnit(File file, Reader reader) throws IOException {
try {
SyntaxErrorCollector errorlistener = new SyntaxErrorCollector(file);
ANTLRInputStream input = new ANTLRInputStream(reader);
ABSLexer lexer = new ABSLexer(input);
lexer.removeErrorListeners();
lexer.addErrorListener(errorlistener);
CommonTokenStream tokens = new CommonTokenStream(lexer);
ABSParser aparser = new ABSParser(tokens);
aparser.removeErrorListeners();
aparser.addErrorListener(errorlistener);
ParseTree tree = aparser.goal();
if (errorlistener.parserErrors.isEmpty()) {
ParseTreeWalker walker = new ParseTreeWalker();
CreateJastAddASTListener l = new CreateJastAddASTListener(file);
walker.walk(l, tree);
CompilationUnit u = new ASTPreProcessor().preprocess(l.getCompilationUnit());
return u;
} else {
String path = "<unknown path>";
if (file != null)
path = file.getPath();
CompilationUnit u = new CompilationUnit();
u.setName(path);
u.setParserErrors(errorlistener.parserErrors);
return u;
}
} finally {
reader.close();
}
}
Aggregations