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

141 lines
2.2 KiB
Go

package wikilink
import (
1 year ago
"log"
"strings"
)
func isOpenLink(s string) bool {
return strings.HasPrefix(s, OpenLink)
}
func isCloseLink(s string) bool {
return strings.HasPrefix(s, CloseLink)
}
func isAlias(s string) bool {
return strings.HasPrefix(s, Alias)
}
func isHeading(s string) bool {
return strings.HasPrefix(s, Heading)
}
func isBlockRef(s string) bool {
return strings.HasPrefix(s, BlockRef)
}
func lexIdent(l *Lexer) stateFn {
1 year ago
log.Println("lexIdent")
for {
r := l.next()
s := l.input[l.pos:]
if r == '\\' { // i think this will handle escape characters?
break
}
switch {
case isBlockRef(s):
l.emit(ItemIdent)
return lexBlockRef
case isAlias(s):
l.emit(ItemIdent)
return lexAlias
case isCloseLink(s):
l.emit(ItemIdent)
return lexCloseLink
case isHeading(s):
l.emit(ItemIdent)
return lexHeading
}
}
1 year ago
return l.errorf("malformed link")
}
func lexHeading(l *Lexer) stateFn {
1 year ago
log.Println("lexHeading")
l.pos += len(Heading)
l.emit(ItemHeading)
return lexIdent
}
func lexBlockRef(l *Lexer) stateFn {
1 year ago
log.Println("lexBlockRef")
l.pos += len(BlockRef)
l.emit(ItemBlockRef)
return lexIdent
}
func lexAlias(l *Lexer) stateFn {
1 year ago
log.Println("lexAlias")
l.pos += len(Alias)
l.emit(ItemAlias)
return lexIdent
}
func lexText(l *Lexer) stateFn {
1 year ago
log.Println("lexText")
for {
if isOpenLink(l.input[l.pos:]) {
return lexOpenLink
}
r := l.next()
switch {
case r == EOF || r == '\n':
l.emit(ItemText)
return nil
}
}
}
func lexOpenLink(l *Lexer) stateFn {
1 year ago
log.Println("lexOpenLink")
l.pos += len(OpenLink)
l.emit(ItemOpenLink)
return lexIdent
}
func lexCloseLink(l *Lexer) stateFn {
1 year ago
log.Println("lexCloseLink")
l.pos += len(CloseLink)
l.emit(ItemCloseLink)
return lexText
}
/*
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)
if l.peek() = '^' { return lexBlockRef }
return lexHeading
case r == '|':
l.emit(ItemText)
return lexAlias
}
}
}
*/