fix: extract <ident> from <array-elem>

This commit is contained in:
Gleb Koval 2025-02-01 20:29:04 +00:00
parent c5b02a00aa
commit f5f6628c89
Signed by: cyclane
GPG Key ID: 15E168A8B332382C

View File

@ -2,7 +2,7 @@ package wacc
import parsley.Result import parsley.Result
import parsley.Parsley import parsley.Parsley
import parsley.Parsley.{atomic, many, pure} import parsley.Parsley.{atomic, many, pure, some}
import parsley.combinator.{countSome, sepBy, sepBy1} import parsley.combinator.{countSome, sepBy, sepBy1}
import parsley.expr.{precedence, SOps, InfixL, InfixN, InfixR, Prefix, Atoms} import parsley.expr.{precedence, SOps, InfixL, InfixN, InfixR, Prefix, Atoms}
@ -44,13 +44,16 @@ object parser {
CharLiter(charLit), CharLiter(charLit),
StrLiter(stringLit), StrLiter(stringLit),
PairLiter from "null", PairLiter from "null",
`<ident>`, `<ident-or-array-elem>`,
`<array-elem>`,
Parens("(" ~> `<expr>` <~ ")") Parens("(" ~> `<expr>` <~ ")")
) )
private val `<ident>` = Ident(ident) private val `<ident>` = Ident(ident)
private lazy val `<array-elem>` = private lazy val `<ident-or-array-elem>` =
ArrayElem(`<ident>` <~ "[", sepBy1(`<expr>`, "]" ~> "[") <~ "]") `<ident>` <**> (`<array-indices>` </> identity)
private val `<array-indices>` =
some("[" ~> `<expr>` <~ "]") map { indices =>
ArrayElem((_: Ident), indices)
}
// Types // Types
private lazy val `<type>`: Parsley[Type] = private lazy val `<type>`: Parsley[Type] =
@ -80,7 +83,8 @@ object parser {
`<stmt>` <~ "end" `<stmt>` <~ "end"
) )
private lazy val `<param>` = Param(`<type>`, `<ident>`) private lazy val `<param>` = Param(`<type>`, `<ident>`)
private lazy val `<stmt>`: Parsley[List[Stmt]] = sepBy1(`<basic-stmt>`, ";") private lazy val `<stmt>`: Parsley[List[Stmt]] =
sepBy1(`<basic-stmt>`, ";")
private lazy val `<basic-stmt>` = private lazy val `<basic-stmt>` =
(Skip from atomic("skip")) (Skip from atomic("skip"))
| Read(atomic("read") ~> `<lvalue>`) | Read(atomic("read") ~> `<lvalue>`)
@ -99,7 +103,7 @@ object parser {
| VarDecl(atomic(`<type>`), `<ident>` <~ "=", `<rvalue>`) | VarDecl(atomic(`<type>`), `<ident>` <~ "=", `<rvalue>`)
| Assign(`<ident>` <~ "=", `<rvalue>`) | Assign(`<ident>` <~ "=", `<rvalue>`)
private lazy val `<lvalue>`: Parsley[LValue] = private lazy val `<lvalue>`: Parsley[LValue] =
atomic(`<pair-elem>`) | atomic(`<array-elem>`) | `<ident>` atomic(`<pair-elem>`) | atomic(`<ident-or-array-elem>`)
private lazy val `<rvalue>`: Parsley[RValue] = private lazy val `<rvalue>`: Parsley[RValue] =
atomic(`<array-liter>`) | atomic(`<array-liter>`) |
atomic( atomic(