From 50730c7784ee742c8c148d4d5b07c4ea312a8e58 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 12 Jan 2025 17:29:05 +0000 Subject: Improve collection of source files --- .../java/org/shedlang/compiler/ShedCompiler.java | 65 ++++------------------ 1 file changed, 12 insertions(+), 53 deletions(-) (limited to 'src/main/java/org/shedlang/compiler/ShedCompiler.java') diff --git a/src/main/java/org/shedlang/compiler/ShedCompiler.java b/src/main/java/org/shedlang/compiler/ShedCompiler.java index 3901da5c..d1bd6101 100644 --- a/src/main/java/org/shedlang/compiler/ShedCompiler.java +++ b/src/main/java/org/shedlang/compiler/ShedCompiler.java @@ -11,20 +11,20 @@ import org.shedlang.compiler.shid.ShidCodeGen; import org.shedlang.compiler.shid.ShidNamespace; import org.shedlang.compiler.typechecker.TypeChecker; import org.shedlang.compiler.typechecker.TypeCheckerContext; -import org.shedlang.compiler.types.*; +import org.shedlang.compiler.types.Effects; +import org.shedlang.compiler.types.FieldsInfo; +import org.shedlang.compiler.types.NamespaceTypeInfo; +import org.shedlang.compiler.types.Types; import org.shedlang.compiler.util.P; import org.zwobble.sourcetext.SourceText; import java.io.IOException; import java.nio.charset.StandardCharsets; -import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; import java.util.*; -import java.util.stream.StreamSupport; +import static org.shedlang.compiler.SourceFiles.collectSourceFiles; import static org.shedlang.compiler.parser.Tokeniser.tokenise; import static org.shedlang.compiler.typechecker.NamespaceDependencyLineariser.orderNamespaces; @@ -42,8 +42,8 @@ public class ShedCompiler { .flatMap(root -> { var sourceRoot = root.resolve("src"); try { - var sourceFilePaths = collectSourceFilePaths(sourceRoot); - return parseSource(sourceRoot, sourceFilePaths).stream(); + var sourceFiles = collectSourceFiles(sourceRoot); + return parseSource(sourceFiles).stream(); } catch (IOException exception) { throw new RuntimeException(exception); } @@ -120,59 +120,18 @@ public class ShedCompiler { return projectRoots; } - private List collectSourceFilePaths(Path sourceRoot) throws IOException { - var paths = new ArrayList(); - Files.walkFileTree(sourceRoot, new SimpleFileVisitor() { - @Override - public FileVisitResult visitFile( - Path file, - BasicFileAttributes attrs - ) { - if (file.getFileName().toString().endsWith(".shed")) { - paths.add(file); - } - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory( - Path dir, - IOException exc - ) { - return FileVisitResult.CONTINUE; - } - }); - paths.sort(Comparator.naturalOrder()); - return paths; - } - private List parseSource( - Path sourceRoot, - List sourceFilePaths + List sourceFiles ) throws IOException { var untypedNamespaceNodes = new ArrayList(); - for (var sourceFilePath : sourceFilePaths) { - // TODO: handle path -> namespace name properly and test - var namespaceNameParts = StreamSupport.stream(sourceRoot.relativize(sourceFilePath).spliterator(), false) - .map(element -> { - var string = element.toString(); - var lastDotIndex = string.lastIndexOf('.'); - if (lastDotIndex == -1) { - return string; - } else { - return string.substring(0, lastDotIndex); - } - }) - .toList(); - var namespaceName = new NamespaceName(namespaceNameParts); - - var sourceContents = Files.readString(sourceFilePath, StandardCharsets.UTF_8); - var sourceText = SourceText.fromString(sourceFilePath.toString(), sourceContents); + for (var sourceFile : sourceFiles) { + var sourceContents = Files.readString(sourceFile.path(), StandardCharsets.UTF_8); + var sourceText = SourceText.fromString(sourceFile.path().toString(), sourceContents); var tokens = tokenise(sourceText); - var untypedNode = Parser.parseNamespace(tokens, namespaceName); + var untypedNode = Parser.parseNamespace(tokens, sourceFile.namespaceName()); untypedNamespaceNodes.add(untypedNode); } -- cgit v1.2.3