Lkt: allow multiple modules for each import ... clause

It was not allowed so far because of an oversight in the recent revamp
of the module system.
This commit is contained in:
Pierre-Marie de Rodat
2026-01-27 13:49:04 +00:00
parent 58598281ed
commit a0f8539c0c
2 changed files with 38 additions and 27 deletions

View File

@@ -5228,27 +5228,23 @@ class GrammarListSep: LktNode {
|" Base node for all import clauses.
@abstract
class BaseImport: LktNode {
@parse_field
module_name: ModuleId
|" Return the unit that contains the module this import clause designates.
|" Load it if needed.
|" Return the list of units that contain the modules that this clause
|" imports. Load them if needed.
@exported
fun referenced_unit(): AnalysisUnit =
node.internal_fetch_referenced_unit(node.module_name.text)
fun referenced_units(): Array[AnalysisUnit] =
node.referenced_modules().map(
(m) => node.internal_fetch_referenced_unit(m.text)
)
|" Return identifiers for the list of modules that this clause imports.
@abstract
fun referenced_modules(): Array[Id]
env_spec {
do(node.referenced_unit())
do(node.referenced_units())
}
}
|" Clause to import a module.
class Import: BaseImport {
@nullable
@parse_field
renaming: DefId
}
|" Couple of imported entity name and renaming identifier.
class ImportedName: LktNode {
@parse_field
@@ -5259,14 +5255,32 @@ class ImportedName: LktNode {
renaming: DefId
}
|" Clause to import modules.
class Import: BaseImport {
@parse_field
imported_names: ASTList[ImportedName]
fun referenced_modules(): Array[Id] =
node.imported_names.map((n) => n.original_name.as[Id])
}
|" Clause to import declarations from another module.
class ImportFrom: BaseImport {
@parse_field
module_name: ModuleId
@parse_field
imported_names: ASTList[ImportedName]
fun referenced_modules(): Array[Id] = [node.module_name.as[Id]]
}
|" Clause to import all declarations from another module.
class ImportAllFrom: BaseImport {
@parse_field
module_name: ModuleId
fun referenced_modules(): Array[Id] = [node.module_name.as[Id]]
}
|" For the moment, root node of a lkt compilation unit.

View File

@@ -15,20 +15,17 @@ grammar lkt_grammar {
module_doc <- ?ModuleDocStringLit(
list+(ModuleDocStringLine(@ModuleDocStringLine))
)
imported_name <- ImportedName(
ImportedId(@Identifier)
?pick(@Identifier("as") / def_id)
imported_names <- list+(
ImportedName(
ImportedId(@Identifier)
/
?pick(@Identifier("as") / def_id)
),
","
)
import_clause <- or(
| Import(
"import"
/
module_id
?pick(@Identifier("as") / def_id)
)
| ImportFrom(
"from" module_id "import" list+(imported_name, ",")
)
| Import("import" / imported_names)
| ImportFrom("from" module_id "import" imported_names)
| ImportAllFrom("from" / module_id "import" "*")
)
imports <- list*(import_clause)