Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Comment Parsing Code #3399

Conversation

InsertCreativityHere
Copy link
Member

This PR simplifies the implementation of parseDocComment, which is the part of libSlice responsible for converting a raw string comment into a structured DocComment.

It's not meant to be a perfect cleanup.
But just to simplify some things before I add the last validation logic/fix the issues related to how @links are formatted.

I used my compiler-comparison script to check, and the generated code is identical before and after my changes here.

@@ -648,48 +648,44 @@ namespace
if (stripMarkup)
{
// Strip HTML markup.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switching to a while loop lets us avoid a redundant check for pos != string::npos.

Copy link
Member

@pepone pepone Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code assumes anything between < and > is an html tag.

What about something like:

a < b or x > y , won't this strip the comment to something like a y?

It is not a new problem, but would be nice to fix now that we are looking at this code.

We can fix in a separate PR.

} while (pos != string::npos);
comment.erase(pos, endpos - pos + 1);
}
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic was nested inside if (stripMarkup). This doesn't seem right to me.
XML escaping should be unrelated to markdown striping.

@@ -742,35 +738,27 @@ namespace
return result;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function took a bool namedTag parameter which was always hard-coded.
And even worse, it took a const string& name which was only used some of the time based on the bool.
So I split it into 2 functions: parseNamedCommentLine and parseCommentLine. No more bool.

@@ -803,94 +807,69 @@ Slice::Contained::parseDocComment(function<string(string, string)> linkFormatter

DocCommentPtr comment = make_shared<DocComment>();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the remaining code below this is where we parse the actual tags (@param etc) into their respective fields on DocComment. Before, we had an entire state machine, but this was overkill.

I removed all this logic, and replaced it with a StringList* currentSection,
which points to whichever part of the doc-comment we're currently parsing.
At the beginning it points to the overview, but as we encounter tags, we re-point it appropiately.
I find this approach simpler than using a state-machine, and switching on the state to determine where we're writing to.

@@ -648,48 +648,44 @@ namespace
if (stripMarkup)
{
// Strip HTML markup.
Copy link
Member

@pepone pepone Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code assumes anything between < and > is an html tag.

What about something like:

a < b or x > y , won't this strip the comment to something like a y?

It is not a new problem, but would be nice to fix now that we are looking at this code.

We can fix in a separate PR.

@InsertCreativityHere
Copy link
Member Author

I agree, it's also a problem I noticed, but didn't want to fix in this PR.
At first I thought about making the check fail if there was whitespace within the < ... >...
but I wasn't sure enough that that was a safe assumption.

@bernardnormier bernardnormier requested review from externl and removed request for bernardnormier January 22, 2025 15:51
@InsertCreativityHere InsertCreativityHere merged commit 1dd0241 into zeroc-ice:main Jan 22, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants