41 lines
884 B
Go
Raw Normal View History

2019-04-26 07:11:36 +03:00
package main
import (
"fmt"
)
// parser keep tracks of the parsing
type parser struct {
lines int // number of parsed lines (for the error messages)
lerr error // the last error occurred
}
2019-04-26 10:28:06 +03:00
// parsed wraps a result for generating parser error
type parsed struct {
2019-04-26 07:11:36 +03:00
result // use struct embedding
err error // inject an error
}
// parse parses a log line and returns a result with an injected error
2019-04-26 10:28:06 +03:00
func (p *parser) parse(line string) (pv parsed) {
2019-04-26 07:11:36 +03:00
// always set the error
2019-04-26 10:28:06 +03:00
defer func() { pv.err = p.lerr }()
2019-04-26 07:11:36 +03:00
// if there was an error do not continue
if p.lerr != nil {
return
}
// chain the parser's error to the result's
res, err := parseLine(line)
if p.lines++; err != nil {
p.lerr = fmt.Errorf("%s: (line #%d)", err, p.lines)
}
2019-04-26 10:28:06 +03:00
return parsed{result: res}
}
// err returns the last error encountered
func (p *parser) err() error {
return p.lerr
2019-04-26 07:11:36 +03:00
}