C++ Logo


Advanced search

Re: [std-proposals] regex_top_level_token_iterator

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 2 May 2022 23:04:44 +0100
On Mon, May 2, 2022 at 7:44 PM Edward Catmur wrote:
> Does it correctly handle angle brackets denoting comparison operators within parentheses?
> template<bool = (5 > 3)> int i;

I hadn't thought about this. I have changed my 'switch' statement to
only process an angle bracket if it's not enclosed within other kinds
of bracket, as follows:

            switch ( *iter )
            case '(': ++(counts[0u]); continue;
            case ')': --(counts[0u]); continue;

            case '[': ++(counts[1u]); continue;
            case ']': --(counts[1u]); continue;

            case '{': ++(counts[2u]); continue;
            case '}': --(counts[2u]); continue;

            bool const process_next_angle_bracket =
                   0u == counts[0u]
                && 0u == counts[1u]
                && 0u == counts[2u];

            if ( process_next_angle_bracket )
                switch ( *iter )
                case '<': ++(counts[3u]); continue;
                case '>': --(counts[3u]); continue;

> What about (various) brackets within string literals? Raw string literals?

The best way to handle string literals is to do an initial scan of the
translation unit and to replace all string literals with A's. For
example, change the following line:

    cout << "I love these <> brackets" << endl;



And then as the final stage of processing, replace all the A's with
the original string again.

> Have you considered that maybe you're using the wrong tool for the job?
> Parsing any language with regex is going to be painful, but C++ in particular is likely to be excruciating.
> It might be worthwhile investing some time in finding a library that has already solved the problem.

I shall persevere and keep it as follows:
(1) My pre-compiler will only need a C++20 compiler with the C++
standard library (it won't even use Boost)
(2) I will strive to use regex where possible
(3) My pre-compiler will be just one source file

I want my pre-compiler to be extremely straight-forward to build and
to use. So for example with g++, you'll build it:

    g++ -o precompiler -std=c++20 precompiler.cpp

And then straight away you'll use it like this:

    g++ -E -P some_source_file.cpp | ./precompiler | g++ -o
some_source_file.o -x c++ -c -

Received on 2022-05-02 22:04:54