Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Cc: Tom Tromey <tromey@adacore.com>
Subject: [PATCH 3/7] Change some Ada parser functions into methods
Date: Thu, 19 Nov 2026 10:58:18 -0700	[thread overview]
Message-ID: <20261119-more-parser-cleanups-v1-3-d85241f3fede@adacore.com> (raw)
In-Reply-To: <20261119-more-parser-cleanups-v1-0-d85241f3fede@adacore.com>

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


  parent reply	other threads:[~2026-11-19 17:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2026-11-19 18:48   ` [PATCH 3/7] Change some Ada parser functions into methods 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261119-more-parser-cleanups-v1-3-d85241f3fede@adacore.com \
    --to=tromey@adacore.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox