Skip to content

Commit

Permalink
Give an example for AMBIG_ERROR.
Browse files Browse the repository at this point in the history
I think at the time of writing, fsm_example() is broken, because I get no output here. Possibly related to #438
  • Loading branch information
katef committed Jun 19, 2024
1 parent aa0eca1 commit b6fe284
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/rx/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
#include <re/literal.h>
#include <re/strings.h>

enum ambig {
AMBIG_ERROR,
AMBIG_EARLIEST,
AMBIG_MULTIPLE
};

enum category {
CATEGORY_EMPTY,
CATEGORY_LITERAL,
Expand All @@ -64,10 +70,9 @@ struct id_set {
fsm_end_id_t *a;
};

enum ambig {
AMBIG_ERROR,
AMBIG_EARLIEST,
AMBIG_MULTIPLE
struct endleaf_env {
enum ambig ambig;
const struct fsm *fsm;
};

typedef void (print_fsm)(struct fsm *, const char *, enum ambig);
Expand Down Expand Up @@ -485,10 +490,11 @@ static int
endleaf_c(FILE *f, const fsm_end_id_t *ids, size_t count,
const void *endleaf_opaque)
{
assert(ids != NULL);
assert(endleaf_opaque != NULL);
const struct endleaf_env *env = endleaf_opaque;

enum ambig ambig = * (const enum ambig *) endleaf_opaque;
assert(ids != NULL);
assert(env != NULL);
assert(env->fsm != NULL);

/* morally an assertion, but I feel better leaving this in for various user data */
if (count == 0) {
Expand All @@ -502,19 +508,27 @@ endleaf_c(FILE *f, const fsm_end_id_t *ids, size_t count,

/* exactly one end id means no ambiguious patterns */

switch (ambig) {
switch (env->ambig) {
case AMBIG_ERROR:
if (count > 1) {
char buf[50]; /* 50 looks reasonable for an on-screen limit */
int n;

n = fsm_example(env->fsm, ids[0], buf, sizeof buf);
if (-1 == n) {
perror("fsm_example");
exit(EXIT_FAILURE);
}

// TODO: explain this more clearly
fprintf(stderr, "ambigious patterns:");

for (fsm_end_id_t i = 0; i < count; i++) {
fprintf(stderr, " %u", ids[i]);
}

// TODO: give example, lx-style

fprintf(stderr, "\n");
fprintf(f, "; for example on input '%s%s'\n", buf,
n >= (int) sizeof buf - 1 ? "..." : "");

exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -598,7 +612,7 @@ print_fsm_vmc(struct fsm *fsm, const char *prefix, enum ambig ambig)

tmp = *old;
tmp.fragment = true,
tmp.endleaf_opaque = &ambig,
tmp.endleaf_opaque = & (struct endleaf_env) { ambig, fsm };
tmp.endleaf = endleaf_c,

fsm_setoptions(fsm, &tmp);
Expand Down

0 comments on commit b6fe284

Please sign in to comment.