diff --git a/murphi-format/src/format.c b/murphi-format/src/format.c index e3524b17..10b9b79e 100644 --- a/murphi-format/src/format.c +++ b/murphi-format/src/format.c @@ -54,6 +54,9 @@ typedef struct state { /// have we output anything to `dst` yet? bool started; + + /// have we seen `==>` and are thus anticipating `begin`? + bool arrow_expecting_begin; } state_t; /// are two strings equal? @@ -135,8 +138,13 @@ static int pend_newline(state_t *st, token_t token) { if (token.type == TOKEN_EOF) goto done; size_t indentation = st->indentation; - if (token.type != TOKEN_ID || !streq(token.text, "begin")) + if (token.type != TOKEN_ID || !streq(token.text, "begin")) { indentation += st->soft_indentation; + } else if (st->arrow_expecting_begin) { + st->indentation--; + --indentation; + st->arrow_expecting_begin = false; + } for (size_t i = 0; i < indentation; ++i) { if (fputs(tab, st->dst) < 0) { rc = EIO; @@ -165,6 +173,7 @@ static int arrow_lookahead(state_t *st, token_t token) { // otherwise infer a newline and indent ++st->indentation; + st->arrow_expecting_begin = true; rc = pend_newline(st, token); done: diff --git a/tests/tests.py b/tests/tests.py index d616ecbd..91ed99dd 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -1074,7 +1074,7 @@ def test_murphi_format_not_unicode(): assert ":= ¬y" in stdout, "`¬` spaced incorrectly" -def test_muprhi_format_smart_quotes(): +def test_murphi_format_smart_quotes(): """murphi-format should handle smart quotes (“”) correctly""" model = "rule begin assert “foo bar” x; end" @@ -1216,6 +1216,29 @@ def test_murphi_format_end_newline(): assert stdout.endswith("\n"), "incorrect file ending" +def test_murphi_format_arrow_break_dedent(): + """ + is murphi-format’s indenting confused by `==>\n\n`? + + https://github.com/Smattr/rumur/issues/339 + """ + + # a rule that has a paragraph break after `==>` + src = 'rule"foo"==>\n\nvar x:boolean;begin x:=false;end;' + + # run this through `murphi-format` + ret, stdout, stderr = run(["murphi-format"], src) + if ret != 0: + sys.stdout.write(stdout) + sys.stderr.write(stderr) + assert ret == 0, "failed to reflow Murphi snippet" + assert stderr == "", "murphi-format printed errors/warnings" + + # if indentation was not mismanaged, ending indentation should be 0 + # did we get the expected reflowing? + assert stdout.endswith("\nend;\n"), "incorrect handling of `==>`" + + @pytest.mark.parametrize("component", ("list", "set")) @pytest.mark.skipif(shutil.which("m4") is None, reason="m4 not available") def test_stdlib(component, tmp_path):