diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-04-02 11:34:58 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-04-02 11:34:58 +0100 |
| commit | 40b4de2450c299209a18e3c504e5b7ac2be674f0 (patch) | |
| tree | 8d6dd4ef896fdb2cedd14ed00f66254d2038bf97 /src | |
| parent | beaedb20590b0cc0589137a8f416ebea78dca463 (diff) | |
Allow multiple types to be read at once in JavaReader
Diffstat (limited to 'src')
3 files changed, 20 insertions, 11 deletions
diff --git a/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java b/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java index 51df264..9925068 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java @@ -14,13 +14,14 @@ import static org.zwobble.couscous.ast.structure.NodeStructure.descendantNodesAn import static org.zwobble.couscous.util.Casts.tryCast; import static org.zwobble.couscous.util.ExtraLists.eagerFlatMap; import static org.zwobble.couscous.util.ExtraLists.flatMap; +import static org.zwobble.couscous.util.ExtraLists.list; import static org.zwobble.couscous.util.ExtraStreams.toStream; public class JavaFrontend { public List<TypeNode> readSourceDirectory(List<Path> sourcePaths, Path directoryPath) throws IOException { List<TypeNode> classNodes = flatMap( findJavaFiles(directoryPath), - javaFile -> JavaReader.readClassFromFile(sourcePaths, javaFile)); + javaFile -> JavaReader.readClassesFromFiles(sourcePaths, list(javaFile))); ensureDeclarationsAreUnique(classNodes); return classNodes; } 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 cb06cfe..41ee30d 100644 --- a/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java +++ b/src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java @@ -44,15 +44,19 @@ import static org.zwobble.couscous.util.ExtraLists.*; import static org.zwobble.couscous.util.ExtraMaps.lookup; public class JavaReader { - public static List<TypeNode> readClassFromFile(List<Path> sourcePaths, Path sourcePath) throws IOException { - CompilationUnit ast = new JavaParser().parseCompilationUnit(sourcePaths, sourcePath); + public static List<TypeNode> readClassesFromFiles(List<Path> sourcePaths, List<Path> sourceFiles) throws IOException { + JavaParser parser = new JavaParser(); + JavaReader reader = new JavaReader(); + for (Path sourceFile : sourceFiles) { + CompilationUnit ast = parser.parseCompilationUnit(sourcePaths, sourceFile); - List<IProblem> errors = eagerFilter(asList(ast.getProblems()), problem -> problem.isError()); - if (!errors.isEmpty()) { - throw new RuntimeException("Errors during parsing:\n\n" + describe(errors)); + List<IProblem> errors = eagerFilter(asList(ast.getProblems()), problem -> problem.isError()); + if (!errors.isEmpty()) { + throw new RuntimeException("Errors during parsing:\n\n" + describe(errors)); + } + reader.readCompilationUnit(ast); } - JavaReader reader = new JavaReader(); - return reader.readCompilationUnit(ast); + return reader.types(); } private static String describe(List<IProblem> errors) { @@ -74,9 +78,13 @@ public class JavaReader { classes = ImmutableList.builder(); } - private List<TypeNode> readCompilationUnit(CompilationUnit ast) { + private void readCompilationUnit(CompilationUnit ast) { TypeDeclaration type = (TypeDeclaration)ast.types().get(0); - List<TypeNode> classes = cons(readTypeDeclaration(type), this.classes.build()); + classes.add(readTypeDeclaration(type)); + } + + private List<TypeNode> types() { + List<TypeNode> classes = this.classes.build(); NodeTransformer transformer = NodeTransformer.builder() .transformExpression(expression -> tryCast(ConstructorCallNode.class, expression) diff --git a/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReading.java b/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReading.java index f7d2c85..a7dc180 100644 --- a/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReading.java +++ b/src/test/java/org/zwobble/couscous/tests/frontends/java/JavaReading.java @@ -66,7 +66,7 @@ public class JavaReading { Files.createDirectories(sourcePath.getParent()); Files.write(sourcePath, list(contents)); - return JavaReader.readClassFromFile(list(directoryPath), directoryPath.resolve(path)); + return JavaReader.readClassesFromFiles(list(directoryPath), list(directoryPath.resolve(path))); } finally { deleteRecursively(directoryPath.toFile()); } |
