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