Implement import aliasing #2231
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
When working with
C3
modules, I found myself wanting to use shorter or more descriptive names for imported modules.This feature request adds Python/TypeScript-style import aliasing to
C3
, allowing developers to import modules with custom aliases.Changes
This PR implements
import as
syntax that allows aliasing imported modules:Key Implementation Details
TOKEN_AS
keyword: Added to lexer and parser infrastructureImportDecl
&Module_
structure: Addedalias
field to store the alias nameparse_import()
to handle optionalas alias_name
syntaxsema_find_decl_by_alias()
to resolve symbols through aliasesFeatures Supported
import std::io as console;
import std::ascii as a, std::io as foo;
import std::ascii, std::io as foo;
Testing
Added tests covering:
import_as_works.c3t
)import_as_list_works.c3t
)import_as_error_ident.c3t
)import_as_error_keyword.c3t
)Implementation Notes
Concerns
C3
is allowing redundant module names and is handling ambiguous declarations so I didn't add a check for redundant import aliases