Parse anonymous function definitions
This commit is contained in:
parent
9a1bb92dfe
commit
ff3edf17e5
4 changed files with 73 additions and 4 deletions
|
|
@ -2,6 +2,8 @@ use crate::span::{HasSpan, Span};
|
|||
|
||||
use super::{Expr, Ident, Space};
|
||||
|
||||
// TODO Make table patterns recursive
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum TablePatternElem {
|
||||
/// `foo`
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
//! made public later.
|
||||
|
||||
// TODO Turn multiple calls to subparsers into clone-s
|
||||
// TODO Remove unnecessary +Clone-s and 'static-s
|
||||
|
||||
mod basic;
|
||||
mod expr;
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ fn table_pattern_elem() -> impl Parser<char, TablePatternElem, Error = Error> +
|
|||
named.or(positional)
|
||||
}
|
||||
|
||||
fn table_pattern() -> impl Parser<char, TablePattern, Error = Error> + Clone {
|
||||
pub fn table_pattern() -> BoxedParser<'static, char, TablePattern, Error> {
|
||||
let elem = space()
|
||||
.then(table_pattern_elem())
|
||||
.then(space())
|
||||
|
|
@ -43,6 +43,7 @@ fn table_pattern() -> impl Parser<char, TablePattern, Error = Error> + Clone {
|
|||
trailing_comma,
|
||||
span,
|
||||
})
|
||||
.boxed()
|
||||
}
|
||||
|
||||
pub fn table_destr(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue