Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom Tromey <tromey@adacore.com>
To: gdb-patches@sourceware.org
Subject: [PATCH 03/12] Introduce ada_parse_state
Date: Thu, 21 Mar 2024 13:03:31 -0600	[thread overview]
Message-ID: <20240321-ada-iterated-assign-v1-3-925cdd4f1f4a@adacore.com> (raw)
In-Reply-To: <20240321-ada-iterated-assign-v1-0-925cdd4f1f4a@adacore.com>

This patch introduces the ada_parse_state class and the ada_parser
global.  It also changes find_completion_bounds to be a method of this
new type.

Note that find_completion_bounds never used its parameter; and because
it is generally fine to use the 'pstate' global throughout the parser,
this patch removes the parameter entirely.
---
 gdb/ada-exp.y | 51 ++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 34 insertions(+), 17 deletions(-)

diff --git a/gdb/ada-exp.y b/gdb/ada-exp.y
index c0a5b0534a6..9452e63ab60 100644
--- a/gdb/ada-exp.y
+++ b/gdb/ada-exp.y
@@ -65,8 +65,26 @@ struct name_info {
 
 static struct parser_state *pstate = NULL;
 
-/* The original expression string.  */
-static const char *original_expr;
+/* Data that must be held for the duration of a parse.  */
+
+struct ada_parse_state
+{
+  explicit ada_parse_state (const char *expr)
+    : m_original_expr (expr)
+  {
+  }
+
+  std::string find_completion_bounds ();
+
+private:
+
+  /* The original expression string.  */
+  const char *m_original_expr;
+};
+
+/* The current Ada parser object.  */
+
+static ada_parse_state *ada_parser;
 
 /* We don't have a good way to manage non-POD data in Yacc, so store
    values here.  The storage here is only valid for the duration of
@@ -102,8 +120,6 @@ static struct type *type_for_char (struct parser_state *, ULONGEST);
 
 static struct type *type_system_address (struct parser_state *);
 
-static std::string find_completion_bounds (struct parser_state *);
-
 using namespace expr;
 
 /* Handle Ada type resolution for OP.  DEPROCEDURE_P and CONTEXT_TYPE
@@ -549,7 +565,7 @@ primary :	primary DOT_COMPLETE
 			  ada_structop_operation *str_op
 			    = (new ada_structop_operation
 			       (std::move (arg), copy_name ($2)));
-			  str_op->set_prefix (find_completion_bounds (pstate));
+			  str_op->set_prefix (ada_parser->find_completion_bounds ());
 			  pstate->push (operation_up (str_op));
 			  pstate->mark_struct_expression (str_op);
 			}
@@ -1223,10 +1239,11 @@ int
 ada_parse (struct parser_state *par_state)
 {
   /* Setting up the parser state.  */
-  scoped_restore pstate_restore = make_scoped_restore (&pstate);
+  scoped_restore pstate_restore = make_scoped_restore (&pstate, par_state);
   gdb_assert (par_state != NULL);
-  pstate = par_state;
-  original_expr = par_state->lexptr;
+
+  ada_parse_state parser (par_state->lexptr);
+  scoped_restore parser_restore = make_scoped_restore (&ada_parser, &parser);
 
   scoped_restore restore_yydebug = make_scoped_restore (&yydebug,
 							par_state->debug);
@@ -1849,13 +1866,13 @@ write_var_or_type (struct parser_state *par_state,
    Without this, an attempt like "complete print abc.d" will give a
    result like "print def" rather than "print abc.def".  */
 
-static std::string
-find_completion_bounds (struct parser_state *par_state)
+std::string
+ada_parse_state::find_completion_bounds ()
 {
   const char *end = pstate->lexptr;
   /* First the end of the prefix.  Here we stop at the token start or
      at '.' or space.  */
-  for (; end > original_expr && end[-1] != '.' && !isspace (end[-1]); --end)
+  for (; end > m_original_expr && end[-1] != '.' && !isspace (end[-1]); --end)
     {
       /* Nothing.  */
     }
@@ -1863,11 +1880,11 @@ find_completion_bounds (struct parser_state *par_state)
   const char *ptr = end;
   /* Here we allow '.'.  */
   for (;
-       ptr > original_expr && (ptr[-1] == '.'
-			       || ptr[-1] == '_'
-			       || (ptr[-1] >= 'a' && ptr[-1] <= 'z')
-			       || (ptr[-1] >= 'A' && ptr[-1] <= 'Z')
-			       || (ptr[-1] & 0xff) >= 0x80);
+       ptr > m_original_expr && (ptr[-1] == '.'
+				 || ptr[-1] == '_'
+				 || (ptr[-1] >= 'a' && ptr[-1] <= 'z')
+				 || (ptr[-1] >= 'A' && ptr[-1] <= 'Z')
+				 || (ptr[-1] & 0xff) >= 0x80);
        --ptr)
     {
       /* Nothing.  */
@@ -1903,7 +1920,7 @@ write_var_or_type_completion (struct parser_state *par_state,
 
   ada_structop_operation *op = write_selectors (par_state,
 						name0.ptr + tail_index);
-  op->set_prefix (find_completion_bounds (par_state));
+  op->set_prefix (ada_parser->find_completion_bounds ());
   par_state->mark_struct_expression (op);
   return nullptr;
 }

-- 
2.43.0


  parent reply	other threads:[~2024-03-21 19:04 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-21 19:03 [PATCH 00/12] Ada iterated assignment, plus parser cleanups Tom Tromey
2024-03-21 19:03 ` [PATCH 01/12] Introduce and use aggregate_assigner type Tom Tromey
2024-03-21 19:03 ` [PATCH 02/12] Implement Ada 2022 iterated assignment Tom Tromey
2024-03-21 19:03 ` Tom Tromey [this message]
2024-03-21 19:03 ` [PATCH 04/12] Move "int_storage" global into ada_parse_state Tom Tromey
2024-03-21 19:03 ` [PATCH 05/12] Move "components" and "associations" " Tom Tromey
2024-03-21 19:03 ` [PATCH 06/12] Move "assignments" global " Tom Tromey
2024-03-21 19:03 ` [PATCH 07/12] Move "iterated_associations" " Tom Tromey
2024-03-21 19:03 ` [PATCH 08/12] Move "temp_parse_space" " Tom Tromey
2024-03-21 19:03 ` [PATCH 09/12] Move "paren_depth" " Tom Tromey
2024-03-21 19:03 ` [PATCH 10/12] Move "returned_complete" " Tom Tromey
2024-03-21 19:03 ` [PATCH 11/12] Remove "numbuf" global Tom Tromey
2024-03-21 19:03 ` [PATCH 12/12] Constify ada-lex.l:attributes Tom Tromey
2024-04-02 17:35 ` [PATCH 00/12] Ada iterated assignment, plus parser cleanups Tom Tromey

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=20240321-ada-iterated-assign-v1-3-925cdd4f1f4a@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