aboutsummaryrefslogtreecommitdiffstats
path: root/rule_parser.go
blob: 3b485af81b3ce33370e757020c7b322417c0cf67 (plain)
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
package main

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) {
	s = trimSpaceBytes(s)
	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) {
	inputs := splitSpacesBytes(s)
	isOrderOnly := false
	for _, input := range inputs {
		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
	}
	assign := &AssignAST{
		rhs: string(trimLeftSpaceBytes(s[eq+1:])),
	}
	assign.filename = r.filename
	assign.lineno = r.lineno
	// TODO(ukai): support override, export.
	switch s[eq-1] { // s[eq] is '='
	case ':':
		assign.lhs = string(trimSpaceBytes(s[:eq-1]))
		assign.op = ":="
	case '+':
		assign.lhs = string(trimSpaceBytes(s[:eq-1]))
		assign.op = "+="
	case '?':
		assign.lhs = string(trimSpaceBytes(s[:eq-1]))
		assign.op = "?="
	default:
		assign.lhs = string(trimSpaceBytes(s[:eq]))
		assign.op = "="
	}
	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]
	outputs := splitSpacesBytes(first)
	pat, isFirstPattern := isPatternRule(first)
	if isFirstPattern {
		if len(outputs) > 1 {
			return nil, errors.New("*** mixed implicit and normal rules: deprecated syntax")
		}
		r.outputPatterns = []pattern{pat}
	} else {
		o := make([]string, len(outputs))
		for i, output := range outputs {
			o[i] = internBytes(output)
		}
		r.outputs = o
	}

	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.
	outputPatterns := splitSpacesBytes(second)
	if len(outputPatterns) == 0 {
		return nil, errors.New("*** missing target pattern.")
	}
	if len(outputPatterns) > 1 {
		return nil, errors.New("*** multiple target patterns.")
	}
	outpat, ok := isPatternRule(outputPatterns[0])
	if !ok {
		return nil, errors.New("*** target pattern contains no '%'.")
	}
	r.outputPatterns = []pattern{outpat}
	r.parseInputs(third)

	return nil, nil
}