varmod-match.mk revision 1.15 1 1.15 rillig # $NetBSD: varmod-match.mk,v 1.15 2023/06/23 04:56:54 rillig Exp $
2 1.1 rillig #
3 1.2 rillig # Tests for the :M variable modifier, which filters words that match the
4 1.2 rillig # given pattern.
5 1.4 rillig #
6 1.4 rillig # See ApplyModifier_Match and ModifyWord_Match for the implementation.
7 1.1 rillig
8 1.4 rillig .MAKEFLAGS: -dc
9 1.3 rillig
10 1.3 rillig NUMBERS= One Two Three Four five six seven
11 1.3 rillig
12 1.5 rillig # Only keep words that start with an uppercase letter.
13 1.4 rillig .if ${NUMBERS:M[A-Z]*} != "One Two Three Four"
14 1.4 rillig . error
15 1.4 rillig .endif
16 1.4 rillig
17 1.5 rillig # Only keep words that start with a character other than an uppercase letter.
18 1.4 rillig .if ${NUMBERS:M[^A-Z]*} != "five six seven"
19 1.4 rillig . error
20 1.4 rillig .endif
21 1.4 rillig
22 1.5 rillig # Only keep words that don't start with s and at the same time end with
23 1.5 rillig # either of [ex].
24 1.5 rillig #
25 1.5 rillig # This test case ensures that the negation from the first character class
26 1.5 rillig # does not propagate to the second character class.
27 1.4 rillig .if ${NUMBERS:M[^s]*[ex]} != "One Three five"
28 1.4 rillig . error
29 1.4 rillig .endif
30 1.3 rillig
31 1.10 rillig # Before 2020-06-13, this expression called Str_Match 601,080,390 times.
32 1.10 rillig # Since 2020-06-13, this expression calls Str_Match 1 time.
33 1.4 rillig .if ${:U****************:M****************b}
34 1.4 rillig .endif
35 1.4 rillig
36 1.14 rillig # Before 2023-06-22, this expression called Str_Match 2,621,112 times.
37 1.14 rillig # Adding another '*?' to the pattern called Str_Match 20,630,572 times.
38 1.14 rillig # Adding another '*?' to the pattern called Str_Match 136,405,672 times.
39 1.14 rillig # Adding another '*?' to the pattern called Str_Match 773,168,722 times.
40 1.14 rillig # Adding another '*?' to the pattern called Str_Match 3,815,481,072 times.
41 1.14 rillig # Since 2023-06-22, Str_Match no longer backtracks.
42 1.10 rillig .if ${:U..................................................b:M*?*?*?*?*?a}
43 1.10 rillig .endif
44 1.10 rillig
45 1.4 rillig # To match a dollar sign in a word, double it.
46 1.5 rillig #
47 1.4 rillig # This is different from the :S and :C variable modifiers, where a '$'
48 1.5 rillig # has to be escaped as '\$'.
49 1.4 rillig .if ${:Ua \$ sign:M*$$*} != "\$"
50 1.4 rillig . error
51 1.4 rillig .endif
52 1.4 rillig
53 1.5 rillig # In the :M modifier, '\$' does not escape a dollar. Instead it is
54 1.5 rillig # interpreted as a backslash followed by whatever expression the
55 1.5 rillig # '$' starts.
56 1.5 rillig #
57 1.5 rillig # This differs from the :S, :C and several other variable modifiers.
58 1.4 rillig ${:U*}= asterisk
59 1.4 rillig .if ${:Ua \$ sign any-asterisk:M*\$*} != "any-asterisk"
60 1.4 rillig . error
61 1.4 rillig .endif
62 1.4 rillig
63 1.6 rillig # TODO: ${VAR:M(((}}}}
64 1.6 rillig # TODO: ${VAR:M{{{)))}
65 1.6 rillig # TODO: ${VAR:M${UNBALANCED}}
66 1.6 rillig # TODO: ${VAR:M${:U(((\}\}\}}}
67 1.6 rillig
68 1.7 rillig .MAKEFLAGS: -d0
69 1.7 rillig
70 1.7 rillig # Special characters:
71 1.7 rillig # * matches 0 or more arbitrary characters
72 1.7 rillig # ? matches a single arbitrary character
73 1.7 rillig # \ starts an escape sequence, only outside ranges
74 1.7 rillig # [ starts a set for matching a single character
75 1.7 rillig # ] ends a set for matching a single character
76 1.7 rillig # - in a set, forms a range of characters
77 1.7 rillig # ^ as the first character in a set, negates the set
78 1.7 rillig # ( during parsing of the pattern, starts a nesting level
79 1.7 rillig # ) during parsing of the pattern, ends a nesting level
80 1.7 rillig # { during parsing of the pattern, starts a nesting level
81 1.7 rillig # } during parsing of the pattern, ends a nesting level
82 1.7 rillig # : during parsing of the pattern, finishes the pattern
83 1.7 rillig # $ during parsing of the pattern, starts a nested expression
84 1.7 rillig # # in a line except a shell command, starts a comment
85 1.7 rillig #
86 1.7 rillig # Pattern parts:
87 1.7 rillig # * matches 0 or more arbitrary characters
88 1.7 rillig # ? matches exactly 1 arbitrary character
89 1.7 rillig # \x matches exactly the character 'x'
90 1.7 rillig # [...] matches exactly 1 character from the set
91 1.7 rillig # [^...] matches exactly 1 character outside the set
92 1.7 rillig # [a-z] matches exactly 1 character from the range 'a' to 'z'
93 1.7 rillig #
94 1.7 rillig
95 1.7 rillig # [] matches never
96 1.7 rillig .if ${ ab a[]b a[b a b :L:M[]} != ""
97 1.7 rillig . error
98 1.7 rillig .endif
99 1.7 rillig
100 1.7 rillig # a[]b matches never
101 1.7 rillig .if ${ ab a[]b a[b a b [ ] :L:Ma[]b} != ""
102 1.7 rillig . error
103 1.7 rillig .endif
104 1.7 rillig
105 1.7 rillig # [^] matches exactly 1 arbitrary character
106 1.7 rillig .if ${ ab a[]b a[b a b [ ] :L:M[^]} != "a b [ ]"
107 1.7 rillig . error
108 1.7 rillig .endif
109 1.7 rillig
110 1.7 rillig # a[^]b matches 'a', then exactly 1 arbitrary character, then 'b'
111 1.7 rillig .if ${ ab a[]b a[b a b :L:Ma[^]b} != "a[b"
112 1.7 rillig . error
113 1.7 rillig .endif
114 1.7 rillig
115 1.7 rillig # [Nn0] matches exactly 1 character from the set 'N', 'n', '0'
116 1.7 rillig .if ${ a b N n 0 Nn0 [ ] :L:M[Nn0]} != "N n 0"
117 1.7 rillig . error
118 1.7 rillig .endif
119 1.7 rillig
120 1.7 rillig # [a-c] matches exactly 1 character from the range 'a' to 'c'
121 1.7 rillig .if ${ A B C a b c d [a-c] [a] :L:M[a-c]} != "a b c"
122 1.7 rillig . error
123 1.7 rillig .endif
124 1.7 rillig
125 1.7 rillig # [c-a] matches the same as [a-c]
126 1.7 rillig .if ${ A B C a b c d [a-c] [a] :L:M[c-a]} != "a b c"
127 1.7 rillig . error
128 1.7 rillig .endif
129 1.7 rillig
130 1.7 rillig # [^a-c67]
131 1.8 rillig # matches a single character, except for 'a', 'b', 'c', '6' or
132 1.8 rillig # '7'
133 1.7 rillig .if ${ A B C a b c d 5 6 7 8 [a-c] [a] :L:M[^a-c67]} != "A B C d 5 8"
134 1.7 rillig . error
135 1.7 rillig .endif
136 1.7 rillig
137 1.11 rillig # [\] matches a single backslash
138 1.11 rillig WORDS= a\b a[\]b ab
139 1.11 rillig .if ${WORDS:Ma[\]b} != "a\\b"
140 1.11 rillig . error
141 1.11 rillig .endif
142 1.11 rillig
143 1.7 rillig # : terminates the pattern
144 1.7 rillig .if ${ A * :L:M:} != ""
145 1.7 rillig . error
146 1.7 rillig .endif
147 1.7 rillig
148 1.7 rillig # \: matches a colon
149 1.7 rillig .if ${ ${:U\: \:\:} :L:M\:} != ":"
150 1.7 rillig . error
151 1.7 rillig .endif
152 1.7 rillig
153 1.7 rillig # ${:U\:} matches a colon
154 1.7 rillig .if ${ ${:U\:} ${:U\:\:} :L:M${:U\:}} != ":"
155 1.7 rillig . error
156 1.7 rillig .endif
157 1.7 rillig
158 1.7 rillig # [:] matches never since the ':' starts the next modifier
159 1.15 rillig # expect+3: warning: Unfinished character list in pattern '[' of modifier ':M'
160 1.7 rillig # expect+2: Unknown modifier "]"
161 1.7 rillig # expect+1: Malformed conditional (${ ${:U\:} ${:U\:\:} :L:M[:]} != ":")
162 1.7 rillig .if ${ ${:U\:} ${:U\:\:} :L:M[:]} != ":"
163 1.7 rillig . error
164 1.7 rillig .else
165 1.7 rillig . error
166 1.7 rillig .endif
167 1.7 rillig
168 1.7 rillig # [\] matches exactly a backslash; no escaping takes place in
169 1.7 rillig # character ranges
170 1.11 rillig # Without the 'a' in the below words, the backslash would end a word and thus
171 1.11 rillig # influence how the string is split into words.
172 1.11 rillig WORDS= 1\a 2\\a
173 1.11 rillig .if ${WORDS:M?[\]a} != "1\\a"
174 1.11 rillig . error
175 1.11 rillig .endif
176 1.11 rillig
177 1.11 rillig # [[-]] May look like it would match a single '[', '\' or ']', but
178 1.11 rillig # the inner ']' has two roles: it is the upper bound of the
179 1.11 rillig # character range as well as the closing character of the
180 1.11 rillig # character list. The outer ']' is just a regular character.
181 1.11 rillig WORDS= [ ] [] \] ]]
182 1.11 rillig .if ${WORDS:M[[-]]} != "[] \\] ]]"
183 1.11 rillig . error
184 1.11 rillig .endif
185 1.11 rillig
186 1.11 rillig # [b[-]a]
187 1.11 rillig # Same as for '[[-]]': the character list stops at the first
188 1.11 rillig # ']', and the 'a]' is treated as a literal string.
189 1.11 rillig WORDS= [a \a ]a []a \]a ]]a [a] \a] ]a] ba]
190 1.11 rillig .if ${WORDS:M[b[-]a]} != "[a] \\a] ]a] ba]"
191 1.11 rillig . error
192 1.11 rillig .endif
193 1.11 rillig
194 1.11 rillig # [-] Matches a single '-' since the '-' only becomes part of a
195 1.11 rillig # character range if it is preceded and followed by another
196 1.11 rillig # character.
197 1.11 rillig WORDS= - -]
198 1.11 rillig .if ${WORDS:M[-]} != "-"
199 1.11 rillig . error
200 1.11 rillig .endif
201 1.11 rillig
202 1.11 rillig # [ Incomplete empty character list, never matches.
203 1.11 rillig WORDS= a a[
204 1.15 rillig # expect+1: warning: Unfinished character list in pattern 'a[' of modifier ':M'
205 1.11 rillig .if ${WORDS:Ma[} != ""
206 1.11 rillig . error
207 1.11 rillig .endif
208 1.11 rillig
209 1.11 rillig # [^ Incomplete negated empty character list, matches any single
210 1.11 rillig # character.
211 1.11 rillig WORDS= a a[ aX
212 1.15 rillig # expect+1: warning: Unfinished character list in pattern 'a[^' of modifier ':M'
213 1.11 rillig .if ${WORDS:Ma[^} != "a[ aX"
214 1.7 rillig . error
215 1.7 rillig .endif
216 1.7 rillig
217 1.11 rillig # [-x1-3 Incomplete character list, matches those elements that can be
218 1.11 rillig # parsed without lookahead.
219 1.11 rillig WORDS= - + x xx 0 1 2 3 4 [x1-3
220 1.15 rillig # expect+1: warning: Unfinished character list in pattern '[-x1-3' of modifier ':M'
221 1.11 rillig .if ${WORDS:M[-x1-3} != "- x 1 2 3"
222 1.11 rillig . error
223 1.11 rillig .endif
224 1.11 rillig
225 1.14 rillig # *[-x1-3 Incomplete character list after a wildcard, matches those
226 1.14 rillig # words that end with one of the characters from the list.
227 1.14 rillig WORDS= - + x xx 0 1 2 3 4 00 01 10 11 000 001 010 011 100 101 110 111 [x1-3
228 1.15 rillig # expect+1: warning: Unfinished character list in pattern '*[-x1-3' of modifier ':M'
229 1.14 rillig .if ${WORDS:M*[-x1-3} != "- x xx 1 2 3 01 11 001 011 101 111 [x1-3"
230 1.14 rillig . warning ${WORDS:M*[-x1-3}
231 1.14 rillig .endif
232 1.14 rillig
233 1.11 rillig # [^-x1-3
234 1.11 rillig # Incomplete negated character list, matches any character
235 1.11 rillig # except those elements that can be parsed without lookahead.
236 1.11 rillig WORDS= - + x xx 0 1 2 3 4 [x1-3
237 1.15 rillig # expect+1: warning: Unfinished character list in pattern '[^-x1-3' of modifier ':M'
238 1.11 rillig .if ${WORDS:M[^-x1-3} != "+ 0 4"
239 1.11 rillig . error
240 1.11 rillig .endif
241 1.11 rillig
242 1.11 rillig # [\ Incomplete character list containing a single '\'.
243 1.11 rillig #
244 1.11 rillig # A word can only end with a backslash if the preceding
245 1.11 rillig # character is a backslash as well; in all other cases the final
246 1.11 rillig # backslash would escape the following space, making the space
247 1.11 rillig # part of the word. Only the very last word of a string can be
248 1.11 rillig # '\', as there is no following space that could be escaped.
249 1.11 rillig WORDS= \\ \a ${:Ux\\}
250 1.11 rillig .if ${WORDS:M?[\]} != "\\\\ x\\"
251 1.11 rillig . error
252 1.11 rillig .endif
253 1.11 rillig
254 1.11 rillig # [x- Incomplete character list containing an incomplete character
255 1.11 rillig # range, matches only the 'x'.
256 1.11 rillig WORDS= [x- x x- y
257 1.15 rillig # expect+1: warning: Unfinished character range in pattern '[x-' of modifier ':M'
258 1.11 rillig .if ${WORDS:M[x-} != "x"
259 1.11 rillig . error
260 1.11 rillig .endif
261 1.11 rillig
262 1.11 rillig # [^x- Incomplete negated character list containing an incomplete
263 1.11 rillig # character range; matches each word that does not have an 'x'
264 1.11 rillig # at the position of the character list.
265 1.11 rillig #
266 1.11 rillig # XXX: Even matches strings that are longer than a single
267 1.11 rillig # character.
268 1.11 rillig WORDS= [x- x x- y yyyyy
269 1.15 rillig # expect+1: warning: Unfinished character range in pattern '[^x-' of modifier ':M'
270 1.11 rillig .if ${WORDS:M[^x-} != "[x- y yyyyy"
271 1.11 rillig . error
272 1.11 rillig .endif
273 1.7 rillig
274 1.7 rillig
275 1.7 rillig # The modifier ':tW' prevents splitting at whitespace. Even leading and
276 1.7 rillig # trailing whitespace is preserved.
277 1.7 rillig .if ${ plain string :L:tW:M*} != " plain string "
278 1.7 rillig . error
279 1.7 rillig .endif
280 1.7 rillig
281 1.7 rillig # Without the modifier ':tW', the string is split into words. All whitespace
282 1.7 rillig # around and between the words is normalized to a single space.
283 1.7 rillig .if ${ plain string :L:M*} != "plain string"
284 1.7 rillig . error
285 1.7 rillig .endif
286 1.9 rillig
287 1.9 rillig
288 1.9 rillig # The pattern can come from a variable expression. For single-letter
289 1.9 rillig # variables, either the short form or the long form can be used, just as
290 1.9 rillig # everywhere else.
291 1.9 rillig PRIMES= 2 3 5 7 11
292 1.9 rillig n= 2
293 1.9 rillig .if ${PRIMES:M$n} != "2"
294 1.9 rillig . error
295 1.9 rillig .endif
296 1.9 rillig .if ${PRIMES:M${n}} != "2"
297 1.9 rillig . error
298 1.9 rillig .endif
299 1.9 rillig .if ${PRIMES:M${:U2}} != "2"
300 1.9 rillig . error
301 1.9 rillig .endif
302 1.12 rillig
303 1.12 rillig
304 1.12 rillig # Before var.c 1.1031 from 2022-08-24, the following expressions caused an
305 1.12 rillig # out-of-bounds read beyond the indirect ':M' modifiers.
306 1.12 rillig .if ${:U:${:UM\\}} # The ':M' pattern need not be unescaped, the
307 1.12 rillig . error # resulting pattern is '\', it never matches
308 1.12 rillig .endif # anything.
309 1.12 rillig .if ${:U:${:UM\\\:\\}} # The ':M' pattern must be unescaped, the
310 1.12 rillig . error # resulting pattern is ':\', it never matches
311 1.12 rillig .endif # anything.
312