From 717e429efd513218f22ec5355728ed9ac98db87a Mon Sep 17 00:00:00 2001 From: Nick Dumas Date: Sun, 25 Jun 2023 08:44:28 -0400 Subject: [PATCH] still trying to get lexAlias working --- lexer.go | 97 -------------------------------------------------- states.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 97 deletions(-) create mode 100644 states.go diff --git a/lexer.go b/lexer.go index 31399bf..327c354 100644 --- a/lexer.go +++ b/lexer.go @@ -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) { diff --git a/states.go b/states.go new file mode 100644 index 0000000..4f5584f --- /dev/null +++ b/states.go @@ -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 +}