mirror of
https://github.com/Stone-Red-Code/YesNt-Interpreter.git
synced 2026-09-08 23:43:18 +02:00
Add string literal parsing with escapes and related tests
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
using System.Text;
|
||||
|
||||
using YesNt.Interpreter.Attributes;
|
||||
using YesNt.Interpreter.Enums;
|
||||
using YesNt.Interpreter.Runtime;
|
||||
using YesNt.Interpreter.Utilities;
|
||||
|
||||
namespace YesNt.Interpreter.Statements;
|
||||
|
||||
internal class StringLiteralStatements : StatementRuntimeInformation
|
||||
{
|
||||
[Statement("\"", SearchMode.Contains, SpaceAround.None, System.ConsoleColor.DarkYellow, Priority = Priority.PreProcessing, KeepStatementInArgs = true)]
|
||||
public void ParseStringLiterals(string args)
|
||||
{
|
||||
if (!args.Contains('"'))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
StringBuilder output = new StringBuilder(args.Length);
|
||||
|
||||
for (int i = 0; i < args.Length; i++)
|
||||
{
|
||||
char current = args[i];
|
||||
if (current != '"')
|
||||
{
|
||||
_ = output.Append(current);
|
||||
continue;
|
||||
}
|
||||
|
||||
StringBuilder literal = new StringBuilder();
|
||||
bool closed = false;
|
||||
i++;
|
||||
|
||||
for (; i < args.Length; i++)
|
||||
{
|
||||
char ch = args[i];
|
||||
if (ch == '\\' && i + 1 < args.Length)
|
||||
{
|
||||
i++;
|
||||
_ = literal.Append(ParseEscape(args[i]));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch == '"')
|
||||
{
|
||||
closed = true;
|
||||
break;
|
||||
}
|
||||
|
||||
_ = literal.Append(ch);
|
||||
}
|
||||
|
||||
if (!closed)
|
||||
{
|
||||
RuntimeInfo.Exit("Invalid string literal", true);
|
||||
return;
|
||||
}
|
||||
|
||||
_ = output.Append(literal.ToString().ToSafeString());
|
||||
}
|
||||
|
||||
RuntimeInfo.CurrentLine = output.ToString();
|
||||
}
|
||||
|
||||
private static char ParseEscape(char escapeChar)
|
||||
{
|
||||
return escapeChar switch
|
||||
{
|
||||
'n' => '\n',
|
||||
'r' => '\r',
|
||||
't' => '\t',
|
||||
'"' => '"',
|
||||
'\\' => '\\',
|
||||
_ => escapeChar
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user