This commit is contained in:
Greg Shuflin 2021-11-19 01:01:26 -08:00
parent 88d2571401
commit 7ae7eaa07b
2 changed files with 26 additions and 10 deletions

View File

@ -86,7 +86,7 @@ pub fn program(input: Span) -> ParseResult<AST> {
map( map(
tuple(( tuple((
many0(statement_delimiter), many0(statement_delimiter),
separated_list0(statement_delimiter, statement), separated_list0(many1(statement_delimiter), statement),
many0(statement_delimiter), many0(statement_delimiter),
)), )),
|(_, items, _)| items.into(), |(_, items, _)| items.into(),
@ -105,7 +105,7 @@ fn block_template<'a, O>(
tok(char('{')), tok(char('{')),
tuple(( tuple((
many0(statement_delimiter), many0(statement_delimiter),
separated_list0(statement_delimiter, input_parser), separated_list0(many1(statement_delimiter), input_parser),
many0(statement_delimiter), many0(statement_delimiter),
)), )),
tok(char('}')), tok(char('}')),
@ -124,11 +124,23 @@ fn statement(input: Span) -> ParseResult<Statement> {
let id = fresh_id(&input); let id = fresh_id(&input);
let (rest, kind) = context( let (rest, kind) = context(
"Parsing-statement", "Parsing-statement",
alt((map(declaration, StatementKind::Declaration), map(expression, StatementKind::Expression))), alt((
map(flow, StatementKind::Flow),
map(declaration, StatementKind::Declaration),
map(expression, StatementKind::Expression),
)),
)(input)?; )(input)?;
Ok((rest, Statement { id, location, kind })) Ok((rest, Statement { id, location, kind }))
} }
fn flow(input: Span) -> ParseResult<FlowControl> {
alt((
map(kw("continue"), |_| FlowControl::Continue),
map(kw("break"), |_| FlowControl::Break),
map(preceded(kw("return"), opt(expression)), FlowControl::Return),
))(input)
}
fn declaration(input: Span) -> ParseResult<Declaration> { fn declaration(input: Span) -> ParseResult<Declaration> {
alt((binding, type_decl, func, annotation, module, interface, implementation))(input) alt((binding, type_decl, func, annotation, module, interface, implementation))(input)
} }
@ -489,10 +501,14 @@ fn record_block(input: Span) -> ParseResult<Vec<(Rc<String>, Expression)>> {
fn lambda_expr(input: Span) -> ParseResult<ExpressionKind> { fn lambda_expr(input: Span) -> ParseResult<ExpressionKind> {
alt(( alt((
map(preceded(tok(char('\\')), tuple((formal_params, opt(type_anno), block))), map(
|(params, type_anno, body)| ExpressionKind::Lambda { params, type_anno, body }), preceded(tok(char('\\')), tuple((formal_params, opt(type_anno), block))),
map(preceded(tok(char('\\')), tuple((formal_param, opt(type_anno), block))), |(params, type_anno, body)| ExpressionKind::Lambda { params, type_anno, body },
|(param, type_anno, body)| ExpressionKind::Lambda { params: vec![param], type_anno, body }), ),
map(
preceded(tok(char('\\')), tuple((formal_param, opt(type_anno), block))),
|(param, type_anno, body)| ExpressionKind::Lambda { params: vec![param], type_anno, body },
),
))(input) ))(input)
} }

View File

@ -1289,7 +1289,7 @@ if (45, "panda", false, 2.2) {
fn flow_control() { fn flow_control() {
use ExpressionKind::*; use ExpressionKind::*;
// This is an incorrect program, but shoudl parse correctly. // This is an incorrect program, but should parse correctly.
let source = r#" let source = r#"
fn test() { fn test() {
let a = 10; let a = 10;
@ -1299,7 +1299,7 @@ fn flow_control() {
return 10; return 10;
}"#; }"#;
assert_ast!( assert_ast_comb!(
source, source,
vec![fn_decl( vec![fn_decl(
Signature { name: rc("test"), operator: false, type_anno: None, params: vec![] }, Signature { name: rc("test"), operator: false, type_anno: None, params: vec![] },
@ -1337,7 +1337,7 @@ fn blocks() {
fn foo() { } fn foo() { }
} }
}"#; }"#;
let block = parser.block(source); let block = parser.block_comb(source);
assert_eq!( assert_eq!(
block.unwrap(), block.unwrap(),
vec![decl(Declaration::FuncDecl( vec![decl(Declaration::FuncDecl(