Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH 0/7] More minor parser-related cleanups
@ 2026-11-19 17:58 Tom Tromey
  2026-11-19 17:58 ` [PATCH 1/7] Use compiled_regex in rust-parse.c Tom Tromey
                   ` (7 more replies)
  0 siblings, 8 replies; 11+ messages in thread
From: Tom Tromey @ 2026-11-19 17:58 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

After the Ada lexer rewrite, there were a few more cleanups to do;
some pointed out by Simon in review, and some I'd come up with.  This
series is the result.  I think each patch is relatively
self-explanatory.

Signed-off-by: Tom Tromey <tromey@adacore.com>
---
Tom Tromey (7):
      Use compiled_regex in rust-parse.c
      Move ada_tick_completer to ada-exp-parser.c
      Change some Ada parser functions into methods
      Simplify post-process-parser-output.sh
      Remove fixed-sized buffer from ada_parse_state::lex_number
      Hoist calls to canonicalize_numeral
      Remove canonicalize_numeral

 gdb/Makefile.in                   |   4 +-
 gdb/ada-exp-parser.c              | 204 ++++++++++++++++++--------------------
 gdb/ada-exp-parser.h              |  54 ++++------
 gdb/post-process-parser-output.sh |  38 ++-----
 gdb/rust-parse.c                  |  12 +--
 5 files changed, 132 insertions(+), 180 deletions(-)
---
base-commit: 8b68f3168018e5a54db2c4f951fd13ba87b004ee
change-id: 20261119-more-parser-cleanups-accbbeb8413c

Best regards,
--  
Tom Tromey <tromey@adacore.com>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 1/7] Use compiled_regex in rust-parse.c
  2026-11-19 17:58 [PATCH 0/7] More minor parser-related cleanups Tom Tromey
@ 2026-11-19 17:58 ` Tom Tromey
  2026-11-19 17:58 ` [PATCH 2/7] Move ada_tick_completer to ada-exp-parser.c Tom Tromey
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2026-11-19 17:58 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This changes rust-parse.c to use compiled_regex, similar to a recent
change done for the Ada lexer.
---
 gdb/rust-parse.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/gdb/rust-parse.c b/gdb/rust-parse.c
index fc441a1b042..c520dc158a6 100644
--- a/gdb/rust-parse.c
+++ b/gdb/rust-parse.c
@@ -77,7 +77,8 @@ static const char number_regex_text[] =
 
 /* The compiled number-matching regex.  */
 
-static regex_t number_regex;
+static compiled_regex number_regex (number_regex_text, REG_EXTENDED,
+				    _("internal error"));
 
 /* The kinds of tokens.  Note that single-character tokens are
    represented by themselves, so for instance '[' is a token.  */
@@ -910,8 +911,8 @@ rust_parser::lex_number ()
   int end_index;
   int type_index = -1;
 
-  match = regexec (&number_regex, pstate->lexptr, ARRAY_SIZE (subexps),
-		   subexps, 0);
+  match = number_regex.exec (pstate->lexptr, ARRAY_SIZE (subexps),
+			     subexps, 0);
   /* Failure means the regexp is broken.  */
   gdb_assert (match == 0);
 
@@ -2403,11 +2404,6 @@ rust_lex_tests ()
 
 INIT_GDB_FILE (rust_exp)
 {
-  int code = regcomp (&number_regex, number_regex_text, REG_EXTENDED);
-  /* If the regular expression was incorrect, it was a programming
-     error.  */
-  gdb_assert (code == 0);
-
 #if GDB_SELF_TEST
   selftests::register_test ("rust-lex", rust_lex_tests);
 #endif

-- 
2.56.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 2/7] Move ada_tick_completer to ada-exp-parser.c
  2026-11-19 17:58 [PATCH 0/7] More minor parser-related cleanups Tom Tromey
  2026-11-19 17:58 ` [PATCH 1/7] Use compiled_regex in rust-parse.c Tom Tromey
@ 2026-11-19 17:58 ` Tom Tromey
  2026-11-19 17:58 ` [PATCH 3/7] Change some Ada parser functions into methods Tom Tromey
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2026-11-19 17:58 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

There's no need to declare ada_tick_completer in the header file, so
move it to ada-exp-parser.c.  This also changes it to use
make_unique_xstrdup.
---
 gdb/ada-exp-parser.c | 85 +++++++++++++++++++++++++++++-----------------------
 gdb/ada-exp-parser.h | 16 ----------
 2 files changed, 48 insertions(+), 53 deletions(-)

diff --git a/gdb/ada-exp-parser.c b/gdb/ada-exp-parser.c
index dbea55abba6..2757877b605 100644
--- a/gdb/ada-exp-parser.c
+++ b/gdb/ada-exp-parser.c
@@ -113,6 +113,54 @@ struct code_entry
   int code;
 };
 
+static const struct code_entry attributes[] =
+{
+  { "address", TICK_ADDRESS },
+  { "unchecked_access", TICK_ACCESS },
+  { "unrestricted_access", TICK_ACCESS },
+  { "access", TICK_ACCESS },
+  { "first", TICK_FIRST },
+  { "last", TICK_LAST },
+  { "length", TICK_LENGTH },
+  { "max", TICK_MAX },
+  { "min", TICK_MIN },
+  { "modulus", TICK_MODULUS },
+  { "object_size", TICK_OBJECT_SIZE },
+  { "pos", TICK_POS },
+  { "range", TICK_RANGE },
+  { "size", TICK_SIZE },
+  { "tag", TICK_TAG },
+  { "val", TICK_VAL },
+  { "enum_rep", TICK_ENUM_REP },
+  { "enum_val", TICK_ENUM_VAL },
+};
+
+/* Expression completer for attributes.  */
+struct ada_tick_completer : public expr_completion_base
+{
+  explicit ada_tick_completer (std::string &&name)
+    : m_name (std::move (name))
+  {
+  }
+
+  bool complete (struct expression *exp,
+		 completion_tracker &tracker) override
+  {
+    completion_list output;
+    for (const auto &item : attributes)
+      {
+	if (strncasecmp (item.name, m_name.c_str (), m_name.length ()) == 0)
+	  output.push_back (make_unique_xstrdup (item.name));
+      }
+    tracker.add_completions (std::move (output));
+    return true;
+  }
+
+private:
+
+  std::string m_name;
+};
+
 /* See ada-exp-parser.h.  */
 
 struct parser_state *pstate;
@@ -384,29 +432,6 @@ subseqMatch (const char *subseq, const char *str)
     return subseqMatch (subseq, str+1);
 }
 
-
-static const struct code_entry attributes[] =
-{
-  { "address", TICK_ADDRESS },
-  { "unchecked_access", TICK_ACCESS },
-  { "unrestricted_access", TICK_ACCESS },
-  { "access", TICK_ACCESS },
-  { "first", TICK_FIRST },
-  { "last", TICK_LAST },
-  { "length", TICK_LENGTH },
-  { "max", TICK_MAX },
-  { "min", TICK_MIN },
-  { "modulus", TICK_MODULUS },
-  { "object_size", TICK_OBJECT_SIZE },
-  { "pos", TICK_POS },
-  { "range", TICK_RANGE },
-  { "size", TICK_SIZE },
-  { "tag", TICK_TAG },
-  { "val", TICK_VAL },
-  { "enum_rep", TICK_ENUM_REP },
-  { "enum_val", TICK_ENUM_VAL },
-};
-
 /* See ada-exp-parser.h.  */
 
 int
@@ -439,20 +464,6 @@ ada_parse_state::process_attribute (const std::string &str, bool completing)
   return *found;
 }
 
-bool
-ada_tick_completer::complete (struct expression *exp,
-			      completion_tracker &tracker)
-{
-  completion_list output;
-  for (const auto &item : attributes)
-    {
-      if (strncasecmp (item.name, m_name.c_str (), m_name.length ()) == 0)
-	output.emplace_back (xstrdup (item.name));
-    }
-  tracker.add_completions (std::move (output));
-  return true;
-}
-
 int
 ada_parse_state::lex_number ()
 {
diff --git a/gdb/ada-exp-parser.h b/gdb/ada-exp-parser.h
index 682d868d528..4de2263ed6c 100644
--- a/gdb/ada-exp-parser.h
+++ b/gdb/ada-exp-parser.h
@@ -159,22 +159,6 @@ struct ada_parse_state
   bool m_before_qual_quote = false;
 };
 
-/* Expression completer for attributes.  */
-struct ada_tick_completer : public expr_completion_base
-{
-  explicit ada_tick_completer (std::string &&name)
-    : m_name (std::move (name))
-  {
-  }
-
-  bool complete (struct expression *exp,
-		 completion_tracker &tracker) override;
-
-private:
-
-  std::string m_name;
-};
-
 /* The current state of the parser, used internally when parsing an
    expression.  */
 

-- 
2.56.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 3/7] Change some Ada parser functions into methods
  2026-11-19 17:58 [PATCH 0/7] More minor parser-related cleanups Tom Tromey
  2026-11-19 17:58 ` [PATCH 1/7] Use compiled_regex in rust-parse.c Tom Tromey
  2026-11-19 17:58 ` [PATCH 2/7] Move ada_tick_completer to ada-exp-parser.c Tom Tromey
@ 2026-11-19 17:58 ` Tom Tromey
  2026-11-19 18:48   ` Simon Marchi
  2026-11-19 17:58 ` [PATCH 4/7] Simplify post-process-parser-output.sh Tom Tromey
                   ` (4 subsequent siblings)
  7 siblings, 1 reply; 11+ messages in thread
From: Tom Tromey @ 2026-11-19 17:58 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This turns a few Ada parser functions into methods of ada_parse_state.
This also renames some functions to conform to the gdb style.

This approach is somewhat cleaner.  Ultimately I think the Ada lexer
(and hopefully someday the parser) would more resemble how the Rust
parser works -- a self-contained class.
---
 gdb/ada-exp-parser.c | 73 ++++++++++++++++++++++++++--------------------------
 gdb/ada-exp-parser.h | 38 ++++++++++++---------------
 2 files changed, 53 insertions(+), 58 deletions(-)

diff --git a/gdb/ada-exp-parser.c b/gdb/ada-exp-parser.c
index 2757877b605..896e97798b4 100644
--- a/gdb/ada-exp-parser.c
+++ b/gdb/ada-exp-parser.c
@@ -189,8 +189,8 @@ canonicalize_numeral (char *s1, const char *s2, const char *end)
 /* See ada-exp-parser.h.  */
 
 int
-processInt (struct parser_state *par_state, const char *base0,
-	    const char *num0, const char *exp0)
+ada_parse_state::process_int (const char *base0, const char *num0,
+			      const char *exp0)
 {
   long exp;
   int base;
@@ -250,58 +250,58 @@ processInt (struct parser_state *par_state, const char *base0,
     {
       struct type *fp_type;
       if (floating_point_l_count == 0)
-	fp_type = language_lookup_primitive_type (par_state->language (),
-						  par_state->gdbarch (),
+	fp_type = language_lookup_primitive_type (pstate->language (),
+						  pstate->gdbarch (),
 						  "float");
       else if (floating_point_l_count == 1)
-	fp_type = language_lookup_primitive_type (par_state->language (),
-						  par_state->gdbarch (),
+	fp_type = language_lookup_primitive_type (pstate->language (),
+						  pstate->gdbarch (),
 						  "long_float");
       else
 	{
 	  /* This assertion is ensured by the pattern.  */
 	  gdb_assert (floating_point_l_count == 2);
-	  fp_type = language_lookup_primitive_type (par_state->language (),
-						    par_state->gdbarch (),
+	  fp_type = language_lookup_primitive_type (pstate->language (),
+						    pstate->gdbarch (),
 						    "long_long_float");
 	}
 
       ada_yylval.typed_val_float.type = fp_type;
       result.write (gdb::make_span (ada_yylval.typed_val_float.val,
-					  fp_type->length ()),
+				    fp_type->length ()),
 		    type_byte_order (fp_type),
 		    true);
 
       return FLOAT;
     }
 
-  const gdb_mpz *value = ada_parser->push_integer (std::move (result));
+  const gdb_mpz *value = push_integer (std::move (result));
 
-  int int_bits = gdbarch_int_bit (par_state->gdbarch ());
-  int long_bits = gdbarch_long_bit (par_state->gdbarch ());
-  int long_long_bits = gdbarch_long_long_bit (par_state->gdbarch ());
+  int int_bits = gdbarch_int_bit (pstate->gdbarch ());
+  int long_bits = gdbarch_long_bit (pstate->gdbarch ());
+  int long_long_bits = gdbarch_long_long_bit (pstate->gdbarch ());
 
   if (fits_in_type (1, *value, int_bits, true))
-    ada_yylval.typed_val.type = parse_type (par_state)->builtin_int;
+    ada_yylval.typed_val.type = parse_type (pstate)->builtin_int;
   else if (fits_in_type (1, *value, long_bits, true))
-    ada_yylval.typed_val.type = parse_type (par_state)->builtin_long;
+    ada_yylval.typed_val.type = parse_type (pstate)->builtin_long;
   else if (fits_in_type (1, *value, long_bits, false))
     ada_yylval.typed_val.type
-      = builtin_type (par_state->gdbarch ())->builtin_unsigned_long;
+      = builtin_type (pstate->gdbarch ())->builtin_unsigned_long;
   else if (fits_in_type (1, *value, long_long_bits, true))
-    ada_yylval.typed_val.type = parse_type (par_state)->builtin_long_long;
+    ada_yylval.typed_val.type = parse_type (pstate)->builtin_long_long;
   else if (fits_in_type (1, *value, long_long_bits, false))
     ada_yylval.typed_val.type
-      = builtin_type (par_state->gdbarch ())->builtin_unsigned_long_long;
+      = builtin_type (pstate->gdbarch ())->builtin_unsigned_long_long;
   else if (fits_in_type (1, *value, 128, true))
     ada_yylval.typed_val.type
-      = language_lookup_primitive_type (par_state->language (),
-					par_state->gdbarch (),
+      = language_lookup_primitive_type (pstate->language (),
+					pstate->gdbarch (),
 					"long_long_long_integer");
   else if (fits_in_type (1, *value, 128, false))
     ada_yylval.typed_val.type
-      = language_lookup_primitive_type (par_state->language (),
-					par_state->gdbarch (),
+      = language_lookup_primitive_type (pstate->language (),
+					pstate->gdbarch (),
 					"unsigned_long_long_long_integer");
   else
     error (_("Integer literal out of range"));
@@ -313,9 +313,9 @@ processInt (struct parser_state *par_state, const char *base0,
 /* See ada-exp-parser.h.  */
 
 int
-processReal (struct parser_state *par_state, const char *num0)
+ada_parse_state::process_real (const char *num0)
 {
-  ada_yylval.typed_val_float.type = parse_type (par_state)->builtin_long_double;
+  ada_yylval.typed_val_float.type = parse_type (pstate)->builtin_long_double;
 
   bool parsed = parse_float (num0, strlen (num0),
 			     ada_yylval.typed_val_float.type,
@@ -378,14 +378,13 @@ ada_parse_state::process_id (std::string_view name0)
 /* See ada-exp-parser.h.  */
 
 struct stoken
-processString (const char *text, int len)
+ada_parse_state::process_string (const char *text, const char *lim)
 {
   const char *p;
   char *q;
-  const char *lim = text + len;
   struct stoken result;
 
-  q = (char *) obstack_alloc (&ada_parser->temp_space, len);
+  q = (char *) obstack_alloc (&temp_space, lim - text);
   result.ptr = q;
   p = text;
   while (p < lim)
@@ -420,16 +419,16 @@ processString (const char *text, int len)
    case.  */
 
 static int
-subseqMatch (const char *subseq, const char *str)
+subseq_match (const char *subseq, const char *str)
 {
   if (subseq[0] == '\0')
     return 1;
   else if (str[0] == '\0')
     return 0;
   else if (c_tolower (subseq[0]) == c_tolower (str[0]))
-    return subseqMatch (subseq+1, str+1) || subseqMatch (subseq, str+1);
+    return subseq_match (subseq+1, str+1) || subseq_match (subseq, str+1);
   else
-    return subseqMatch (subseq, str+1);
+    return subseq_match (subseq, str+1);
 }
 
 /* See ada-exp-parser.h.  */
@@ -451,7 +450,7 @@ ada_parse_state::process_attribute (const std::string &str, bool completing)
 
   std::optional<int> found;
   for (const auto &item : attributes)
-    if (subseqMatch (str.c_str (), item.name))
+    if (subseq_match (str.c_str (), item.name))
       {
 	if (!found.has_value ())
 	  found = item.code;
@@ -482,19 +481,19 @@ ada_parse_state::lex_number ()
   if (subexps[HEX_NUMBER].rm_so != -1)
     {
       canonicalize_numeral (numbuf, num_start + 2, pstate->lexptr);
-      return processInt (pstate, "16#", numbuf, nullptr);
+      return process_int ("16#", numbuf, nullptr);
     }
   else if (subexps[FLOAT_POSEXP].rm_so != -1)
     {
       canonicalize_numeral (numbuf, num_start, pstate->lexptr);
       char *e_ptr = strrchr (numbuf, 'e');
       *e_ptr = '\0';
-      return processInt (pstate, nullptr, numbuf, e_ptr + 1);
+      return process_int (nullptr, numbuf, e_ptr + 1);
     }
   else if (subexps[FLOAT_OPTEXP].rm_so != -1)
     {
       canonicalize_numeral (numbuf, num_start, pstate->lexptr);
-      return processReal (pstate, numbuf);
+      return process_real (numbuf);
     }
   else if (subexps[FLOAT_EXTENSION].rm_so != -1)
     {
@@ -512,14 +511,14 @@ ada_parse_state::lex_number ()
 	  *e_ptr = '\0';
 	  ++e_ptr;
 	}
-      return processInt (pstate, numbuf, strchr (numbuf, '#') + 1, e_ptr);
+      return process_int (numbuf, strchr (numbuf, '#') + 1, e_ptr);
     }
   else if (subexps[BASED_REAL].rm_so != -1)
     error (_("Based real literals not implemented yet."));
 
   /* Must be a simple integer.  */
   canonicalize_numeral (numbuf, num_start, pstate->lexptr);
-  return processInt (pstate, nullptr, numbuf, nullptr);
+  return process_int (nullptr, numbuf, nullptr);
 }
 
 static const code_entry keywords[] =
@@ -859,7 +858,7 @@ ada_parse_state::lex_double_quote ()
   if (*iter != '"')
     error (_("ill-formed or non-terminated string literal"));
 
-  ada_yylval.sval = processString (start, iter - start);
+  ada_yylval.sval = process_string (start, iter);
   pstate->lexptr = iter + 1;
   return STRING;
 }
diff --git a/gdb/ada-exp-parser.h b/gdb/ada-exp-parser.h
index 4de2263ed6c..3d851e8fe7d 100644
--- a/gdb/ada-exp-parser.h
+++ b/gdb/ada-exp-parser.h
@@ -137,6 +137,23 @@ struct ada_parse_state
   */
   stoken process_id (std::string_view name0);
 
+  /* Return TEXT (up to LIM), a string literal without surrounding
+     quotes, with special hex character notations replaced with
+     characters.  Result valid until the next call to ada_parse.  */
+  struct stoken process_string (const char *text, const char *lim);
+
+  /* Interprets the prefix of NUM that consists of digits of the given BASE
+     as an integer of that BASE, with the string EXP as an exponent.
+     Puts value in yylval, and returns INT, if the string is valid.  Causes
+     an error if the number is improperly formatted.  BASE, if NULL, defaults
+     to "10", and EXP to "1".  The EXP does not contain a leading 'e' or 'E'.
+  */
+  int process_int (const char *base0, const char *num0, const char *exp0);
+
+  /* Parse NUM0 as a floating-point literal, store the result in
+     yylval, and return the FLOAT token.  */
+  int process_real (const char *num0);
+
   /* Throw an exception when lexing, indicating that an invalid
      character was seen.  */
   [[noreturn]] void invalid_char ();
@@ -168,27 +185,6 @@ extern struct parser_state *pstate;
 
 extern struct ada_parse_state *ada_parser;
 
-/* Return TEXT[0..LEN-1], a string literal without surrounding quotes,
-   with special hex character notations replaced with characters.
-   Result valid until the next call to ada_parse.  */
-
-stoken processString (const char *text, int len);
-
-/* Interprets the prefix of NUM that consists of digits of the given BASE
-   as an integer of that BASE, with the string EXP as an exponent.
-   Puts value in yylval, and returns INT, if the string is valid.  Causes
-   an error if the number is improperly formatted.   BASE, if NULL, defaults
-   to "10", and EXP to "1".  The EXP does not contain a leading 'e' or 'E'.
- */
-
-int processInt (parser_state *par_state, const char *base0, const char *num0,
-		const char *exp0);
-
-/* Parse NUM0 as a floating-point literal, store the result in yylval,
-   and return the FLOAT token.  */
-
-int processReal (struct parser_state *par_state, const char *num0);
-
 /* Like parser_state::pop, but handles Ada type resolution.
    DEPROCEDURE_P and CONTEXT_TYPE are passed to the resolve method, if
    called.  */

-- 
2.56.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 4/7] Simplify post-process-parser-output.sh
  2026-11-19 17:58 [PATCH 0/7] More minor parser-related cleanups Tom Tromey
                   ` (2 preceding siblings ...)
  2026-11-19 17:58 ` [PATCH 3/7] Change some Ada parser functions into methods Tom Tromey
@ 2026-11-19 17:58 ` Tom Tromey
  2026-11-19 17:58 ` [PATCH 5/7] Remove fixed-sized buffer from ada_parse_state::lex_number Tom Tromey
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2026-11-19 17:58 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This simplifies post-process-parser-output.sh, now that the only use
of flex is gone.
---
 gdb/Makefile.in                   |  4 ++--
 gdb/post-process-parser-output.sh | 38 +++++++++++---------------------------
 2 files changed, 13 insertions(+), 29 deletions(-)

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index c623cb04753..04ae7c3900d 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -2814,9 +2814,9 @@ YY_PREFIX_p-exp-parser = pascal_yy
 		y.tab.h $*-gen.h.tmp -- \
 		$(YACC) $(YFLAGS) -p $(YY_PREFIX_$*) -t -d \
 			|| (rm -f $*-gen.c.tmp $*-gen.h.tmp; false)
-	@$(POST_PROCESS_PARSER_OUTPUT) bison $* < $*-gen.c.tmp > $*-gen.c.new \
+	@$(POST_PROCESS_PARSER_OUTPUT) $* < $*-gen.c.tmp > $*-gen.c.new \
 		&& rm -f $*-gen.c.tmp && mv $*-gen.c.new $*-gen.c
-	@$(POST_PROCESS_PARSER_OUTPUT) bison $* < $*-gen.h.tmp > $*-gen.h.new \
+	@$(POST_PROCESS_PARSER_OUTPUT) $* < $*-gen.h.tmp > $*-gen.h.new \
 		&& rm -f $*-gen.h.tmp && mv $*-gen.h.new $*-gen.h
 
 # XML rules
diff --git a/gdb/post-process-parser-output.sh b/gdb/post-process-parser-output.sh
index ca5f0ba302f..506bd02f7aa 100755
--- a/gdb/post-process-parser-output.sh
+++ b/gdb/post-process-parser-output.sh
@@ -17,24 +17,20 @@
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-# Post-process a file produced by flex or bison/byacc, reading it from
+# Post-process a file produced by bison/byacc, reading it from
 # standard input and writing the result to standard output.
 #
 # Usage:
 #
-#    ./post-process-parser-output.sh GENERATOR NAME < INPUT > OUTPUT
+#    ./post-process-parser-output.sh NAME < INPUT > OUTPUT
 #
-# Where GENERATOR is "bison" (regardless of whether we're using the actual
-# bison or byacc) or "flex", and NAME is the name of the parser (e.g.
-# "cp-name-parser"), used to make the global symbols names bison produces
-# unique.
+# Where NAME is the name of the parser (e.g.  "cp-name-parser"), used
+# to make the global symbols names bison produces unique.
 
 set -e
 
-generator="$1"
-
 # Convert e.g. "cp-name-parser" to "cp_name_parser".
-name=$(echo "$2" | sed -e 's/-/_/g')
+name=$(echo "$1" | sed -e 's/-/_/g')
 
 common='
 /extern.*malloc/d
@@ -47,21 +43,9 @@ s/\([ \t;,(]\)free\([ \t]*[&(),]\)/\1xfree\2/g
 s/\([ \t;,(]\)free$/\1xfree/g
 '
 
-case "$generator" in
-    bison)
-	sed -e "$common" \
-	    -e '/^#line.*y.tab.c/d' \
-	    -e "s/YYSTYPE/${name}_YYSTYPE/g" \
-	    -e "s/yyalloc/${name}_yyalloc/g" \
-	    -e "s/yysymbol_kind_t/${name}_yysymbol_kind_t/g" \
-	    -e "s/YYSTACKDATA/${name}_YYSTACKDATA/g"
-	;;
-    flex)
-	sed -e "$common" \
-	    -e 's/yy_flex_xrealloc/yyxrealloc/g'
-	;;
-    *)
-	echo "$0: unknown parser generator \"$generator\"" >&2
-	exit 1
-	;;
-esac
+sed -e "$common" \
+    -e '/^#line.*y.tab.c/d' \
+    -e "s/YYSTYPE/${name}_YYSTYPE/g" \
+    -e "s/yyalloc/${name}_yyalloc/g" \
+    -e "s/yysymbol_kind_t/${name}_yysymbol_kind_t/g" \
+    -e "s/YYSTACKDATA/${name}_YYSTACKDATA/g"

-- 
2.56.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 5/7] Remove fixed-sized buffer from ada_parse_state::lex_number
  2026-11-19 17:58 [PATCH 0/7] More minor parser-related cleanups Tom Tromey
                   ` (3 preceding siblings ...)
  2026-11-19 17:58 ` [PATCH 4/7] Simplify post-process-parser-output.sh Tom Tromey
@ 2026-11-19 17:58 ` Tom Tromey
  2026-11-19 17:58 ` [PATCH 6/7] Hoist calls to canonicalize_numeral Tom Tromey
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2026-11-19 17:58 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This patch remove a fixed-sized buffer from
ada_parse_state::lex_number, in favor of allocating some space on the
parser's temporary obstack.
---
 gdb/ada-exp-parser.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/gdb/ada-exp-parser.c b/gdb/ada-exp-parser.c
index 896e97798b4..56d3620e480 100644
--- a/gdb/ada-exp-parser.c
+++ b/gdb/ada-exp-parser.c
@@ -476,8 +476,8 @@ ada_parse_state::lex_number ()
 
   pstate->lexptr += subexps[0].rm_eo;
 
-#define NUMERAL_WIDTH 256
-  char numbuf[NUMERAL_WIDTH];
+  char *numbuf = (char *) obstack_alloc (&temp_space,
+					 pstate->lexptr - num_start);
   if (subexps[HEX_NUMBER].rm_so != -1)
     {
       canonicalize_numeral (numbuf, num_start + 2, pstate->lexptr);

-- 
2.56.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 6/7] Hoist calls to canonicalize_numeral
  2026-11-19 17:58 [PATCH 0/7] More minor parser-related cleanups Tom Tromey
                   ` (4 preceding siblings ...)
  2026-11-19 17:58 ` [PATCH 5/7] Remove fixed-sized buffer from ada_parse_state::lex_number Tom Tromey
@ 2026-11-19 17:58 ` Tom Tromey
  2026-11-19 17:58 ` [PATCH 7/7] Remove canonicalize_numeral Tom Tromey
  2026-11-19 18:55 ` [PATCH 0/7] More minor parser-related cleanups Simon Marchi
  7 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2026-11-19 17:58 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

I realized that canonicalize_numeral can be called a single time from
process_int.  The one "odd" case is just skipping a leading "0x"; this
can be done just as well after canonicalization.
---
 gdb/ada-exp-parser.c | 15 ++++-----------
 1 file changed, 4 insertions(+), 11 deletions(-)

diff --git a/gdb/ada-exp-parser.c b/gdb/ada-exp-parser.c
index 56d3620e480..67f40368e9b 100644
--- a/gdb/ada-exp-parser.c
+++ b/gdb/ada-exp-parser.c
@@ -478,26 +478,20 @@ ada_parse_state::lex_number ()
 
   char *numbuf = (char *) obstack_alloc (&temp_space,
 					 pstate->lexptr - num_start);
+  canonicalize_numeral (numbuf, num_start, pstate->lexptr);
+
   if (subexps[HEX_NUMBER].rm_so != -1)
-    {
-      canonicalize_numeral (numbuf, num_start + 2, pstate->lexptr);
-      return process_int ("16#", numbuf, nullptr);
-    }
+    return process_int ("16#", numbuf + 2, nullptr);
   else if (subexps[FLOAT_POSEXP].rm_so != -1)
     {
-      canonicalize_numeral (numbuf, num_start, pstate->lexptr);
       char *e_ptr = strrchr (numbuf, 'e');
       *e_ptr = '\0';
       return process_int (nullptr, numbuf, e_ptr + 1);
     }
   else if (subexps[FLOAT_OPTEXP].rm_so != -1)
-    {
-      canonicalize_numeral (numbuf, num_start, pstate->lexptr);
-      return process_real (numbuf);
-    }
+    return process_real (numbuf);
   else if (subexps[FLOAT_EXTENSION].rm_so != -1)
     {
-      canonicalize_numeral (numbuf, num_start, pstate->lexptr);
       char *e_ptr = nullptr;
       if (subexps[EXPONENT].rm_so != -1)
 	{
@@ -517,7 +511,6 @@ ada_parse_state::lex_number ()
     error (_("Based real literals not implemented yet."));
 
   /* Must be a simple integer.  */
-  canonicalize_numeral (numbuf, num_start, pstate->lexptr);
   return process_int (nullptr, numbuf, nullptr);
 }
 

-- 
2.56.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH 7/7] Remove canonicalize_numeral
  2026-11-19 17:58 [PATCH 0/7] More minor parser-related cleanups Tom Tromey
                   ` (5 preceding siblings ...)
  2026-11-19 17:58 ` [PATCH 6/7] Hoist calls to canonicalize_numeral Tom Tromey
@ 2026-11-19 17:58 ` Tom Tromey
  2026-11-19 18:55 ` [PATCH 0/7] More minor parser-related cleanups Simon Marchi
  7 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2026-11-19 17:58 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

This inlines canonicalize_numeral into its sole caller.
---
 gdb/ada-exp-parser.c | 35 ++++++++++++-----------------------
 1 file changed, 12 insertions(+), 23 deletions(-)

diff --git a/gdb/ada-exp-parser.c b/gdb/ada-exp-parser.c
index 67f40368e9b..c1c5927664a 100644
--- a/gdb/ada-exp-parser.c
+++ b/gdb/ada-exp-parser.c
@@ -169,23 +169,6 @@ struct parser_state *pstate;
 
 struct ada_parse_state *ada_parser;
 
-/* Copy S2 to S1, removing all underscores, and downcasing all
-   letters.  Stops at END.  */
-
-static void
-canonicalize_numeral (char *s1, const char *s2, const char *end)
-{
-  for (; s2 != end; s2 += 1)
-    {
-      if (*s2 != '_')
-	{
-	  *s1 = c_tolower(*s2);
-	  s1 += 1;
-	}
-    }
-  s1[0] = '\000';
-}
-
 /* See ada-exp-parser.h.  */
 
 int
@@ -478,7 +461,14 @@ ada_parse_state::lex_number ()
 
   char *numbuf = (char *) obstack_alloc (&temp_space,
 					 pstate->lexptr - num_start);
-  canonicalize_numeral (numbuf, num_start, pstate->lexptr);
+  /* Canonicalize the number, copying it into NUMBUF.  */
+  char *out_iter = numbuf;
+  for (const char *iter = num_start; iter != pstate->lexptr; ++iter)
+    {
+      if (*iter != '_')
+	*out_iter++ = c_tolower (*iter);
+    }
+  *out_iter = '\0';
 
   if (subexps[HEX_NUMBER].rm_so != -1)
     return process_int ("16#", numbuf + 2, nullptr);
@@ -495,11 +485,10 @@ ada_parse_state::lex_number ()
       char *e_ptr = nullptr;
       if (subexps[EXPONENT].rm_so != -1)
 	{
-	  /* We can't use the 'rm_so' here because
-	     canonicalize_numeral might have removed some underscores.
-	     But we know the 'e' appears after the '#' so we can
-	     search for it and not get one of the (possibly hex)
-	     numerals.  */
+	  /* We can't use the 'rm_so' here because canonicalization
+	     might have removed some underscores.  But we know the 'e'
+	     appears after the '#' so we can search for it and not get
+	     one of the (possibly hex) numerals.  */
 	  e_ptr = strrchr (numbuf, 'e');
 	  gdb_assert (e_ptr != nullptr);
 	  *e_ptr = '\0';

-- 
2.56.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/7] Change some Ada parser functions into methods
  2026-11-19 17:58 ` [PATCH 3/7] Change some Ada parser functions into methods Tom Tromey
@ 2026-11-19 18:48   ` Simon Marchi
  2026-11-20 20:57     ` Tom Tromey
  0 siblings, 1 reply; 11+ messages in thread
From: Simon Marchi @ 2026-11-19 18:48 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches

On 11/19/26 12:58 PM, Tom Tromey wrote:
> This turns a few Ada parser functions into methods of ada_parse_state.
> This also renames some functions to conform to the gdb style.
> 
> This approach is somewhat cleaner.  Ultimately I think the Ada lexer
> (and hopefully someday the parser) would more resemble how the Rust
> parser works -- a self-contained class.

I think this is a good direction.

Do you plan on switching more functions to become methods and working
your way towards making ada-expt.y a "pure" parser?  Is that what you
mean by "resemble how the Rust parser works"?

I noted some minor comments below, the patch LGTM.

> @@ -378,14 +378,13 @@ ada_parse_state::process_id (std::string_view name0)
>  /* See ada-exp-parser.h.  */
>  
>  struct stoken
> -processString (const char *text, int len)
> +ada_parse_state::process_string (const char *text, const char *lim)

Sounds like that should take a string_view.

> diff --git a/gdb/ada-exp-parser.h b/gdb/ada-exp-parser.h
> index 4de2263ed6c..3d851e8fe7d 100644
> --- a/gdb/ada-exp-parser.h
> +++ b/gdb/ada-exp-parser.h
> @@ -137,6 +137,23 @@ struct ada_parse_state
>    */
>    stoken process_id (std::string_view name0);
>  
> +  /* Return TEXT (up to LIM), a string literal without surrounding
> +     quotes, with special hex character notations replaced with
> +     characters.  Result valid until the next call to ada_parse.  */
> +  struct stoken process_string (const char *text, const char *lim);
> +
> +  /* Interprets the prefix of NUM that consists of digits of the given BASE
> +     as an integer of that BASE, with the string EXP as an exponent.
> +     Puts value in yylval, and returns INT, if the string is valid.  Causes
> +     an error if the number is improperly formatted.  BASE, if NULL, defaults
> +     to "10", and EXP to "1".  The EXP does not contain a leading 'e' or 'E'.
> +  */
> +  int process_int (const char *base0, const char *num0, const char *exp0);

The names in the comment don't have the "0" suffix.  Does the 0 have a
meaning, like to indicate we expect this string to be null-terminated?

Simon

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 0/7] More minor parser-related cleanups
  2026-11-19 17:58 [PATCH 0/7] More minor parser-related cleanups Tom Tromey
                   ` (6 preceding siblings ...)
  2026-11-19 17:58 ` [PATCH 7/7] Remove canonicalize_numeral Tom Tromey
@ 2026-11-19 18:55 ` Simon Marchi
  7 siblings, 0 replies; 11+ messages in thread
From: Simon Marchi @ 2026-11-19 18:55 UTC (permalink / raw)
  To: Tom Tromey, gdb-patches

On 11/19/26 12:58 PM, Tom Tromey wrote:
> After the Ada lexer rewrite, there were a few more cleanups to do;
> some pointed out by Simon in review, and some I'd come up with.  This
> series is the result.  I think each patch is relatively
> self-explanatory.
> 
> Signed-off-by: Tom Tromey <tromey@adacore.com>
> ---
> Tom Tromey (7):
>       Use compiled_regex in rust-parse.c
>       Move ada_tick_completer to ada-exp-parser.c
>       Change some Ada parser functions into methods
>       Simplify post-process-parser-output.sh
>       Remove fixed-sized buffer from ada_parse_state::lex_number
>       Hoist calls to canonicalize_numeral
>       Remove canonicalize_numeral
> 
>  gdb/Makefile.in                   |   4 +-
>  gdb/ada-exp-parser.c              | 204 ++++++++++++++++++--------------------
>  gdb/ada-exp-parser.h              |  54 ++++------
>  gdb/post-process-parser-output.sh |  38 ++-----
>  gdb/rust-parse.c                  |  12 +--
>  5 files changed, 132 insertions(+), 180 deletions(-)
> ---
> base-commit: 8b68f3168018e5a54db2c4f951fd13ba87b004ee
> change-id: 20261119-more-parser-cleanups-accbbeb8413c
> 
> Best regards,
> --  
> Tom Tromey <tromey@adacore.com>

Approved-By: Simon Marchi <simon.marchi@efficios.com>

Simon

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH 3/7] Change some Ada parser functions into methods
  2026-11-19 18:48   ` Simon Marchi
@ 2026-11-20 20:57     ` Tom Tromey
  0 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2026-11-20 20:57 UTC (permalink / raw)
  To: Simon Marchi; +Cc: Tom Tromey, gdb-patches

Simon> Do you plan on switching more functions to become methods and working
Simon> your way towards making ada-expt.y a "pure" parser?  Is that what you
Simon> mean by "resemble how the Rust parser works"?

Yeah, I would like to do that.

I personally find debugging and modifying recursive descent parsers much
easier than dealing with yacc.

That said who knows how long it will take.  It's kind of involved.
For Ada it would probably let us remove some lexing hacks though.

>> @@ -378,14 +378,13 @@ ada_parse_state::process_id (std::string_view name0)
>> /* See ada-exp-parser.h.  */
>> 
>> struct stoken
>> -processString (const char *text, int len)
>> +ada_parse_state::process_string (const char *text, const char *lim)

Simon> Sounds like that should take a string_view.

I had tried this before, but it's a pain due to the inner part of the
loop.  I guess the loop could remain and just use .data(), but that
doesn't really seem like much of an improvement in the end.

>> +  int process_int (const char *base0, const char *num0, const char *exp0);

Simon> The names in the comment don't have the "0" suffix.  Does the 0 have a
Simon> meaning, like to indicate we expect this string to be null-terminated?

No idea, these were just leftovers.
I'll look and change them.

Tom

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2026-11-20 20:58 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19 17:58 [PATCH 0/7] More minor parser-related cleanups Tom Tromey
2026-11-19 17:58 ` [PATCH 1/7] Use compiled_regex in rust-parse.c Tom Tromey
2026-11-19 17:58 ` [PATCH 2/7] Move ada_tick_completer to ada-exp-parser.c Tom Tromey
2026-11-19 17:58 ` [PATCH 3/7] Change some Ada parser functions into methods Tom Tromey
2026-11-19 18:48   ` Simon Marchi
2026-11-20 20:57     ` Tom Tromey
2026-11-19 17:58 ` [PATCH 4/7] Simplify post-process-parser-output.sh Tom Tromey
2026-11-19 17:58 ` [PATCH 5/7] Remove fixed-sized buffer from ada_parse_state::lex_number Tom Tromey
2026-11-19 17:58 ` [PATCH 6/7] Hoist calls to canonicalize_numeral Tom Tromey
2026-11-19 17:58 ` [PATCH 7/7] Remove canonicalize_numeral Tom Tromey
2026-11-19 18:55 ` [PATCH 0/7] More minor parser-related cleanups Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox