From 559779b031a5d0e226daa6c9832ff698e74c5e42 Mon Sep 17 00:00:00 2001 From: Andreas Zeller Date: Mon, 10 Feb 2025 16:52:04 +0100 Subject: [PATCH 1/3] Fix: did not check bit values during parsing (#329) --- src/fandango/language/grammar.py | 5 ++++- tests/resources/b.txt | 1 + tests/resources/bitstream-a.fan | 2 ++ tests/test_parsing.py | 17 ++++++++++++++++- 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 tests/resources/b.txt create mode 100644 tests/resources/bitstream-a.fan diff --git a/src/fandango/language/grammar.py b/src/fandango/language/grammar.py index ea3dec8..5d2a2a7 100644 --- a/src/fandango/language/grammar.py +++ b/src/fandango/language/grammar.py @@ -711,7 +711,10 @@ def scan_bit( byte = ord(word[w]) if isinstance(word, str) else word[w] bit = (byte >> bit_count) & 1 - if not state.dot.check(bit): + # LOGGER.debug(f"Checking {state.dot} against {bit}") + match, match_length = state.dot.check(bit) + if not match: + # LOGGER.debug(f"No match") return False # Found a match diff --git a/tests/resources/b.txt b/tests/resources/b.txt new file mode 100644 index 0000000..63d8dbd --- /dev/null +++ b/tests/resources/b.txt @@ -0,0 +1 @@ +b \ No newline at end of file diff --git a/tests/resources/bitstream-a.fan b/tests/resources/bitstream-a.fan new file mode 100644 index 0000000..5ee33e3 --- /dev/null +++ b/tests/resources/bitstream-a.fan @@ -0,0 +1,2 @@ + ::= + + ::= 0 1 1 0 0 0 0 1 # Encodes b'a' diff --git a/tests/test_parsing.py b/tests/test_parsing.py index fcf33f3..1fbbb73 100755 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -273,7 +273,7 @@ def test_gif(self): self.assertEqual("", out) self.assertEqual(0, code) -class TestEntryParsing(TestCLIParsing): +class TestBitstreamParsing(TestCLIParsing): def test_bitstream(self): command = shlex.split("fandango parse -f tests/resources/bitstream.fan tests/resources/abcd.txt --validate") out, err, code = self.run_command(command) @@ -281,3 +281,18 @@ def test_bitstream(self): # self.assertEqual("", err) self.assertEqual("", out) self.assertEqual(0, code) + + def test_bitstream_a(self): + command = shlex.split("fandango parse -f tests/resources/bitstream-a.fan tests/resources/a.txt --validate") + out, err, code = self.run_command(command) + self.assertEqual("", err) + self.assertEqual("", out) + self.assertEqual(0, code) + + def test_bitstream_b(self): + command = shlex.split("fandango parse -f tests/resources/bitstream-a.fan tests/resources/b.txt --validate") + out, err, code = self.run_command(command) + # This should fail + self.assertNotEqual("", err) + self.assertEqual("", out) + self.assertEqual(1, code) From 8e17b3e2174ea440b279f28e3661debd17f5469b Mon Sep 17 00:00:00 2001 From: Andreas Zeller Date: Mon, 10 Feb 2025 16:54:08 +0100 Subject: [PATCH 2/3] Moved rgb test to test_parsing --- tests/test_cli.py | 9 --------- tests/test_parsing.py | 10 ++++++++++ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index daab381..8b38190 100755 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -94,15 +94,6 @@ def test_unsat(self): self.assertEqual("", out) self.assertEqual(expected, err) - def test_parse(self): - command = shlex.split( - "fandango parse -f tests/resources/rgb.fan tests/resources/rgb.txt" - ) - out, err, code = self.run_command(command) - self.assertEqual(0, code) - self.assertEqual("", out) - self.assertEqual("", err) - def test_binfinity(self): command = shlex.split( "fandango fuzz -f docs/binfinity.fan -n 1 --format=none --validate --random-seed 426912" diff --git a/tests/test_parsing.py b/tests/test_parsing.py index 1fbbb73..933f824 100755 --- a/tests/test_parsing.py +++ b/tests/test_parsing.py @@ -296,3 +296,13 @@ def test_bitstream_b(self): self.assertNotEqual("", err) self.assertEqual("", out) self.assertEqual(1, code) + + def test_rgb(self): + command = shlex.split( + "fandango parse -f tests/resources/rgb.fan tests/resources/rgb.txt --validate" + ) + out, err, code = self.run_command(command) + self.assertEqual(0, code) + self.assertEqual("", out) + self.assertEqual("", err) + From 2516aba13c9ade2e152d7d03395452b8354dfa9e Mon Sep 17 00:00:00 2001 From: Andreas Zeller Date: Mon, 10 Feb 2025 16:57:17 +0100 Subject: [PATCH 3/3] New: avoid * for now --- tests/resources/rgb.fan | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/resources/rgb.fan b/tests/resources/rgb.fan index 444491c..25fbdb5 100644 --- a/tests/resources/rgb.fan +++ b/tests/resources/rgb.fan @@ -1,5 +1,5 @@ ::= - ::= * + ::= + ::= ::= b'r' ::=