bundle method
Parse a string content and generate a library, will import other files.
Implementation
ASTCompilation bundle({
required HTSource source,
required HTParser parser,
bool normalizePath = true,
bool debugPerformance = false,
}) {
final sourceParseResult =
parser.parseSource(source, debugPerformance: debugPerformance);
final tik = DateTime.now().millisecondsSinceEpoch;
final sourceParseErrors = sourceParseResult.errors;
final values = <String, ASTSource>{};
final sources = <String, ASTSource>{};
final Set cachedParsingTargets = <String>{};
void handleImport(ASTSource astSource) {
cachedParsingTargets.add(astSource.fullName);
for (final decl in astSource.imports) {
try {
if (decl.isPreloadedModule) {
decl.fullFromPath = decl.fromPath;
continue;
}
late final ASTSource importedSource;
String importFullName;
if (normalizePath) {
final currentDir = astSource.fullName
.startsWith(InternalIdentifier.anonymousScript)
? sourceContext.root
: path.dirname(astSource.fullName);
decl.fullFromPath = importFullName = sourceContext.getAbsolutePath(
key: decl.fromPath!, dirName: currentDir);
} else {
decl.fullFromPath = importFullName = decl.fromPath!;
}
if (sources.keys.contains(importFullName) ||
cachedParsingTargets.contains(importFullName)) continue;
final source2 = sourceContext.getResource(importFullName);
importedSource =
parser.parseSource(source2, debugPerformance: debugPerformance);
// final parser2 = HTParser(sourceContext: sourceContext);
// importedSource = parser2.parseSource(source2);
sourceParseErrors.addAll(importedSource.errors);
// _cachedParseResults[importFullName] = importedSource;
if (importedSource.resourceType == HTResourceType.hetuValue) {
values[importFullName] = importedSource;
} else {
handleImport(importedSource);
sources[importFullName] = importedSource;
}
} catch (error) {
if (error is HTError &&
error.code != ErrorCode.resourceDoesNotExist) {
sourceParseErrors.add(error);
} else {
final convertedError = HTError.sourceProviderError(
decl.fromPath!, astSource.fullName,
filename: source.fullName,
line: decl.line,
column: decl.column,
offset: decl.offset,
length: decl.length);
sourceParseErrors.add(convertedError);
}
}
}
cachedParsingTargets.remove(astSource.fullName);
}
if (sourceParseResult.resourceType == HTResourceType.hetuValue) {
values[sourceParseResult.fullName] = sourceParseResult;
} else {
handleImport(sourceParseResult);
sources[sourceParseResult.fullName] = sourceParseResult;
}
final compilation = ASTCompilation(
values: values,
sources: sources,
entryFullname: source.fullName,
entryResourceType: source.type,
errors: sourceParseErrors);
final tok = DateTime.now().millisecondsSinceEpoch;
if (debugPerformance) {
print('hetu: ${tok - tik}ms\tto bundle\t[${source.fullName}]');
}
return compilation;
}