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

View File

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