Fix parsing of enums with trailing comma with -doxygen

To correctly parse Doxygen post-comments after the trailing comma,
switch to right recursion in "enumlist" production rule definition: this
does consume more stack space when parsing, but makes the rules much
easier to write and to understand and hopefully shouldn't result in any
problems with real code (which shouldn't have thousands of enums items
in it).

Closes #1514.
This commit is contained in:
Vadim Zeitlin 2019-04-18 17:46:13 +02:00
commit 74adaa5738
6 changed files with 43 additions and 36 deletions

View file

@ -32,4 +32,9 @@
SOME_ITEM_30 ///< Post comment for the third item
};
enum SomeEnumWithTrailingComma
{
SOME_ITEM_100, ///< Post comment after comma.
SOME_ITEM_200, ///< Post comment after last comma.
};
%}

View file

@ -55,6 +55,10 @@ public class doxygen_parsing_enums_proper_runme {
wantedComments.put("doxygen_parsing_enums_proper.SomeAnotherEnum2.SOME_ITEM_20",
"Post comment for the second item \n" +
"");
wantedComments.put("doxygen_parsing_enums_proper.SomeEnumWithTrailingComma.SOME_ITEM_100",
"Post comment after comma.");
wantedComments.put("doxygen_parsing_enums_proper.SomeEnumWithTrailingComma.SOME_ITEM_200",
"Post comment after last comma.");
// and ask the parser to check comments for us
System.exit(parser.check(wantedComments));

View file

@ -47,6 +47,10 @@ public class doxygen_parsing_enums_simple_runme {
" The comment for the first item \n" +
" \n" +
"");
wantedComments.put("doxygen_parsing_enums_simple.doxygen_parsing_enums_simpleConstants.SOME_ITEM_100",
"Post comment after comma.");
wantedComments.put("doxygen_parsing_enums_simple.doxygen_parsing_enums_simpleConstants.SOME_ITEM_200",
"Post comment after last comma.");
// and ask the parser to check comments for us
System.exit(parser.check(wantedComments));

View file

@ -55,6 +55,10 @@ public class doxygen_parsing_enums_typesafe_runme {
wantedComments.put("doxygen_parsing_enums_typesafe.SomeAnotherEnum2.SOME_ITEM_30",
"Post comment for the third item \n" +
"");
wantedComments.put("doxygen_parsing_enums_typesafe.SomeEnumWithTrailingComma.SOME_ITEM_100",
"Post comment after comma.");
wantedComments.put("doxygen_parsing_enums_typesafe.SomeEnumWithTrailingComma.SOME_ITEM_200",
"Post comment after last comma.");
// and ask the parser to check comments for us

View file

@ -55,6 +55,10 @@ public class doxygen_parsing_enums_typeunsafe_runme {
wantedComments.put("doxygen_parsing_enums_typeunsafe.SomeAnotherEnum2.SOME_ITEM_10",
"Post comment for the first item \n" +
"");
wantedComments.put("doxygen_parsing_enums_typeunsafe.SomeEnumWithTrailingComma.SOME_ITEM_100",
"Post comment after comma.");
wantedComments.put("doxygen_parsing_enums_typeunsafe.SomeEnumWithTrailingComma.SOME_ITEM_200",
"Post comment after last comma.");
// and ask the parser to check comments for us
System.exit(parser.check(wantedComments));

View file

@ -1650,7 +1650,7 @@ static String *add_qualifier_to_declarator(SwigType *type, SwigType *qualifier)
/* C declarations */
%type <node> c_declaration c_decl c_decl_tail c_enum_key c_enum_inherit c_enum_decl c_enum_forward_decl c_constructor_decl;
%type <node> enumlist enumlist_tail enumlist_item edecl_with_dox edecl;
%type <node> enumlist enumlist_item edecl_with_dox edecl;
/* C++ declarations */
%type <node> cpp_declaration cpp_class_decl cpp_forward_class_decl cpp_template_decl cpp_alternate_rettype;
@ -6374,21 +6374,31 @@ optional_ignored_defines
| empty
;
optional_ignored_define_after_comma
: empty
| COMMA
| COMMA constant_directive
;
/* Enum lists - any #define macros (constant directives) within the enum list are ignored. Trailing commas accepted. */
enumlist : enumlist_item optional_ignored_define_after_comma {
enumlist : enumlist_item {
Setattr($1,"_last",$1);
$$ = $1;
}
| enumlist_item enumlist_tail optional_ignored_define_after_comma {
set_nextSibling($1, $2);
Setattr($1,"_last",Getattr($2,"_last"));
Setattr($2,"_last",NULL);
| enumlist_item DOXYGENPOSTSTRING {
Setattr($1,"_last",$1);
set_comment($1, $2);
$$ = $1;
}
| enumlist_item COMMA enumlist {
if ($3) {
set_nextSibling($1, $3);
Setattr($1,"_last",Getattr($3,"_last"));
Setattr($3,"_last",NULL);
}
$$ = $1;
}
| enumlist_item COMMA DOXYGENPOSTSTRING enumlist {
if ($4) {
set_nextSibling($1, $4);
Setattr($1,"_last",Getattr($4,"_last"));
Setattr($4,"_last",NULL);
}
set_comment($1, $3);
$$ = $1;
}
| optional_ignored_defines {
@ -6396,17 +6406,6 @@ enumlist : enumlist_item optional_ignored_define_after_comma {
}
;
enumlist_tail : COMMA enumlist_item {
Setattr($2,"_last",$2);
$$ = $2;
}
| enumlist_tail COMMA enumlist_item {
set_nextSibling(Getattr($1,"_last"), $3);
Setattr($1,"_last",$3);
$$ = $1;
}
;
enumlist_item : optional_ignored_defines edecl_with_dox optional_ignored_defines {
$$ = $2;
}
@ -6419,19 +6418,6 @@ edecl_with_dox : edecl {
$$ = $2;
set_comment($2, $1);
}
| edecl DOXYGENPOSTSTRING {
$$ = $1;
set_comment($1, $2);
}
| DOXYGENPOSTSTRING edecl {
$$ = $2;
set_comment(previousNode, $1);
}
| DOXYGENPOSTSTRING edecl DOXYGENPOSTSTRING {
$$ = $2;
set_comment(previousNode, $1);
set_comment($2, $3);
}
;
edecl : identifier {