Debian Codegraph @

internal/token/gen/golang/token.go

raw symbols SCIP
1 //Copyright 2013 Vastech SA (PTY) LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package golang
16
17 import (
18 "bytes"
19 "fmt"
20 "go/format"
21 "path"
22 "text/template"
23
24 "github.com/goccmack/gocc/internal/io"
25 "github.com/goccmack/gocc/internal/token"
26 )
27
28 func GenToken(pkg, outdir string, tokMap *token.TokenMap) {
29 tokenPath := path.Join(outdir, "token", "token.go")
30 tmpl, err := template.New("token").Parse(TokenMapSrc[1:])
31 if err != nil {
32 panic(err)
33 }
34 buf := new(bytes.Buffer)
35 err = tmpl.Execute(buf, TokenData{TypMap: tokMap.TypeMap, IdMap: typeMap(tokMap)})
36 if err != nil {
37 println(err.Error())
38 }
39
40 // Use go/format to indent the idMap literal correctly.
41 source, err := format.Source(buf.Bytes())
42 if err != nil {
43 println(err.Error())
44 // panic(err)
45 }
46 io.WriteFile(tokenPath, source)
47 }
48
49 func typeMap(tokMap *token.TokenMap) []string {
50 tm := make([]string, len(tokMap.TypeMap))
51 for i, sym := range tokMap.TypeMap {
52 tm[i] = fmt.Sprintf("\"%s\": %d", sym, i)
53 }
54 return tm
55 }
56
57 type TokenData struct {
58 IdMap []string
59 TypMap []string
60 }
61
62 // TODO(TokenMap.TokenString): refactor to print pos & token string properly
63 const TokenMapSrc string = `
64 // Code generated by gocc; DO NOT EDIT.
65
66 package token
67
68 import (
69 "bytes"
70 "fmt"
71 "strconv"
72 "unicode/utf8"
73 )
74
75 type Token struct {
76 Type
77 Lit []byte
78 Pos
79 }
80
81 type Type int
82
83 const (
84 INVALID Type = iota
85 EOF
86 )
87
88 type Pos struct {
89 Offset int
90 Line int
91 Column int
92 Context Context
93 }
94
95 func (p Pos) String() string {
96 // If the context provides a filename, provide a human-readable File:Line:Column representation.
97 switch src := p.Context.(type) {
98 case Sourcer:
99 return fmt.Sprintf("%s:%d:%d", src.Source(), p.Line, p.Column)
100 default:
101 return fmt.Sprintf("Pos(offset=%d, line=%d, column=%d)", p.Offset, p.Line, p.Column)
102 }
103 }
104
105 type TokenMap struct {
106 typeMap []string
107 idMap map[string]Type
108 }
109
110 func (m TokenMap) Id(tok Type) string {
111 if int(tok) < len(m.typeMap) {
112 return m.typeMap[tok]
113 }
114 return "unknown"
115 }
116
117 func (m TokenMap) Type(tok string) Type {
118 if typ, exist := m.idMap[tok]; exist {
119 return typ
120 }
121 return INVALID
122 }
123
124 func (m TokenMap) TokenString(tok *Token) string {
125 return fmt.Sprintf("%s(%d,%s)", m.Id(tok.Type), tok.Type, tok.Lit)
126 }
127
128 func (m TokenMap) StringType(typ Type) string {
129 return fmt.Sprintf("%s(%d)", m.Id(typ), typ)
130 }
131
132 // Equals returns returns true if the token Type and Lit are matches.
133 func (t *Token) Equals(rhs interface{}) bool {
134 switch rhsT := rhs.(type) {
135 case *Token:
136 return t == rhsT || (t.Type == rhsT.Type && bytes.Equal(t.Lit, rhsT.Lit))
137 default:
138 return false
139 }
140 }
141
142 // CharLiteralValue returns the string value of the char literal.
143 func (t *Token) CharLiteralValue() string {
144 return string(t.Lit[1 : len(t.Lit)-1])
145 }
146
147 // Float32Value returns the float32 value of the token or an error if the token literal does not
148 // denote a valid float32.
149 func (t *Token) Float32Value() (float32, error) {
150 if v, err := strconv.ParseFloat(string(t.Lit), 32); err != nil {
151 return 0, err
152 } else {
153 return float32(v), nil
154 }
155 }
156
157 // Float64Value returns the float64 value of the token or an error if the token literal does not
158 // denote a valid float64.
159 func (t *Token) Float64Value() (float64, error) {
160 return strconv.ParseFloat(string(t.Lit), 64)
161 }
162
163 // IDValue returns the string representation of an identifier token.
164 func (t *Token) IDValue() string {
165 return string(t.Lit)
166 }
167
168 // Int32Value returns the int32 value of the token or an error if the token literal does not
169 // denote a valid float64.
170 func (t *Token) Int32Value() (int32, error) {
171 if v, err := strconv.ParseInt(string(t.Lit), 10, 64); err != nil {
172 return 0, err
173 } else {
174 return int32(v), nil
175 }
176 }
177
178 // Int64Value returns the int64 value of the token or an error if the token literal does not
179 // denote a valid float64.
180 func (t *Token) Int64Value() (int64, error) {
181 return strconv.ParseInt(string(t.Lit), 10, 64)
182 }
183
184 // UTF8Rune decodes the UTF8 rune in the token literal. It returns utf8.RuneError if
185 // the token literal contains an invalid rune.
186 func (t *Token) UTF8Rune() (rune, error) {
187 r, _ := utf8.DecodeRune(t.Lit)
188 if r == utf8.RuneError {
189 err := fmt.Errorf("Invalid rune")
190 return r, err
191 }
192 return r, nil
193 }
194
195 // StringValue returns the string value of the token literal.
196 func (t *Token) StringValue() string {
197 return string(t.Lit[1:len(t.Lit)-1])
198 }
199
200 var TokMap = TokenMap{
201 typeMap: []string{
202 {{- range .TypMap }}
203 {{printf "%q" .}},
204 {{- end }}
205 },
206
207 idMap: map[string]Type{
208 {{- range .IdMap }}
209 {{printf "%s" .}},
210 {{- end }}
211 },
212 }
213
214
215 `