package wikilink import ( "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 { 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 } } return l.errorf("malformed link") } func lexHeading(l *Lexer) stateFn { log.Println("lexHeading") l.pos += len(Heading) l.emit(ItemHeading) return lexIdent } func lexBlockRef(l *Lexer) stateFn { log.Println("lexBlockRef") l.pos += len(BlockRef) l.emit(ItemBlockRef) return lexIdent } func lexAlias(l *Lexer) stateFn { log.Println("lexAlias") l.pos += len(Alias) l.emit(ItemAlias) return lexIdent } func lexText(l *Lexer) stateFn { 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 { log.Println("lexOpenLink") l.pos += len(OpenLink) l.emit(ItemOpenLink) return lexIdent } func lexCloseLink(l *Lexer) stateFn { 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 } } } */