diff --git a/src/Lua/CodeAnalysis/Syntax/Lexer.cs b/src/Lua/CodeAnalysis/Syntax/Lexer.cs index 5ecbe7e7..6ac4a4aa 100644 --- a/src/Lua/CodeAnalysis/Syntax/Lexer.cs +++ b/src/Lua/CodeAnalysis/Syntax/Lexer.cs @@ -324,7 +324,13 @@ public bool MoveNext() if (c is '\\') { Advance(1); + if (span.Length <= offset) break; + if (span[offset] == '\r') + { + if (span.Length<=offset +1) continue; + if (span[offset+1] == '\n')Advance(1); + } } else if (c == quote) { @@ -530,8 +536,8 @@ void ReadNumber(ref ReadOnlySpan span, ref int offset, out int readCount) static bool IsIdentifier(char c) { return c == '_' || - ('A' <= c && c <= 'Z') || - ('a' <= c && c <= 'z') || - StringHelper.IsNumber(c); + ('A' <= c && c <= 'Z') || + ('a' <= c && c <= 'z') || + StringHelper.IsNumber(c); } -} +} \ No newline at end of file diff --git a/src/Lua/Internal/StringHelper.cs b/src/Lua/Internal/StringHelper.cs index 0342d289..993dfc44 100644 --- a/src/Lua/Internal/StringHelper.cs +++ b/src/Lua/Internal/StringHelper.cs @@ -37,7 +37,7 @@ public static bool TryFromStringLiteral(ReadOnlySpan literal, [NotNullWhen builder.Append('\n'); break; case '\r': - builder.Append('\r'); + builder.Append('\n'); // check CRLF if (i + 1 < literal.Length && literal[i + 1] is '\n') { @@ -306,6 +306,7 @@ public static Regex ToRegex(ReadOnlySpan pattern) builder.Append(c); break; } + isEscapeSequence = false; } } @@ -358,7 +359,7 @@ public static bool IsNumber(char c) public static bool IsDigit(char c) { return IsNumber(c) || - ('a' <= c && c <= 'f') || - ('A' <= c && c <= 'F'); + ('a' <= c && c <= 'f') || + ('A' <= c && c <= 'F'); } } \ No newline at end of file diff --git a/tests/Lua.Tests/StringTests.cs b/tests/Lua.Tests/StringTests.cs new file mode 100644 index 00000000..921205d0 --- /dev/null +++ b/tests/Lua.Tests/StringTests.cs @@ -0,0 +1,17 @@ +using Lua.CodeAnalysis.Syntax; +using Lua.CodeAnalysis.Syntax.Nodes; + +namespace Lua.Tests; + +public class StringTests +{ + [TestCase("\r")] + [TestCase("\n")] + [TestCase("\r\n")] + public async Task Test_ShortString_RealNewLine(string newLine) + { + var result = await LuaState.Create().DoStringAsync($"return \"\\{newLine}\""); + Assert.That(result, Has.Length.EqualTo(1)); + Assert.That(result[0], Is.EqualTo(new LuaValue("\n"))); + } +} \ No newline at end of file