aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-04-02 11:34:58 +0100
committerMichael Williamson <mike@zwobble.org>2016-04-02 11:34:58 +0100
commit40b4de2450c299209a18e3c504e5b7ac2be674f0 (patch)
tree8d6dd4ef896fdb2cedd14ed00f66254d2038bf97 /src/main
parentbeaedb20590b0cc0589137a8f416ebea78dca463 (diff)
Allow multiple types to be read at once in JavaReader
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/zwobble/couscous/frontends/java/JavaFrontend.java3
-rw-r--r--src/main/java/org/zwobble/couscous/frontends/java/JavaReader.java26
2 files changed, 19 insertions, 10 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)