25 itemError itemType = iota
39 func (typ itemType) String() string {
49 case itemRightBracket:
66 return fmt.Sprintf("itemType(%d)", typ)
76 type stateFn func(*lexer) stateFn
78 func (l *lexer) run() {
79 for state := lexStart; state != nil; {
85 func (l *lexer) emitValue(t itemType, value string) {
86 l.items <- item{t, value, l.start}
90 func (l *lexer) emit(t itemType) {
91 l.items <- item{t, l.input[l.start:l.pos], l.start}
95 func lexStart(l *lexer) stateFn {
96 switch r := l.next(); {
100 case unicode.IsSpace(r):
103 l.emit(itemLeftParen)
105 l.emit(itemRightParen)
107 l.emit(itemLeftBracket)
109 l.emit(itemRightBracket)
119 case unicode.IsUpper(r):
122 case unicode.IsLower(r):
126 return l.errorf("unexpected character %c", r)
131 func (l *lexer) next() (r rune) {
132 if l.pos >= len(l.input) {
136 r, l.width = utf8.DecodeRuneInString(l.input[l.pos:])
147 func (l *lexer) ignore() {
151 func (l *lexer) backup() {
155 func (l *lexer) errorf(format string, args ...interface{}) stateFn {
156 // TODO(dh): emit position information in errors
159 fmt.Sprintf(format, args...),
165 func isAlphaNumeric(r rune) bool {
166 return r >= '0' && r <= '9' ||
167 r >= 'a' && r <= 'z' ||
171 func lexString(l *lexer) stateFn {
172 l.next() // skip quote
177 switch r := l.next(); r {
179 return l.errorf("unterminated string")
182 l.emitValue(itemString, string(runes))
185 runes = append(runes, '"')
190 runes = append(runes, '\\')
196 runes = append(runes, r)
201 func lexType(l *lexer) stateFn {
204 if !isAlphaNumeric(l.next()) {
212 func lexVariable(l *lexer) stateFn {
215 if !isAlphaNumeric(l.next()) {