diff --git a/schala-lang/src/parsing/combinator.rs b/schala-lang/src/parsing/combinator.rs index 28ffae8..75dd493 100644 --- a/schala-lang/src/parsing/combinator.rs +++ b/schala-lang/src/parsing/combinator.rs @@ -95,28 +95,28 @@ pub fn program(input: Span) -> ParseResult { ), )(input)?; - println!("REST: {}", rest.fragment()); - let ast = AST { id, statements }; Ok((rest, ast)) } -pub fn block(input: Span) -> ParseResult { - context( - "block", - map( - delimited( - tok(char('{')), - tuple(( - many0(statement_delimiter), - separated_list0(statement_delimiter, statement), - many0(statement_delimiter), - )), - tok(char('}')), - ), - |(_, items, _)| items.into(), +fn block_template<'a, O>(input_parser: impl Parser, O, VerboseError>>) -> impl FnMut(Span<'a>) -> +IResult, Vec, VerboseError>> { + map( + delimited( + tok(char('{')), + tuple(( + many0(statement_delimiter), + separated_list0(statement_delimiter, input_parser), + many0(statement_delimiter), + )), + tok(char('}')), ), - )(input) + |(_, items, _)| items, + ) +} + +pub fn block(input: Span) -> ParseResult { + map(block_template(statement), |items| items.into())(input) } fn statement(input: Span) -> ParseResult { @@ -151,18 +151,7 @@ fn implementation(input: Span) -> ParseResult { } fn decl_block(input: Span) -> ParseResult> { - delimited( - tok(char('{')), - map( - tuple(( - many0(statement_delimiter), - separated_list0(statement_delimiter, func_decl), - many0(statement_delimiter), - )), - |(_, signatures, _)| signatures, - ), - tok(char('}')), - )(input) + block_template(func_decl)(input) } fn interface(input: Span) -> ParseResult { @@ -171,20 +160,8 @@ fn interface(input: Span) -> ParseResult { })(input) } -//TODO make the blocks parameterizable fn signature_block(input: Span) -> ParseResult> { - delimited( - tok(char('{')), - map( - tuple(( - many0(statement_delimiter), - separated_list0(statement_delimiter, func_signature), - many0(statement_delimiter), - )), - |(_, signatures, _)| signatures, - ), - tok(char('}')), - )(input) + block_template(func_signature)(input) } fn annotation(input: Span) -> ParseResult {