datatype: misspell support with symbol table parser for error reporting
Pablo Neira Ayuso
maintainerPATCH 1/2 datatype: misspell support with symbol table parser for error reporting
Some datatypes provide a symbol table that is parsed as an integer. Improve error reporting by using the misspell infrastructure, to provide a hint to the user, whenever possible.
If base datatype, usually the integer datatype, fails to parse the symbol, then try a fuzzy match on the symbol table to provide a hint in case the user has mistype it.
For instance:
test.nft:3:11-14: Error: Could not parse Differentiated Services Code Point expression; did you you mean cs0?
ip dscp ccs0
^^^^
--- src/datatype.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-)
src/datatype.c +48 -2
@@ -28,6 +28,7 @@#include <erec.h> #include <netlink.h> #include <json.h> +#include <misspell.h> #include <netinet/ip_icmp.h>
@@ -141,6 +142,43 @@ struct error_record *symbol_parse(struct parse_ctx *ctx, const struct expr *sym, sym->dtype->desc); } +static struct error_record *__symbol_parse_fuzzy (const struct expr *sym, +const struct symbol_table *tbl) +{ +const struct symbolic_constant *s; +struct string_misspell_state st; + +string_misspell_init (&st); + +for (s = tbl->symbols; s->identifier !=NULL ; s++) { +string_misspell_update (sym->identifier, s->identifier, + (void *)s->identifier, &st); + } + +if (st.obj) { +return error (&sym->location, +"Could not parse %s expression; did you you mean `%s`?" , + sym->dtype->desc, st.obj); + } + +return NULL ; +} + +static struct error_record *symbol_parse_fuzzy (const struct expr *sym, +const struct symbol_table *tbl) +{ +struct error_record *erec; + +if (!tbl) +return NULL ; + + erec =__symbol_parse_fuzzy (sym, tbl); +if (erec) +return erec; + +return NULL ; +} + struct error_record *symbolic_constant_parse(struct parse_ctx *ctx, const struct expr *sym, const struct symbol_table *tbl,
@@ -163,8 +201,16 @@ struct error_record *symbolic_constant_parse(struct parse_ctx *ctx, do { if (dtype->basetype->parse) { erec = dtype->basetype->parse(ctx, sym, res); - if (erec != NULL) - return erec; + if (erec != NULL) { + struct error_record *fuzzy_erec; + + fuzzy_erec = symbol_parse_fuzzy(sym, tbl); + if (!fuzzy_erec) + return erec; + + erec_destroy(erec); + return fuzzy_erec; + } if (*res) return NULL; goto out;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/2 datatype: add hint on error handler
If user provides a symbol that cannot be parsed and the datatype provides an error handler, provide a hint through the misspell infrastructure.
For instance:
# cat test.nft
table ip x {
map y {
typeof ip saddr : verdict
elements = { 1.2.3.4 : filter_server1 }
}
}
# nft -f test.nft
test.nft:4:26-39: Error: Could not parse netfilter verdict; did you mean `jump filter_server1'?
elements = { 1.2.3.4 : filter_server1 }
^^^^^^^^^^^^^^While at it, normalize error to "Could not parse symbolic %s expression".
--- include/datatype.h | 1 + src/datatype.c | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-)
include/datatype.h +1 -0
@@ -167,6 +167,7 @@ struct datatype {struct error_record *(*parse)(struct parse_ctx *ctx,const struct expr *sym,struct expr **res); +struct error_record *(*err)(const struct expr *sym);void (*describe)(struct output_ctx *octx);const struct symbol_table *sym_tbl;unsigned int refcnt;
src/datatype.c +39 -2
64 lines not on this page · raw message
Signature · 1 line
2.30.2