Skip to content

Commit 987d0fc

Browse files
committed
Fix escaping behavior in DescendGroup and throw some tests at it
1 parent e1baa1e commit 987d0fc

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

Rubberduck.RegexAssistant/Rubberduck.RegexAssistant.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@
9797
</None>
9898
<None Include="packages.config" />
9999
</ItemGroup>
100+
<ItemGroup>
101+
<ProjectReference Include="..\Rubberduck.VBEEditor\Rubberduck.VBEditor.csproj">
102+
<Project>{8CE35EB3-8852-4BA1-84DD-DF3F5D2967B0}</Project>
103+
<Name>Rubberduck.VBEditor</Name>
104+
</ProjectReference>
105+
</ItemGroup>
100106
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
101107
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
102108
Other similar extension points exist, see Microsoft.Common.targets.

Rubberduck.RegexAssistant/VBRegexParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private static string DescendGroup(string specifier)
128128
return openingCount == 0 ? specifier.Substring(0, length + 1) : "";
129129
}
130130
}
131-
if (digit == '\\')
131+
if (digit == '\\' || escapeToggle)
132132
{
133133
escapeToggle = !escapeToggle;
134134
}

RubberduckTests/RegexAssistant/RegularExpressionTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,5 +286,58 @@ public void UnbalancedGroupCorrectlyMarksTrailingParenAsError()
286286
Assert.AreEqual(expected[i], subexpressions[i]);
287287
}
288288
}
289+
290+
[Category("RegexAssistant")]
291+
[Test]
292+
public void EscapedParensGroupIsParsedProperly()
293+
{
294+
var expected = new List<IRegularExpression>()
295+
{
296+
new SingleAtomExpression(new Literal("\\(", Quantifier.None)),
297+
new SingleAtomExpression(new Group(new ConcatenatedExpression(new IRegularExpression[]{
298+
new SingleAtomExpression(new Literal("\\(", Quantifier.None)),
299+
new SingleAtomExpression(new Literal("\\)", Quantifier.None)),
300+
}.ToList()),"(\\(\\))", Quantifier.None)),
301+
new SingleAtomExpression(new Literal("\\)", Quantifier.None)),
302+
};
303+
var expression = VBRegexParser.Parse(@"\((\(\))\)");
304+
Assert.IsInstanceOf(typeof(ConcatenatedExpression), expression);
305+
var subexpressions = (expression as ConcatenatedExpression).Subexpressions;
306+
Assert.AreEqual(expected.Count, subexpressions.Count);
307+
for (var i = 0; i < expected.Count; i++)
308+
{
309+
Assert.AreEqual(expected[i], subexpressions[i]);
310+
}
311+
}
312+
313+
[Category("RegexAssistant")]
314+
[Test]
315+
public void IncorrectEscapeSequenceIsGroupSubExpression()
316+
{
317+
var expected = new List<IRegularExpression>
318+
{
319+
new ErrorExpression("\\"),
320+
new SingleAtomExpression(new Literal("m", Quantifier.None))
321+
};
322+
var expression = VBRegexParser.Parse("(\\m)");
323+
Assert.IsInstanceOf(typeof(SingleAtomExpression), expression);
324+
var subexpressions = (((expression as SingleAtomExpression).Atom as Group)?.Subexpression as ConcatenatedExpression)?.Subexpressions ?? new List<IRegularExpression>();
325+
Assert.AreEqual(expected.Count, subexpressions.Count);
326+
for (var i = 0; i < expected.Count; i++)
327+
{
328+
Assert.AreEqual(expected[i], subexpressions[i]);
329+
}
330+
}
331+
332+
[Category("RegexAssistant")]
333+
[Test]
334+
public void CorrectEscapeSequenceIsGroupSubExpression()
335+
{
336+
var expression = VBRegexParser.Parse(@"(\\)");
337+
Assert.IsInstanceOf(typeof(SingleAtomExpression), expression);
338+
var inner = (((expression as SingleAtomExpression).Atom as Group)?.Subexpression as SingleAtomExpression);
339+
Assert.IsNotNull(inner);
340+
Assert.AreEqual(new SingleAtomExpression(new Literal("\\\\", Quantifier.None)), inner);
341+
}
289342
}
290343
}

0 commit comments

Comments
 (0)