block template

This commit is contained in:
Greg Shuflin 2021-11-18 19:12:28 -08:00
parent d5cd0dada7
commit 6454cc5ad1

View File

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