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