ww: add file-scoped import aliases

This commit is contained in:
2026-08-14 10:56:34 +09:00
parent 351f4a25bc
commit 792b6ecbe5
14 changed files with 587 additions and 430 deletions

View File

@@ -1315,30 +1315,40 @@ parseblock(Parser *p)
return n;
}
/* `import encoding.utf8;` — the driver resolves the dotted path to a
* directory; the checker only needs the leaf (`utf8`) as the module
* bareword for n_use→decl disambiguation, mirroring Hare's
* `use encoding::utf8;` → `utf8::name` (ref/hare/hare/ast/import.ha:7
* stores `ident: []str` but identifier-resolution uses the last
* component). */
/* The optional alias is source-local; the dotted path remains the dependency
* identity supplied to the package driver. */
static Node *
parseuse(Parser *p)
{
Pos pp = p->cur.pos;
expect(p, TK_USE);
Node *n = newnode(p->a, N_USE, pp);
/* M1 #22: accumulate the full dotted import path in usepath. `str`
* starts as its leaf; direct export metadata later installs the imported
* declaration's default name without changing canonical identity. */
const char *leaf = expectident(p);
const char *alias = NULL;
const char *first;
if (p->cur.kind == TK_UNDER) {
errorf(p->cur.pos, "blank import alias _ is not implemented");
p->errs++;
alias = "_";
advance(p);
first = expectident(p);
} else {
first = expectident(p);
if (p->cur.kind == TK_IDENT) {
alias = first;
first = expectident(p);
}
}
const char *leaf = first;
const char *path = leaf;
while (accept(p, TK_DOT)) {
leaf = expectident(p);
path = aprintf(p->a, "%s.%s", path, leaf);
}
n->str = leaf;
n->strlen = strlen(leaf);
n->str = alias ? alias : leaf;
n->strlen = strlen(n->str);
n->usesource = path;
n->usepath = path;
n->usealias = alias;
expect(p, TK_SEMI);
return n;
}