Search in sources :

Example 1 with ABSParser

use of org.abs_models.frontend.antlr.parser.ABSParser 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();
    }
}
Also used : CompilationUnit(org.abs_models.frontend.ast.CompilationUnit) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) ABSLexer(org.abs_models.frontend.antlr.parser.ABSLexer) ABSParser(org.abs_models.frontend.antlr.parser.ABSParser) CreateJastAddASTListener(org.abs_models.frontend.antlr.parser.CreateJastAddASTListener) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) SyntaxErrorCollector(org.abs_models.frontend.antlr.parser.SyntaxErrorCollector)

Aggregations

ABSLexer (org.abs_models.frontend.antlr.parser.ABSLexer)1 ABSParser (org.abs_models.frontend.antlr.parser.ABSParser)1 CreateJastAddASTListener (org.abs_models.frontend.antlr.parser.CreateJastAddASTListener)1 SyntaxErrorCollector (org.abs_models.frontend.antlr.parser.SyntaxErrorCollector)1 CompilationUnit (org.abs_models.frontend.ast.CompilationUnit)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 ParseTree (org.antlr.v4.runtime.tree.ParseTree)1 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)1