forked from dcjones/mk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
374 lines (313 loc) · 8.84 KB
/
parse.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// This is a mkfile parser. It executes assignments and includes as it goes, and
// collects a set of rules, which are returned as a ruleSet object.
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
type parser struct {
l *lexer // underlying lexer
name string // name of the file being parsed
path string // full path of the file being parsed
tokenbuf []token // tokens consumed on the current statement
rules *ruleSet // current ruleSet
}
// Pretty errors.
func (p *parser) parseError(context string, expected string, found token) {
mkPrintError(fmt.Sprintf("%s:%d: syntax error: ", p.name, found.line))
mkPrintError(fmt.Sprintf("while %s, expected %s but found '%s'.\n",
context, expected, found.String()))
mkError("")
}
// More basic errors.
func (p *parser) basicErrorAtToken(what string, found token) {
p.basicErrorAtLine(what, found.line)
}
func (p *parser) basicErrorAtLine(what string, line int) {
mkError(fmt.Sprintf("%s:%d: syntax error: %s\n", p.name, line, what))
}
// Accept a token for use in the current statement being parsed.
func (p *parser) push(t token) {
p.tokenbuf = append(p.tokenbuf, t)
}
// Clear all the accepted tokens. Called when a statement is finished.
func (p *parser) clear() {
p.tokenbuf = p.tokenbuf[:0]
}
// A parser state function takes a parser and the next token and returns a new
// state function, or nil if there was a parse error.
type parserStateFun func(*parser, token) parserStateFun
// Parse a mkfile, returning a new ruleSet.
func parse(input string, name string, path string) *ruleSet {
rules := &ruleSet{make(map[string][]string),
make([]rule, 0),
make(map[string][]int)}
parseInto(input, name, rules, path)
return rules
}
// Parse a mkfile inserting rules and variables into a given ruleSet.
func parseInto(input string, name string, rules *ruleSet, path string) {
l, tokens := lex(input)
p := &parser{l, name, path, []token{}, rules}
oldmkfiledir := p.rules.vars["mkfiledir"]
p.rules.vars["mkfiledir"] = []string{filepath.Dir(path)}
state := parseTopLevel
for t := range tokens {
if t.typ == tokenError {
p.basicErrorAtLine(l.errmsg, t.line)
break
}
state = state(p, t)
}
// insert a dummy newline to allow parsing of any assignments or recipeless
// rules to finish.
state = state(p, token{tokenNewline, "\n", l.line, l.col})
p.rules.vars["mkfiledir"] = oldmkfiledir
// TODO: Error when state != parseTopLevel
}
// We are at the top level of a mkfile, expecting rules, assignments, or
// includes.
func parseTopLevel(p *parser, t token) parserStateFun {
switch t.typ {
case tokenNewline:
return parseTopLevel
case tokenPipeInclude:
return parsePipeInclude
case tokenRedirInclude:
return parseRedirInclude
case tokenWord:
return parseAssignmentOrTarget(p, t)
default:
p.parseError("parsing mkfile",
"a rule, include, or assignment", t)
}
return parseTopLevel
}
// Consumed a '<|'
func parsePipeInclude(p *parser, t token) parserStateFun {
switch t.typ {
case tokenNewline:
if len(p.tokenbuf) == 0 {
p.basicErrorAtToken("empty pipe include", t)
}
args := make([]string, len(p.tokenbuf))
for i := 0; i < len(p.tokenbuf); i++ {
args[i] = p.tokenbuf[i].val
}
output, success := subprocess("sh", args, "", true)
if !success {
p.basicErrorAtToken("subprocess include failed", t)
}
parseInto(output, fmt.Sprintf("%s:sh", p.name), p.rules, p.path)
p.clear()
return parseTopLevel
// Almost anything goes. Let the shell sort it out.
case tokenPipeInclude:
fallthrough
case tokenRedirInclude:
fallthrough
case tokenColon:
fallthrough
case tokenAssign:
fallthrough
case tokenWord:
p.tokenbuf = append(p.tokenbuf, t)
default:
p.parseError("parsing piped include", "a shell command", t)
}
return parsePipeInclude
}
// Consumed a '<'
func parseRedirInclude(p *parser, t token) parserStateFun {
switch t.typ {
case tokenNewline:
filename := ""
for i := range p.tokenbuf {
filename += p.tokenbuf[i].val
}
file, err := os.Open(filename)
if err != nil {
p.basicErrorAtToken(fmt.Sprintf("cannot open %s", filename), p.tokenbuf[0])
}
input, _ := ioutil.ReadAll(file)
path, err := filepath.Abs(filename)
if err != nil {
mkError("unable to find mkfile's absolute path")
}
parseInto(string(input), filename, p.rules, path)
p.clear()
return parseTopLevel
case tokenWord:
p.tokenbuf = append(p.tokenbuf, t)
default:
p.parseError("parsing include", "a file name", t)
}
return parseRedirInclude
}
// Encountered a bare string at the beginning of the line.
func parseAssignmentOrTarget(p *parser, t token) parserStateFun {
p.push(t)
return parseEqualsOrTarget
}
// Consumed one bare string ot the beginning of the line.
func parseEqualsOrTarget(p *parser, t token) parserStateFun {
switch t.typ {
case tokenAssign:
return parseAssignment
case tokenWord:
p.push(t)
return parseTargets
case tokenColon:
p.push(t)
return parseAttributesOrPrereqs
default:
p.parseError("reading a target or assignment",
"'=', ':', or another target", t)
}
return parseTopLevel // unreachable
}
// Consumed 'foo='. Everything else is a value being assigned to foo.
func parseAssignment(p *parser, t token) parserStateFun {
switch t.typ {
case tokenNewline:
err := p.rules.executeAssignment(p.tokenbuf)
if err != nil {
p.basicErrorAtToken(err.what, err.where)
}
p.clear()
return parseTopLevel
default:
p.push(t)
}
return parseAssignment
}
// Everything up to ':' must be a target.
func parseTargets(p *parser, t token) parserStateFun {
switch t.typ {
case tokenWord:
p.push(t)
case tokenColon:
p.push(t)
return parseAttributesOrPrereqs
default:
p.parseError("reading a rule's targets",
"filename or pattern", t)
}
return parseTargets
}
// Consume one or more strings followed by a first ':'.
func parseAttributesOrPrereqs(p *parser, t token) parserStateFun {
switch t.typ {
case tokenNewline:
return parseRecipe
case tokenColon:
p.push(t)
return parsePrereqs
case tokenWord:
p.push(t)
default:
p.parseError("reading a rule's attributes or prerequisites",
"an attribute, pattern, or filename", t)
}
return parseAttributesOrPrereqs
}
// Targets and attributes and the second ':' have been consumed.
func parsePrereqs(p *parser, t token) parserStateFun {
switch t.typ {
case tokenNewline:
return parseRecipe
case tokenWord:
p.push(t)
default:
p.parseError("reading a rule's prerequisites",
"filename or pattern", t)
}
return parsePrereqs
}
// An entire rule has been consumed.
func parseRecipe(p *parser, t token) parserStateFun {
// Assemble the rule!
r := rule{}
// find one or two colons
i := 0
for ; i < len(p.tokenbuf) && p.tokenbuf[i].typ != tokenColon; i++ {
}
j := i + 1
for ; j < len(p.tokenbuf) && p.tokenbuf[j].typ != tokenColon; j++ {
}
// rule has attributes
if j < len(p.tokenbuf) {
attribs := make([]string, 0)
for k := i + 1; k < j; k++ {
exparts := expand(p.tokenbuf[k].val, p.rules.vars, true)
attribs = append(attribs, exparts...)
}
err := r.parseAttribs(attribs)
if err != nil {
msg := fmt.Sprintf("while reading a rule's attributes expected an attribute but found \"%c\".", err.found)
p.basicErrorAtToken(msg, p.tokenbuf[i+1])
}
if r.attributes.regex {
r.ismeta = true
}
} else {
j = i
}
// targets
r.targets = make([]pattern, 0)
for k := 0; k < i; k++ {
exparts := expand(p.tokenbuf[k].val, p.rules.vars, true)
for i := range exparts {
targetstr := exparts[i]
r.targets = append(r.targets, pattern{spat: targetstr})
if r.attributes.regex {
rpat, err := regexp.Compile("^" + targetstr + "$")
if err != nil {
msg := fmt.Sprintf("invalid regular expression: %q", err)
p.basicErrorAtToken(msg, p.tokenbuf[k])
}
r.targets[len(r.targets)-1].rpat = rpat
} else {
idx := strings.IndexRune(targetstr, '%')
if idx >= 0 {
var left, right string
if idx > 0 {
left = regexp.QuoteMeta(targetstr[:idx])
}
if idx < len(targetstr)-1 {
right = regexp.QuoteMeta(targetstr[idx+1:])
}
patstr := fmt.Sprintf("^%s(.*)%s$", left, right)
rpat, err := regexp.Compile(patstr)
if err != nil {
msg := fmt.Sprintf("error compiling suffix rule. This is a bug. Error: %s", err)
p.basicErrorAtToken(msg, p.tokenbuf[k])
}
r.targets[len(r.targets)-1].rpat = rpat
r.targets[len(r.targets)-1].issuffix = true
r.ismeta = true
}
}
}
}
// prereqs
r.prereqs = make([]string, 0)
for k := j + 1; k < len(p.tokenbuf); k++ {
exparts := expand(p.tokenbuf[k].val, p.rules.vars, true)
r.prereqs = append(r.prereqs, exparts...)
}
if t.typ == tokenRecipe {
r.recipe = expandRecipeSigils(stripIndentation(t.val, t.col), p.rules.vars)
}
p.rules.add(r)
p.clear()
// the current token doesn't belong to this rule
if t.typ != tokenRecipe {
return parseTopLevel(p, t)
}
return parseTopLevel
}