Home > Back-end >  Regular Expression to convert m4 code to jinja code
Regular Expression to convert m4 code to jinja code

Time:05-23

I've been working for a few days for work and I don't know how to convert these lines of code:

m4_define(\-:PM_Requirement:-/,\-:m4_dnl
<requirement><id>EG_Crypto_Test_0815</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0816</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0817</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0818</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0819</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0820</id><version>1</version></requirement>:-/)

to:

{%- set Requirement = "<requirement><id>EG_Crypto_Test_0815</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0816</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0817</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0818</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0819</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0820</id><version>1</version></requirement>"
-%}

I tried the following regex in sed:

sed -i -E 's/m4_define\(\\-:PM_Requirement:-\/,\\-:m4_dnl([(\r\n|\r|\n)\W[:alnum:]_] ):-\/\)

i tried debugg in https://regex101.com/r/ExM1Qx/1

Could anyone give me a bit of help, what i could do to make it work?

I tried to replace \W with something else, but did not find the right combination that could be accepted, i don't know the "flavor" of regex that is used here

The above command I used is for PCRE (PHP <7.3)

Anyone has some good idea about this ?

PS: Other commands that work look like this:

sed -i -E 's/m4_define\(\\-:PM_([[:alnuum:]_] ):-\/,\\:-([[:alnum:]_\/] ):-\/\)  {%- set \1 = "\2" -%}/'

The result is this (convert m4 in jinja):

m4 code:

m4_define(\-:PM_ImplementationFile:-/,\-:Crypto_1_Test:-/)

to Jinja code:

{%- set ImplementationFile = "Crypto_1_Test" -%}

CodePudding user response:

If you want to use sed you might use

sed -E ':a;$!{N;ba};s|m4_define\(\\-:PM_([[:alnum:]_] ):-/,\\-:([[:alnum:]_]*\r?\n)?([[:print:][:space:]]*):-/\)|{%- set \1 = "\3" -%}|' file

See a first sed demo and second sed demo.

Output for the first example

{%- set Requirement = "<requirement><id>EG_Crypto_Test_0815</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0816</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0817</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0818</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0819</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0820</id><version>1</version></requirement>" -%}

Output for the second example:

{%- set ImplementationFile = "Crypto_1_Test" -%}

CodePudding user response:

As the input consists of M4 macros you can use M4 to do the replacement.

With the following file macros.m4

define(m4_dnl, `dnl')dnl
define(m4_define, {%- set regexp($1, \(PM_\)?\(.*\), \2) = "$2" -%})dnl
changequote(\-:, :-/)dnl

run

m4 macros.m4 inputfile

to get (nearly) the requested output. The only difference is that with the first input, the trailing -%} is not in a separate line.

{%- set Requirement = "<requirement><id>EG_Crypto_Test_0815</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0816</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0817</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0818</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0819</id><version>1</version></requirement>
<requirement><id>EG_Crypto_Test_0820</id><version>1</version></requirement>" -%}

Explanation

The macro m4_define is defined to generate the requested output from the macro arguments.

The regexp macro inside m4_define's replacement is used to remove a leading PM_ in the first macro argument $1 if present. (The second macro argument $2 is used unchanged.)

The changequote macro defines the unusual quote characters \-: and :-/ used in the input data.

  • Related