From b2d9339cd335460d55571b70715882c7f0dd6f92 Mon Sep 17 00:00:00 2001
From: Andrea Shepard <andrea@persephoneslair.org>
Date: Thu, 18 May 2017 15:00:41 +0000
Subject: [PATCH] Make packrat and regex backends understand names/descriptions

---
 src/backends/packrat.c |  7 ++++++-
 src/backends/regex.c   |  7 ++++++-
 src/hammer.c           | 36 ++++++++++++++++++++++++++++++++++++
 src/internal.h         | 10 ++++++++++
 4 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/src/backends/packrat.c b/src/backends/packrat.c
index 276dfd17..e561b7ef 100644
--- a/src/backends/packrat.c
+++ b/src/backends/packrat.c
@@ -285,5 +285,10 @@ HParseResult *h_packrat_parse(HAllocator* mm__, const HParser* parser, HInputStr
 HParserBackendVTable h__packrat_backend_vtable = {
   .compile = h_packrat_compile,
   .parse = h_packrat_parse,
-  .free = h_packrat_free
+  .free = h_packrat_free,
+  /* Name/param resolution functions */
+  .backend_short_name = "packrat",
+  .backend_description = "Packrat parser with Warth's recursion",
+  .get_description_with_params = h_get_description_with_no_params,
+  .get_short_name_with_params = h_get_short_name_with_no_params
 };
diff --git a/src/backends/regex.c b/src/backends/regex.c
index 9646ddd5..2b6cf95d 100644
--- a/src/backends/regex.c
+++ b/src/backends/regex.c
@@ -449,7 +449,12 @@ static HParseResult *h_regex_parse(HAllocator* mm__, const HParser* parser, HInp
 HParserBackendVTable h__regex_backend_vtable = {
   .compile = h_regex_compile,
   .parse = h_regex_parse,
-  .free = h_regex_free
+  .free = h_regex_free,
+  /* Name/param resolution functions */
+  .backend_short_name = "regex",
+  .backend_description = "Regular expression matcher (broken)",
+  .get_description_with_params = h_get_description_with_no_params,
+  .get_short_name_with_params = h_get_short_name_with_no_params
 };
 
 #ifndef NDEBUG
diff --git a/src/hammer.c b/src/hammer.c
index 2d34f9e0..ba4928e6 100644
--- a/src/hammer.c
+++ b/src/hammer.c
@@ -269,6 +269,42 @@ char * h_get_descriptive_text_for_backend_with_params(
       &system_allocator, be_with_params);
 }
 
+/* Helpers for the above for backends with no params */
+
+static char * h_get_backend_text_with_no_params(HAllocator *mm__,
+                                                HParserBackend be,
+                                                int description) {
+  char *text = NULL;
+  const char *src = NULL;
+  int size;
+
+  if (!(mm__ != NULL && be != PB_INVALID &&
+        be >= PB_MIN && be <= PB_MAX)) goto done;
+
+  src = description ?
+    backends[be]->backend_description :
+    backends[be]->backend_short_name;
+
+  if (src) {
+    size = strlen(src) + 1;
+    text = h_new(char, size);
+    if (text) strncpy(text, src, size);
+  }
+
+ done:
+  return text;
+}
+
+char * h_get_description_with_no_params(HAllocator *mm__,
+                                        HParserBackend be, void *params) {
+  return h_get_backend_text_with_no_params(mm__, be, 1);
+}
+
+char * h_get_short_name_with_no_params(HAllocator *mm__,
+                                           HParserBackend be, void *params) {
+  return h_get_backend_text_with_no_params(mm__, be, 0);
+}
+
 #define DEFAULT_ENDIANNESS (BIT_BIG_ENDIAN | BYTE_BIG_ENDIAN)
 
 HParseResult* h_parse(const HParser* parser, const uint8_t* input, size_t length) {
diff --git a/src/internal.h b/src/internal.h
index 4fc9eb98..df720151 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -351,6 +351,16 @@ extern HParserBackendVTable h__glr_backend_vtable;
 
 // TODO(thequux): Set symbol visibility for these functions so that they aren't exported.
 
+/*
+ * Helper functions for backend with params names and descriptions for
+ * backends which take no params.
+ */
+
+char * h_get_description_with_no_params(HAllocator *mm__,
+                                        HParserBackend be, void *params);
+char * h_get_short_name_with_no_params(HAllocator *mm__,
+                                       HParserBackend be, void *params);
+
 int64_t h_read_bits(HInputStream* state, int count, char signed_p);
 static inline size_t h_input_stream_pos(HInputStream* state) {
   return state->index * 8 + state->bit_offset + state->margin;
-- 
GitLab