boost – How to ignore keywords in regex before variables?

I trying to create a function which is capable of

  • putting precision modulator keywords in front of float variables where
    there is no such a keyword

So it converts

const float x;

to

lowp const float x;

However I would like to ignore the following scenarios:

  • When there is already a precision modifier: lowp const float x;
  • When there is a default precision modulator: precision lowp float;

Based on my previous question link,This is my regex:

(highp|lowp|mediump((?:\s+\w+)*)(float)(*SKIP)(?!)|(?=\s+(float)))

So, I just would like to ignore those cases, when there is a highp|lowp|mediump before float.

I have this regex command:

#include 
#include 
#include 

using namespace boost;

std::string precisionModulation(std::string& shaderSource) {

    const regex highpFloatRegex2(R"(highp|lowp|mediump((?:\s+\w+)*)(float)(*SKIP)(?!)|(?=\s+(float)))");
    shaderSource = regex_replace(shaderSource, highpFloatRegex2, "lowp");

    return shaderSource;
}

int main() {
    std::string shaderSource = R"(
float foo1;
highp const float foo1;
precision highp float foo2;
    )";

    std::cout << "Original Shader Source:\n" << shaderSource << std::endl;
    std::string modifiedSource = precisionModulation(shaderSource);
    std::cout << "\nModified Shader Source:\n" << modifiedSource << std::endl;

    return 0;
}

Unfortunately I got weird results:

Original Shader Source:

float foo1;
highp const float foo1;
precision highp float foo2;


Modified Shader Source:
 lowp
float foo1;
 lowp  const lowp  float foo1;
precision  lowp  lowp  float foo2;

I also tried:

\w*(?

Read more here: Source link