|
|
|
@ -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
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|