still trying to get lexAlias working

main
Nick Dumas 1 year ago
parent 126dd495ba
commit 717e429efd

@ -115,103 +115,6 @@ func (l *Lexer) errorf(format string, args ...interface{}) stateFn {
return nil
}
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()
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
case l.peek() == ']':
l.emit(ItemText)
return lexCloseLink
}
}
}
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
}
func (l *Lexer) next() rune {
var r rune
if l.pos >= len(l.input) {

@ -0,0 +1,103 @@
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
}
Loading…
Cancel
Save