importStatement method
Parses an import <module> [as alias] statement
importStmt ::= "import" module ("as" alias)?
Implementation
Stmt importStatement() {
Token keyword = previous();
List<ImportSpecifier> imports = [];
do {
// module path: math.utils.os etc
List<String> path = [];
Token name = consume(TokenType.IDENTIFIER, "Expect module name.");
path.add(name.lexeme);
while (match([TokenType.DOT])) {
Token part = consume(TokenType.IDENTIFIER, "Expect module part.");
path.add(part.lexeme);
}
String? alias;
if (match([TokenType.AS])) {
Token aliasToken = consume(TokenType.IDENTIFIER, "Expect alias name.");
alias = aliasToken.lexeme;
}
imports.add(ImportSpecifier(path, alias));
} while (match([TokenType.COMMA]));
return ImportStmt(imports, keyword);
}