jamescourtney / FlatSharp

Fast, idiomatic C# implementation of Flatbuffers
Apache License 2.0
510 stars 51 forks source link

fix: emit attribute with escaped quote and backslash #445

Closed sssooonnnggg closed 2 weeks ago

sssooonnnggg commented 2 weeks ago

The fbs below cannot be compiled because of unescaped double quotes.

attribute custom_attribute;
namespace test;
table foo
{
    bar: int (custom_attribute: "hello, \"world\"");
}

This PR fixes it.

jamescourtney commented 2 weeks ago

Thanks! Are there other escape characters we need to be mindful of as well?

jamescourtney commented 2 weeks ago

I'd appreciate you adding a unit test here. FlatSharp.Compiler.UnitTests is the correct project.

sssooonnnggg commented 2 weeks ago

Thank you for your review. According to the source code of flatc's parser (idl_parser.cpp), we need to handle three cases: \", \', \\ \' does not need to be escaped in FlatSharp because "'" is a valid string in C# In addition to \\", we also need to handle \\ I will handle it and add unit tests later.

sssooonnnggg commented 2 weeks ago

@jamescourtney Please review again, process backslash and double quote, some unit tests added😊

jamescourtney commented 2 weeks ago

Thanks for your efforts here! I'm a little perplexed that flatc doesn't handle this for me, since it is the grammar parser after all. In any case, I'll take a look later today and hopefully approve.

sssooonnnggg commented 2 weeks ago

FYI,the string readed from flatc parser is converted from an escaped string in fbs file to the original string. When we generate C# code with a string literal, we need to convert the original string back to an escaped string.