feat: detect function declarations mid-body

This commit is contained in:
Gleb Koval 2025-02-07 15:39:04 +00:00
parent af909b1492
commit 0955f19a81
Signed by: cyclane
GPG Key ID: 15E168A8B332382C

View File

@ -6,6 +6,7 @@ import parsley.Parsley.{atomic, many, notFollowedBy, pure}
import parsley.combinator.{countSome, sepBy} import parsley.combinator.{countSome, sepBy}
import parsley.expr.{precedence, SOps, InfixL, InfixN, InfixR, Prefix, Atoms} import parsley.expr.{precedence, SOps, InfixL, InfixN, InfixR, Prefix, Atoms}
import parsley.errors.combinator._ import parsley.errors.combinator._
import parsley.errors.patterns.VerifiedErrors
import parsley.syntax.zipped._ import parsley.syntax.zipped._
import parsley.cats.combinator.{some} import parsley.cats.combinator.{some}
import cats.data.NonEmptyList import cats.data.NonEmptyList
@ -126,7 +127,7 @@ object parser {
FuncDecl( FuncDecl(
sepBy(`<param>`, ",") <~ ")" <~ "is", sepBy(`<param>`, ",") <~ ")" <~ "is",
`<stmt>`.guardAgainst { `<stmt>`.guardAgainst {
case stmts if !stmts.isReturning => Seq("All functions must end in a returning statement") case stmts if !stmts.isReturning => Seq("all functions must end in a returning statement")
} <~ "end" } <~ "end"
) )
private lazy val `<param>` = Param(`<type>`, `<ident>`) private lazy val `<param>` = Param(`<type>`, `<ident>`)
@ -151,7 +152,13 @@ object parser {
) )
| While("while" ~> `<expr>`.labelWithType(LabelType.Expr) <~ "do", `<stmt>` <~ "done") | While("while" ~> `<expr>`.labelWithType(LabelType.Expr) <~ "do", `<stmt>` <~ "done")
| Block("begin" ~> `<stmt>` <~ "end") | Block("begin" ~> `<stmt>` <~ "end")
| VarDecl(`<type>`, `<ident>` <~ "=", `<rvalue>`.label("valid initial value for variable")) | VarDecl(
`<type>`,
`<ident>` <~ ("=" | "(".verifiedExplain(
"all function declarations must be above the main program body"
)),
`<rvalue>`.label("valid initial value for variable")
)
// TODO: Can we inline the name of the variable in the message // TODO: Can we inline the name of the variable in the message
| Assign(`<lvalue>` <~ "=", `<rvalue>`) | Assign(`<lvalue>` <~ "=", `<rvalue>`)
private lazy val `<lvalue>`: Parsley[LValue] = private lazy val `<lvalue>`: Parsley[LValue] =