Skip to content

Commit

Permalink
Allow implicit grouping with --posix-captures.
Browse files Browse the repository at this point in the history
Allow the use of named definitions that cause implicit grouping and
circumvent the absence of syntax for non-capturing groups. This is not
allowed by the POSIX standard, but this feature is useful in practice
and many regexp engines support it.

Update the tests that should no longer result into a compilation error.

This addresses bugs #438 "POSIX captures processing" and #308
"Overloading precedence marker (e.g. (pattern)) with 'posix' group
markers (also parenthesis) causes loss of full functionality".
  • Loading branch information
skvadrik committed Mar 24, 2023
1 parent 8bae0b0 commit f519385
Show file tree
Hide file tree
Showing 3 changed files with 592 additions and 15 deletions.
13 changes: 0 additions & 13 deletions src/regexp/ast_to_re.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,6 @@ LOCAL_NODISCARD(Regexp* insert_between_tags(RESpec& spec, Regexp* tags, Regexp*
return tags;
}

LOCAL_NODISCARD(Ret check_misuse_of_named_def(RESpec& spec, const AstNode* ast)) {
DCHECK(ast->kind == AstKind::REF);
if (spec.opts->tags_posix_syntax) {
RET_FAIL(spec.msg.error(ast->loc,
"implicit grouping is forbidden with '--posix-captures' option, "
"please wrap '%s' in capturing parenthesis",
ast->ref.name));
}
return Ret::OK;
}

LOCAL_NODISCARD(Ret check_tags_used_once(
RESpec& spec, const Rule& rule, const std::vector<Tag>& tags)) {
std::set<std::string> names;
Expand Down Expand Up @@ -330,7 +319,6 @@ LOCAL_NODISCARD(Ret diff_to_range(RESpec& spec,
break;

case AstKind::REF:
CHECK_RET(check_misuse_of_named_def(spec, ast));
x.ast = ast->ref.ast; // replace on stack
break;

Expand Down Expand Up @@ -458,7 +446,6 @@ LOCAL_NODISCARD(Ret ast_to_re(RESpec& spec,
break;

case AstKind::REF:
CHECK_RET(check_misuse_of_named_def(spec, ast));
x.ast = ast->ref.ast;
break;

Expand Down
274 changes: 273 additions & 1 deletion test/posix_captures/implicit_grouping1.c
Original file line number Diff line number Diff line change
@@ -1 +1,273 @@
posix_captures/implicit_grouping1.re:10:8: error: implicit grouping is forbidden with '--posix-captures' option, please wrap 'x' in capturing parenthesis
/* Generated by re2c */
// re2c $INPUT -o $OUTPUT -i --posix-captures


{
YYCTYPE yych;
if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
yych = *YYCURSOR;
switch (yych) {
case 'a': goto yy3;
default: goto yy1;
}
yy1:
++YYCURSOR;
yy2:
{}
yy3:
yych = *++YYCURSOR;
switch (yych) {
case 'a': goto yy4;
default: goto yy2;
}
yy4:
++YYCURSOR;
yynmatch = 1;
yypmatch[0] = YYCURSOR - 2;
yypmatch[1] = YYCURSOR;
{}
}



{
YYCTYPE yych;
if ((YYLIMIT - YYCURSOR) < 4) YYFILL(4);
yych = *YYCURSOR;
switch (yych) {
case 'a': goto yy8;
default: goto yy6;
}
yy6:
++YYCURSOR;
yy7:
{}
yy8:
yych = *(YYMARKER = ++YYCURSOR);
switch (yych) {
case 'b': goto yy9;
default: goto yy7;
}
yy9:
yych = *++YYCURSOR;
switch (yych) {
case 'a': goto yy11;
default: goto yy10;
}
yy10:
YYCURSOR = YYMARKER;
goto yy7;
yy11:
yych = *++YYCURSOR;
switch (yych) {
case 'b': goto yy12;
default: goto yy10;
}
yy12:
++YYCURSOR;
yynmatch = 1;
yypmatch[0] = YYCURSOR - 4;
yypmatch[1] = YYCURSOR;
{}
}



{
YYCTYPE yych;
if ((YYLIMIT - YYCURSOR) < 4) YYFILL(4);
yych = *YYCURSOR;
switch (yych) {
case 'a': goto yy16;
default: goto yy14;
}
yy14:
++YYCURSOR;
yy15:
{}
yy16:
yych = *(YYMARKER = ++YYCURSOR);
switch (yych) {
case 'b': goto yy17;
default: goto yy15;
}
yy17:
yych = *++YYCURSOR;
switch (yych) {
case 'a': goto yy19;
default: goto yy18;
}
yy18:
YYCURSOR = YYMARKER;
goto yy15;
yy19:
yych = *++YYCURSOR;
switch (yych) {
case 'b': goto yy20;
default: goto yy18;
}
yy20:
++YYCURSOR;
yynmatch = 3;
yypmatch[0] = YYCURSOR - 4;
yypmatch[1] = YYCURSOR;
yypmatch[2] = YYCURSOR - 3;
yypmatch[3] = YYCURSOR - 2;
yypmatch[4] = YYCURSOR - 1;
yypmatch[5] = YYCURSOR;
{}
}



{
YYCTYPE yych;
if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
yych = *YYCURSOR;
switch (yych) {
case 'a':
case 'b': goto yy24;
default: goto yy22;
}
yy22:
++YYCURSOR;
yy23:
{}
yy24:
yych = *++YYCURSOR;
switch (yych) {
case 'a':
case 'b': goto yy25;
default: goto yy23;
}
yy25:
++YYCURSOR;
yynmatch = 1;
yypmatch[0] = YYCURSOR - 2;
yypmatch[1] = YYCURSOR;
{}
}



{
YYCTYPE yych;
if ((YYLIMIT - YYCURSOR) < 2) YYFILL(2);
yych = *YYCURSOR;
switch (yych) {
case 'a': goto yy29;
case 'b': goto yy30;
default: goto yy27;
}
yy27:
++YYCURSOR;
yy28:
{}
yy29:
yych = *++YYCURSOR;
switch (yych) {
case 'a':
yyt1 = NULL;
goto yy31;
case 'b':
yyt1 = NULL;
goto yy33;
default: goto yy28;
}
yy30:
yych = *++YYCURSOR;
switch (yych) {
case 'a':
yyt1 = YYCURSOR;
goto yy31;
case 'b':
yyt1 = YYCURSOR;
goto yy33;
default: goto yy28;
}
yy31:
++YYCURSOR;
yyt2 = NULL;
yy32:
yynmatch = 3;
yypmatch[3] = yyt1;
yypmatch[5] = yyt2;
yypmatch[0] = YYCURSOR - 2;
yypmatch[1] = YYCURSOR;
yypmatch[2] = yyt1;
if (yyt1 != NULL) yypmatch[2] -= 1;
yypmatch[4] = yyt2;
if (yyt2 != NULL) yypmatch[4] -= 1;
{}
yy33:
++YYCURSOR;
yyt2 = YYCURSOR;
goto yy32;
}



{
YYCTYPE yych;
if (YYLIMIT <= YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
yyt1 = YYCURSOR;
switch (yych) {
case 'a': goto yy36;
default: goto yy35;
}
yy35:
yynmatch = 1;
yypmatch[0] = yyt1;
yypmatch[1] = YYCURSOR;
{}
yy36:
++YYCURSOR;
if (YYLIMIT <= YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
switch (yych) {
case 'a': goto yy36;
default: goto yy35;
}
}



{
YYCTYPE yych;
if (YYLIMIT <= YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
switch (yych) {
case 'a':
yyt1 = YYCURSOR;
goto yy39;
default:
yyt2 = NULL;
yyt1 = YYCURSOR;
goto yy38;
}
yy38:
yynmatch = 3;
yypmatch[0] = yyt1;
yypmatch[3] = yyt2;
yypmatch[5] = yyt2;
yypmatch[1] = YYCURSOR;
yypmatch[2] = yyt2;
if (yyt2 != NULL) yypmatch[2] -= 2;
yypmatch[4] = yyt2;
if (yyt2 != NULL) yypmatch[4] -= 2;
{}
yy39:
++YYCURSOR;
if (YYLIMIT <= YYCURSOR) YYFILL(1);
yych = *YYCURSOR;
switch (yych) {
case 'a': goto yy39;
default:
yyt2 = NULL;
goto yy38;
}
}

posix_captures/implicit_grouping1.re:35:9: warning: rule matches empty string [-Wmatch-empty-string]
posix_captures/implicit_grouping1.re:40:9: warning: rule matches empty string [-Wmatch-empty-string]

0 comments on commit f519385

Please sign in to comment.