From c3883cbb0d368c35d7ce4becf4d7c18e8423637f Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 4 Sep 2023 13:48:08 +0000 Subject: [PATCH 1/3] parser: introduce NewError() to create lexer.Error using lexer.Position Signed-off-by: Alejandro Mery --- parser/error.go | 16 ++++++++++++++++ parser/parser.go | 8 +------- 2 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 parser/error.go diff --git a/parser/error.go b/parser/error.go new file mode 100644 index 0000000..45aa254 --- /dev/null +++ b/parser/error.go @@ -0,0 +1,16 @@ +package parser + +import ( + "asciigoat.org/core/lexer" +) + +// NewError creates a lexer.Error using a lexer.Position +func NewError(pos lexer.Position, content, hint string, err error) *lexer.Error { + return &lexer.Error{ + Line: pos.Line, + Column: pos.Column, + Content: content, + Hint: hint, + Err: err, + } +} diff --git a/parser/parser.go b/parser/parser.go index 570e766..5e4c5b8 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -31,13 +31,7 @@ func defaultOnToken(pos lexer.Position, typ TokenType, value string) error { func defaultOnError(pos lexer.Position, content string, err error) error { log.Printf("%s:%v:%v: %q: %s", "error", pos.Line, pos.Column, content, err) - return lexer.Error{ - Line: pos.Line, - Column: pos.Column, - - Content: content, - Err: err, - } + return NewError(pos, content, "", err) } func (p *Parser) setDefaults() { -- 2.17.1 From d316031c445f03dfbe0b61863149ab99530b0561 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 4 Sep 2023 13:50:08 +0000 Subject: [PATCH 2/3] basic: cleanup using parser.NewError() Signed-off-by: Alejandro Mery --- basic/error.go | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/basic/error.go b/basic/error.go index fec162d..0f74179 100644 --- a/basic/error.go +++ b/basic/error.go @@ -4,6 +4,7 @@ import ( "errors" "asciigoat.org/core/lexer" + "asciigoat.org/ini/parser" ) var ( @@ -11,23 +12,11 @@ var ( ) func newErrInvalidToken(t *token) *lexer.Error { - err := &lexer.Error{ - Line: t.pos.Line, - Column: t.pos.Column, - Content: t.value, - Err: errInvalidToken, - } - return err + return parser.NewError(t.pos, t.value, "", errInvalidToken) } func (dec *decoder) OnError(pos lexer.Position, content string, err error) error { - err = &lexer.Error{ - Line: pos.Line, - Column: pos.Column, - Content: content, - Err: err, - } - + err = parser.NewError(pos, content, "", err) dec.executeFinal() return err } -- 2.17.1 From 8f3e59ec3649b08fab5842336b0e536d7d38c497 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 4 Sep 2023 13:52:34 +0000 Subject: [PATCH 3/3] parser: introduce ErrPlusPosition to apply a position offset to a lexer.Error Signed-off-by: Alejandro Mery --- parser/error.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/parser/error.go b/parser/error.go index 45aa254..3cc5a53 100644 --- a/parser/error.go +++ b/parser/error.go @@ -14,3 +14,14 @@ func NewError(pos lexer.Position, content, hint string, err error) *lexer.Error Err: err, } } + +// ErrPlusPosition returns a copy of the given [lexer.Error] +// offsetting the Line/Column information. +func ErrPlusPosition(pos lexer.Position, e *lexer.Error) *lexer.Error { + pos.Add(lexer.Position{ + Line: e.Line, + Column: e.Column, + }) + + return NewError(pos, e.Content, e.Hint, e.Err) +} -- 2.17.1