Skip to content

Commit

Permalink
Separate calling points for the comments hook.
Browse files Browse the repository at this point in the history
This gives better control over whitespace and punctuation between the hooks. For example we can output "<accept>, <comment>\n" with a comma between, and that sits more nicely for single-line comments. Previously these had to be "<accept> <comment>,\n"
  • Loading branch information
katef committed Aug 14, 2024
1 parent 690f0e2 commit 0f4d838
Show file tree
Hide file tree
Showing 17 changed files with 150 additions and 38 deletions.
2 changes: 1 addition & 1 deletion include/fsm/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct fsm_hooks {

int (*comment)(FILE *, const struct fsm_options *opt,
const fsm_end_id_t *ids, size_t count,
void *lang_opaque, void *hook_opaque);
void *hook_opaque);

/* only called for AMBIG_ERROR; see opt.ambig */
int (*conflict)(FILE *, const struct fsm_options *opt,
Expand Down
29 changes: 19 additions & 10 deletions src/libfsm/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ print_hook_accept(FILE *f,
void *lang_opaque, void *hook_opaque),
void *lang_opaque)
{
int r;

assert(f != NULL);
assert(opt != NULL);
assert(hooks != NULL);
Expand All @@ -74,25 +72,36 @@ print_hook_accept(FILE *f,
}

if (hooks->accept != NULL) {
r = hooks->accept(f, opt, ids, count,
return hooks->accept(f, opt, ids, count,
lang_opaque, hooks->hook_opaque);
} else if (default_accept != NULL) {
r = default_accept(f, opt, ids, count,
return default_accept(f, opt, ids, count,
lang_opaque, hooks->hook_opaque);
} else {
r = 0;
}

if (r != 0) {
return r;
return 0;
}

int
print_hook_comment(FILE *f,
const struct fsm_options *opt,
const struct fsm_hooks *hooks,
const fsm_end_id_t *ids, size_t count)
{
assert(f != NULL);
assert(opt != NULL);
assert(hooks != NULL);

if (opt->ambig == AMBIG_ERROR) {
assert(count <= 1);
}

if (opt->comments && hooks->comment != NULL) {
/* this space is a polyglot */
fprintf(f, " ");

r = hooks->comment(f, opt, ids, count,
lang_opaque, hooks->hook_opaque);
return hooks->comment(f, opt, ids, count,
hooks->hook_opaque);
}

return 0;
Expand Down
6 changes: 6 additions & 0 deletions src/libfsm/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ print_hook_accept(FILE *f,
void *lang_opaque, void *hook_opaque),
void *lang_opaque);

int
print_hook_comment(FILE *f,
const struct fsm_options *opt,
const struct fsm_hooks *hooks,
const fsm_end_id_t *ids, size_t count);

int
print_hook_reject(FILE *f,
const struct fsm_options *opt,
Expand Down
15 changes: 13 additions & 2 deletions src/libfsm/print/awk.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,21 @@ print_end(FILE *f, const struct dfavm_op_ir *op,
return print_hook_reject(f, opt, hooks, default_reject, NULL);

case VM_END_SUCC:
return print_hook_accept(f, opt, hooks,
if (-1 == print_hook_accept(f, opt, hooks,
op->ret->ids, op->ret->count,
default_accept,
NULL);
NULL))
{
return -1;
}

if (-1 == print_hook_comment(f, opt, hooks,
op->ret->ids, op->ret->count))
{
return -1;
}

return 0;

default:
assert(!"unreached");
Expand Down
6 changes: 6 additions & 0 deletions src/libfsm/print/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,12 @@ print_endstates(FILE *f,
return -1;
}

if (-1 == print_hook_comment(f, opt, hooks,
ir->states[i].endids.ids, ir->states[i].endids.count))
{
return -1;
}

fprintf(f, "\n");
}

Expand Down
10 changes: 10 additions & 0 deletions src/libfsm/print/dot.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ print_dotfrag(FILE *f,
return -1;
}

if (opt->comments && hooks->comment != NULL) {
fprintf(f, ",");

if (-1 == print_hook_comment(f, opt, hooks,
ids, count))
{
return -1;
}
}

fprintf(f, " ];\n");

f_free(fsm->alloc, ids);
Expand Down
8 changes: 7 additions & 1 deletion src/libfsm/print/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,13 @@ fsm_print_fsm(FILE *f,
if (-1 == print_hook_accept(f, opt, hooks,
ids, count,
default_accept,
NULL))
NULL))
{
return -1;
}

if (-1 == print_hook_comment(f, opt, hooks,
ids, count))
{
return -1;
}
Expand Down
6 changes: 6 additions & 0 deletions src/libfsm/print/go.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ print_end(FILE *f, const struct dfavm_op_ir *op,
return -1;
}

if (-1 == print_hook_comment(f, opt, hooks,
op->ret->ids, op->ret->count))
{
return -1;
}

fprintf(f, "\n\t}\n");

return 0;
Expand Down
2 changes: 0 additions & 2 deletions src/libfsm/print/irdot.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,6 @@ print_state(FILE *f,
fprintf(f, "</TD></TR>\n");
}

/* TODO: leaf callback for dot output */

/* showing hook in addition to existing content */
if (cs->isend && hooks->accept != NULL) {
fprintf(f, "\t\t <TR><TD COLSPAN='3' ALIGN='LEFT'>");
Expand Down
18 changes: 17 additions & 1 deletion src/libfsm/print/llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,13 @@ fsm_print_llvm(FILE *f,
{
return -1;
}

if (-1 == print_hook_comment(f, opt, hooks,
retlist->a[i].ids, retlist->a[i].count))
{
return -1;
}

fprintf(f, "\n");
}
}
Expand All @@ -678,15 +685,24 @@ fsm_print_llvm(FILE *f,
if (opt->ambig == AMBIG_MULTIPLE) {
fprintf(f, "%%rt { %s bitcast ([%zu x i32]* @%sr%zu to %s), i64 %zu }",
ptr_i32, retlist->a[i].count, prefix, i, ptr_i32, retlist->a[i].count);
fprintf(f, ",");
} else {
if (-1 == print_hook_accept(f, opt, hooks,
retlist->a[i].ids, retlist->a[i].count,
default_accept, NULL))
{
return -1;
}

fprintf(f, ",");

if (-1 == print_hook_comment(f, opt, hooks,
retlist->a[i].ids, retlist->a[i].count))
{
return -1;
}
}
fprintf(f, ",\n");
fprintf(f, "\n");
}
fprintf(f, "\t ");
if (-1 == print_hook_reject(f, opt, hooks, default_reject, NULL)) {
Expand Down
8 changes: 8 additions & 0 deletions src/libfsm/print/rust.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,14 @@ fsm_print_rustfrag(FILE *f,
fprintf(f, " }");
}

if (op->u.stop.end_bits == VM_END_SUCC) {
if (-1 == print_hook_comment(f, opt, hooks,
op->ret->ids, op->ret->count))
{
return -1;
}
}

if (op->cmp == VM_CMP_ALWAYS) {
/* the code for fallthrough would be unreachable */
fallthrough = false;
Expand Down
15 changes: 13 additions & 2 deletions src/libfsm/print/sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,21 @@ print_end(FILE *f, const struct dfavm_op_ir *op,
return print_hook_reject(f, opt, hooks, default_reject, NULL);

case VM_END_SUCC:
return print_hook_accept(f, opt, hooks,
if (-1 == print_hook_accept(f, opt, hooks,
op->ret->ids, op->ret->count,
default_accept,
NULL);
NULL))
{
return -1;
}

if (-1 == print_hook_comment(f, opt, hooks,
op->ret->ids, op->ret->count))
{
return -1;
}

return 0;

default:
assert(!"unreached");
Expand Down
5 changes: 5 additions & 0 deletions src/libfsm/print/vmasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ print_end(FILE *f, const struct dfavm_op_ir *op,
{
return -1;
}
if (-1 == print_hook_comment(f, opt, hooks,
op->ret->ids, op->ret->count))
{
return -1;
}
break;

default:
Expand Down
15 changes: 13 additions & 2 deletions src/libfsm/print/vmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,21 @@ print_end(FILE *f, const struct dfavm_op_ir *op,
return print_hook_reject(f, opt, hooks, default_reject, NULL);

case VM_END_SUCC:
return print_hook_accept(f, opt, hooks,
if (-1 == print_hook_accept(f, opt, hooks,
op->ret->ids, op->ret->count,
default_accept,
NULL);
NULL))
{
return -1;
}

if (-1 == print_hook_comment(f, opt, hooks,
op->ret->ids, op->ret->count))
{
return -1;
}

return 0;

default:
assert(!"unreached");
Expand Down
11 changes: 9 additions & 2 deletions src/libfsm/print/vmdot.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,17 @@ print_end(FILE *f,
return print_hook_reject(f, opt, hooks, default_reject, NULL);

case VM_END_SUCC:
return print_hook_accept(f, opt, hooks,
if (-1 == print_hook_accept(f, opt, hooks,
op->ret->ids, op->ret->count,
default_accept,
NULL);
NULL))
{
return -1;
}

/* no print_hook_comment() for dot output */

return 0;

default:
assert(!"unreached");
Expand Down
12 changes: 11 additions & 1 deletion src/libfsm/print/vmops.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,17 @@ fsm_print_vmops_c(FILE *f,
break;
}

fprintf(f, "},\n");
fprintf(f, "},");

if (op->instr == VM_OP_STOP && op->u.stop.end_bits == VM_END_SUCC) {
if (-1 == print_hook_comment(f, opt, hooks,
op->ret->ids, op->ret->count))
{
return -1;
}
}

fprintf(f, "\n");
}

fprintf(f, "\t};\n");
Expand Down
Loading

0 comments on commit 0f4d838

Please sign in to comment.