load method
DartCompilationUnit
load()
Load the source code from the filesystem or a String and parse it
(internally using parseString
from the Dart analyzer) into an AST
Implementation
DartCompilationUnit load() {
LibraryDirective? libraryDirective;
PartOfDirective? partOfDirective;
final imports = <ImportDirective>[];
final exports = <ExportDirective>[];
final parts = <PartDirective>[];
final unit = _parse(toString());
for (final directive in unit.directives) {
if (directive is ImportDirective) {
imports.add(directive);
} else if (directive is ExportDirective) {
exports.add(directive);
} else if (directive is PartDirective) {
parts.add(directive);
} else if (directive is PartOfDirective) {
if (partOfDirective != null) {
throw CompileError(
'Library $uri must not contain multiple "part of" directives');
}
partOfDirective = directive;
} else if (directive is LibraryDirective) {
if (libraryDirective != null) {
throw CompileError(
'Library $uri must not contain multiple "library" directives');
}
libraryDirective = directive;
}
}
if (partOfDirective != null && imports.isNotEmpty) {
throw CompileError(
"Library $uri is a part, so it can't have 'import' directives");
}
if (partOfDirective != null && exports.isNotEmpty) {
throw CompileError(
"Library $uri is a part, so it can't have 'export' directives");
}
if (partOfDirective != null && parts.isNotEmpty) {
throw CompileError(
"Library $uri is a part, so it can't have 'part' directives");
}
return DartCompilationUnit(uri,
imports: imports,
exports: exports,
parts: parts,
declarations: unit.declarations,
library: libraryDirective,
partOf: partOfDirective);
}