diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java | 20 | ||||
| -rw-r--r-- | src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java | 8 |
2 files changed, 19 insertions, 9 deletions
diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java index 5602434..a436bd7 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java @@ -2,6 +2,7 @@ package org.zwobble.couscous.frontends.java; import com.google.common.collect.ImmutableList; import com.google.common.collect.Maps; +import org.eclipse.jdt.core.compiler.IProblem; import org.eclipse.jdt.core.dom.*; import org.zwobble.couscous.ast.*; import org.zwobble.couscous.ast.sugar.AnonymousClass; @@ -16,7 +17,7 @@ import java.util.Optional; import java.util.function.Function; import static com.google.common.collect.Iterables.concat; -import static com.google.common.collect.Iterables.transform; +import static com.google.common.collect.Iterables.*; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static org.zwobble.couscous.ast.AnnotationNode.annotation; @@ -33,14 +34,25 @@ import static org.zwobble.couscous.util.ExtraLists.*; public class JavaReader { public static List<ClassNode> readClassFromFile(List<Path> sourcePaths, Path sourcePath) throws IOException { CompilationUnit ast = new JavaParser().parseCompilationUnit(sourcePaths, sourcePath); - System.out.println(sourcePath); - for (Message message : ast.getMessages()) { - System.out.println(message.getMessage()); + + ImmutableList<IProblem> errors = ImmutableList.copyOf(filter(asList(ast.getProblems()), problem -> problem.isError())); + if (!errors.isEmpty()) { + throw new RuntimeException("Errors during parsing:\n\n" + describe(errors)); } JavaReader reader = new JavaReader(); return cons(reader.readCompilationUnit(ast), reader.classes.build()); } + private static String describe(List<IProblem> errors) { + return String.join("\n\n", transform(errors, JavaReader::describe)); + } + + private static String describe(IProblem error) { + return "File: " + String.copyValueOf(error.getOriginatingFileName()) + "\n" + + "Line number: " + error.getSourceLineNumber() + "\n" + + error.getMessage(); + } + private final ImmutableList.Builder<ClassNode> classes; private int anonymousClassCount = 0; diff --git a/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java b/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java index 46e010c..74db410 100644 --- a/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java +++ b/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReaderTests.java @@ -1,6 +1,5 @@ package org.zwobble.couscous.tests.frontends.java; -import com.google.common.collect.ImmutableList; import org.junit.Test; import org.zwobble.couscous.ast.*; import org.zwobble.couscous.frontends.java.JavaReader; @@ -13,7 +12,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.List; -import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static org.hamcrest.Matchers.hasSize; import static org.junit.Assert.assertEquals; @@ -370,9 +368,9 @@ public class JavaReaderTests { @Test public void initialValueOfLocalVariableDeclarationsAreTypeCoercedIfNecessary() { - List<StatementNode> statements = readStatements("Object x = 4;"); + StatementNode statement = readStatement("void", "Object x = 4;"); - LocalVariableDeclarationNode declaration = (LocalVariableDeclarationNode) statements.get(0); + LocalVariableDeclarationNode declaration = (LocalVariableDeclarationNode) statement; assertEquals(typeCoercion(literal(4), ObjectValues.OBJECT), declaration.getInitialValue()); } @@ -509,7 +507,7 @@ public class JavaReaderTests { "parseInt", list(literal("42")), IntegerValue.REF)), - readStatement("Integer.parseInt(\"42\");")); + readStatement("void", "Integer.parseInt(\"42\");")); } @Test |
