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
|
// Copyright 2015 Google Inc. All rights reserved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kati
import (
"bytes"
"errors"
"strings"
)
type pattern struct {
prefix, suffix string
}
func (p pattern) String() string {
return p.prefix + "%" + p.suffix
}
func (p pattern) match(s string) bool {
return strings.HasPrefix(s, p.prefix) && strings.HasSuffix(s, p.suffix)
}
func (p pattern) subst(repl, str string) string {
in := str
trimed := str
if p.prefix != "" {
trimed = strings.TrimPrefix(in, p.prefix)
if trimed == in {
return str
}
}
in = trimed
if p.suffix != "" {
trimed = strings.TrimSuffix(in, p.suffix)
if trimed == in {
return str
}
}
rs := strings.SplitN(repl, "%", 2)
if len(rs) != 2 {
return repl
}
return rs[0] + trimed + rs[1]
}
type Rule struct {
outputs []string
inputs []string
orderOnlyInputs []string
outputPatterns []pattern
isDoubleColon bool
isSuffixRule bool
cmds []string
filename string
lineno int
cmdLineno int
}
func isPatternRule(s []byte) (pattern, bool) {
i := bytes.IndexByte(s, '%')
if i < 0 {
return pattern{}, false
}
return pattern{prefix: string(s[:i]), suffix: string(s[i+1:])}, true
}
func (r *Rule) parseInputs(s []byte) {
ws := newWordScanner(s)
isOrderOnly := false
for ws.Scan() {
input := ws.Bytes()
if len(input) == 1 && input[0] == '|' {
isOrderOnly = true
continue
}
if isOrderOnly {
r.orderOnlyInputs = append(r.orderOnlyInputs, internBytes(input))
} else {
r.inputs = append(r.inputs, internBytes(input))
}
}
}
func (r *Rule) parseVar(s []byte) *AssignAST {
eq := bytes.IndexByte(s, '=')
if eq <= 0 {
return nil
}
rhs := trimLeftSpaceBytes(s[eq+1:])
var lhs []byte
var op string
// TODO(ukai): support override, export.
switch s[eq-1] { // s[eq] is '='
case ':':
lhs = trimSpaceBytes(s[:eq-1])
op = ":="
case '+':
lhs = trimSpaceBytes(s[:eq-1])
op = "+="
case '?':
lhs = trimSpaceBytes(s[:eq-1])
op = "?="
default:
lhs = trimSpaceBytes(s[:eq])
op = "="
}
assign := newAssignAST(nil, lhs, rhs, op)
assign.filename = r.filename
assign.lineno = r.lineno
return assign
}
func (r *Rule) parse(line []byte) (*AssignAST, error) {
index := bytes.IndexByte(line, ':')
if index < 0 {
return nil, errors.New("*** missing separator.")
}
first := line[:index]
ws := newWordScanner(first)
pat, isFirstPattern := isPatternRule(first)
if isFirstPattern {
n := 0
for ws.Scan() {
n++
if n > 1 {
return nil, errors.New("*** mixed implicit and normal rules: deprecated syntax")
}
}
r.outputPatterns = []pattern{pat}
} else {
for ws.Scan() {
r.outputs = append(r.outputs, internBytes(ws.Bytes()))
}
}
index++
if index < len(line) && line[index] == ':' {
r.isDoubleColon = true
index++
}
rest := line[index:]
if assign := r.parseVar(rest); assign != nil {
return assign, nil
}
index = bytes.IndexByte(rest, ':')
if index < 0 {
r.parseInputs(rest)
return nil, nil
}
// %.x: %.y: %.z
if isFirstPattern {
return nil, errors.New("*** mixed implicit and normal rules: deprecated syntax")
}
second := rest[:index]
third := rest[index+1:]
// r.outputs is already set.
ws = newWordScanner(second)
if !ws.Scan() {
return nil, errors.New("*** missing target pattern.")
}
outpat, ok := isPatternRule(ws.Bytes())
if !ok {
return nil, errors.New("*** target pattern contains no '%'.")
}
r.outputPatterns = []pattern{outpat}
if ws.Scan() {
return nil, errors.New("*** multiple target patterns.")
}
r.parseInputs(third)
return nil, nil
}
|