5 Commits

Author SHA1 Message Date
amery 5f816300f6 Merge pull request 'lexer: introduce Position.Add()' (#13)
Reviewed-on: #13
2023-09-04 15:18:27 +02:00
amery 5f81eb0ea5 Merge pull request 'lexer: add Hint to Error, which is expanded as "%s" instead of "%q"' (#12)
Reviewed-on: #12
2023-09-04 15:15:03 +02:00
amery ce75299e74 lexer: add Hint to Error, which is expanded as "%s" instead of "%q"
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-03 16:34:56 +00:00
amery 1d7ee69ab7 lexer: refactor Error.Error()'s prefix generator
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-03 16:34:56 +00:00
amery ec0fc8e508 lexer: introduce Position.Add()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-09-03 16:32:47 +00:00
2 changed files with 41 additions and 5 deletions
+21 -5
View File
@@ -25,17 +25,29 @@ type Error struct {
Column int
Content string
Hint string
Err error
}
func (err Error) prefix() string {
switch {
case err.Line > 0 || err.Column > 0:
if err.Filename != "" {
return fmt.Sprintf("%s:%v:%v", err.Filename, err.Line, err.Column)
}
return fmt.Sprintf("%v:%v", err.Line, err.Column)
default:
return err.Filename
}
}
func (err Error) Error() string {
var s []string
switch {
case err.Line > 0 || err.Column > 0:
s = append(s, fmt.Sprintf("%s:%v:%v", err.Filename, err.Line, err.Column))
case err.Filename != "":
s = append(s, err.Filename)
prefix := err.prefix()
if prefix != "" {
s = append(s, prefix)
}
if err.Err != nil {
@@ -46,6 +58,10 @@ func (err Error) Error() string {
s = append(s, fmt.Sprintf("%q", err.Content))
}
if err.Hint != "" {
s = append(s, err.Hint)
}
return strings.Join(s, ": ")
}
+20
View File
@@ -64,3 +64,23 @@ func (p *Position) StepLine() {
p.Line++
p.Column = 1
}
// Add adds a relative position considering
// potential new lines
func (p *Position) Add(rel Position) {
if p.Line == 0 {
p.Reset()
}
switch {
case rel.Line == 0:
// nothing
case rel.Line > 1:
// includes new lines
p.Line += rel.Line - 1
p.Column = rel.Column
default:
// same line
p.Column += rel.Column - 1
}
}