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