This commit is contained in:
nora 2022-04-09 11:29:45 +02:00
parent 83676704e1
commit aa4da62e2c

View file

@ -130,34 +130,8 @@ fn expr_parser<'src>() -> impl Parser<Token<'src>, Expr, Error = Error<'src>> +
}) })
} }
fn name_ty_pair_parser<'src>() -> impl Parser<Token<'src>, NameTyPair, Error = Error<'src>> + Clone fn statement_parser<'src>() -> impl Parser<Token<'src>, Stmt, Error = Error<'src>> + Clone {
{ recursive(|stmt| {
ident_parser()
.then_ignore(just(Token::Colon))
.then(ty_parser())
.map_with_span(|(name, ty), span| NameTyPair { name, ty, span })
}
fn struct_parser<'src>() -> impl Parser<Token<'src>, StructDecl, Error = Error<'src>> + Clone {
let name = just(Token::Struct).ignore_then(ident_parser());
let fields = name_ty_pair_parser()
.separated_by(just(Token::Comma))
.delimited_by(just(Token::BraceO), just(Token::BraceC));
name.then(fields)
.map(|(name, fields)| StructDecl {
name,
fields,
span: Default::default(),
})
.labelled("struct")
}
fn item_parser<'src>() -> impl Parser<Token<'src>, Item, Error = Error<'src>> + Clone {
recursive(|item| {
// ---- statement
let var_decl = ty_parser() let var_decl = ty_parser()
.then(ident_parser()) .then(ident_parser())
.then_ignore(just(Token::Eq)) .then_ignore(just(Token::Eq))
@ -182,7 +156,6 @@ fn item_parser<'src>() -> impl Parser<Token<'src>, Item, Error = Error<'src>> +
}) })
}); });
let mut stmt = Recursive::declare();
let if_stmt = recursive(|if_stmt| { let if_stmt = recursive(|if_stmt| {
just(Token::If) just(Token::If)
.ignore_then(expr_parser()) .ignore_then(expr_parser())
@ -211,18 +184,37 @@ fn item_parser<'src>() -> impl Parser<Token<'src>, Item, Error = Error<'src>> +
}) })
}) })
.map(Stmt::IfStmt); .map(Stmt::IfStmt);
choice((var_decl, assignment, if_stmt, expr_parser().map(Stmt::Expr)))
.then_ignore(just(Token::Semi))
})
.labelled("statement")
}
stmt.define( fn name_ty_pair_parser<'src>() -> impl Parser<Token<'src>, NameTyPair, Error = Error<'src>> + Clone
choice(( {
var_decl, ident_parser()
assignment, .then_ignore(just(Token::Colon))
if_stmt, .then(ty_parser())
expr_parser().map(Stmt::Expr), .map_with_span(|(name, ty), span| NameTyPair { name, ty, span })
item.map(Stmt::Item), }
))
.then_ignore(just(Token::Semi)),
);
fn struct_parser<'src>() -> impl Parser<Token<'src>, StructDecl, Error = Error<'src>> + Clone {
let name = just(Token::Struct).ignore_then(ident_parser());
let fields = name_ty_pair_parser()
.separated_by(just(Token::Comma))
.delimited_by(just(Token::BraceO), just(Token::BraceC));
name.then(fields)
.map(|(name, fields)| StructDecl {
name,
fields,
span: Default::default(),
})
.labelled("struct")
}
fn item_parser<'src>() -> impl Parser<Token<'src>, Item, Error = Error<'src>> + Clone {
// ---- function // ---- function
let name = ident_parser(); let name = ident_parser();
@ -240,7 +232,8 @@ fn item_parser<'src>() -> impl Parser<Token<'src>, Item, Error = Error<'src>> +
.then(params) .then(params)
.then(ret_ty) .then(ret_ty)
.then( .then(
stmt.repeated() statement_parser()
.repeated()
.delimited_by(just(Token::BraceO), just(Token::BraceC)), .delimited_by(just(Token::BraceO), just(Token::BraceC)),
) )
.map(|((((fn_span, name), params), ret_ty), body)| FnDecl { .map(|((((fn_span, name), params), ret_ty), body)| FnDecl {
@ -257,7 +250,6 @@ fn item_parser<'src>() -> impl Parser<Token<'src>, Item, Error = Error<'src>> +
function function
.map(Item::FnDecl) .map(Item::FnDecl)
.or(struct_parser().map(Item::StructDecl)) .or(struct_parser().map(Item::StructDecl))
})
.labelled("item") .labelled("item")
} }