@@ -466,7 +466,7 @@ class Value(metaclass=ABCMeta):
466
466
value-castable objects, and the Amaranth standard library provides several built-in ones:
467
467
468
468
* :mod:`amaranth.lib.enum` classes are a drop-in replacement for the standard Python
469
- :class :`enum` classes that can be defined with an Amaranth shape;
469
+ :mod :`enum` classes that can be defined with an Amaranth shape;
470
470
* :mod:`amaranth.lib.data` classes allow defining complex data structures such as structures
471
471
and unions.
472
472
@@ -1087,22 +1087,35 @@ def __len__(self):
1087
1087
def __getitem__ (self , key ):
1088
1088
"""Bit slicing.
1089
1089
1090
- .. todo::
1090
+ Selects a constant-width, constant-offset part of :py:`self`. All three slicing syntaxes
1091
+ (:py:`self[i]`, :py:`self[i:j]`, and :py:`self[i:j:k]`) as well as negative indices are
1092
+ supported. Like with other Python containers, out-of-bounds indices are trimmed to
1093
+ the bounds of :py:`self`.
1091
1094
1092
- Describe this operation.
1093
- """
1094
- n = len (self )
1095
+ To select a variable-offset part of :py:`self`, use :meth:`bit_select` or
1096
+ :meth:`word_select` instead.
1097
+
1098
+ Returns
1099
+ -------
1100
+ :class:`Value`, :py:`unsigned(1)`, :ref:`assignable <lang-assignable>`
1101
+ If :py:`key` is an :class:`int`.
1102
+ :class:`Value`, :py:`unsigned(j - i)`, :ref:`assignable <lang-assignable>`
1103
+ If :py:`key` is a slice :py:`i:j` where :py:`i` and :py:`j` are :class:`int`\\ s.
1104
+ :class:`Value`, :py:`unsigned(len(range(*slice(i, j, k).indices(len(self)))))`, :ref:`assignable <lang-assignable>`
1105
+ If :py:`key` is a slice :py:`i:j:k` where :py:`i`, :py:`j`, and :py:`k` are :class:`int`\\ s.
1106
+ """
1107
+ length = len (self )
1095
1108
if isinstance (key , int ):
1096
- if key not in range (- n , n ):
1097
- raise IndexError (f"Index { key } is out of bounds for a { n } -bit value" )
1109
+ if key not in range (- length , length ):
1110
+ raise IndexError (f"Index { key } is out of bounds for a { length } -bit value" )
1098
1111
if key < 0 :
1099
- key += n
1112
+ key += length
1100
1113
return Slice (self , key , key + 1 , src_loc_at = 1 )
1101
1114
elif isinstance (key , slice ):
1102
1115
if isinstance (key .start , Value ) or isinstance (key .stop , Value ):
1103
1116
raise TypeError (f"Cannot slice value with a value; use Value.bit_select() or "
1104
1117
f"Value.word_select() instead" )
1105
- start , stop , step = key .indices (n )
1118
+ start , stop , step = key .indices (length )
1106
1119
if step != 1 :
1107
1120
return Cat (self [i ] for i in range (start , stop , step ))
1108
1121
return Slice (self , start , stop , src_loc_at = 1 )
@@ -1220,14 +1233,19 @@ def matches(self, *patterns):
1220
1233
"""Pattern matching.
1221
1234
1222
1235
Matches against a set of patterns, recognizing the same grammar as :py:`with m.Case()`.
1236
+ The pattern syntax is described in the :ref:`language guide <lang-matchop>`.
1223
1237
1224
- .. todo::
1225
-
1226
- Describe the pattern language in detail.
1238
+ Each of the :py:`patterns` may be a :class:`str` or a :ref:`constant-castable object
1239
+ <lang-constcasting>`.
1227
1240
1228
1241
Returns
1229
1242
-------
1230
1243
:class:`Value`, :py:`unsigned(1)`
1244
+
1245
+ Raises
1246
+ ------
1247
+ :exc:`SyntaxError`
1248
+ If a pattern has invalid syntax.
1231
1249
"""
1232
1250
matches = []
1233
1251
# This code should accept exactly the same patterns as `with m.Case(...):`.
0 commit comments