-
Notifications
You must be signed in to change notification settings - Fork 0
/
coregogenerate.go
261 lines (237 loc) · 7.42 KB
/
coregogenerate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright (c) 2016 Paul Jolly <paul@myitcv.org.uk>, all rights reserved.
// Use of this document is governed by a license found in the LICENSE document.
package gogenerate
import (
"bufio"
"bytes"
"fmt"
"go/build"
"io"
"os"
"path/filepath"
"strconv"
"strings"
)
// The following code is largely adapted from cmd/go. We reproduce the license
// here therefore
// Copyright (c) 2009 The Go Authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// A generator represents the state of a single Go source file
// being scanned for generator commands.
type generator struct {
f func(line int, dirArgs []string) error
r io.Reader
dir string // full rooted directory of file.
file string // base name of file.
pkg string
commands map[string][]string
lineNum int // current line number.
env []string
}
// DirFunc runs f(cmds) on each go generate directive (as defined by
// go generate -help) found in the absolute-named file that is part
// of package pkg
func DirFunc(pkg string, dir, file string, f func(line int, dirArgs []string) error) error {
fi, err := os.Open(filepath.Join(dir, file))
if err != nil {
return err
}
defer fi.Close()
g := &generator{
f: f,
pkg: pkg,
commands: make(map[string][]string),
dir: dir,
file: file,
r: fi,
}
return g.matches()
}
// run runs the generators in the current file.
func (g *generator) matches() (err error) {
// Processing below here calls g.errorf on failure, which does panic(stop).
// If we encounter an error, we abort the package.
defer func() {
e := recover()
if e, isErr := e.(error); isErr {
err = e
}
}()
// Scan for lines that start "//go:generate".
// Can't use bufio.Scanner because it can't handle long lines,
// which are likely to appear when using generate.
input := bufio.NewReader(g.r)
// One line per loop.
for {
g.lineNum++ // 1-indexed.
var buf []byte
buf, err = input.ReadSlice('\n')
if err == bufio.ErrBufferFull {
// Line too long - consume and ignore.
if isGoGenerate(buf) {
g.errorf("directive too long")
}
for err == bufio.ErrBufferFull {
_, err = input.ReadSlice('\n')
}
if err != nil {
break
}
continue
}
if err != nil {
// Check for marker at EOF without final \n.
if err == io.EOF && isGoGenerate(buf) {
err = io.ErrUnexpectedEOF
}
break
}
if !isGoGenerate(buf) {
continue
}
g.setEnv()
words := g.split(string(buf))
if len(words) == 0 {
g.errorf("no arguments to directive")
}
if words[0] == "-command" {
g.setShorthand(words)
continue
}
err := g.f(g.lineNum, words)
if err != nil {
g.errorf("callback error: %v", err)
}
}
if err != nil && err != io.EOF {
g.errorf("error reading")
}
return nil
}
func isGoGenerate(buf []byte) bool {
return bytes.HasPrefix(buf, []byte(GoGeneratePrefix+" ")) || bytes.HasPrefix(buf, []byte(GoGeneratePrefix+"\t"))
}
// setEnv sets the extra environment variables used when executing a
// single go:generate command.
func (g *generator) setEnv() {
g.env = []string{
"GOARCH=" + build.Default.GOARCH,
"GOOS=" + build.Default.GOOS,
"GOFILE=" + g.file,
"GOLINE=" + strconv.Itoa(g.lineNum),
"GOPACKAGE=" + g.pkg,
"DOLLAR=" + "$",
}
}
// split breaks the line into words, evaluating quoted
// strings and evaluating environment variables.
// The initial //go:generate element is present in line.
func (g *generator) split(line string) []string {
// Parse line, obeying quoted strings.
var words []string
line = line[len("//go:generate ") : len(line)-1] // Drop preamble and final newline.
// There may still be a carriage return.
if len(line) > 0 && line[len(line)-1] == '\r' {
line = line[:len(line)-1]
}
// One (possibly quoted) word per iteration.
Words:
for {
line = strings.TrimLeft(line, " \t")
if len(line) == 0 {
break
}
if line[0] == '"' {
for i := 1; i < len(line); i++ {
c := line[i] // Only looking for ASCII so this is OK.
switch c {
case '\\':
if i+1 == len(line) {
g.errorf("bad backslash")
}
i++ // Absorb next byte (If it's a multibyte we'll get an error in Unquote).
case '"':
word, err := strconv.Unquote(line[0 : i+1])
if err != nil {
g.errorf("bad quoted string")
}
words = append(words, word)
line = line[i+1:]
// Check the next character is space or end of line.
if len(line) > 0 && line[0] != ' ' && line[0] != '\t' {
g.errorf("expect space after quoted argument")
}
continue Words
}
}
g.errorf("mismatched quoted string")
}
i := strings.IndexAny(line, " \t")
if i < 0 {
i = len(line)
}
words = append(words, line[0:i])
line = line[i:]
}
// Substitute command if required.
if len(words) > 0 && g.commands[words[0]] != nil {
// Replace 0th word by command substitution.
words = append(g.commands[words[0]], words[1:]...)
}
// Substitute environment variables.
for i, word := range words {
words[i] = os.Expand(word, g.expandVar)
}
return words
}
// errorf logs an error message prefixed with the file and line number.
// It then exits the program (with exit status 1) because generation stops
// at the first error.
func (g *generator) errorf(format string, args ...interface{}) {
panic(fmt.Errorf("%s:%d: %s", filepath.Join(g.dir, g.file), g.lineNum, fmt.Sprintf(format, args...)))
}
// expandVar expands the $XXX invocation in word. It is called
// by os.Expand.
func (g *generator) expandVar(word string) string {
w := word + "="
for _, e := range g.env {
if strings.HasPrefix(e, w) {
return e[len(w):]
}
}
return os.Getenv(word)
}
// setShorthand installs a new shorthand as defined by a -command directive.
func (g *generator) setShorthand(words []string) {
// Create command shorthand.
if len(words) == 1 {
g.errorf("no command specified for -command")
}
command := words[1]
if g.commands[command] != nil {
g.errorf("command %q defined multiply defined", command)
}
g.commands[command] = words[2:len(words):len(words)] // force later append to make copy
}