Parse anonymous function definitions

This commit is contained in:
Joscha 2022-11-19 19:13:30 +01:00
parent 9a1bb92dfe
commit ff3edf17e5
4 changed files with 73 additions and 4 deletions

View file

@ -2,10 +2,75 @@ use chumsky::prelude::*;
use crate::ast::{Expr, FuncDef};
use super::basic::Error;
use super::basic::{ident, space, Error};
use super::table_destr::table_pattern;
fn func_def_anon_no_arg(
expr: impl Parser<char, Expr, Error = Error>,
) -> impl Parser<char, FuncDef, Error = Error> {
text::keyword("function")
.ignore_then(space())
.then_ignore(just('('))
.then(space())
.then_ignore(just(')'))
.then(space())
.then(expr)
.map_with_span(|(((s0, s1), s2), body), span| FuncDef::AnonNoArg {
s0,
s1,
s2,
body: Box::new(body),
span,
})
}
fn func_def_anon_arg(
expr: impl Parser<char, Expr, Error = Error>,
) -> impl Parser<char, FuncDef, Error = Error> {
text::keyword("function")
.ignore_then(space())
.then_ignore(just('('))
.then(space())
.then(ident())
.then(space())
.then_ignore(just(')'))
.then(space())
.then(expr)
.map_with_span(
|(((((s0, s1), arg), s2), s3), body), span| FuncDef::AnonArg {
s0,
s1,
arg,
s2,
s3,
body: Box::new(body),
span,
},
)
}
fn func_def_anon_destr(
expr: impl Parser<char, Expr, Error = Error>,
) -> impl Parser<char, FuncDef, Error = Error> {
text::keyword("function")
.ignore_then(space())
.then(table_pattern())
.then(space())
.then(expr)
.map_with_span(|(((s0, pattern), s1), body), span| FuncDef::AnonDestr {
s0,
pattern,
s1,
body: Box::new(body),
span,
})
}
pub fn func_def(
expr: impl Parser<char, Expr, Error = Error>,
expr: impl Parser<char, Expr, Error = Error> + Clone + 'static,
) -> BoxedParser<'static, char, FuncDef, Error> {
todo().boxed()
func_def_anon_no_arg(expr.clone())
.or(func_def_anon_arg(expr.clone()))
.or(func_def_anon_destr(expr))
.boxed()
}