Search in sources :

Example 1 with BallerinaLexer

use of org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaLexer in project ballerina by ballerina-lang.

the class Parser method generateCompilationUnit.

private CompilationUnitNode generateCompilationUnit(PackageSourceEntry sourceEntry) {
    try {
        int prevErrCount = dlog.errorCount;
        BDiagnosticSource diagnosticSrc = getDiagnosticSource(sourceEntry);
        String entryName = sourceEntry.getEntryName();
        BLangCompilationUnit compUnit = (BLangCompilationUnit) TreeBuilder.createCompilationUnit();
        compUnit.setName(sourceEntry.getEntryName());
        compUnit.pos = new DiagnosticPos(diagnosticSrc, 1, 1, 1, 1);
        ANTLRInputStream ais = new ANTLRInputStream(new ByteArrayInputStream(sourceEntry.getCode()));
        ais.name = entryName;
        BallerinaLexer lexer = new BallerinaLexer(ais);
        lexer.removeErrorListeners();
        lexer.addErrorListener(new BallerinaParserErrorListener(context, diagnosticSrc));
        CommonTokenStream tokenStream = new CommonTokenStream(lexer);
        BallerinaParser parser = new BallerinaParser(tokenStream);
        parser.setErrorHandler(getErrorStrategy(diagnosticSrc));
        parser.addParseListener(newListener(tokenStream, compUnit, diagnosticSrc));
        parser.compilationUnit();
        return compUnit;
    } catch (IOException e) {
        throw new RuntimeException("Error in populating package model: " + e.getMessage(), e);
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BDiagnosticSource(org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnosticSource) IOException(java.io.IOException) BallerinaParser(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser) DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) ByteArrayInputStream(java.io.ByteArrayInputStream) BallerinaParserErrorListener(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParserErrorListener) BLangCompilationUnit(org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit) BallerinaLexer(org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaLexer) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)1 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)1 BallerinaLexer (org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaLexer)1 BallerinaParser (org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParser)1 BallerinaParserErrorListener (org.wso2.ballerinalang.compiler.parser.antlr4.BallerinaParserErrorListener)1 BLangCompilationUnit (org.wso2.ballerinalang.compiler.tree.BLangCompilationUnit)1 BDiagnosticSource (org.wso2.ballerinalang.compiler.util.diagnotic.BDiagnosticSource)1 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)1