You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wikilinks-parser/states.go

104 lines
1.6 KiB
Go

package wikilink
import (
"log"
"strings"
)
func lexFragment(l *Lexer) stateFn {
log.Println("entering lexFragment")
for {
if strings.HasPrefix(l.input[l.pos:], CloseLink) {
return lexCloseLink
}
if l.peek() == '^' {
l.next()
l.emit(ItemFragment)
l.acceptRun("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 -.,")
return lexInsideLink
}
return lexInsideLink
}
}
func lexAlias(l *Lexer) stateFn {
// l.accept
log.Println("entering lexAlias")
if strings.HasPrefix(l.input[l.pos:], CloseLink) {
return lexCloseLink
}
l.emit(ItemAlias)
return lexInsideLink
}
func lexInsideLink(l *Lexer) stateFn {
log.Println("entering lexInsideLink")
for {
if strings.HasPrefix(l.input[l.pos:], CloseLink) {
return lexCloseLink
}
r := l.next()
log.Printf("inspecting %q\n", string(r))
switch {
case r == EOF:
case r == '\n':
return l.errorf("unclosed link")
case r == '#':
l.emit(ItemText)
return lexFragment
case r == '|':
l.emit(ItemText)
return lexAlias
}
}
}
func lexOpenLink(l *Lexer) stateFn {
log.Println("entering lexOpenLink")
l.pos += len(OpenLink)
l.emit(ItemOpenLink)
return lexInsideLink
}
func lexCloseLink(l *Lexer) stateFn {
log.Println("entering lexCloseLink")
l.pos += len(CloseLink)
l.emit(ItemCloseLink)
return lexText
}
func lexText(l *Lexer) stateFn {
log.Println("entering lexText")
for {
if strings.HasPrefix(l.input[l.pos:], OpenLink) {
if l.pos > l.start {
l.emit(ItemText)
}
return lexOpenLink
}
if l.next() == EOF {
break
}
if l.pos > l.start {
l.emit(ItemText)
}
l.emit(ItemEOF)
return nil
}
return nil
}