Search in sources :

Example 1 with StartContext

use of boa.parser.BoaParser.StartContext in project compiler by boalang.

the class BaseTest method codegen.

protected StartContext codegen(final String input, final String error) throws IOException {
    final File outputRoot = new File(new File(System.getProperty("java.io.tmpdir")), UUID.randomUUID().toString());
    final File outputSrcDir = new File(outputRoot, "boa");
    if (!outputSrcDir.mkdirs())
        throw new IOException("unable to mkdir " + outputSrcDir);
    final File outputFile = new File(outputSrcDir, "Test.java");
    CodeGeneratingVisitor.combineAggregatorStrings.clear();
    CodeGeneratingVisitor.reduceAggregatorStrings.clear();
    final List<String> jobnames = new ArrayList<String>();
    final List<String> jobs = new ArrayList<String>();
    final List<Integer> seeds = new ArrayList<Integer>();
    final StartContext ctx = typecheck(input);
    // use the whole input string to seed the RNG
    seeds.add(input.hashCode());
    final Start p = ctx.ast;
    try {
        new InheritedAttributeTransformer().start(p);
        new LocalAggregationTransformer().start(p);
        new VisitorOptimizingTransformer().start(p);
        final CodeGeneratingVisitor cg = new CodeGeneratingVisitor("1");
        cg.start(p);
        jobs.add(cg.getCode());
        jobnames.add("1");
        final ST st = AbstractCodeGeneratingVisitor.stg.getInstanceOf("Program");
        st.add("name", "Test");
        st.add("numreducers", 1);
        st.add("jobs", jobs);
        st.add("jobnames", jobnames);
        st.add("combineTables", CodeGeneratingVisitor.combineAggregatorStrings);
        st.add("reduceTables", CodeGeneratingVisitor.reduceAggregatorStrings);
        st.add("splitsize", 64 * 1024 * 1024);
        st.add("seeds", seeds);
        final BufferedOutputStream o = new BufferedOutputStream(new FileOutputStream(outputFile));
        try {
            o.write(st.render().getBytes());
        } finally {
            o.close();
        }
        final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        final DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
        final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
        final Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromFiles(Arrays.asList(new File[] { outputFile }));
        if (!compiler.getTask(null, fileManager, diagnostics, Arrays.asList(new String[] { "-cp", System.getProperty("java.class.path") }), null, compilationUnits).call())
            for (final Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) throw new RuntimeException("Error on line " + diagnostic.getLineNumber() + ": " + diagnostic.getMessage(null));
        if (error != null)
            fail("expected to see exception: " + error);
    } catch (final Exception e) {
        if (error == null) {
            if (e.getMessage() == null) {
                e.printStackTrace();
                fail("unexpected exception");
            } else
                fail("found unexpected exception: " + e.getMessage());
        } else
            assertEquals(error, e.getMessage());
    }
    delete(outputSrcDir);
    return ctx;
}
Also used : Start(boa.compiler.ast.Start) ArrayList(java.util.ArrayList) Diagnostic(javax.tools.Diagnostic) JavaFileObject(javax.tools.JavaFileObject) VisitorOptimizingTransformer(boa.compiler.transforms.VisitorOptimizingTransformer) StandardJavaFileManager(javax.tools.StandardJavaFileManager) DiagnosticCollector(javax.tools.DiagnosticCollector) BufferedOutputStream(java.io.BufferedOutputStream) ST(org.stringtemplate.v4.ST) InheritedAttributeTransformer(boa.compiler.transforms.InheritedAttributeTransformer) JavaCompiler(javax.tools.JavaCompiler) LocalAggregationTransformer(boa.compiler.transforms.LocalAggregationTransformer) IOException(java.io.IOException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) IOException(java.io.IOException) RecognitionException(org.antlr.v4.runtime.RecognitionException) StartContext(boa.parser.BoaParser.StartContext) FileOutputStream(java.io.FileOutputStream) AbstractCodeGeneratingVisitor(boa.compiler.visitors.AbstractCodeGeneratingVisitor) CodeGeneratingVisitor(boa.compiler.visitors.CodeGeneratingVisitor) File(java.io.File)

Example 2 with StartContext

use of boa.parser.BoaParser.StartContext in project compiler by boalang.

the class BaseTest method parse.

protected StartContext parse(final String input, final String[] errors) throws IOException {
    final CommonTokenStream tokens = lex(input);
    final BoaParser parser = new BoaParser(tokens);
    final List<String> foundErr = new ArrayList<String>();
    parser.removeErrorListeners();
    parser.addErrorListener(new BaseErrorListener() {

        @Override
        public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) throws ParseCancellationException {
            throw new ParseCancellationException(e);
        }
    });
    parser.setBuildParseTree(false);
    parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
    StartContext p;
    try {
        p = parser.start();
    } catch (final Exception e) {
        // fall-back to LL mode parsing if SLL fails
        tokens.reset();
        parser.reset();
        parser.removeErrorListeners();
        parser.addErrorListener(new BaseErrorListener() {

            @Override
            public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line, final int charPositionInLine, final String msg, final RecognitionException e) {
                foundErr.add(line + "," + charPositionInLine + ": " + msg);
            }
        });
        parser.getInterpreter().setPredictionMode(PredictionMode.LL);
        p = parser.start();
    }
    if (!DEBUG)
        assertEquals("wrong number of errors", errors.length, foundErr.size());
    for (int i = 0; i < foundErr.size(); i++) {
        if (DEBUG)
            System.out.println(foundErr.get(i));
        else
            assertEquals("wrong error", errors[i], foundErr.get(i));
    }
    return p;
}
Also used : BoaParser(boa.parser.BoaParser) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) BaseErrorListener(org.antlr.v4.runtime.BaseErrorListener) ArrayList(java.util.ArrayList) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) IOException(java.io.IOException) RecognitionException(org.antlr.v4.runtime.RecognitionException) StartContext(boa.parser.BoaParser.StartContext) Recognizer(org.antlr.v4.runtime.Recognizer) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) JavaFileObject(javax.tools.JavaFileObject) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 3 with StartContext

use of boa.parser.BoaParser.StartContext in project compiler by boalang.

the class BaseTest method typecheck.

protected StartContext typecheck(final String input, final String error) throws IOException {
    final StartContext ctx = parse(input);
    try {
        new TypeCheckingVisitor().start(ctx.ast, new SymbolTable());
        if (error != null)
            fail("expected error: " + error);
    } catch (final Exception e) {
        if (error == null)
            fail("found unexpected error: " + e.getMessage());
        else
            assertEquals(error, e.getMessage());
    }
    return ctx;
}
Also used : StartContext(boa.parser.BoaParser.StartContext) SymbolTable(boa.compiler.SymbolTable) TypeCheckingVisitor(boa.compiler.visitors.TypeCheckingVisitor) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) IOException(java.io.IOException) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Aggregations

StartContext (boa.parser.BoaParser.StartContext)3 IOException (java.io.IOException)3 RecognitionException (org.antlr.v4.runtime.RecognitionException)3 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)3 ArrayList (java.util.ArrayList)2 JavaFileObject (javax.tools.JavaFileObject)2 SymbolTable (boa.compiler.SymbolTable)1 Start (boa.compiler.ast.Start)1 InheritedAttributeTransformer (boa.compiler.transforms.InheritedAttributeTransformer)1 LocalAggregationTransformer (boa.compiler.transforms.LocalAggregationTransformer)1 VisitorOptimizingTransformer (boa.compiler.transforms.VisitorOptimizingTransformer)1 AbstractCodeGeneratingVisitor (boa.compiler.visitors.AbstractCodeGeneratingVisitor)1 CodeGeneratingVisitor (boa.compiler.visitors.CodeGeneratingVisitor)1 TypeCheckingVisitor (boa.compiler.visitors.TypeCheckingVisitor)1 BoaParser (boa.parser.BoaParser)1 BufferedOutputStream (java.io.BufferedOutputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Diagnostic (javax.tools.Diagnostic)1 DiagnosticCollector (javax.tools.DiagnosticCollector)1