]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: merge error handling from different expression parsers
authorAndrew Burgess <aburgess@redhat.com>
Tue, 2 Jan 2024 14:02:44 +0000 (14:02 +0000)
committerAndrew Burgess <aburgess@redhat.com>
Thu, 4 Jan 2024 09:24:18 +0000 (09:24 +0000)
Many (all?) of the expression parsers implement yyerror to handle
parser errors, and all of these functions are basically identical.

This commit adds a new parser_state::parse_error() function, which
implements the common error handling code, this function can then be
called from all the different yyerror functions.

The benefit of this is that (in a future commit) I can improve the
error output, and all the expression parsers will benefit.

This commit is pure refactoring though, and so, there should be no
user visible changes after this commit.

Approved-By: John Baldwin <jhb@FreeBSD.org>
gdb/ada-exp.y
gdb/c-exp.y
gdb/d-exp.y
gdb/f-exp.y
gdb/go-exp.y
gdb/m2-exp.y
gdb/p-exp.y
gdb/parse.c
gdb/parser-defs.h

index fcb5aa4379b02ff0d91d0ef010637cf061f532df..2a1cff5088700f7f177b530df38966859e42303b 100644 (file)
@@ -1212,7 +1212,7 @@ ada_parse (struct parser_state *par_state)
 static void
 yyerror (const char *msg)
 {
-  error (_("Error in expression, near `%s'."), pstate->lexptr);
+  pstate->parse_error (msg);
 }
 
 /* Emit expression to access an instance of SYM, in block BLOCK (if
index 2b4c21850d39e7646c29b6a349d4f677a0ab5127..6697b3b2278bcc107ee4cce99b6b4ced338c593e 100644 (file)
@@ -3482,8 +3482,5 @@ c_print_token (FILE *file, int type, YYSTYPE value)
 static void
 yyerror (const char *msg)
 {
-  if (pstate->prev_lexptr)
-    pstate->lexptr = pstate->prev_lexptr;
-
-  error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
+  pstate->parse_error (msg);
 }
index e2507982d502f0e8b53ab619f5f8c76a57227427..627c681d895bbc228978255b98cef510b955a714 100644 (file)
@@ -1631,9 +1631,6 @@ d_parse (struct parser_state *par_state)
 static void
 yyerror (const char *msg)
 {
-  if (pstate->prev_lexptr)
-    pstate->lexptr = pstate->prev_lexptr;
-
-  error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
+  pstate->parse_error (msg);
 }
 
index e4e2171d6418b2508ec6273b4308482d7300fbb0..88a95bccb11aa75318be0696e27c100ed84be3c4 100644 (file)
@@ -1736,8 +1736,5 @@ f_language::parser (struct parser_state *par_state) const
 static void
 yyerror (const char *msg)
 {
-  if (pstate->prev_lexptr)
-    pstate->lexptr = pstate->prev_lexptr;
-
-  error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
+  pstate->parse_error (msg);
 }
index c9b9c0b1ab72e398e615b046fcc1f4b8731652f6..561a3bef1b0a16970b859b5cfcba2aebab0c217a 100644 (file)
@@ -1545,8 +1545,5 @@ go_language::parser (struct parser_state *par_state) const
 static void
 yyerror (const char *msg)
 {
-  if (pstate->prev_lexptr)
-    pstate->lexptr = pstate->prev_lexptr;
-
-  error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
+  pstate->parse_error (msg);
 }
index 092a8be248d46e4699852a39c8b08fde64c4f970..9a8767f5ac72b54f9b422ae8acc17e8cf64b74c9 100644 (file)
@@ -1006,8 +1006,5 @@ m2_language::parser (struct parser_state *par_state) const
 static void
 yyerror (const char *msg)
 {
-  if (pstate->prev_lexptr)
-    pstate->lexptr = pstate->prev_lexptr;
-
-  error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
+  pstate->parse_error (msg);
 }
index b0f334897ad6380e64e6ebe0d5e7a5c4b1bfba1f..9dfa8c5fd4fe84f88b443372040cf12e4d42d558 100644 (file)
@@ -1660,8 +1660,5 @@ pascal_language::parser (struct parser_state *par_state) const
 static void
 yyerror (const char *msg)
 {
-  if (pstate->prev_lexptr)
-    pstate->lexptr = pstate->prev_lexptr;
-
-  error (_("A %s in expression, near `%s'."), msg, pstate->lexptr);
+  pstate->parse_error (msg);
 }
index b57d112fafd9515ff9d2ea09af6e0be7ccdd2d74..efac0dee1af5a4c9d715c4a3ba9fbc36ae7a9764 100644 (file)
@@ -244,6 +244,17 @@ parser_state::push_dollar (struct stoken str)
     (create_internalvar (copy.c_str () + 1));
 }
 
+/* See parser-defs.h.  */
+
+void
+parser_state::parse_error (const char *msg)
+{
+  if (this->prev_lexptr)
+    this->lexptr = this->prev_lexptr;
+
+  error (_("A %s in expression, near `%s'."), msg, this->lexptr);
+}
+
 \f
 
 const char *
index 93ebdf5c06145b5cd3decb29f07f6f73674b554b..24522bbbb15fcdbd73a8c0b047c2d71a88dd40d5 100644 (file)
@@ -262,6 +262,11 @@ struct parser_state : public expr_builder
     push (expr::make_operation<T> (std::move (lhs), std::move (rhs)));
   }
 
+  /* Function called from the various parsers' yyerror functions to throw
+     an error.  The error will include a message identifying the location
+     of the error within the current expression.  */
+  void parse_error (const char *msg);
+
   /* If this is nonzero, this block is used as the lexical context for
      symbol names.  */