Skip to content

Commit

Permalink
improve parser error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Glowman554 committed May 27, 2024
1 parent 75f3808 commit 475ea55
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
47 changes: 47 additions & 0 deletions fire/firestorm/lexer/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package lexer

import "strconv"

var debug = map[TokenType]string{
ID: "identifier",
LPAREN: "(",
RPAREN: ")",
LBRACE: "{",
RBRACE: "}",
LBRACKET: "[",
RBRACKET: "]",
DIVIDE: "/",
COMMA: ",",
ARROW: "->",
STRING: "string",
ASSIGN: "=",
PLUS: "+",
MINUS: "-",
MULTIPLY: "*",
NUMBER: "number",
MODULO: "%",
XOR: "^",
AND: "&",
OR: "|",
SHIFT_LEFT: "<<",
SHIFT_RIGHT: ">>",
BIT_NOT: "~",
END_OF_LINE: ";",
EQUALS: "==",
NOT_EQUALS: "!=",
LESS: "<",
LESS_EQUALS: "<=",
MORE: ">",
MORE_EQUALS: ">=",
NOT: "!",
INCREASE: "++",
DECREASE: "--",
}

func ToString(token TokenType) string {
if str, ok := debug[token]; ok {
return str
} else {
return strconv.Itoa(int(token))
}
}
4 changes: 2 additions & 2 deletions fire/firestorm/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (p *Parser) error(message string, pos int) {

func (p *Parser) expect(tokenType lexer.TokenType) {
if p.current.Type != tokenType {
p.error("Expected "+strconv.Itoa(int(tokenType))+" but was "+strconv.Itoa(int(p.current.Type)), p.current.Pos)
p.error("Expected "+lexer.ToString(tokenType)+" but was "+lexer.ToString(p.current.Type), p.current.Pos)
}
}

Expand All @@ -74,7 +74,7 @@ func (p *Parser) commaOrRparen() bool {
p.advance()
return true
} else {
p.error("Unexpected "+strconv.Itoa(int(p.current.Type)), p.current.Pos)
p.error("Unexpected "+lexer.ToString(p.current.Type), p.current.Pos)
}
panic("?")
}
Expand Down

0 comments on commit 475ea55

Please sign in to comment.