varmod-match.mk revision 1.10 1 1.10 rillig # $NetBSD: varmod-match.mk,v 1.10 2022/06/11 07:54:25 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.10 rillig # As of 2022-06-11, this expression calls Str_Match 5,242,223 times.
37 1.10 rillig # Adding another '*?' to the pattern calls Str_Match 41,261,143 times.
38 1.10 rillig .if ${:U..................................................b:M*?*?*?*?*?a}
39 1.10 rillig .endif
40 1.10 rillig
41 1.4 rillig # To match a dollar sign in a word, double it.
42 1.5 rillig #
43 1.4 rillig # This is different from the :S and :C variable modifiers, where a '$'
44 1.5 rillig # has to be escaped as '\$'.
45 1.4 rillig .if ${:Ua \$ sign:M*$$*} != "\$"
46 1.4 rillig . error
47 1.4 rillig .endif
48 1.4 rillig
49 1.5 rillig # In the :M modifier, '\$' does not escape a dollar. Instead it is
50 1.5 rillig # interpreted as a backslash followed by whatever expression the
51 1.5 rillig # '$' starts.
52 1.5 rillig #
53 1.5 rillig # This differs from the :S, :C and several other variable modifiers.
54 1.4 rillig ${:U*}= asterisk
55 1.4 rillig .if ${:Ua \$ sign any-asterisk:M*\$*} != "any-asterisk"
56 1.4 rillig . error
57 1.4 rillig .endif
58 1.4 rillig
59 1.6 rillig # TODO: ${VAR:M(((}}}}
60 1.6 rillig # TODO: ${VAR:M{{{)))}
61 1.6 rillig # TODO: ${VAR:M${UNBALANCED}}
62 1.6 rillig # TODO: ${VAR:M${:U(((\}\}\}}}
63 1.6 rillig
64 1.7 rillig .MAKEFLAGS: -d0
65 1.7 rillig
66 1.7 rillig # Special characters:
67 1.7 rillig # * matches 0 or more arbitrary characters
68 1.7 rillig # ? matches a single arbitrary character
69 1.7 rillig # \ starts an escape sequence, only outside ranges
70 1.7 rillig # [ starts a set for matching a single character
71 1.7 rillig # ] ends a set for matching a single character
72 1.7 rillig # - in a set, forms a range of characters
73 1.7 rillig # ^ as the first character in a set, negates the set
74 1.7 rillig # ( during parsing of the pattern, starts a nesting level
75 1.7 rillig # ) during parsing of the pattern, ends a nesting level
76 1.7 rillig # { during parsing of the pattern, starts a nesting level
77 1.7 rillig # } during parsing of the pattern, ends a nesting level
78 1.7 rillig # : during parsing of the pattern, finishes the pattern
79 1.7 rillig # $ during parsing of the pattern, starts a nested expression
80 1.7 rillig # # in a line except a shell command, starts a comment
81 1.7 rillig #
82 1.7 rillig # Pattern parts:
83 1.7 rillig # * matches 0 or more arbitrary characters
84 1.7 rillig # ? matches exactly 1 arbitrary character
85 1.7 rillig # \x matches exactly the character 'x'
86 1.7 rillig # [...] matches exactly 1 character from the set
87 1.7 rillig # [^...] matches exactly 1 character outside the set
88 1.7 rillig # [a-z] matches exactly 1 character from the range 'a' to 'z'
89 1.7 rillig #
90 1.7 rillig
91 1.7 rillig # [] matches never
92 1.7 rillig .if ${ ab a[]b a[b a b :L:M[]} != ""
93 1.7 rillig . error
94 1.7 rillig .endif
95 1.7 rillig
96 1.7 rillig # a[]b matches never
97 1.7 rillig .if ${ ab a[]b a[b a b [ ] :L:Ma[]b} != ""
98 1.7 rillig . error
99 1.7 rillig .endif
100 1.7 rillig
101 1.7 rillig # [^] matches exactly 1 arbitrary character
102 1.7 rillig .if ${ ab a[]b a[b a b [ ] :L:M[^]} != "a b [ ]"
103 1.7 rillig . error
104 1.7 rillig .endif
105 1.7 rillig
106 1.7 rillig # a[^]b matches 'a', then exactly 1 arbitrary character, then 'b'
107 1.7 rillig .if ${ ab a[]b a[b a b :L:Ma[^]b} != "a[b"
108 1.7 rillig . error
109 1.7 rillig .endif
110 1.7 rillig
111 1.7 rillig # [Nn0] matches exactly 1 character from the set 'N', 'n', '0'
112 1.7 rillig .if ${ a b N n 0 Nn0 [ ] :L:M[Nn0]} != "N n 0"
113 1.7 rillig . error
114 1.7 rillig .endif
115 1.7 rillig
116 1.7 rillig # [a-c] matches exactly 1 character from the range 'a' to 'c'
117 1.7 rillig .if ${ A B C a b c d [a-c] [a] :L:M[a-c]} != "a b c"
118 1.7 rillig . error
119 1.7 rillig .endif
120 1.7 rillig
121 1.7 rillig # [c-a] matches the same as [a-c]
122 1.7 rillig .if ${ A B C a b c d [a-c] [a] :L:M[c-a]} != "a b c"
123 1.7 rillig . error
124 1.7 rillig .endif
125 1.7 rillig
126 1.7 rillig # [^a-c67]
127 1.8 rillig # matches a single character, except for 'a', 'b', 'c', '6' or
128 1.8 rillig # '7'
129 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"
130 1.7 rillig . error
131 1.7 rillig .endif
132 1.7 rillig
133 1.7 rillig # : terminates the pattern
134 1.7 rillig .if ${ A * :L:M:} != ""
135 1.7 rillig . error
136 1.7 rillig .endif
137 1.7 rillig
138 1.7 rillig # \: matches a colon
139 1.7 rillig .if ${ ${:U\: \:\:} :L:M\:} != ":"
140 1.7 rillig . error
141 1.7 rillig .endif
142 1.7 rillig
143 1.7 rillig # ${:U\:} matches a colon
144 1.7 rillig .if ${ ${:U\:} ${:U\:\:} :L:M${:U\:}} != ":"
145 1.7 rillig . error
146 1.7 rillig .endif
147 1.7 rillig
148 1.7 rillig # [:] matches never since the ':' starts the next modifier
149 1.7 rillig # expect+2: Unknown modifier "]"
150 1.7 rillig # expect+1: Malformed conditional (${ ${:U\:} ${:U\:\:} :L:M[:]} != ":")
151 1.7 rillig .if ${ ${:U\:} ${:U\:\:} :L:M[:]} != ":"
152 1.7 rillig . error
153 1.7 rillig .else
154 1.7 rillig . error
155 1.7 rillig .endif
156 1.7 rillig
157 1.7 rillig # [\] matches exactly a backslash; no escaping takes place in
158 1.7 rillig # character ranges
159 1.7 rillig # Without the 'a' in the below expressions, the backslash would end a word and
160 1.7 rillig # thus influence how the string is split into words.
161 1.7 rillig .if ${ ${:U\\a} ${:U\\\\a} :L:M[\]a} != "\\a"
162 1.7 rillig . error
163 1.7 rillig .endif
164 1.7 rillig
165 1.7 rillig #.MAKEFLAGS: -dcv
166 1.7 rillig #
167 1.7 rillig # Incomplete patterns:
168 1.7 rillig # [ matches TODO
169 1.7 rillig # [x matches TODO
170 1.7 rillig # [^ matches TODO
171 1.7 rillig # [- matches TODO
172 1.7 rillig # [xy matches TODO
173 1.7 rillig # [^x matches TODO
174 1.7 rillig # [\ matches TODO
175 1.7 rillig #
176 1.7 rillig # [x- matches exactly 'x', doesn't match 'x-'
177 1.7 rillig # [^x- matches TODO
178 1.7 rillig # \ matches never
179 1.7 rillig
180 1.7 rillig
181 1.7 rillig # The modifier ':tW' prevents splitting at whitespace. Even leading and
182 1.7 rillig # trailing whitespace is preserved.
183 1.7 rillig .if ${ plain string :L:tW:M*} != " plain string "
184 1.7 rillig . error
185 1.7 rillig .endif
186 1.7 rillig
187 1.7 rillig # Without the modifier ':tW', the string is split into words. All whitespace
188 1.7 rillig # around and between the words is normalized to a single space.
189 1.7 rillig .if ${ plain string :L:M*} != "plain string"
190 1.7 rillig . error
191 1.7 rillig .endif
192 1.9 rillig
193 1.9 rillig
194 1.9 rillig # The pattern can come from a variable expression. For single-letter
195 1.9 rillig # variables, either the short form or the long form can be used, just as
196 1.9 rillig # everywhere else.
197 1.9 rillig PRIMES= 2 3 5 7 11
198 1.9 rillig n= 2
199 1.9 rillig .if ${PRIMES:M$n} != "2"
200 1.9 rillig . error
201 1.9 rillig .endif
202 1.9 rillig .if ${PRIMES:M${n}} != "2"
203 1.9 rillig . error
204 1.9 rillig .endif
205 1.9 rillig .if ${PRIMES:M${:U2}} != "2"
206 1.9 rillig . error
207 1.9 rillig .endif
208