C++ Logo

std-proposals

Advanced search

Re: [std-proposals] regex over istreams

From: Phil Bouchard <boost_at_[hidden]>
Date: Thu, 20 Feb 2025 16:35:49 -0500
On 2/20/25 16:17, Jonathan Wakely wrote:
>
>
> On Thu, 20 Feb 2025 at 13:48, Phil Bouchard via Std-Proposals
> <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>>
> wrote:
>
> __
>
> Greetings,
>
> Seems to me regex over istreams would be quite useful. Here's an
> inefficient implementation:
>
>
> inline std::istream& operator >> (std::istream& in, std::string
> & v)
> {
> static const std::regex string_regex(R"(^\"(.*?)\"$)");
>
>
> This uses a hardcoded regular expression to extract from the stream.
> That doesn't seem very useful. Surely you would want to be able to
> customize the regex? What API are you proposing for that?

This is just an example function. My goal is to write a robust parser
using standard C++ operator >>.

>
>
> std::smatch match;
>
> std::string x;
>
> for (x += in.get(); in; x += in.get())
> if (std::regex_match(x, match, string_regex))
> {
> v = match[1].str();
>
> return in;
> }
>
> throw std::invalid_argument("Parse error.");
> }
>
> Is there any interests in such implementation?

Anybody who wants a clean parser using the C++ syntax. I always thought
the reading capabilities of operator >> were lacking a robust lexer.

On top of that you could easily write a parser:

void parse(istream & in)
{
     declaration d;
     statement s;

     if (in >> d, in.ok())
         // process declarations
     else if (in >> s, in.ok())
         // process statements
}

Here each failed conditional parsing would rewind and keep a good()
rdstate to keep on parsing. ok() here would be another distinct rdstate
flag representing the state of the last lexed token.


>
> Regards,
>
> --
> Fornux Logo <https://www.fornux.com/>
> *Phil Bouchard* LinkedIn Icon
> <https://www.linkedin.com/in/phil-bouchard-5723a910/>
> Founder & CEO
> T: (819) 328-4743
> E: phil_at_[hidden] <mailto:phil_at_[hidden]>| www.fornux.com
> <http://www.fornux.com>
> 320-345 de la Gauchetière Ouest| Montréal (Qc), H2Z 0A2 Canada
>
> The Best Predictable C++ Memory Manager
> <https://static.fornux.com/c-superset/> Le message ci-dessus, ainsi
> que les documents l'accompagnant, sont destinés uniquement aux
> personnes identifiées et peuvent contenir des informations
> privilégiées, confidentielles ou ne pouvant être divulguées. Si vous
> avez reçu ce message par erreur, veuillez le détruire.
> This communication (and/or the attachments) is intended for named
> recipients only and may contain privileged or confidential
> information which is not to be disclosed. If you received this
> communication by mistake please destroy all copies.
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden] <mailto:Std-Proposals_at_[hidden]>
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> <https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals>
>

-- 
Fornux Logo <https://www.fornux.com/>  
*Phil Bouchard*  LinkedIn Icon
<https://www.linkedin.com/in/phil-bouchard-5723a910/> 
Founder & CEO
T: (819) 328-4743
E: phil_at_[hidden]| www.fornux.com <http://www.fornux.com>
320-345 de la Gauchetière Ouest| Montréal (Qc), H2Z 0A2 Canada
The Best Predictable C++ Memory Manager
<https://static.fornux.com/c-superset/> Le message ci-dessus, ainsi que
les documents l'accompagnant, sont destinés uniquement aux personnes
identifiées et peuvent contenir des informations privilégiées,
confidentielles ou ne pouvant être divulguées. Si vous avez reçu ce
message par erreur, veuillez le détruire.
This communication (and/or the attachments) is intended for named
recipients only and may contain privileged or confidential information
which is not to be disclosed. If you received this communication by
mistake please destroy all copies.

Received on 2025-02-20 21:35:51