refactor: style fixes in lexer and parser

This commit is contained in:
Guy C 2025-02-07 11:23:18 +00:00
parent ee1ea6c63b
commit 8583a815a8
2 changed files with 13 additions and 7 deletions

View File

@ -33,7 +33,7 @@ val errConfig = new ErrorConfig {
"false" -> Label("boolean literal"),
"true" -> Label("boolean literal"),
"=" -> Label("assignment"),
"[" -> Label("array index"),
"[" -> Label("array index")
)
}
object lexer {
@ -88,7 +88,7 @@ object lexer {
(lexer.nonlexeme.character.ascii).map(c => s"character literal \'$c\'"),
lexer.nonlexeme.string.ascii.map(s => s"string literal \"$s\""),
// lexer.nonlexeme.symbol("()").as("function call, bruh use keyword 'call' to call functions"),
character.whitespace.map(_ => ""),
character.whitespace.map(_ => "")
) ++ desc.symbolDesc.hardKeywords.map { k =>
lexer.nonlexeme.symbol(k).as(s"keyword $k")
}

View File

@ -53,7 +53,6 @@ object parser {
val _parensCheck =
char('(').verifiedExplain("use keyword 'call' to call functions")
implicit val builder: ErrorBuilder[String] = new DefaultErrorBuilder with LexToken {
def tokens = errTokens
}
@ -123,9 +122,13 @@ object parser {
// Statements
private lazy val `<program>` = Program(
"begin" ~> many(
atomic((`<type>`.label("function declaration") <~> `<ident>` <~ "(")) <**> `<partial-func-decl>`
atomic(
(`<type>`.label("function declaration") <~> `<ident>` <~ "(")
) <**> `<partial-func-decl>`
).label("function declaration"),
((`<ident>` <~ "(")*> fail("function is missing return type") | `<stmt>`.label("main program body")) <~ "end"
((`<ident>` <~ "(") *> fail("function is missing return type") | `<stmt>`.label(
"main program body"
)) <~ "end"
)
private lazy val `<partial-func-decl>` =
FuncDecl(
@ -156,8 +159,11 @@ object parser {
)
| While("while" ~> `<expr>`.labelWithType(LabelType.Expr) <~ "do", `<stmt>` <~ "done")
| Block("begin" ~> `<stmt>` <~ "end")
| VarDecl(`<type>`, `<ident>` <~ "=".explain("functions must be defined on top of the main block"),
`<rvalue>`.label("valid initial value for variable"))
| VarDecl(
`<type>`,
`<ident>` <~ "=".explain("functions must be defined on top of the main block"),
`<rvalue>`.label("valid initial value for variable")
)
| Assign(`<lvalue>` <~ "=", `<rvalue>`)
private lazy val `<lvalue>`: Parsley[LValue] =
`<pair-elem>` | `<ident-or-array-elem>`