1package query 2 3import ( 4 "fmt" 5 "strconv" 6 7 "github.com/pelletier/go-toml" 8) 9 10// Define tokens 11type tokenType int 12 13const ( 14 eof = -(iota + 1) 15) 16 17const ( 18 tokenError tokenType = iota 19 tokenEOF 20 tokenKey 21 tokenString 22 tokenInteger 23 tokenFloat 24 tokenLeftBracket 25 tokenRightBracket 26 tokenLeftParen 27 tokenRightParen 28 tokenComma 29 tokenColon 30 tokenDollar 31 tokenStar 32 tokenQuestion 33 tokenDot 34 tokenDotDot 35) 36 37var tokenTypeNames = []string{ 38 "Error", 39 "EOF", 40 "Key", 41 "String", 42 "Integer", 43 "Float", 44 "[", 45 "]", 46 "(", 47 ")", 48 ",", 49 ":", 50 "$", 51 "*", 52 "?", 53 ".", 54 "..", 55} 56 57type token struct { 58 toml.Position 59 typ tokenType 60 val string 61} 62 63func (tt tokenType) String() string { 64 idx := int(tt) 65 if idx < len(tokenTypeNames) { 66 return tokenTypeNames[idx] 67 } 68 return "Unknown" 69} 70 71func (t token) Int() int { 72 if result, err := strconv.Atoi(t.val); err != nil { 73 panic(err) 74 } else { 75 return result 76 } 77} 78 79func (t token) String() string { 80 switch t.typ { 81 case tokenEOF: 82 return "EOF" 83 case tokenError: 84 return t.val 85 } 86 87 return fmt.Sprintf("%q", t.val) 88} 89 90func isSpace(r rune) bool { 91 return r == ' ' || r == '\t' 92} 93 94func isAlphanumeric(r rune) bool { 95 return 'a' <= r && r <= 'z' || 'A' <= r && r <= 'Z' || r == '_' 96} 97 98func isDigit(r rune) bool { 99 return '0' <= r && r <= '9' 100} 101 102func isHexDigit(r rune) bool { 103 return isDigit(r) || 104 (r >= 'a' && r <= 'f') || 105 (r >= 'A' && r <= 'F') 106} 107