Use: add alias use foo as bar form
This commit is contained in:
@@ -178,6 +178,7 @@ impl<'a> Visit<'a> for ToLisp {
|
|||||||
match item {
|
match item {
|
||||||
Use::Glob => print!("(use-glob)"),
|
Use::Glob => print!("(use-glob)"),
|
||||||
Use::Name(name) => name.visit_in(self)?,
|
Use::Name(name) => name.visit_in(self)?,
|
||||||
|
Use::Alias(name, alias) => print!("(use-alias {name} {alias})"),
|
||||||
Use::Path(name, tree) => {
|
Use::Path(name, tree) => {
|
||||||
print!("(use-path {name} ");
|
print!("(use-path {name} ");
|
||||||
tree.visit_in(self)?;
|
tree.visit_in(self)?;
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ pub enum Expr<A: Annotation = Span> {
|
|||||||
Lit(Literal),
|
Lit(Literal),
|
||||||
/// use Use
|
/// use Use
|
||||||
Use(Use),
|
Use(Use),
|
||||||
/// `(let | const | static) Pat::NoTopAlt (= expr (else expr)?)?` |
|
/// `let Pat::NoTopAlt (= expr (else expr)?)?` |
|
||||||
/// `(fn | mod | impl) Pat::Fn Expr`
|
/// `(fn | mod | impl) Pat::Fn Expr`
|
||||||
Bind(Box<Bind<A>>),
|
Bind(Box<Bind<A>>),
|
||||||
/// Expr { (Ident (: Expr)?),* }
|
/// Expr { (Ident (: Expr)?),* }
|
||||||
@@ -169,6 +169,8 @@ pub enum Use {
|
|||||||
Glob,
|
Glob,
|
||||||
/// Identifier
|
/// Identifier
|
||||||
Name(String),
|
Name(String),
|
||||||
|
/// Identifier as Identifier
|
||||||
|
Alias(String, String),
|
||||||
/// Identifier :: Use
|
/// Identifier :: Use
|
||||||
Path(String, Box<Use>),
|
Path(String, Box<Use>),
|
||||||
/// { Use, * }
|
/// { Use, * }
|
||||||
|
|||||||
@@ -158,6 +158,7 @@ impl Display for Use {
|
|||||||
match self {
|
match self {
|
||||||
Self::Glob => "*".fmt(f),
|
Self::Glob => "*".fmt(f),
|
||||||
Self::Name(name) => name.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::Path(segment, rest) => write!(f, "{segment}::{rest}"),
|
||||||
Self::Tree(items) => match items.len() {
|
Self::Tree(items) => match items.len() {
|
||||||
0 => "{}".fmt(f),
|
0 => "{}".fmt(f),
|
||||||
|
|||||||
@@ -131,6 +131,7 @@ impl<A: Annotation> Foldable<A> for Use {
|
|||||||
Ok(match self {
|
Ok(match self {
|
||||||
Self::Glob => Self::Glob,
|
Self::Glob => Self::Glob,
|
||||||
Self::Name(name) => Self::Name(name),
|
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::Path(name, rest) => Self::Path(name, rest.fold_in(folder)?),
|
||||||
Self::Tree(items) => Self::Tree(items.fold_in(folder)?),
|
Self::Tree(items) => Self::Tree(items.fold_in(folder)?),
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -89,6 +89,10 @@ impl<'a> Walk<'a> for Use {
|
|||||||
match self {
|
match self {
|
||||||
Self::Glob => Ok(()),
|
Self::Glob => Ok(()),
|
||||||
Self::Name(name) => name.visit_in(v),
|
Self::Name(name) => name.visit_in(v),
|
||||||
|
Self::Alias(name, alias) => {
|
||||||
|
name.visit_in(v)?;
|
||||||
|
alias.visit_in(v)
|
||||||
|
}
|
||||||
Self::Path(name, rest) => {
|
Self::Path(name, rest) => {
|
||||||
name.visit_in(v)?;
|
name.visit_in(v)?;
|
||||||
rest.visit_in(v)
|
rest.visit_in(v)
|
||||||
|
|||||||
@@ -235,8 +235,17 @@ impl<'t> Parse<'t> for Use {
|
|||||||
TKind::Star => p.then(Use::Glob),
|
TKind::Star => p.then(Use::Glob),
|
||||||
TKind::Identifier => {
|
TKind::Identifier => {
|
||||||
let name = tok.lexeme.string().expect("should have String");
|
let name = tok.lexeme.string().expect("should have String");
|
||||||
match p.next_if(TKind::ColonColon).allow_eof()? {
|
match p.peek().map(Token::kind).allow_eof()? {
|
||||||
Some(Ok(_)) => Use::Path(name, p.parse(())?),
|
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),
|
_ => Use::Name(name),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user