Skip to content
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

Add support for left bracket escaping in the regex. #160

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/RunTests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.101'
dotnet-version: '6.0.425'
- name: Test
run: |
cd Sources
Expand Down
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 (ch == ']') return ch;
if (escaped == null) throw new FormatException($"invalid escape \\{ch} in grouping (position {this.index - 2})");
return escaped.Value;
Expand Down
18 changes: 18 additions & 0 deletions Sources/SynKit/Tests/Lexer.Tests/AdvancedLexerGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public enum TokenType
[End] End,

[Regex("[\\-]")] MinusInRegexGroup,
[Regex("[\\[]")] LeftBracketInRegexGroup,
[Regex("[\\]]")] RightBracketInRegexGroup,
}

[Lexer(typeof(TokenType))]
Expand All @@ -32,4 +34,20 @@ public void AdvancedRulesSequence()
Assert.Equal(Token("-", TokenType.MinusInRegexGroup, Range((0, 0), 1)), lexer.Next());
Assert.Equal(Token(string.Empty, TokenType.End, Range((0, 1), 0)), lexer.Next());
}

[Fact]
public void LeftBracketInRegex()
{
var lexer = new AdvancedLexer("[");
Assert.Equal(Token("[", TokenType.LeftBracketInRegexGroup, Range((0, 0), 1)), lexer.Next());
Assert.Equal(Token(string.Empty, TokenType.End, Range((0, 1), 0)), lexer.Next());
}

[Fact]
public void RightBracketInRegex()
{
var lexer = new AdvancedLexer("]");
Assert.Equal(Token("]", TokenType.RightBracketInRegexGroup, Range((0, 0), 1)), lexer.Next());
Assert.Equal(Token(string.Empty, TokenType.End, Range((0, 1), 0)), lexer.Next());
}
}
Loading