Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A different kind of json #198

Merged
merged 3 commits into from
Jan 19, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Output for endleaf values.
katef committed Jan 17, 2020
commit bb1c0837edc559bfdc6afc275439482b6e7a16bc
24 changes: 24 additions & 0 deletions src/libfsm/print/json.c
Original file line number Diff line number Diff line change
@@ -235,6 +235,30 @@ fsm_print_json(FILE *f, const struct fsm *fsm)
fprintf(f, " ],\n");
}

if (fsm->opt->endleaf != NULL) {
int notfirst;

notfirst = 0;

fprintf(f, " \"endleaf\": [ ");
for (i = 0; i < fsm->statecount; i++) {
if (fsm_isend(fsm, i)) {
if (notfirst) {
fprintf(f, ", ");
}

fprintf(f, "{ %u, ", i);

fsm->opt->endleaf(f, &fsm->states[i], fsm->opt->endleaf_opaque);

fprintf(f, " }");

notfirst = 1;
}
}
fprintf(f, " ],\n");
}

{
int notfirst;

31 changes: 31 additions & 0 deletions src/re/main.c
Original file line number Diff line number Diff line change
@@ -437,6 +437,35 @@ endleaf_dot(FILE *f, const void *state_opaque, const void *endleaf_opaque)
return 0;
}

static int
endleaf_json(FILE *f, const void *state_opaque, const void *endleaf_opaque)
{
const struct match *m;

assert(f != NULL);
assert(state_opaque != NULL);
assert(endleaf_opaque == NULL);

(void) endleaf_opaque;

fprintf(f, "[ ");

for (m = state_opaque; m != NULL; m = m->next) {
fprintf(f, "%u", m->i);

if (m->next != NULL) {
fprintf(f, ", ");
}
}

fprintf(f, " ]");

/* TODO: only if comments */
/* TODO: centralise to libfsm/print/json.c */

return 0;
}

int
main(int argc, char *argv[])
{
@@ -869,6 +898,8 @@ main(int argc, char *argv[])
opt.endleaf = endleaf_c;
} else if (print_fsm == fsm_print_dot) {
opt.endleaf = patterns ? endleaf_dot : NULL;
} else if (print_fsm == fsm_print_json) {
opt.endleaf = patterns ? endleaf_json : NULL;
}

print_fsm(stdout, fsm);