Use: add alias use foo as bar form

This commit is contained in:
2025-12-20 06:09:31 -05:00
parent f551e3aef3
commit a836de32c7
6 changed files with 21 additions and 3 deletions

View File

@@ -178,6 +178,7 @@ impl<'a> Visit<'a> for ToLisp {
match item {
Use::Glob => print!("(use-glob)"),
Use::Name(name) => name.visit_in(self)?,
Use::Alias(name, alias) => print!("(use-alias {name} {alias})"),
Use::Path(name, tree) => {
print!("(use-path {name} ");
tree.visit_in(self)?;

View File

@@ -42,7 +42,7 @@ pub enum Expr<A: Annotation = Span> {
Lit(Literal),
/// use Use
Use(Use),
/// `(let | const | static) Pat::NoTopAlt (= expr (else expr)?)?` |
/// `let Pat::NoTopAlt (= expr (else expr)?)?` |
/// `(fn | mod | impl) Pat::Fn Expr`
Bind(Box<Bind<A>>),
/// Expr { (Ident (: Expr)?),* }
@@ -169,6 +169,8 @@ pub enum Use {
Glob,
/// Identifier
Name(String),
/// Identifier as Identifier
Alias(String, String),
/// Identifier :: Use
Path(String, Box<Use>),
/// { Use, * }

View File

@@ -158,6 +158,7 @@ impl Display for Use {
match self {
Self::Glob => "*".fmt(f),
Self::Name(name) => name.fmt(f),
Self::Alias(name, alias) => write!(f, "{name} as {alias}"),
Self::Path(segment, rest) => write!(f, "{segment}::{rest}"),
Self::Tree(items) => match items.len() {
0 => "{}".fmt(f),

View File

@@ -131,6 +131,7 @@ impl<A: Annotation> Foldable<A> for Use {
Ok(match self {
Self::Glob => Self::Glob,
Self::Name(name) => Self::Name(name),
Self::Alias(name, alias) => Self::Alias(name, alias),
Self::Path(name, rest) => Self::Path(name, rest.fold_in(folder)?),
Self::Tree(items) => Self::Tree(items.fold_in(folder)?),
})

View File

@@ -89,6 +89,10 @@ impl<'a> Walk<'a> for Use {
match self {
Self::Glob => Ok(()),
Self::Name(name) => name.visit_in(v),
Self::Alias(name, alias) => {
name.visit_in(v)?;
alias.visit_in(v)
}
Self::Path(name, rest) => {
name.visit_in(v)?;
rest.visit_in(v)

View File

@@ -235,8 +235,17 @@ impl<'t> Parse<'t> for Use {
TKind::Star => p.then(Use::Glob),
TKind::Identifier => {
let name = tok.lexeme.string().expect("should have String");
match p.next_if(TKind::ColonColon).allow_eof()? {
Some(Ok(_)) => Use::Path(name, p.parse(())?),
match p.peek().map(Token::kind).allow_eof()? {
Some(TKind::ColonColon) => Use::Path(name, p.consume().parse(())?),
Some(TKind::As) => Use::Alias(
name,
p.consume()
.next_if(TKind::Identifier)?
.map_err(|e| ParseError::Expected(TKind::Identifier, e, p.span()))?
.lexeme
.string()
.expect("Identifier should have string"),
),
_ => Use::Name(name),
}
}