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