Skip to content

Commit ee7f8d3

Browse files
authored
[doc/ref] Fix descriptions of initializer list (#2284)
And other doc/ref fixes
1 parent 4305f5e commit ee7f8d3

File tree

1 file changed

+38
-39
lines changed

1 file changed

+38
-39
lines changed

doc/ref/chap-osh-assign.md

+38-39
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,21 @@ cleared before starting the modifications. When the assignment is performed
4545
with `+=`, the modifications are applied to the existing content of the LHS
4646
value.
4747

48-
An initializer list has the following form: `(<words>...)`, where each word has
49-
one of the following forms:
50-
51-
- `[<key>]=<value>` ... This assigns `<value>` to an element of the LHS
52-
specified by `<key>`. The `<value>` is not subject to word splitting and
53-
pathname expansions as if it is the RHS of an assignment.
54-
- `[<key>]+=<value>` ... This appends `<value>` to an element of the LHS
55-
specified by `<key>`. If the corresponding element does not exist, it simply
56-
assigns `<value>` to a new element associated with `<key>`. The `<value>` is
57-
not subject to word splitting and pathname expansions.
58-
- `<value>` ... If the word does not have the above two forms, it is considered
59-
a normal word. In this case, this assigns `<value>` to the *next* element,
60-
where the next element is determined by the LHS. Unlike the previous two
61-
forms, the `<value>` is subject to word splitting and pathname expansions as
62-
if it is a normal argument to a command.
48+
An initializer list has the following form: `'(' ITEMS* ')'`, where each item
49+
has one of the following forms:
50+
51+
- `[KEY]=VALUE` ... This assigns `VALUE` to an element of the LHS specified by
52+
`KEY`. The `VALUE` is not subject to word splitting and pathname expansions
53+
as if it is the RHS of an assignment.
54+
- `[KEY]+=VALUE` ... This appends `VALUE` to an element of the LHS specified by
55+
`KEY`. If the corresponding element does not exist, it simply assigns
56+
`VALUE` to a new element associated with `KEY`. The `VALUE` is not subject
57+
to word splitting and pathname expansions.
58+
- `VALUE` ... If the item does not have the above two forms, it is considered a
59+
normal word. In this case, this assigns `VALUE` to the *next* element, where
60+
the next element is determined by the LHS. Unlike the previous two forms,
61+
the `VALUE` is subject to word splitting and pathname expansions as if it is
62+
a normal argument to a command.
6363

6464
The above three forms can be mixed within one initializer list, though there
6565
may be additional limitations depending on the type of the LHS of the
@@ -73,21 +73,20 @@ applied.
7373
In the first phase, the type adjustment is performed in the following way:
7474

7575
- When the LHS variable is unset, the assignment creates an empty indexed array
76-
(BashArray) and apply the initializer list to the created array. If the
76+
(BashArray). If the
7777
assignment is performed through an assignment builtin and flag `-A` is
7878
supplied to the builtin, an empty associative array (BashAssoc) is created
7979
instead of an empty BashArray.
8080
- When the LHS is a scalar string, the assignment creates a BashArray with one
8181
element, where the original value is stored at index `0`. If the assignment
8282
is performed through an assignment builtin and flag `-A` is supplied to the
8383
builtin, the assignment creates a BashAssoc with one element, where the
84-
original value is stored at key `"0"` instead of a BashArray. If the
84+
original value is stored at key `"0"`, instead of a BashArray. If the
8585
assignment operator is `+=`, OSH issues an error "Can't append an array to
8686
string", while Bash is permissive.
87-
- When the LHS is an indexed or associative arrays, the initializer list is
88-
applied to the original array following [sh-array](#sh-array) (for an indexed
89-
array) or [sh-assoc](#sh-assoc) (for an associative array). If the
90-
assignemnt is performed through an assignment builtin and mismatching flag
87+
- When the LHS is an indexed or associative arrays, the original array is
88+
directly used for the modification target. If the
89+
assignment is performed through an assignment builtin and mismatching flag
9190
(i.e., `-A` and `-a` for BashArray and BashAssoc, respectively) is supplied,
9291
OSH discards the original array and creates a new empty BashArray (for flag
9392
`-a`) or BashAssoc (for flag `-A`), while Bash issues an error preserving the
@@ -116,7 +115,7 @@ These rules are summarized in the following table.
116115
- tr
117116
- <!-- empty -->
118117
- `-A`
119-
- an empty BashArray
118+
- an empty BashAssoc
120119
- <!-- empty -->
121120
- tr
122121
- Str
@@ -133,11 +132,6 @@ These rules are summarized in the following table.
133132
- `-A`
134133
- BashAssoc with one element, with the original string at key `"0"`
135134
- OSH does not accept `+=`, so the element is never used
136-
- tr
137-
- <!-- empty -->
138-
- `-A`
139-
- an empty BashArray
140-
- <!-- empty -->
141135
- tr
142136
- BashArray
143137
- (none)
@@ -168,12 +162,17 @@ These rules are summarized in the following table.
168162
- `-A`
169163
- the original BashAssoc
170164
- <!-- empty -->
165+
- tr
166+
- (others)
167+
- <!-- empty -->
168+
- N/A
169+
- Error
171170

172171
</table>
173172

174173
In the second phase, the modifications are applied depending on the result of
175174
the first phase. When the result is BashArray, see [sh-array](#sh-array).
176-
When the result is BashAssoc, see [sh-array](#sh-array).
175+
When the result is BashAssoc, see [sh-assoc](#sh-assoc).
177176

178177
### sh-array
179178

@@ -191,14 +190,14 @@ cleared if the assignment operator is `=`. Then, an element of the array is
191190
modified for each item in the initializer list in order. The index of the
192191
element to be modified is determined in the following way:
193192

194-
- When the first initializer item does not have `[<key>]=` or `[<key>]+=`, the
193+
- When the first initializer item does not have `[KEY]=` or `[KEY]+=`, the
195194
index is the maximum existing index in the array plus one, or `0` if the
196195
array is empty.
197-
- When the second or later initializer item does not have `[<key>]=` or
198-
`[<key>]+=`, the index is larger by one than the one modified by the previous
196+
- When the second or later initializer item does not have `[KEY]=` or
197+
`[KEY]+=`, the index is larger by one than the one modified by the previous
199198
initializer item.
200-
- When the initialier item has `[<key>]=` or `[<key>]+=`, an arithmetic
201-
evaluation is applied to `<key>` to obtain the index in `BigInt`
199+
- When the initializer item has `[KEY]=` or `[KEY]+=`, an arithmetic evaluation
200+
is applied to `KEY` to obtain the index in `BigInt`
202201

203202
Here are examples:
204203

@@ -210,9 +209,9 @@ Here are examples:
210209
declare -a a=([k]=v 2) # This creates a sparse array with two elements,
211210
# ([10]=v [11]=2)
212211

213-
declare -a a+=(3 4) # This appends two values to the existing array:
212+
a+=(3 4) # This appends two values to the existing array:
214213
# ([10]=v [11]=2 [12]=3 [13]=4)
215-
declare -a a+=([k]=5 6) # This overwrites two elements in the existing
214+
a+=([k]=5 6) # This overwrites two elements in the existing
216215
# array: ([10]=5 [11]=6 [12]=3 [13]=4)
217216

218217
In YSH, use a [list-literal][] to create a [List][] instance.
@@ -236,8 +235,8 @@ The initialization/mutation of BashAssoc is performed in a manner similar to
236235
BashArray. The associative array is first cleared if the assignment operator
237236
is `=`. Then, the modification of an element is performed for each initializer
238237
item in order. An item in the initializer list must be in the forms
239-
`[<key>]=<value>` or `[<key>]=<value>`. The element to be modified is
240-
specified by `<key>`.
238+
`[KEY]=VALUE` or `[KEY]=VALUE`. The element to be modified is specified by
239+
`KEY`.
241240

242241
declare -A a # This creates an empty BashAssoc (OSH)
243242
declare -A a=() # This creates an empty BashAssoc
@@ -249,9 +248,9 @@ specified by `<key>`.
249248
declare -A a=([k]=v) # This creates a BashAssoc with one element,
250249
# (['k']=1). Unlike BashArray, "k" is not
251250
# processed by arithmetic expansion.
252-
declare -a a+=([a]=3 [b]=4) # This adds two elements to the existing array.
251+
a+=([a]=3 [b]=4) # This adds two elements to the original array.
253252
# The result is ([a]=3 [b]=4 [k]=v)
254-
declare -a a+=([k]=5) # This overwrites an element in the existing
253+
a+=([k]=5) # This overwrites an element in the original
255254
# array. The result is ([a]=3 [b]=4 [k]=5).
256255

257256
In YSH, use a [dict-literal][] to create a [Dict][] instance.

0 commit comments

Comments
 (0)