Parser errors make more sense

This commit is contained in:
Sasha Koshka 2024-02-06 18:58:09 -05:00
parent c2d9b4d40f
commit f3f8b324dc
2 changed files with 16 additions and 9 deletions

View File

@ -64,8 +64,8 @@ func (this *Parser) ParseInto (tree *Tree) error {
func (this *Parser) expect (allowed ...lexer.TokenKind) error { func (this *Parser) expect (allowed ...lexer.TokenKind) error {
if !this.token.Is(allowed...) { if !this.token.Is(allowed...) {
return errors.Errorf ( return errors.Errorf (
this.token.Position, "expected %s", this.token.Position, "unexpected %v; expected %s",
commaList(allowed...)) this.token, commaList(allowed...))
} }
return nil return nil
} }
@ -76,8 +76,8 @@ func (this *Parser) expect (allowed ...lexer.TokenKind) error {
func (this *Parser) expectDesc (description string, allowed ...lexer.TokenKind) error { func (this *Parser) expectDesc (description string, allowed ...lexer.TokenKind) error {
if !this.token.Is(allowed...) { if !this.token.Is(allowed...) {
return errors.Errorf ( return errors.Errorf (
this.token.Position, "expected %s", this.token.Position, "unexpected %v; expected %s",
description) this.token, description)
} }
return nil return nil
} }
@ -101,8 +101,8 @@ func (this *Parser) expectNextDesc (description string, allowed ...lexer.TokenKi
func (this *Parser) expectValue (kind lexer.TokenKind, allowed ...string) error { func (this *Parser) expectValue (kind lexer.TokenKind, allowed ...string) error {
if !this.token.Is(kind) || !this.token.ValueIs(allowed...){ if !this.token.Is(kind) || !this.token.ValueIs(allowed...){
return errors.Errorf ( return errors.Errorf (
this.token.Position, "expected %s", this.token.Position, "unexpected %v; expected %s",
commaList(allowed)) this.token, commaList(allowed))
} }
return nil return nil
} }
@ -112,8 +112,8 @@ func (this *Parser) expectValue (kind lexer.TokenKind, allowed ...string) error
func (this *Parser) expectValueDesc (description string, kind lexer.TokenKind, allowed ...string) error { func (this *Parser) expectValueDesc (description string, kind lexer.TokenKind, allowed ...string) error {
if !this.token.Is(kind) || !this.token.ValueIs(allowed...) { if !this.token.Is(kind) || !this.token.ValueIs(allowed...) {
return errors.Errorf ( return errors.Errorf (
this.token.Position, "expected %s", this.token.Position, "unexpected %v; expected %s",
description) this.token, description)
} }
return nil return nil
} }

View File

@ -3,13 +3,20 @@ package parser
import "io" import "io"
import "testing" import "testing"
import "strings" import "strings"
import "git.tebibyte.media/sashakoshka/fspl/errors"
import "git.tebibyte.media/sashakoshka/fspl/testcommon" import "git.tebibyte.media/sashakoshka/fspl/testcommon"
func testString (test *testing.T, correct string, input string) { func testString (test *testing.T, correct string, input string) {
ast := Tree { } ast := Tree { }
err := ast.Parse("input.fspl", strings.NewReader(input)) err := ast.Parse("input.fspl", strings.NewReader(input))
if err != nil && err != io.EOF{ if err != nil && err != io.EOF{
test.Error("parser returned error:", err) if err, ok := err.(*errors.Error); ok {
test.Log("parser returned error:")
test.Log(err.Format())
test.Fail()
} else {
test.Error("parser returned error:", err)
}
return return
} }