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