now i'm using zap

dev
Nick Dumas 1 year ago
parent fa80f3ceea
commit 4748dab2ff

@ -3,10 +3,11 @@ package wikilink
import (
"fmt"
"log"
"strings"
// "unicode"
"unicode/utf8"
"go.uber.org/zap"
)
const (
@ -35,6 +36,7 @@ const (
func Lex(name, input string) *Lexer {
l := &Lexer{
L: zap.NewExample().Sugar().Named("lexer"),
name: name,
input: input,
state: lexText,
@ -72,6 +74,7 @@ func (l *Lexer) backup() {
}
type Lexer struct {
L *zap.SugaredLogger
name, input string
start, pos, width int
state stateFn
@ -103,18 +106,29 @@ func (l *Lexer) acceptRun(valid string) {
func (l *Lexer) emit(t ItemType) {
i := Item{t, l.input[l.start:l.pos]}
log.Printf("emitting Item: %s\n", i.String())
L := l.L.With(
zap.Int("pos", l.pos),
zap.Int("width", l.width),
).Named("emit")
L.Debugw("emitting item",
zap.String("item", i.String()),
)
l.items <- i
l.start = l.pos
}
func (l *Lexer) errorf(format string, args ...interface{}) stateFn {
log.Printf("entering errorf: %q\n", format)
l.items <- Item{
L := l.L.Named("errorf")
errorItem := Item{
ItemError,
fmt.Sprintf(format, args...),
}
L.Debugw("emitting errorItem",
zap.String("error", errorItem.String()),
)
l.items <- errorItem
return nil
}

@ -93,6 +93,7 @@ func Test_Lexer(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
// t.Parallel()
l := wikilink.Lex("testLexer", tc.in)
defer l.L.Sync()
for _, e := range tc.expected {
n := l.NextItem()
if e.Typ != n.Typ {

Loading…
Cancel
Save