gonum.org/v1/gonum@v0.14.0/graph/formats/dot/internal/token/token.go (about) 1 // Code generated by gocc; DO NOT EDIT. 2 3 // This file is dual licensed under CC0 and The Gonum License. 4 // 5 // Copyright ©2017 The Gonum Authors. All rights reserved. 6 // Use of this source code is governed by a BSD-style 7 // license that can be found in the LICENSE file. 8 // 9 // Copyright ©2017 Robin Eklind. 10 // This file is made available under a Creative Commons CC0 1.0 11 // Universal Public Domain Dedication. 12 13 package token 14 15 import ( 16 "bytes" 17 "fmt" 18 "strconv" 19 "unicode/utf8" 20 ) 21 22 type Token struct { 23 Type 24 Lit []byte 25 Pos 26 } 27 28 type Type int 29 30 const ( 31 INVALID Type = iota 32 EOF 33 ) 34 35 type Pos struct { 36 Offset int 37 Line int 38 Column int 39 Context Context 40 } 41 42 func (p Pos) String() string { 43 // If the context provides a filename, provide a human-readable File:Line:Column representation. 44 switch src := p.Context.(type) { 45 case Sourcer: 46 return fmt.Sprintf("%s:%d:%d", src.Source(), p.Line, p.Column) 47 default: 48 return fmt.Sprintf("Pos(offset=%d, line=%d, column=%d)", p.Offset, p.Line, p.Column) 49 } 50 } 51 52 type TokenMap struct { 53 typeMap []string 54 idMap map[string]Type 55 } 56 57 func (m TokenMap) Id(tok Type) string { 58 if int(tok) < len(m.typeMap) { 59 return m.typeMap[tok] 60 } 61 return "unknown" 62 } 63 64 func (m TokenMap) Type(tok string) Type { 65 if typ, exist := m.idMap[tok]; exist { 66 return typ 67 } 68 return INVALID 69 } 70 71 func (m TokenMap) TokenString(tok *Token) string { 72 return fmt.Sprintf("%s(%d,%s)", m.Id(tok.Type), tok.Type, tok.Lit) 73 } 74 75 func (m TokenMap) StringType(typ Type) string { 76 return fmt.Sprintf("%s(%d)", m.Id(typ), typ) 77 } 78 79 // Equals returns returns true if the token Type and Lit are matches. 80 func (t *Token) Equals(rhs interface{}) bool { 81 switch rhsT := rhs.(type) { 82 case *Token: 83 return t == rhsT || (t.Type == rhsT.Type && bytes.Equal(t.Lit, rhsT.Lit)) 84 default: 85 return false 86 } 87 } 88 89 // CharLiteralValue returns the string value of the char literal. 90 func (t *Token) CharLiteralValue() string { 91 return string(t.Lit[1 : len(t.Lit)-1]) 92 } 93 94 // Float32Value returns the float32 value of the token or an error if the token literal does not 95 // denote a valid float32. 96 func (t *Token) Float32Value() (float32, error) { 97 if v, err := strconv.ParseFloat(string(t.Lit), 32); err != nil { 98 return 0, err 99 } else { 100 return float32(v), nil 101 } 102 } 103 104 // Float64Value returns the float64 value of the token or an error if the token literal does not 105 // denote a valid float64. 106 func (t *Token) Float64Value() (float64, error) { 107 return strconv.ParseFloat(string(t.Lit), 64) 108 } 109 110 // IDValue returns the string representation of an identifier token. 111 func (t *Token) IDValue() string { 112 return string(t.Lit) 113 } 114 115 // Int32Value returns the int32 value of the token or an error if the token literal does not 116 // denote a valid float64. 117 func (t *Token) Int32Value() (int32, error) { 118 if v, err := strconv.ParseInt(string(t.Lit), 10, 64); err != nil { 119 return 0, err 120 } else { 121 return int32(v), nil 122 } 123 } 124 125 // Int64Value returns the int64 value of the token or an error if the token literal does not 126 // denote a valid float64. 127 func (t *Token) Int64Value() (int64, error) { 128 return strconv.ParseInt(string(t.Lit), 10, 64) 129 } 130 131 // UTF8Rune decodes the UTF8 rune in the token literal. It returns utf8.RuneError if 132 // the token literal contains an invalid rune. 133 func (t *Token) UTF8Rune() (rune, error) { 134 r, _ := utf8.DecodeRune(t.Lit) 135 if r == utf8.RuneError { 136 err := fmt.Errorf("Invalid rune") 137 return r, err 138 } 139 return r, nil 140 } 141 142 // StringValue returns the string value of the token literal. 143 func (t *Token) StringValue() string { 144 return string(t.Lit[1 : len(t.Lit)-1]) 145 } 146 147 var TokMap = TokenMap{ 148 typeMap: []string{ 149 "INVALID", 150 "␚", 151 "{", 152 "}", 153 "empty", 154 "strict", 155 "graphx", 156 "digraph", 157 ";", 158 "--", 159 "->", 160 "node", 161 "edge", 162 "[", 163 "]", 164 ",", 165 "=", 166 "subgraph", 167 ":", 168 "id", 169 }, 170 171 idMap: map[string]Type{ 172 "INVALID": 0, 173 "␚": 1, 174 "{": 2, 175 "}": 3, 176 "empty": 4, 177 "strict": 5, 178 "graphx": 6, 179 "digraph": 7, 180 ";": 8, 181 "--": 9, 182 "->": 10, 183 "node": 11, 184 "edge": 12, 185 "[": 13, 186 "]": 14, 187 ",": 15, 188 "=": 16, 189 "subgraph": 17, 190 ":": 18, 191 "id": 19, 192 }, 193 }