-
Notifications
You must be signed in to change notification settings - Fork 35
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
Support for escape sequences in String #485
Comments
@Remo, I don't well understand your issue. What i have guessed is that you want CSLY to automatically replace escaped sequence. So for example replace \n by a (char)10 character, or (char)09 by a tabulation. Is this what want ? |
@Remo, you could use lexer callbacks to replace to replace all escaped sequences before the parse phase. this could be something like (warning not tested) using sly.lexer;
namespace escape
{
[CallBacks(typeof(TestCallbacks))]
public enum CallbackTokens
{
[String] STRING
}
public class TestCallbacks
{
[TokenCallback((int)CallbackTokens.STRING)]
public static Token<CallbackTokens> EscapeEscapedSequences(Token<CallbackTokens> token)
{
string value = token.Value;
value = value.Replace("\\", "\\\\");
value = value.Replace("\n", "\\n");
value = value.Replace("\r", "\\r");
value = value.Replace("\t", "\\t");
// ... address all escaped sequences
token.SpanValue = new ReadOnlyMemory<char>(value.ToCharArray());
return token;
}
}
} |
Thanks for your reply!
My lexer:
And then a simple rule like this;
What I would like to see in C# is a string like this:
What I currently get is: I'm not expecting csly to do the magic for me, but the |
oh ok I see. when lexing string escape char (here |
@Remo i've publish CSLY nuget 3.3.0, you can test it. In fact i 've found some other issues fixing yours, so great thank to report. I've done a simple test with cslyviz - escape So you can have a look. As graphviz automatically substitute if you look at the JSON (on the source tab) you will see the |
let me know if it works as expected. |
You can also have a look at my commit fe72161, it embeds a test case that more or less matches what you're trying to do using a lexerCallback |
Wonderful, everything is working great! I don't think I would have been able to fix this myself, at least not yet. I still don't understand the code of csly fully, but I'm a happy user for sure. |
I've been looking around to find a way to support escape sequences inside my string.
I've got
[String] STRING : "\"" "\\";
in my grammar file. It works very well for single and multi line strings.Sometimes, when I'm only asking for a specific character like a line break, I don't want to have an actual line break in my code, especially because different editors will have different line breaks.
What I wanted in my input is something like
\n
.In the long run, I would like to support all of these:
https://learn.microsoft.com/en-us/cpp/c-language/escape-sequences?view=msvc-170
Any pointer where I have to go to find a solution for this?
The text was updated successfully, but these errors were encountered: