Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simark@simark.ca>
To: Tom Tromey <tromey@adacore.com>, gdb-patches@sourceware.org
Subject: Re: [PATCH] Rewrite the Ada lexer
Date: Tue, 3 Nov 2026 15:28:58 -0500	[thread overview]
Message-ID: <bbfa94b7-3150-4a10-945f-7b58ea57a3d2@simark.ca> (raw)
In-Reply-To: <20261103142330.1512630-1-tromey@adacore.com>

On 11/3/26 9:23 AM, Tom Tromey wrote:
> Recently a series landed that changed how the Ada lexer was built.  In
> particular, commit d3d68d79 ("gdb: let the parser and lexer generators
> prefix their symbols") changed gdb to require a newer version of flex.
> 
> Unfortunately, this version of flex is reported as being difficult to
> build with mingw; and this change broke all the Windows-hosted builds
> at AdaCore.

I am terribly sorry about that... I could have helped find a workaround,
or worst case reverted the patch.

> 
> Rather than try to puzzle out the flex build, I decided to rewrite the
> lexer to avoid the flex dependency.  This patch is the result.  I
> tried for a fairly minimal transition, so some oddities are preserved.
> Further cleanups are possible (I have a few I will send later).

At least this has the advantage of making the Ada parser more like the
others, and it's one less hassle to deal with.

> I've also added some unit tests.  In some cases these test things that
> previously were not tested in the tree.

The flex-based lexer calls processId like this:

  processId (yytext, yyleng);

IIUC correctly, yytext points to a null-terminated string that contains
only the just-lexed token.

The new lexer calls it like this:

  processId (pstate->lexptr, id_end - pstate->lexptr);

which means processId now receives a pointer into the original text.
processId does an unbounded search:

  if (name0[0] == '<' || strstr (name0, "___") != NULL)

That strstr will search past LEN, and could match something else later
in the string, outside the current token.  It would need to be adjusted
to stop at "name0 + LEN".

You can try with the input:

  FOO + x___y

> +/* Helper to check a lexing sequence, also checking string values of
> +   tokens.  */
> +static void
> +ada_lex_test_sequence (const char *input, gdb::array_view<int> expected,
> +		       std::optional<gdb::array_view<const char *>> strings,
> +		       bool completion = false)
> +{
> +  gdb_assert (!strings.has_value () || expected.size () == strings->size ());

It might be easier to follow to have just one combined array_view.  This
way, it would be easier to see what is produced at each step, since the
expected string will be next to the corresponding expected token.  For
instance, this:

  std::array cc_test { NAME, COLONCOLON, NAME, 0 };
  std::array<const char *, 4> cc_text { "blockname", nullptr,
					"duck", nullptr };
  ada_lex_test_sequence ("blockname::duck", cc_test, cc_text);

could become:

  lex_res cc_test[]
    = { { NAME, "blockname" },
	{ COLONCOLON },
	{ NAME, "duck" },
	{} };
  ada_lex_test_sequence ("blockname::duck", cc_test);

Where lex_res is a dummy struct:

struct lex_res
{
  int token;
  const char *str;
};

> @@ -64,18 +61,69 @@ struct ada_parse_state
>  
>    auto_obstack temp_space;
>  
> -  /* Depth of parentheses, used by the lexer.  */
> -  int paren_depth = 0;
> -
> -  /* When completing, we'll return a special character at the end of the
> -     input, to signal the completion position to the lexer.  This is
> -     done because flex does not have a generally useful way to detect
> -     EOF in a pattern.  This variable records whether the special
> -     character has been emitted.  */
> -  bool returned_complete = false;
> +  /* Lex a single token.  ada_yylval is updated if needed.  The token
> +     value is returned.  The lexer state in this object or in the
> +     parser_state object will be updated as appropriate.  */
> +  int lex ();
>  
>  private:
>  
> +  /* A lexer helper function to lex a number.  */
> +  int lex_number ();
> +
> +  /* A lexer helper function to lex a dollar variable like $x or
> +     $$.  */
> +  int lex_dollar ();
> +
> +  /* A lexer helper that is called when a '.' is seen.  */
> +  int lex_dot ();
> +
> +  /* A lexer helper that is called when a single quote is seen.  */
> +  int lex_single_quote ();
> +
> +  /* A lexer helper for lexing a string.  */
> +  int lex_double_quote ();
> +
> +  /* A helper function that examines TEXT.  If TEXT looks like a
> +     simple identifier, a pointer to the character past the end of the
> +     identifier is returned.  Otherwise, nullptr is returned.  */
> +  const char *lex_simple_identifier (const char *text);
> +
> +  /* A helper that does some extra checking for the "thread"
> +     pseudo-keyword.  It returns true if this instance of "thread"
> +     should be reported as an EOF rather than an identifier.  TEXT
> +     points to just past the end of the "thread" keyword.  */
> +  bool check_thread_keyword (const char *text);
> +
> +  /* Check whether the current token, held at pstate->lexptr with
> +     length LEN, is a keyword.  Returns the token value if so; returns
> +     nullopt if not.  */
> +  std::optional<int> check_keyword (size_t len);

I think it would be a bit clearer if you passed the string to this
function instead of having it read pstate->lexptr.

> diff --git a/gdb/ada-exp-parser.y b/gdb/ada-exp-parser.y
> index d37ab12c3bc..f5ceb7d390a 100644
> --- a/gdb/ada-exp-parser.y
> +++ b/gdb/ada-exp-parser.y
> @@ -50,7 +50,11 @@
>  using namespace expr;
>  using namespace ada_exp_parser;
>  
> -static int yylex (void);
> +static inline int
> +yylex ()
> +{
> +  return ada_parser->lex ();
> +}

"inline" not really needed I guess.

> +int
> +ada_parse_state::lex_single_quote ()
> +{
> +  gdb_assert (*pstate->lexptr == '\'');
> +
> +  bool before_qual_quote = m_before_qual_quote;
> +  m_before_qual_quote = false;
> +
> +  if (!before_qual_quote)
> +    {
> +      /* Look for the GDB block extension "'STUFF'::".  */
> +      const char *block_iter = pstate->lexptr + 1;
> +      while (*block_iter != '\0' && *block_iter != '\'')
> +	++block_iter;
> +      /* Require a non-empty block.  */
> +      if (block_iter[0] == '\'' && block_iter - pstate->lexptr - 1 > 0)
> +	{
> +	  const char *block_end = skip_spaces (block_iter + 1);
> +	  if (block_end[0] == ':' && block_end[1] == ':')
> +	    {
> +	      ada_yylval.sval = processId (pstate->lexptr,
> +					   block_end - pstate->lexptr);
> +	      /* Leave the "::" to be lexed later.  */
> +	      pstate->lexptr = block_end;
> +	      return NAME;
> +	    }
> +	}
> +
> +      if (c_isprint (pstate->lexptr[1]) && pstate->lexptr[2] == '\'')
> +	{
> +	  /* Ordinary character literal.  */
> +	  int the_char = pstate->lexptr[1];
> +	  ada_yylval.typed_char.val = the_char;
> +	  ada_yylval.typed_char.type = type_for_char (pstate, the_char);
> +	  pstate->lexptr += 3;
> +	  return CHARLIT;
> +	}
> +
> +      const char *end_bracket = recognize_encoded_char (pstate->lexptr + 1);
> +      if (end_bracket != nullptr)
> +	{
> +	  /* The character starts at an offset from the initial single
> +	     quote.  */
> +	  ULONGEST v = strtoulst (pstate->lexptr + 3, nullptr, 16);
> +	  ada_yylval.typed_char.val = v;
> +	  ada_yylval.typed_char.type = type_for_char (pstate, v);
> +	  pstate->lexptr = end_bracket + 1;
> +	  return CHARLIT;
> +	}
> +    }

The new code misses checking that *end_bracket is a single quote, so I
guess it can accept some invalid inputs:

  '["61"]

  '["61"]x


> diff --git a/gdb/ada-exp-parser.h b/gdb/ada-exp-parser.h
> index 09a716e6636..48bc9e7fb72 100644
> --- a/gdb/ada-exp-parser.h
> +++ b/gdb/ada-exp-parser.h
> @@ -23,9 +23,6 @@
>  #include "ada-exp.h"
>  #include "parser-defs.h"
>  
> -/* The character we use to represent the completion point.  */
> -constexpr char COMPLETE_CHAR = '\001';
> -
>  namespace ada_exp_parser
>  {
>  
> @@ -64,18 +61,69 @@ struct ada_parse_state
>  
>    auto_obstack temp_space;
>  
> -  /* Depth of parentheses, used by the lexer.  */
> -  int paren_depth = 0;
> -
> -  /* When completing, we'll return a special character at the end of the
> -     input, to signal the completion position to the lexer.  This is
> -     done because flex does not have a generally useful way to detect
> -     EOF in a pattern.  This variable records whether the special
> -     character has been emitted.  */
> -  bool returned_complete = false;
> +  /* Lex a single token.  ada_yylval is updated if needed.  The token
> +     value is returned.  The lexer state in this object or in the
> +     parser_state object will be updated as appropriate.  */
> +  int lex ();
>  
>  private:
>  
> +  /* A lexer helper function to lex a number.  */
> +  int lex_number ();
> +
> +  /* A lexer helper function to lex a dollar variable like $x or
> +     $$.  */
> +  int lex_dollar ();
> +
> +  /* A lexer helper that is called when a '.' is seen.  */
> +  int lex_dot ();
> +
> +  /* A lexer helper that is called when a single quote is seen.  */
> +  int lex_single_quote ();
> +
> +  /* A lexer helper for lexing a string.  */
> +  int lex_double_quote ();
> +
> +  /* A helper function that examines TEXT.  If TEXT looks like a
> +     simple identifier, a pointer to the character past the end of the
> +     identifier is returned.  Otherwise, nullptr is returned.  */
> +  const char *lex_simple_identifier (const char *text);
> +
> +  /* A helper that does some extra checking for the "thread"
> +     pseudo-keyword.  It returns true if this instance of "thread"
> +     should be reported as an EOF rather than an identifier.  TEXT
> +     points to just past the end of the "thread" keyword.  */
> +  bool check_thread_keyword (const char *text);
> +
> +  /* Check whether the current token, held at pstate->lexptr with
> +     length LEN, is a keyword.  Returns the token value if so; returns
> +     nullopt if not.  */
> +  std::optional<int> check_keyword (size_t len);

I think it would be a bit clearer if you passed the string to this
function instead of having it read pstate->lexptr.

> diff --git a/gdb/ada-exp-parser.y b/gdb/ada-exp-parser.y
> index d37ab12c3bc..f5ceb7d390a 100644
> --- a/gdb/ada-exp-parser.y
> +++ b/gdb/ada-exp-parser.y
> @@ -50,7 +50,11 @@
>  using namespace expr;
>  using namespace ada_exp_parser;
>  
> -static int yylex (void);
> +static inline int
> +yylex ()
> +{
> +  return ada_parser->lex ();
> +}

"inline" not really needed I guess.

Simon

  reply	other threads:[~2026-11-03 20:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03 14:23 Tom Tromey
2026-11-03 20:28 ` Simon Marchi [this message]
2026-11-17 18:40   ` 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=bbfa94b7-3150-4a10-945f-7b58ea57a3d2@simark.ca \
    --to=simark@simark.ca \
    --cc=gdb-patches@sourceware.org \
    --cc=tromey@adacore.com \
    /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