Fixes #316.
raise SyntaxError("Match pattern must be an integer, a string, or an enumeration, "
"not {!r}"
.format(pattern))
- if isinstance(pattern, str) and any(bit not in "01-" for bit in pattern):
+ if isinstance(pattern, str) and any(bit not in "01- \t" for bit in pattern):
raise SyntaxError("Match pattern '{}' must consist of 0, 1, and - (don't care) "
- "bits"
+ "bits, and may include whitespace"
.format(pattern))
- if isinstance(pattern, str) and len(pattern) != len(self):
+ if (isinstance(pattern, str) and
+ len("".join(pattern.split())) != len(self)):
raise SyntaxError("Match pattern '{}' must have the same width as match value "
"(which is {})"
.format(pattern, len(self)))
SyntaxWarning, stacklevel=3)
continue
if isinstance(pattern, str):
+ pattern = "".join(pattern.split()) # remove whitespace
mask = int(pattern.replace("0", "1").replace("-", "0"), 2)
pattern = int(pattern.replace("-", "0"), 2)
matches.append((self & mask) == pattern)
new_keys = ()
for key in keys:
if isinstance(key, str):
- pass
+ key = "".join(key.split()) # remove whitespace
elif isinstance(key, int):
key = format(key, "b").rjust(len(self.test), "0")
elif isinstance(key, Enum):
raise SyntaxError("Case pattern must be an integer, a string, or an enumeration, "
"not {!r}"
.format(pattern))
- if isinstance(pattern, str) and any(bit not in "01-" for bit in pattern):
- raise SyntaxError("Case pattern '{}' must consist of 0, 1, and - (don't care) bits"
+ if isinstance(pattern, str) and any(bit not in "01- \t" for bit in pattern):
+ raise SyntaxError("Case pattern '{}' must consist of 0, 1, and - (don't care) "
+ "bits, and may include whitespace"
.format(pattern))
- if isinstance(pattern, str) and len(pattern) != len(switch_data["test"]):
+ if (isinstance(pattern, str) and
+ len("".join(pattern.split())) != len(switch_data["test"])):
raise SyntaxError("Case pattern '{}' must have the same width as switch value "
"(which is {})"
.format(pattern, len(switch_data["test"])))
self.assertRepr(s.matches("10--"), """
(== (& (sig s) (const 4'd12)) (const 4'd8))
""")
+ self.assertRepr(s.matches("1 0--"), """
+ (== (& (sig s) (const 4'd12)) (const 4'd8))
+ """)
def test_matches_enum(self):
s = Signal(SignedEnum)
def test_matches_bits_wrong(self):
s = Signal(4)
with self.assertRaises(SyntaxError,
- msg="Match pattern 'abc' must consist of 0, 1, and - (don't care) bits"):
+ msg="Match pattern 'abc' must consist of 0, 1, and - (don't care) bits, "
+ "and may include whitespace"):
s.matches("abc")
def test_matches_pattern_wrong(self):
m.d.comb += self.c1.eq(1)
with m.Case("11--"):
m.d.comb += self.c2.eq(1)
+ with m.Case("1 0--"):
+ m.d.comb += self.c2.eq(1)
m._flush()
self.assertRepr(m._statements, """
(
(switch (sig w1)
(case 0011 (eq (sig c1) (const 1'd1)))
(case 11-- (eq (sig c2) (const 1'd1)))
+ (case 10-- (eq (sig c2) (const 1'd1)))
)
)
""")
m = Module()
with m.Switch(self.w1):
with self.assertRaises(SyntaxError,
- msg="Case pattern 'abc' must consist of 0, 1, and - (don't care) bits"):
+ msg="Case pattern 'abc' must consist of 0, 1, and - (don't care) bits, "
+ "and may include whitespace"):
with m.Case("abc"):
pass