kunt

golang IRC bot
git clone git://git.2f30.org/kunt
Log | Files | Refs | LICENSE

commit 8f623f9e0a87688ad47848436f41e3c8276eaca4
parent f3d567ea883752eebd998d599c659fa434c2e0ca
Author: sin <sin@2f30.org>
Date:   Thu,  2 May 2013 14:59:08 +0100

remove irc lexer for now

Diffstat:
Dsrc/irc/lexer.go | 210-------------------------------------------------------------------------------
1 file changed, 0 insertions(+), 210 deletions(-)

diff --git a/src/irc/lexer.go b/src/irc/lexer.go @@ -1,210 +0,0 @@ -package irc - -import ( - "fmt" - "strings" - "unicode/utf8" -) - -const eof = -1 - -type item struct { - typ itemType - val string -} - -type itemType int - -const ( - itemError itemType = iota - - itemEOF - itemMessage - itemStartColon - itemColon - itemPrefix - itemCommand - itemParams - itemMiddle - itemTrailing -) - -func (i item) String() string { - switch i.typ { - case itemEOF: - return "EOF" - case itemError: - return i.val - } - return fmt.Sprintf("%q", i.val) -} - -type stateFn func(*lexer) stateFn - -type lexer struct { - name string - input string - start int - pos int - width int - items chan item -} - -func lex(name, input string) (*lexer, chan item) { - l := &lexer{ - name: name, - input: input, - items: make(chan item), - } - go l.run() - return l, l.items -} - -func (l *lexer) emit(t itemType) { - l.items <- item{t, l.input[l.start:l.pos]} - l.start = l.pos -} - -func (l *lexer) run() { - for state := lexMessage; state != nil; { - state = state(l) - } - close(l.items) -} - -func (l *lexer) next() (r rune) { - if l.pos >= len(l.input) { - l.width = 0 - return eof - } - r, l.width = - utf8.DecodeRuneInString(l.input[l.pos:]) - l.pos += l.width - return r -} - -func (l *lexer) ignore() { - l.start = l.pos -} - -func (l *lexer) backup() { - l.pos -= l.width -} - -func (l *lexer) peek() rune { - r := l.next() - l.backup() - return r -} - -func (l *lexer) eat() { - l.next() - l.ignore() -} - -const colon = ":" - -func (l *lexer) errorf(format string, args ...interface{}) stateFn { - l.items <- item{ - itemError, - fmt.Sprintf(format, args...), - } - return nil -} - -func lexMessage(l *lexer) stateFn { - for { - if strings.HasPrefix(l.input[l.pos:], colon) { - return lexStartColon - } - if l.next() == eof { - break - } - } - l.emit(itemEOF) - return nil -} - -func lexStartColon(l *lexer) stateFn { - l.pos += len(colon) - l.emit(itemStartColon) - return lexPrefix -} - -func lexPrefix(l *lexer) stateFn { - for { - if l.next() != ' ' { - continue - } - l.ignore() - return lexCommand - } - return nil -} - -func lexCommand(l *lexer) stateFn { - for { - if l.next() != ' ' { - continue - } - l.backup() - l.emit(itemCommand) - return lexParams - } - return nil -} - -func lexParams(l *lexer) stateFn { - r := l.next() - if r == ' ' { - l.ignore() - } - if l.peek() != ':' { - return lexMiddle - } else { - l.next() - l.emit(itemColon) - return lexTrailing - } - l.emit(itemParams) - return nil -} - -func lexMiddle(l *lexer) stateFn { - for { - r := l.next() - if r == eof { - break - } - if r == ' ' { - l.backup() - l.emit(itemMiddle) - return lexParams - } - if r == '\r' { - if l.peek() == '\n' { - l.backup() - l.emit(itemMiddle) - return nil - } - } - } - return nil -} - -func lexTrailing(l *lexer) stateFn { - for { - r := l.next() - if r == eof { - break - } - if r == '\r' { - if l.peek() == '\n' { - l.backup() - l.emit(itemTrailing) - return nil - } - } - } - return nil -}