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

Refactor and add tests for console command parsing #1459

Merged
merged 17 commits into from
Sep 18, 2023

Conversation

dgelessus
Copy link
Contributor

Converts the very "old C-like" console command parsing code into its own class that has a bit more structure and uses modern C++ data types. Also, unit tests!

Aside from cleaning up the code, my motivation here is to allow parsing console commands without actually executing them. This would be especially useful for parsing .ini files - thinking of server.ini or cases like in #1244.

Suggestions for improving the parsing API are welcome. It has to be a bit complex unfortunately, to support situations where the input should only be partially parsed (e. g. tab completion), and because of the wacky command name syntax where parsing depends on which commands are defined...

@dpogue
Copy link
Member

dpogue commented Sep 6, 2023

There's one (fairly trivial) conflict here, but this seems good to me from a quick skim over things

Copy link
Member

@Hoikas Hoikas left a comment

Choose a reason for hiding this comment

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

Looking forward to seeing this rebased so it can be merged

return begin;
}

std::pair<pfConsoleCmdGroup*, std::optional<ST::string>> pfConsoleParser::ParseGroupAndName()
Copy link
Member

Choose a reason for hiding this comment

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

Slight preference for this to be an std::tuple because of std::pair implying the usage of a map. But that's just a preference because a lot of implementations just have pair use tuple.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah hm, is there a convention that std::pair is specifically for map-related things? I assumed it was just a specialized alternative to a 2-element std::tuple, but I don't know the stdlib too well...

@dgelessus
Copy link
Contributor Author

(I'll get the rebase done this weekend probably)

Although they share a bit of code, the core logic is quite different.
This code could only be reached when *line == '\0', so strlen(line) was
always 0 and the addition did nothing.
This way the input buffer no longer needs to be modified, because the
returned tokens are copied and not returned "in-place" from the buffer.
In preparation for refactoring and extending it into a more usable
standalone parser.
This avoids having to pass a non-const reference into every call and
allows signaling errors in a less dirty way.
Should now handle embedded zero bytes correctly (as if anyone cares...).
These have to be coupled, because command name parts can also be
separated using spaces instead of dots/underscores.
Anything that needs possibly partial or ambiguous lookups (help, tab
completion) still needs the complex API that parses "as far as
possible".
@Hoikas Hoikas merged commit 7ebef73 into H-uru:master Sep 18, 2023
14 checks passed
@dgelessus dgelessus deleted the console_parsing branch November 29, 2023 21:31
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.

3 participants