Skip to content

Commit

Permalink
Allow escape ] inside [] construct
Browse files Browse the repository at this point in the history
Fixes #145
  • Loading branch information
kant2002 committed Apr 8, 2024
1 parent 4138189 commit 0d6121b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions Sources/SynKit/Libraries/Lexer.Generator/RegExParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ private char ParseLiteralRangeAtom()
var ch = this.Consume();
var escaped = Escape(ch);
if (ch == '-') return ch;
if (ch == ']') return ch;
if (escaped == null) throw new FormatException($"invalid escape \\{ch} in grouping (position {this.index - 2})");
return escaped.Value;
}
Expand Down
29 changes: 29 additions & 0 deletions Sources/SynKit/Tests/Lexer.Tests/RegexesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,30 @@ internal partial class WhitespaceLexer
{
}

public enum Issue140Token
{
[End] End,
[Error] Error,
[Regex(@"'((\\[^\n\r])|[^\r\n\\'])*'")] Whitespace,
}

[Lexer(typeof(Issue140Token))]
internal partial class Issue140Lexer
{
}

public enum EscapingInsideSingleLiteralRangeToken
{
[End] End,
[Error] Error,
[Regex(@"[\]]")] Whitespace,
}

[Lexer(typeof(EscapingInsideSingleLiteralRangeToken))]
internal partial class Issue145Lexer
{
}

[Theory]

[InlineData("foo", typeof(Identifier), true, typeof(IdentifierLexer))]
Expand Down Expand Up @@ -200,6 +224,11 @@ internal partial class WhitespaceLexer
[InlineData("\r", typeof(Whitespace), true, typeof(WhitespaceLexer))]
[InlineData("\n", typeof(Whitespace), true, typeof(WhitespaceLexer))]
[InlineData("a", typeof(Whitespace), false, typeof(WhitespaceLexer))]

[InlineData(@"'hello'", typeof(Issue140Token), true, typeof(Issue140Lexer))]
[InlineData(@"'hello \' bye'", typeof(Issue140Token), true, typeof(Issue140Lexer))]

[InlineData(@"]", typeof(EscapingInsideSingleLiteralRangeToken), true, typeof(Issue145Lexer))]
public void SingleTokenAcceptance(string input, Type enumType, bool shouldAccept, Type lexerType)
{
dynamic lexer = Activator.CreateInstance(lexerType, input)!;
Expand Down

0 comments on commit 0d6121b

Please sign in to comment.