cl-ast: Add inline closure expressions
This commit is contained in:
@@ -97,6 +97,7 @@ pub enum Parsing {
|
||||
|
||||
Expr,
|
||||
ExprKind,
|
||||
Closure,
|
||||
Assign,
|
||||
AssignKind,
|
||||
Binary,
|
||||
@@ -214,6 +215,7 @@ impl Display for Parsing {
|
||||
|
||||
Parsing::Expr => "an expression",
|
||||
Parsing::ExprKind => "an expression",
|
||||
Parsing::Closure => "an anonymous function",
|
||||
Parsing::Assign => "an assignment",
|
||||
Parsing::AssignKind => "an assignment operator",
|
||||
Parsing::Binary => "a binary expression",
|
||||
|
||||
@@ -884,6 +884,31 @@ impl Parse<'_> for Expr {
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse<'_> for Closure {
|
||||
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
|
||||
let args = sep(
|
||||
Pattern::parse,
|
||||
TokenKind::Comma,
|
||||
TokenKind::Bar,
|
||||
Parsing::Closure,
|
||||
);
|
||||
|
||||
let arg = match p.peek_kind(Parsing::Closure)? {
|
||||
TokenKind::BarBar => {
|
||||
p.consume_peeked();
|
||||
Box::new(Pattern::Tuple(vec![]))
|
||||
}
|
||||
_ => Box::new(delim(
|
||||
|p| args(p).map(Pattern::Tuple),
|
||||
(TokenKind::Bar, TokenKind::Bar),
|
||||
Parsing::Closure,
|
||||
)(p)?),
|
||||
};
|
||||
let body = p.parse()?;
|
||||
Ok(Closure { arg, body })
|
||||
}
|
||||
}
|
||||
|
||||
impl Parse<'_> for Quote {
|
||||
fn parse(p: &mut Parser<'_>) -> PResult<Self> {
|
||||
let quote = delim(
|
||||
@@ -1104,6 +1129,12 @@ impl Parse<'_> for MatchArm {
|
||||
fn ret_body(p: &mut Parser, while_parsing: Parsing) -> PResult<Option<Box<Expr>>> {
|
||||
Ok(match p.peek_kind(while_parsing)? {
|
||||
TokenKind::Semi => None,
|
||||
_ => Some(Expr::parse(p)?.into()),
|
||||
_ => Some(p.parse()?),
|
||||
})
|
||||
}
|
||||
|
||||
impl<'t, P: Parse<'t>> Parse<'t> for Box<P> {
|
||||
fn parse(p: &mut Parser<'t>) -> PResult<Self> {
|
||||
p.parse().map(Box::new)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ pub fn expr(p: &mut Parser, power: u8) -> PResult<Expr> {
|
||||
literal_like!() => Literal::parse(p)?.into(),
|
||||
path_like!() => exprkind_pathlike(p)?,
|
||||
TokenKind::Amp | TokenKind::AmpAmp => AddrOf::parse(p)?.into(),
|
||||
TokenKind::Bar | TokenKind::BarBar => Closure::parse(p)?.into(),
|
||||
TokenKind::Grave => Quote::parse(p)?.into(),
|
||||
TokenKind::LCurly => Block::parse(p)?.into(),
|
||||
TokenKind::LBrack => exprkind_arraylike(p)?,
|
||||
|
||||
Reference in New Issue
Block a user