From f3f8b324dca09f0c1caae80e70e58e23a5bf7030 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Tue, 6 Feb 2024 18:58:09 -0500 Subject: [PATCH] Parser errors make more sense --- parser/parser.go | 16 ++++++++-------- parser/test-common.go | 9 ++++++++- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index f9e9d92..9fb8f14 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -64,8 +64,8 @@ func (this *Parser) ParseInto (tree *Tree) error { func (this *Parser) expect (allowed ...lexer.TokenKind) error { if !this.token.Is(allowed...) { return errors.Errorf ( - this.token.Position, "expected %s", - commaList(allowed...)) + this.token.Position, "unexpected %v; expected %s", + this.token, commaList(allowed...)) } return nil } @@ -76,8 +76,8 @@ func (this *Parser) expect (allowed ...lexer.TokenKind) error { func (this *Parser) expectDesc (description string, allowed ...lexer.TokenKind) error { if !this.token.Is(allowed...) { return errors.Errorf ( - this.token.Position, "expected %s", - description) + this.token.Position, "unexpected %v; expected %s", + this.token, description) } 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 { if !this.token.Is(kind) || !this.token.ValueIs(allowed...){ return errors.Errorf ( - this.token.Position, "expected %s", - commaList(allowed)) + this.token.Position, "unexpected %v; expected %s", + this.token, commaList(allowed)) } 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 { if !this.token.Is(kind) || !this.token.ValueIs(allowed...) { return errors.Errorf ( - this.token.Position, "expected %s", - description) + this.token.Position, "unexpected %v; expected %s", + this.token, description) } return nil } diff --git a/parser/test-common.go b/parser/test-common.go index b011015..1dd835b 100644 --- a/parser/test-common.go +++ b/parser/test-common.go @@ -3,13 +3,20 @@ package parser import "io" import "testing" import "strings" +import "git.tebibyte.media/sashakoshka/fspl/errors" import "git.tebibyte.media/sashakoshka/fspl/testcommon" func testString (test *testing.T, correct string, input string) { ast := Tree { } err := ast.Parse("input.fspl", strings.NewReader(input)) 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 }