1# $NetBSD: moderrs.mk,v 1.25 2020/11/15 20:20:58 rillig Exp $
2#
3# various modifier error tests
4
5'=		'\''
6VAR=		TheVariable
7# in case we have to change it ;-)
8MOD_UNKN=	Z
9MOD_TERM=	S,V,v
10MOD_S:=		${MOD_TERM},
11
12FIB=	1 1 2 3 5 8 13 21 34
13
14all:	mod-unknown-direct mod-unknown-indirect
15all:	unclosed-direct unclosed-indirect
16all:	unfinished-indirect unfinished-loop
17all:	loop-close
18all:	words
19all:	exclam
20all:	mod-subst-delimiter
21all:	mod-regex-delimiter
22all:	mod-regex-undefined-subexpression
23all:	mod-ts-parse
24all:	mod-t-parse
25all:	mod-ifelse-parse
26all:	mod-remember-parse
27all:	mod-sysv-parse
28
29mod-unknown-direct: print-header print-footer
30	@echo 'want: Unknown modifier $'Z$''
31	@echo 'VAR:Z=before-${VAR:Z}-after'
32
33mod-unknown-indirect: print-header print-footer
34	@echo 'want: Unknown modifier $'Z$''
35	@echo 'VAR:${MOD_UNKN}=before-${VAR:${MOD_UNKN}:inner}-after'
36
37unclosed-direct: print-header print-footer
38	@echo 'want: Unclosed variable specification (expecting $'}$') for "VAR" (value "Thevariable") modifier S'
39	@echo VAR:S,V,v,=${VAR:S,V,v,
40
41unclosed-indirect: print-header print-footer
42	@echo 'want: Unclosed variable specification after complex modifier (expecting $'}$') for VAR'
43	@echo VAR:${MOD_TERM},=${VAR:${MOD_S}
44
45unfinished-indirect: print-header print-footer
46	@echo 'want: Unfinished modifier for VAR ($',$' missing)'
47	-@echo "VAR:${MOD_TERM}=${VAR:${MOD_TERM}}"
48
49unfinished-loop: print-header print-footer
50	@echo 'want: Unfinished modifier for UNDEF ($'@$' missing)'
51	@echo ${UNDEF:U1 2 3:@var}
52	@echo 'want: Unfinished modifier for UNDEF ($'@$' missing)'
53	@echo ${UNDEF:U1 2 3:@var@...}
54	@echo ${UNDEF:U1 2 3:@var@${var}@}
55
56# The closing brace after the ${var} is part of the replacement string.
57# In ParseModifierPart, braces and parentheses don't have to be balanced.
58# This is contrary to the :M, :N modifiers, where both parentheses and
59# braces must be balanced.
60# This is also contrary to the SysV modifier, where only the actually
61# used delimiter (either braces or parentheses) must be balanced.
62loop-close: print-header print-footer
63	@echo ${UNDEF:U1 2 3:@var@${var}}...@
64	@echo ${UNDEF:U1 2 3:@var@${var}}...@}
65
66words: print-header print-footer
67	@echo 'want: Unfinished modifier for UNDEF ($']$' missing)'
68	@echo ${UNDEF:U1 2 3:[}
69	@echo 'want: Unfinished modifier for UNDEF ($']$' missing)'
70	@echo ${UNDEF:U1 2 3:[#}
71
72	# out of bounds => empty
73	@echo 13=${UNDEF:U1 2 3:[13]}
74
75	# Word index out of bounds.
76	#
77	# Until 2020-11-01, the behavior in this case depended upon the size
78	# of unsigned long.
79	#
80	# On LP64I32, strtol returns LONG_MAX, which was then truncated to
81	# int (undefined behavior), typically resulting in -1.  This -1 was
82	# interpreted as "the last word".
83	#
84	# On ILP32, strtol returns LONG_MAX, which is a large number.  This
85	# resulted in a range from LONG_MAX - 1 to 3, which was empty.
86	#
87	# Since 2020-11-01, the numeric overflow is detected and generates an
88	# error.  In the remainder of the text, the '$,' is no longer parsed
89	# as part of a variable modifier, where it would have been interpreted
90	# as an anchor to the :S modifier, but as a normal variable named ','.
91	# That variable is undefined, resulting in an empty string.
92	@echo 12345=${UNDEF:U1 2 3:[123451234512345123451234512345]:S,^$,ok,:S,^3$,ok,}
93
94exclam: print-header print-footer
95	@echo 'want: Unfinished modifier for VARNAME ($'!$' missing)'
96	@echo ${VARNAME:!echo}
97	# When the final exclamation mark is missing, there is no
98	# fallback to the SysV substitution modifier.
99	# If there were a fallback, the output would be "exclam",
100	# and the above would have produced an "Unknown modifier '!'".
101	@echo 'want: Unfinished modifier for ! ($'!$' missing)'
102	@echo ${!:L:!=exclam}
103
104mod-subst-delimiter: print-header print-footer
105	@echo 1: ${VAR:S
106	@echo 2: ${VAR:S,
107	@echo 3: ${VAR:S,from
108	@echo 4: ${VAR:S,from,
109	@echo 5: ${VAR:S,from,to
110	@echo 6: ${VAR:S,from,to,
111	@echo 7: ${VAR:S,from,to,}
112
113mod-regex-delimiter: print-header print-footer
114	@echo 1: ${VAR:C
115	@echo 2: ${VAR:C,
116	@echo 3: ${VAR:C,from
117	@echo 4: ${VAR:C,from,
118	@echo 5: ${VAR:C,from,to
119	@echo 6: ${VAR:C,from,to,
120	@echo 7: ${VAR:C,from,to,}
121
122# In regular expressions with alternatives, not all capturing groups are
123# always set; some may be missing.  Warn about these.
124#
125# Since there is no way to turn off this warning, the combination of
126# alternative matches and capturing groups is seldom used, if at all.
127#
128# A newly added modifier 'U' such as in :C,(a.)|(b.),\1\2,U might be added
129# for treating undefined capturing groups as empty, but that would create a
130# syntactical ambiguity since the :S and :C modifiers are open-ended (see
131# mod-subst-chain).  Luckily the modifier :U does not make sense after :C,
132# therefore this case does not happen in practice.
133# The sub-modifier for the :S and :C modifiers would have to be chosen
134# wisely, to not create ambiguities while parsing.
135mod-regex-undefined-subexpression: print-header print-footer
136	@echo ${FIB:C,1(.*),one\1,}		# all ok
137	@echo ${FIB:C,1(.*)|2(.*),(\1)+(\2),:Q}	# no match for subexpression
138
139mod-ts-parse: print-header print-footer
140	@echo ${FIB:ts}
141	@echo ${FIB:ts\65}	# octal 065 == U+0035 == '5'
142	@echo ${FIB:ts\65oct}	# bad modifier
143	@echo ${FIB:tsxy}	# modifier too long
144
145mod-t-parse: print-header print-footer
146	@echo ${FIB:t
147	@echo ${FIB:txy}
148	@echo ${FIB:t}
149	@echo ${FIB:t:M*}
150
151mod-ifelse-parse: print-header print-footer
152	@echo ${FIB:?
153	@echo ${FIB:?then
154	@echo ${FIB:?then:
155	@echo ${FIB:?then:else
156	@echo ${FIB:?then:else}
157
158mod-remember-parse: print-header print-footer
159	@echo ${FIB:_}		# ok
160	@echo ${FIB:__}		# modifier name too long
161
162mod-sysv-parse: print-header print-footer
163	@echo ${FIB:3
164	@echo ${FIB:3=
165	@echo ${FIB:3=x3
166	@echo ${FIB:3=x3}	# ok
167
168print-header: .USEBEFORE
169	@echo $@:
170print-footer: .USE
171	@echo
172