Checking data processing for capturing groups in regular expressions (with SmPL)
Hello,
I tried script variants out again for the semantic patch language. Software combnination: Coccinelle 1.1.1-00448-g8e007c83 (OCaml 5.0)
@initialize:python@
@@
import re, sys
@script:python selection1@
@@
text = '"]"'sys.stderr.write('text: ' + text + '\n')
match = re.match('"([^"]+)"', text)
if match:
content = match.group(1)
sys.stderr.write('content: ' + content + '\n')
if (len(content) == 1) or ((len(content) == 2) and (content[0] == '\\')):
sys.stderr.write('Result: c\n')
else:
sys.stderr.write('Result: s\n')else:
sys.stderr.write('no match\n')
Expected test result:
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatch match-20230422-debug.cocci seq_printf1.c …
text: "]"
content: ]
Result: c
A corresponding source file example:
int main(void)
{
seq_printf(seq, "]");
seq_printf(seq, "<>");
seq_printf(seq, "%s", "Test");
return 0;
}@initialize:python@
@@
import re, sys
@find_update_candidate@
constant char[] input_string;
expression input_context;
position pos;
@@
seq_printf@pos(input_context, input_string);
@script:python selection2@
call;
param << find_update_candidate.input_string;
pos << find_update_candidate.pos;
text;
@@sys.stderr.write('param: ' + param + '\n')
match = re.match('"([^"]+)"', param)
if match:
content = match.group(1)
sys.stderr.write('content: ' + content + '\n')
if (len(content) == 1) or ((len(content) == 2) and (content[0] == '\\')):
x = "c"
mark = ["'", content, "'"]
t = ''.join(mark) else:
x = "s"
t = param
coccinelle.call = "seq_put" + x sys.stderr.write('call: ' + coccinelle.call + '\n') coccinelle.text = cocci.make_expr(t)
else:
cocci.include_match(False) sys.stderr.write('include_match(False)\n')
Unexpected test result:
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatch use_seq_putc_or_seq_puts-20230422-debug2.cocci seq_printf1.c …
param: "<>"include_match(False)
param: "]"include_match(False)
Why do I observe an undesirable difference for the evaluation of regular expressions here?
Regards,
Markus
Quoted 91 lines · Markus Elfring
Hello,
I tried script variants out again for the semantic patch language. Software combnination: Coccinelle 1.1.1-00448-g8e007c83 (OCaml 5.0)
@initialize:python@
@@
import re, sys@script:python selection1@
@@
text = '"]"'sys.stderr.write('text: ' + text + '\n')
match =re.match('"([^"]+)"', text)
if match:content = match.group(1) sys.stderr.write('content: ' + content + '\n') if (len(content) == 1) or ((len(content) == 2) and (content[0] == '\\')): sys.stderr.write('Result: c\n') else: sys.stderr.write('Result: s\n')else:
sys.stderr.write('no match\n')Expected test result:
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatchmatch-20230422-debug.cocciseq_printf1.c…
text: "]"
content: ]
Result: cA corresponding source file example:
int main(void){ seq_printf(seq, "]"); seq_printf(seq, "<>"); seq_printf(seq, "%s", "Test"); return 0; }@initialize:python@
@@
import re, sys@find_update_candidate@
constant char[]input_string;
expressioninput_context;
position pos;
@@
seq_printf@pos(input_context,input_string);@script:python selection2@
call;
param << find_update_candidate.input_string;
pos << find_update_candidate.pos;
text;
@@sys.stderr.write('param: ' + param + '\n')
match =re.match('"([^"]+)"', param)
if match:content = match.group(1) sys.stderr.write('content: ' + content + '\n') if (len(content) == 1) or ((len(content) == 2) and (content[0] == '\\')): x = "c" mark = ["'", content, "'"] t = ''.join(mark) else: x = "s" t = param coccinelle.call = "seq_put" + x sys.stderr.write('call: ' + coccinelle.call + '\n') coccinelle.text = cocci.make_expr(t)else:
cocci.include_match(False)
sys.stderr.write('include_match(False)\n')Unexpected test result:
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatchuse_seq_putc_or_seq_puts-20230422-debug2.cocciseq_printf1.c…
param: "<>"include_match(False)
param: "]"include_match(False)
Quoted 1 line
Why do I observe an undesirable difference for the evaluation of regular expressions here?
I know almost nothing about either python or regular expressions. Furthermore your semantic patch is very complicated and you have not provided the .c file. If you want a response, greatly simplify your semantic patch to the minimum necessary to show the problem, and provide the .c file, which should also be the minimum necessary to show the problem.
julia
Quoted 89 lines
Quoted 89 lines
I tried script variants out again for the semantic patch language. Software combination: Coccinelle 1.1.1-00448-g8e007c83 (OCaml 5.0)
@initialize:python@
@@
import re, sys@script:python selection1@
@@
text = '"]"'sys.stderr.write('text: ' + text + '\n')
match =re.match('"([^"]+)"', text)
if match:content = match.group(1) sys.stderr.write('content: ' + content + '\n') if (len(content) == 1) or ((len(content) == 2) and (content[0] == '\\')): sys.stderr.write('Result: c\n') else: sys.stderr.write('Result: s\n')else:
sys.stderr.write('no match\n')Expected test result:
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatchmatch-20230422-debug.cocciseq_printf1.c…
text: "]"
content: ]
Result: cA corresponding source file example:
int main(void){ seq_printf(seq, "]"); seq_printf(seq, "<>"); seq_printf(seq, "%s", "Test"); return 0; }@initialize:python@
@@
import re, sys@find_update_candidate@
constant char[]input_string;
expressioninput_context;
position pos;
@@
seq_printf@pos(input_context,input_string);@script:python selection2@
call;
param << find_update_candidate.input_string;
pos << find_update_candidate.pos;
text;
@@sys.stderr.write('param: ' + param + '\n')
match =re.match('"([^"]+)"', param)
if match:content = match.group(1) sys.stderr.write('content: ' + content + '\n') if (len(content) == 1) or ((len(content) == 2) and (content[0] == '\\')): x = "c" mark = ["'", content, "'"] t = ''.join(mark) else: x = "s" t = param coccinelle.call = "seq_put" + x sys.stderr.write('call: ' + coccinelle.call + '\n') coccinelle.text = cocci.make_expr(t)else:
cocci.include_match(False)
sys.stderr.write('include_match(False)\n')Unexpected test result:
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatchuse_seq_putc_or_seq_puts-20230422-debug2.cocciseq_printf1.c…
param: "<>"include_match(False)
param: "]"include_match(False)
Quoted 3 lines
Quoted 1 line
Why do I observe an undesirable difference for the evaluation of regular expressions here?
I know almost nothing about either python or regular expressions.
You probably know more about such software areas than you express here.
Quoted 2 lines
Furthermore your semantic patch is very complicated and you have not provided the .c file.
Please take another look at the provided source file example.
Quoted 2 lines
If you want a response, greatly simplify your semantic patch to the minimum necessary to show the problem,
I did that for this bug report.
Quoted 1 line
and provide the .c file, which should also be the minimum necessary to show the problem.
Have you accidentally overlooked any available information?
Do we need to compare the desired data processing by the means of OCaml code?
Regards,
Markus
Quoted 93 lines · Markus Elfring
Quoted 89 lines
Quoted 89 lines
I tried script variants out again for the semantic patch language. Software combination: Coccinelle 1.1.1-00448-g8e007c83 (OCaml 5.0)
@initialize:python@
@@
import re, sys@script:python selection1@
@@
text = '"]"'sys.stderr.write('text: ' + text + '\n')
match =re.match('"([^"]+)"', text)
if match:content = match.group(1) sys.stderr.write('content: ' + content + '\n') if (len(content) == 1) or ((len(content) == 2) and (content[0] == '\\')): sys.stderr.write('Result: c\n') else: sys.stderr.write('Result: s\n')else:
sys.stderr.write('no match\n')Expected test result:
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatchmatch-20230422-debug.cocciseq_printf1.c…
text: "]"
content: ]
Result: cA corresponding source file example:
int main(void){ seq_printf(seq, "]"); seq_printf(seq, "<>"); seq_printf(seq, "%s", "Test"); return 0; }@initialize:python@
@@
import re, sys@find_update_candidate@
constant char[]input_string;
expressioninput_context;
position pos;
@@
seq_printf@pos(input_context,input_string);@script:python selection2@
call;
param << find_update_candidate.input_string;
pos << find_update_candidate.pos;
text;
@@sys.stderr.write('param: ' + param + '\n')
match =re.match('"([^"]+)"', param)
if match:content = match.group(1) sys.stderr.write('content: ' + content + '\n') if (len(content) == 1) or ((len(content) == 2) and (content[0] == '\\')): x = "c" mark = ["'", content, "'"] t = ''.join(mark) else: x = "s" t = param coccinelle.call = "seq_put" + x sys.stderr.write('call: ' + coccinelle.call + '\n') coccinelle.text = cocci.make_expr(t)else:
cocci.include_match(False)
sys.stderr.write('include_match(False)\n')Unexpected test result:
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatchuse_seq_putc_or_seq_puts-20230422-debug2.cocciseq_printf1.c…
param: "<>"include_match(False)
param: "]"include_match(False)Quoted 3 lines
Quoted 1 line
Why do I observe an undesirable difference for the evaluation of regular expressions here?
I know almost nothing about either python or regular expressions.
Quoted 7 lines
You probably know more about such software areas than you express here.
Quoted 2 lines
Furthermore your semantic patch is very complicated and you have not provided the .c file.
Please take another look at the provided source file example.
OK, it was strange to put the expected results before the .c file input.
Quoted 6 lines
Quoted 4 lines
If you want a response, greatly simplify your semantic patch to the minimum necessary to show the problem,
I did that for this bug report.
This is clearly not the case. As a trivial example, your semantic patches don't contain any rules after the python code. But the python code contains cocci.include_match(False). What is the point of that? For another example, is your concern what is printed with text in the first rule and with param in the second rule? Those prints are on or near the first lines of the python scripts. What do you need the rest for? Or if those are not your concern, then why are they there at all. If you have multiple concerns, please just don't do that. Ask about only one thing at a time.
julia
Quoted 10 lines
Quoted 2 lines
and provide the .c file, which should also be the minimum necessary to show the problem.
Have you accidentally overlooked any available information?
Do we need to compare the desired data processing by the means of OCaml code?
Regards,
Markus
Quoted 3 lines
Quoted 1 line
Please take another look at the provided source file example.
OK, it was strange to put the expected results before the .c file input.
The first SmPL script example should not need an extra source file because the relevant data processing is actually performed by a bit of Python code. (The Coccinelle software insists to get another file name for the program “spatch”, doesn't it?)
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
If you want a response, greatly simplify your semantic patch to the minimum necessary to show the problem,
I did that for this bug report.
This is clearly not the case.
I suggest to reconsider this view.
Quoted 2 lines
As a trivial example, your semantic patches don't contain any rules after the python code.
This is intended because further clarification should happen for the evaluation of regular expressions in different application contexts.
Quoted 2 lines
But the python code contains
cocci.include_match(False). What is the point of that?
This statement should indicate another bit of intended data processing.
Quoted 2 lines
For another example, is your concern what is printed with text in the first rule and with param in the second rule?
Yes.
Such debug output should demonstrate questionable control flows.
Quoted 1 line
Ask about only one thing at a time.
Under which circumstances would you understand the presented data mismatch better?
I got the impression that the usage of an SmPL rule like “selection2” influences the control flow in an unexpected way.
Regards,
Markus
A message that is not in the archive (sent off-list or not yet received): alpine.DEB.2.22.394.2304221200090.2766@hadrien
Quoted 6 lines
Quoted 4 lines
Under which circumstances would you understand the presented data mismatch better?
I got the impression that the usage of an SmPL rule like “selection2” influences the control flow in an unexpected way.
I'm looking for 1-2 lines of python code,
Do you find it easier to interpret the affected code in smaller steps?
…
@find_update_candidate@
constant char[] input_string;
expression input_context;
position pos;
@@
seq_printf@pos(input_context, input_string);
@script:python selection2@
…
param << find_update_candidate.input_string;
pos << find_update_candidate.pos;
…
@@sys.stderr.write('param: ' + param + '\n')
match = re.match('"([^"]+)"', param)
if match:
content = match.group(1)
sys.stderr.write('content: ' + content + '\n')
if (len(content) == 1) or (…):
x = "c"
…
else:
x = "s"
… coccinelle.call = "seq_put" + x sys.stderr.write('call: ' + coccinelle.call + '\n') …
else:
… sys.stderr.write('include_match(False)\n')
Quoted 1 line
and 1-2 lines in the .c file.
Example:
…
seq_printf(seq, "]"); seq_printf(seq, "<>");
…
How do you think about to avoid undesirable communication difficulties by the means of an ordinary telephone call (or even a video conference)?
Regards,
Markus
A message that is not in the archive (sent off-list or not yet received): alpine.DEB.2.22.394.2304221329450.2766@hadrien
Quoted 22 lines
Quoted 10 lines
Do you find it easier to interpret the affected code in smaller steps?
…
@find_update_candidate@
constant char[]input_string;
expressioninput_context;
position pos;
@@
seq_printf@pos(input_context,input_string);Quoted 11 lines
@script:python selection2@
…
param << find_update_candidate.input_string;
pos << find_update_candidate.pos;
…
@@sys.stderr.write('param: ' + param + '\n')
match =re.match('"([^"]+)"', param)
if match:
content =match.group(1)
sys.stderr.write('content: ' + content + '\n')
Quoted 1 line
Why can't this branch just stop here?
Special circumstances trigger data processing surprises for the determination which data were assigned to the variable “param” (as a string literal).
Quoted 1 line
Can't the user just see whether the length of content is 1 from the line above?
No.
Quoted 6 lines
Quoted 6 lines
if (len(content) == 1) or (…): x = "c" … else: x = "s" …
But I expect that this case distinction will also occasionally be performed so that an appropriate character can be appended to a function name.
Quoted 6 lines
Quoted 6 lines
coccinelle.call = "
seq_put" + xsys.stderr.write('call: ' + coccinelle.call + '\n')…
else:
… sys.stderr.write('include_match(False)\n')
Does such a code part work as expected in your software test environment?
Regards,
Markus
Quoted 3 lines
If you want a response, greatly simplify your semantic patch to the minimum necessary to show the problem, and provide the .c file, which should also be the minimum necessary to show the problem.
Will the understanding grow for a questionable software situation with the following test code?
Another SmPL script variant:
@initialize:python@
@@
import re, sys
@find_update_candidate@
constant char[] input_string;
expression input_context;
@@
action(input_context, input_string);
@script:python selection3@
text << find_update_candidate.input_string;
@@sys.stderr.write('text: ' + text + '\n')
match = re.match('"([^"]+)"', text)
if match:
sys.stderr.write('content: ' + match.group(1) + '\n')
else:
sys.stderr.write('no match\n')
Another corresponding source file example:
void f(void)
{action(x, "X");action(y, "++");
}
How reasonable do you find the following test result?
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatch check_string_literal3-20230422-debug.cocci string_literal_usage4.c …
text: "++"
no match
text: "X"
no match
How do you think about to omit extra white-space characters here before a double quotation character?
https://en.cppreference.com/w/cpp/language/string_literal
Regards,
Markus
Quoted 36 lines · Markus Elfring
Quoted 3 lines
If you want a response, greatly simplify your semantic patch to the minimum necessary to show the problem, and provide the .c file, which should also be the minimum necessary to show the problem.
Will the understanding grow for a questionable software situation with the following test code?
Another SmPL script variant:
@initialize:python@
@@
import re, sys@find_update_candidate@
constant char[]input_string;
expressioninput_context;
@@
action(input_context, input_string);@script:python selection3@
text << find_update_candidate.input_string;
@@sys.stderr.write('text: ' + text + '\n')
match =re.match('"([^"]+)"', text)
if match:sys.stderr.write('content: ' + match.group(1) + '\n')else:
sys.stderr.write('no match\n')Another corresponding source file example:
void f(void)
{action(x, "X");action(y, "++");
}
Quoted 8 lines
How reasonable do you find the following test result?
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatch
check_string_literal3-20230422-debug.coccistring_literal_usage4.c…
text: "++"
no match
text: "X"
no match
OK, it seems that you expect the value of a metavariable bound to a string to have extra string quotes inside of it. Maybe it would be useful.
julia
Quoted 9 lines
How do you think about to omit extra white-space characters here before a double quotation character?
https://en.cppreference.com/w/cpp/language/string_literalRegards,
Markus
Re: [cocci] Checking data processing for provided string literals (with SmPL)
Quoted 11 lines
Quoted 8 lines
How reasonable do you find the following test result?
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatch
check_string_literal3-20230422-debug.coccistring_literal_usage4.c…
text: "++"
no match
text: "X"
no matchOK, it seems that you expect the value of a metavariable bound to a string to have extra string quotes inside of it. Maybe it would be useful.
I got the impression also from this feedback that we stumble on a different understanding for desirable software behaviour.
String literals must be enclosed by double quotation characters.
https://en.cppreference.com/w/cpp/language/string_literal
Should white-space characters be excluded around them for the contents of corresponding metavariables?
Regards,
Markus
Quoted 46 lines · Markus Elfring
Quoted 3 lines
If you want a response, greatly simplify your semantic patch to the minimum necessary to show the problem, and provide the .c file, which should also be the minimum necessary to show the problem.
Will the understanding grow for a questionable software situation with the following test code?
Another SmPL script variant:
@initialize:python@
@@
import re, sys@find_update_candidate@
constant char[]input_string;
expressioninput_context;
@@
action(input_context, input_string);@script:python selection3@
text << find_update_candidate.input_string;
@@sys.stderr.write('text: ' + text + '\n')
match =re.match('"([^"]+)"', text)
if match:sys.stderr.write('content: ' + match.group(1) + '\n')else:
sys.stderr.write('no match\n')Another corresponding source file example:
void f(void)
{action(x, "X");action(y, "++");
}How reasonable do you find the following test result?
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatch
check_string_literal3-20230422-debug.coccistring_literal_usage4.c…
text: "++"
no match
text: "X"
no match
Quoted 3 lines
How do you think about to omit extra white-space characters here before a double quotation character?
https://en.cppreference.com/w/cpp/language/string_literal
OK, maybe not. Maybe you are concerned about something related to white space. But a helpful way to show some issue about whitespace around a string would be somethng like
Printf.printf "|%s|\n" x
(or whatever is the equivalent in python).
Not 20 lines of python code.
julia
Quoted 4 lines
Regards,
Markus
Quoted 5 lines
Quoted 3 lines
How do you think about to omit extra white-space characters here before a double quotation character?
https://en.cppreference.com/w/cpp/language/string_literalOK, maybe not. Maybe you are concerned about something related to white space.
I would appreciate another bit of data type safety also for this use case.
How much repeated data format checks can eventually be avoided accordingly?
See also:
2016-01-06
Support for string literals by another metavariable type
https://github.com/coccinelle/coccinelle/issues/55
Regards,
Markus
Re: [cocci] Checking selected contents from metavariables by SmPL scripting rules
In reply to Julia LawallQuoted 2 lines
But a helpful way to show some issue about whitespace around a string would be somethng like
Source file example:
void f(void)
{action(x, "X");action(y, "++");
}
SmPL script variant:
@find@
constant char[] input_string;
expression input_context;
@@
action(input_context, input_string);
@script:ocaml special_display1@
text << find.input_string;
@@
Printf.printf "text: =>%s<=\n" text
Test result:
Markus_Elfring@Sonne:/home/altes_Heim2/elfring/Projekte/Coccinelle/Probe> spatch ../janitor/show_string_data_with_OCaml-20230423.cocci string_literal_usage4.c …
text: =>"++"<=
text: =>"X"<=
Another SmPL script variant:
@initialize:python@
@@
import sys
@find@
constant char[] input_string;
expression input_context;
@@
action(input_context, input_string);
@script:python special_display2@
text << find.input_string;
@@sys.stdout.write('text: =>' + text + '<=\n')
Another test result:
Markus_Elfring@Sonne:/home/altes_Heim2/elfring/Projekte/Coccinelle/Probe> spatch ../janitor/show_string_data_with_Python-20230423.cocci string_literal_usage4.c …
text: => "++"<=
text: => "X"<=
Will such a data presentation trigger further software evolution?
Regards,
Markus
Re: [cocci] Checking selected contents from metavariables by SmPL scripting rules
Quoted 49 lines · Markus Elfring
Quoted 2 lines
But a helpful way to show some issue about whitespace around a string would be somethng like
Source file example:
void f(void)
{action(x, "X");action(y, "++");
}SmPL script variant:
@find@
constant char[]input_string;
expressioninput_context;
@@
action(input_context, input_string);@script:ocaml special_display1@
text << find.input_string;
@@
Printf.printf "text: =>%s<=\n" textTest result:
Markus_Elfring@Sonne:/home/altes_Heim2/elfring/Projekte/Coccinelle/Probe> spatch../janitor/show_string_data_with_OCaml-20230423.coccistring_literal_usage4.c…
text: =>"++"<=
text: =>"X"<=Another SmPL script variant:
@initialize:python@
@@
import sys@find@
constant char[]input_string;
expressioninput_context;
@@
action(input_context, input_string);@script:python special_display2@
text << find.input_string;
@@sys.stdout.write('text: =>' + text + '<=\n')
Quoted 8 lines
Another test result:
Markus_Elfring@Sonne:/home/altes_Heim2/elfring/Projekte/Coccinelle/Probe> spatch../janitor/show_string_data_with_Python-20230423.coccistring_literal_usage4.c…
text: => "++"<=
text: => "X"<=Will such a data presentation trigger further software evolution?
Thank you for presenting the problem in a much more understandable way. I will take a look.
julia
Re: [cocci] Checking selected contents from metavariables by SmPL scripting rules
Quoted 4 lines
Quoted 1 line
Will such a data presentation trigger further software evolution?
Thank you for presenting the problem in a much more understandable way. I will take a look.
Thanks for such a positive feedback.
I became curious how this look will evolve further after you got frightened by a bit of Python code.
Which software components would be responsible for the addition of space characters according to a data representation (by Python objects)?
Regards,
Markus
Re: [cocci] Checking data processing order for SmPL rule evaluations
In reply to Markus ElfringQuoted 8 lines
Another corresponding source file example:
void f(void)
{action(x, "X");action(y, "++");
}
Quoted 8 lines
How reasonable do you find the following test result?
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatch
check_string_literal3-20230422-debug.coccistring_literal_usage4.c…
text: "++"
no match
text: "X"
no match
I find that another detail will need further clarification. The debug output indicates that data were processed in the opposite order according to the shown two function calls.
May I expect that the original control flow order should be preserved?
Regards,
Markus
Re: [cocci] Checking data processing order for SmPL rule evaluations
Quoted 17 lines · Markus Elfring
Quoted 8 lines
Another corresponding source file example:
void f(void)
{action(x, "X");action(y, "++");
}Quoted 8 lines
How reasonable do you find the following test result?
Markus_Elfring@Sonne:…/Projekte/Coccinelle/Probe> spatch
check_string_literal3-20230422-debug.coccistring_literal_usage4.c…
text: "++"
no match
text: "X"
no match
Quoted 4 lines
I find that another detail will need further clarification. The debug output indicates that data were processed in the opposite order according to the shown two function calls.
May I expect that the original control flow order should be preserved?
No.
julia
Re: [cocci] Checking data processing order for SmPL rule evaluations
Quoted 3 lines
Quoted 1 line
May I expect that the original control flow order should be preserved?
No.
I find such a feedback surprising.
Will there be a need to be more sure about the desired order for data processing items?
Regards,
Markus