fix: make parser use only parsley parser bridge apply

This commit is contained in:
2025-02-05 04:47:43 +00:00
parent f3a59460ef
commit 5fffd01a6f
2 changed files with 51 additions and 47 deletions

View File

@@ -53,10 +53,7 @@ object parser {
private val `<ident>` = Ident(ident)
private lazy val `<ident-or-array-elem>` =
`<ident>` <**> (`<array-indices>` </> identity)
private val `<array-indices>` =
some("[" ~> `<expr>` <~ "]") map { indices =>
ArrayElem((_: Ident), indices)
}
private val `<array-indices>` = ArrayElem(some("[" ~> `<expr>` <~ "]"))
// Types
private lazy val `<type>`: Parsley[Type] =
@@ -64,7 +61,7 @@ object parser {
private val `<base-type>` =
(IntType from "int") | (BoolType from "bool") | (CharType from "char") | (StringType from "string")
private lazy val `<array-type>` =
countSome("[" ~> "]") map { cnt => ArrayType((_: Type), cnt) }
ArrayType(countSome("[" ~> "]"))
private val `<pair-type>` = "pair"
private val `<pair-elems-type>`: Parsley[PairType] = PairType(
"(" ~> `<pair-elem-type>` <~ ",",
@@ -72,7 +69,9 @@ object parser {
)
private lazy val `<pair-elem-type>` =
(`<base-type>` <**> (`<array-type>` </> identity)) |
`<pair-type>` ~> ((`<pair-elems-type>` <**> `<array-type>`) </> UntypedPairType)
((UntypedPairType from `<pair-type>`) <**>
((`<pair-elems-type>` <**> `<array-type>`)
.map(arr => (_: UntypedPairType) => arr) </> identity))
// Statements
private lazy val `<program>` = Program(
@@ -80,11 +79,12 @@ object parser {
`<stmt>` <~ "end"
)
private lazy val `<partial-func-decl>` =
(sepBy(`<param>`, ",") <~ ")" <~ "is" <~> `<stmt>`.guardAgainst {
case stmts if !stmts.isReturning => Seq("All functions must end in a returning statement")
} <~ "end") map { (params, stmt) =>
(FuncDecl((_: Type), (_: Ident), params, stmt)).tupled
}
FuncDecl(
sepBy(`<param>`, ",") <~ ")" <~ "is",
`<stmt>`.guardAgainst {
case stmts if !stmts.isReturning => Seq("All functions must end in a returning statement")
} <~ "end"
)
private lazy val `<param>` = Param(`<type>`, `<ident>`)
private lazy val `<stmt>`: Parsley[NonEmptyList[Stmt]] =
sepBy1(`<basic-stmt>`, ";")