Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion murphi-format/src/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
25 changes: 24 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 `==><NL><NL>`"


@pytest.mark.parametrize("component", ("list", "set"))
@pytest.mark.skipif(shutil.which("m4") is None, reason="m4 not available")
def test_stdlib(component, tmp_path):
Expand Down
Loading