| 1 | // Code generated by gocc; DO NOT EDIT. |
| 2 | |
| 3 | package token |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "fmt" |
| 8 | "strconv" |
| 9 | "unicode/utf8" |
| 10 | ) |
| 11 | |
| 12 | type Token struct { |
| 13 | Type |
| 14 | Lit []byte |
| 15 | Pos |
| 16 | } |
| 17 | |
| 18 | type Type int |
| 19 | |
| 20 | const ( |
| 21 | INVALID Type = iota |
| 22 | EOF |
| 23 | ) |
| 24 | |
| 25 | type Pos struct { |
| 26 | Offset int |
| 27 | Line int |
| 28 | Column int |
| 29 | Context Context |
| 30 | } |
| 31 | |
| 32 | func (p Pos) String() string { |
| 33 | // If the context provides a filename, provide a human-readable File:Line:Column representation. |
| 34 | switch src := p.Context.(type) { |
| 35 | case Sourcer: |
| 36 | return fmt.Sprintf("%s:%d:%d", src.Source(), p.Line, p.Column) |
| 37 | default: |
| 38 | return fmt.Sprintf("Pos(offset=%d, line=%d, column=%d)", p.Offset, p.Line, p.Column) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | type TokenMap struct { |
| 43 | typeMap []string |
| 44 | idMap map[string]Type |
| 45 | } |
| 46 | |
| 47 | func (m TokenMap) Id(tok Type) string { |
| 48 | if int(tok) < len(m.typeMap) { |
| 49 | return m.typeMap[tok] |
| 50 | } |
| 51 | return "unknown" |
| 52 | } |
| 53 | |
| 54 | func (m TokenMap) Type(tok string) Type { |
| 55 | if typ, exist := m.idMap[tok]; exist { |
| 56 | return typ |
| 57 | } |
| 58 | return INVALID |
| 59 | } |
| 60 | |
| 61 | func (m TokenMap) TokenString(tok *Token) string { |
| 62 | return fmt.Sprintf("%s(%d,%s)", m.Id(tok.Type), tok.Type, tok.Lit) |
| 63 | } |
| 64 | |
| 65 | func (m TokenMap) StringType(typ Type) string { |
| 66 | return fmt.Sprintf("%s(%d)", m.Id(typ), typ) |
| 67 | } |
| 68 | |
| 69 | // Equals returns returns true if the token Type and Lit are matches. |
| 70 | func (t *Token) Equals(rhs interface{}) bool { |
| 71 | switch rhsT := rhs.(type) { |
| 72 | case *Token: |
| 73 | return t == rhsT || (t.Type == rhsT.Type && bytes.Equal(t.Lit, rhsT.Lit)) |
| 74 | default: |
| 75 | return false |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // CharLiteralValue returns the string value of the char literal. |
| 80 | func (t *Token) CharLiteralValue() string { |
| 81 | return string(t.Lit[1 : len(t.Lit)-1]) |
| 82 | } |
| 83 | |
| 84 | // Float32Value returns the float32 value of the token or an error if the token literal does not |
| 85 | // denote a valid float32. |
| 86 | func (t *Token) Float32Value() (float32, error) { |
| 87 | if v, err := strconv.ParseFloat(string(t.Lit), 32); err != nil { |
| 88 | return 0, err |
| 89 | } else { |
| 90 | return float32(v), nil |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // Float64Value returns the float64 value of the token or an error if the token literal does not |
| 95 | // denote a valid float64. |
| 96 | func (t *Token) Float64Value() (float64, error) { |
| 97 | return strconv.ParseFloat(string(t.Lit), 64) |
| 98 | } |
| 99 | |
| 100 | // IDValue returns the string representation of an identifier token. |
| 101 | func (t *Token) IDValue() string { |
| 102 | return string(t.Lit) |
| 103 | } |
| 104 | |
| 105 | // Int32Value returns the int32 value of the token or an error if the token literal does not |
| 106 | // denote a valid float64. |
| 107 | func (t *Token) Int32Value() (int32, error) { |
| 108 | if v, err := strconv.ParseInt(string(t.Lit), 10, 64); err != nil { |
| 109 | return 0, err |
| 110 | } else { |
| 111 | return int32(v), nil |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Int64Value returns the int64 value of the token or an error if the token literal does not |
| 116 | // denote a valid float64. |
| 117 | func (t *Token) Int64Value() (int64, error) { |
| 118 | return strconv.ParseInt(string(t.Lit), 10, 64) |
| 119 | } |
| 120 | |
| 121 | // UTF8Rune decodes the UTF8 rune in the token literal. It returns utf8.RuneError if |
| 122 | // the token literal contains an invalid rune. |
| 123 | func (t *Token) UTF8Rune() (rune, error) { |
| 124 | r, _ := utf8.DecodeRune(t.Lit) |
| 125 | if r == utf8.RuneError { |
| 126 | err := fmt.Errorf("Invalid rune") |
| 127 | return r, err |
| 128 | } |
| 129 | return r, nil |
| 130 | } |
| 131 | |
| 132 | // StringValue returns the string value of the token literal. |
| 133 | func (t *Token) StringValue() string { |
| 134 | return string(t.Lit[1 : len(t.Lit)-1]) |
| 135 | } |
| 136 | |
| 137 | var TokMap = TokenMap{ |
| 138 | typeMap: []string{ |
| 139 | "INVALID", |
| 140 | "␚", |
| 141 | "var", |
| 142 | ";", |
| 143 | "identifier", |
| 144 | "_", |
| 145 | "=", |
| 146 | ":=", |
| 147 | "float", |
| 148 | "integer", |
| 149 | }, |
| 150 | |
| 151 | idMap: map[string]Type{ |
| 152 | "INVALID": 0, |
| 153 | "␚": 1, |
| 154 | "var": 2, |
| 155 | ";": 3, |
| 156 | "identifier": 4, |
| 157 | "_": 5, |
| 158 | "=": 6, |
| 159 | ":=": 7, |
| 160 | "float": 8, |
| 161 | "integer": 9, |
| 162 | }, |
| 163 | } |