org.aya.concrete.resolve.module.FileModuleLoader Maven / Gradle / Ivy
// Copyright (c) 2020-2021 Yinsen (Tesla) Zhang.
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
package org.aya.concrete.resolve.module;
import kala.collection.immutable.ImmutableSeq;
import org.aya.api.error.Reporter;
import org.aya.api.error.SourceFileLocator;
import org.aya.api.util.InternalException;
import org.aya.concrete.parse.AyaParsing;
import org.aya.concrete.resolve.ResolveInfo;
import org.aya.concrete.resolve.context.EmptyContext;
import org.aya.generic.Constants;
import org.aya.tyck.trace.Trace;
import org.aya.util.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.nio.file.Path;
public record FileModuleLoader(
@NotNull SourceFileLocator locator,
@NotNull Path basePath,
@Override @NotNull Reporter reporter,
Trace.@Nullable Builder builder
) implements ModuleLoader {
@Override public @Nullable ResolveInfo load(@NotNull ImmutableSeq<@NotNull String> path, @NotNull ModuleLoader recurseLoader) {
var sourcePath = FileUtil.resolveFile(basePath, path, Constants.AYA_POSTFIX);
try {
var program = AyaParsing.program(locator, reporter, sourcePath);
var context = new EmptyContext(reporter, sourcePath).derive(path);
return tyckModule(builder, resolveModule(context, program, recurseLoader), null);
} catch (IOException e) {
return null;
}
}
public static void handleInternalError(@NotNull InternalException e) {
e.printStackTrace();
e.printHint();
System.err.println("""
Please report the stacktrace to the developers so a better error handling could be made.
Don't forget to inform the version of Aya you're using and attach your code for reproduction.""");
}
}