mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-28 11:49:44 +00:00
LibWeb: Fix IDL Generation error for enums
In the case, where IDL enums start with a character, that is an invalid start character for C++ identifiers (e.g. a number), the C++ generaion for enums fails. An example would be "2d" see #3788
This commit is contained in:
parent
1182ee6c69
commit
678f531fe5
Notes:
github-actions[bot]
2025-03-04 08:32:26 +00:00
Author: https://github.com/Totto16
Commit: 678f531fe5
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3789
Reviewed-by: https://github.com/tcl3 ✅
1 changed files with 29 additions and 0 deletions
|
@ -51,13 +51,42 @@
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool is_valid_cpp_identifier_start(char start)
|
||||||
|
{
|
||||||
|
// https://en.cppreference.com/w/cpp/language/identifiers
|
||||||
|
// to be a valid C++ identifier it must be a:
|
||||||
|
|
||||||
|
// uppercase Latin letter A-Z
|
||||||
|
// lowercase Latin letter a-z
|
||||||
|
if (is_ascii_alpha(start)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// underscore
|
||||||
|
if (start == '_') {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: any Unicode character with the Unicode property XID_Start
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
static ByteString convert_enumeration_value_to_cpp_enum_member(ByteString const& value, HashTable<ByteString>& names_already_seen)
|
static ByteString convert_enumeration_value_to_cpp_enum_member(ByteString const& value, HashTable<ByteString>& names_already_seen)
|
||||||
{
|
{
|
||||||
StringBuilder builder;
|
StringBuilder builder;
|
||||||
GenericLexer lexer { value };
|
GenericLexer lexer { value };
|
||||||
|
|
||||||
|
bool first_word = true;
|
||||||
while (!lexer.is_eof()) {
|
while (!lexer.is_eof()) {
|
||||||
lexer.ignore_while([](auto c) { return is_ascii_space(c) || c == '-' || c == '_'; });
|
lexer.ignore_while([](auto c) { return is_ascii_space(c) || c == '-' || c == '_'; });
|
||||||
|
if (first_word) {
|
||||||
|
auto const first_letter = lexer.peek();
|
||||||
|
if (!is_valid_cpp_identifier_start(first_letter))
|
||||||
|
builder.append('_');
|
||||||
|
first_word = false;
|
||||||
|
}
|
||||||
|
|
||||||
auto word = lexer.consume_while([](auto c) { return is_ascii_alphanumeric(c); });
|
auto word = lexer.consume_while([](auto c) { return is_ascii_alphanumeric(c); });
|
||||||
if (!word.is_empty()) {
|
if (!word.is_empty()) {
|
||||||
builder.append(word.to_titlecase_string());
|
builder.append(word.to_titlecase_string());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue