From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id RNvMJC1E6mr/TA0AWB0awg (envelope-from ) for ; Tue, 03 Nov 2026 15:29:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1793737772; bh=Ia/PeKHcpRaL1wMdMRTIOUh4DX7larMVQy/7k31OSLU=; h=Date:Subject:To:References:From:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From; b=acg/eItUQkamUPnJiyo/3dXKjcnMJjR/TvpqXlkPBImHUlcnW/hw8amOOtH2AsNZZ faLFa0YuB+0zjzgw5uwwVE2cOimVdmt41NYY31lrc7/PTSY7IWOQpXYtitvAXj0av8 ntz6DVszbCaiEYmQNcd2drUhMkH+UEmfBLeSKTn0= Received: by simark.ca (Postfix, from userid 112) id EA2DE1E043; Tue, 03 Nov 2026 15:29:32 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-5.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham autolearn_force=no version=4.0.1 Authentication-Results: simark.ca; dkim=pass (1024-bit key; unprotected) header.d=simark.ca header.i=@simark.ca header.a=rsa-sha256 header.s=mail header.b=Haej9gl8; dkim-atps=neutral Received: from vm01.sourceware.org (vm01.sourceware.org [IPv6:2620:52:6:3111::32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 7A3041E043 for ; Tue, 03 Nov 2026 15:29:27 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4A9C04BA903B for ; Tue, 3 Nov 2026 20:29:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4A9C04BA903B Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=simark.ca header.i=@simark.ca header.a=rsa-sha256 header.s=mail header.b=Haej9gl8 Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 3C24A4BA23F3 for ; Tue, 3 Nov 2026 20:29:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3C24A4BA23F3 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=simark.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=simark.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 3C24A4BA23F3 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=158.69.221.121 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793737740; cv=none; b=IFy1wDI1T3k8eXg0c/YeMgHrPQCeEtHrwBy8y8hbzenA9TewcjPFV0mmobRO/NHpo6DnjWp6zA41C59lxgYnN4jqGXmdOoXujYqKdMl8lgX5ynOsTxo3O5kQnQE3awzHXcXe6hrMOFHbIiEz4oOv8n0t6FSB63D3g+AUuBQ0axU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1793737740; c=relaxed/simple; bh=Ia/PeKHcpRaL1wMdMRTIOUh4DX7larMVQy/7k31OSLU=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=L2oD0M6tnlBH+exWyzkb9Ylwx/W+WI3IA7Y3GaBvb7G4GHoPviidJohsl7aLqJ80O2hWCBFs0kzVwQepLYmY0NexFiF0nGc/eD+k7xwQE3JFa3gyz2qTqkLg1KDv3wK1/ZuvHsuslSoKJFp+dunlkreIF4HQJgzrBsLwHK2nvyo= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=simark.ca header.i=@simark.ca header.a=rsa-sha256 header.s=mail header.b=Haej9gl8 DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3C24A4BA23F3 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=simark.ca; s=mail; t=1793737739; bh=Ia/PeKHcpRaL1wMdMRTIOUh4DX7larMVQy/7k31OSLU=; h=Date:Subject:To:References:From:In-Reply-To:From; b=Haej9gl8FYmA0hEzOySdVRDQadWiARyGvIbWngWE3MUzBGdZrMNiAEmCOrpj2z6EC CBFt0ogEa2PhS8DqqjrdZs0LcOBQ3uBjF1ttSAqYett/1QW1Ivez5sn41AHxVzElaj md3hWZiETpieGBUGk471Z8+FYd8oVHgdp7lBd6Lc= Received: by simark.ca (Postfix) id 3CEBA1E043; Tue, 03 Nov 2026 15:28:59 -0500 (EST) Message-ID: Date: Tue, 3 Nov 2026 15:28:58 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] Rewrite the Ada lexer To: Tom Tromey , gdb-patches@sourceware.org References: <20261103142330.1512630-1-tromey@adacore.com> Content-Language: fr From: Simon Marchi In-Reply-To: <20261103142330.1512630-1-tromey@adacore.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org 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 expected, > + std::optional> 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 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 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 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