Skip to content

Commit 609f8f8

Browse files
author
Andrin Meier
committed
fix like pattern special char bugs
1 parent 6d75457 commit 609f8f8

File tree

10 files changed

+299
-94
lines changed

10 files changed

+299
-94
lines changed

Rubberduck.Parsing/Preprocessing/VBAExpressionEvaluator.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -491,9 +491,9 @@ private object VisitLike(VBAConditionalCompilationParser.CcExpressionContext con
491491
StringBuilder regexStr = new StringBuilder();
492492
foreach (var element in likePattern.likePatternElement())
493493
{
494-
if (element.NORMALCHAR() != null)
494+
if (element.likePatternChar() != null)
495495
{
496-
regexStr.Append(element.NORMALCHAR().GetText());
496+
regexStr.Append(element.likePatternChar().GetText());
497497
}
498498
else if (element.QUESTIONMARK() != null)
499499
{
@@ -510,8 +510,11 @@ private object VisitLike(VBAConditionalCompilationParser.CcExpressionContext con
510510
else
511511
{
512512
var charlist = element.likePatternCharlist().GetText();
513-
var cleaned = charlist.Replace("[!", "[^");
514-
regexStr.Append(cleaned);
513+
if (charlist.StartsWith("[!"))
514+
{
515+
charlist = "[^" + charlist.Substring(2);
516+
}
517+
regexStr.Append(charlist);
515518
}
516519
}
517520
// Full string match, e.g. "abcd" should NOT match "a.c"

Rubberduck.Parsing/Preprocessing/VBALike.g4

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ grammar VBALike;
33
compilationUnit : likePatternString EOF;
44

55
likePatternString : likePatternElement*;
6-
likePatternElement : NORMALCHAR | QUESTIONMARK | HASH | STAR | likePatternCharlist;
6+
likePatternElement : likePatternChar | QUESTIONMARK | HASH | STAR | likePatternCharlist;
7+
likePatternChar : ~(QUESTIONMARK | HASH | STAR | L_SQUARE_BRACKET);
78
likePatternCharlist : L_SQUARE_BRACKET EXCLAMATION? DASH? likePatternCharlistElement* DASH? R_SQUARE_BRACKET;
8-
likePatternCharlistElement : NORMALCHAR | likePatternCharlistRange;
9-
likePatternCharlistRange : NORMALCHAR DASH NORMALCHAR;
9+
likePatternCharlistElement : likePatternCharlistChar | likePatternCharlistRange;
10+
likePatternCharlistRange : likePatternCharlistChar DASH likePatternCharlistChar;
11+
likePatternCharlistChar : ~(DASH | R_SQUARE_BRACKET);
1012

1113
QUESTIONMARK : '?';
1214
HASH : '#';
@@ -15,4 +17,4 @@ L_SQUARE_BRACKET : '[';
1517
R_SQUARE_BRACKET : ']';
1618
DASH : '-';
1719
EXCLAMATION : '!';
18-
NORMALCHAR : ~[?#*[\]-!];
20+
ANYCHAR : .;

Rubberduck.Parsing/Preprocessing/VBALikeBaseListener.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ public virtual void EnterCompilationUnit([NotNull] VBALikeParser.CompilationUnit
5959
/// <param name="context">The parse tree.</param>
6060
public virtual void ExitCompilationUnit([NotNull] VBALikeParser.CompilationUnitContext context) { }
6161

62+
/// <summary>
63+
/// Enter a parse tree produced by <see cref="VBALikeParser.likePatternCharlistChar"/>.
64+
/// <para>The default implementation does nothing.</para>
65+
/// </summary>
66+
/// <param name="context">The parse tree.</param>
67+
public virtual void EnterLikePatternCharlistChar([NotNull] VBALikeParser.LikePatternCharlistCharContext context) { }
68+
/// <summary>
69+
/// Exit a parse tree produced by <see cref="VBALikeParser.likePatternCharlistChar"/>.
70+
/// <para>The default implementation does nothing.</para>
71+
/// </summary>
72+
/// <param name="context">The parse tree.</param>
73+
public virtual void ExitLikePatternCharlistChar([NotNull] VBALikeParser.LikePatternCharlistCharContext context) { }
74+
6275
/// <summary>
6376
/// Enter a parse tree produced by <see cref="VBALikeParser.likePatternCharlist"/>.
6477
/// <para>The default implementation does nothing.</para>
@@ -85,6 +98,19 @@ public virtual void EnterLikePatternElement([NotNull] VBALikeParser.LikePatternE
8598
/// <param name="context">The parse tree.</param>
8699
public virtual void ExitLikePatternElement([NotNull] VBALikeParser.LikePatternElementContext context) { }
87100

101+
/// <summary>
102+
/// Enter a parse tree produced by <see cref="VBALikeParser.likePatternChar"/>.
103+
/// <para>The default implementation does nothing.</para>
104+
/// </summary>
105+
/// <param name="context">The parse tree.</param>
106+
public virtual void EnterLikePatternChar([NotNull] VBALikeParser.LikePatternCharContext context) { }
107+
/// <summary>
108+
/// Exit a parse tree produced by <see cref="VBALikeParser.likePatternChar"/>.
109+
/// <para>The default implementation does nothing.</para>
110+
/// </summary>
111+
/// <param name="context">The parse tree.</param>
112+
public virtual void ExitLikePatternChar([NotNull] VBALikeParser.LikePatternCharContext context) { }
113+
88114
/// <summary>
89115
/// Enter a parse tree produced by <see cref="VBALikeParser.likePatternCharlistElement"/>.
90116
/// <para>The default implementation does nothing.</para>

Rubberduck.Parsing/Preprocessing/VBALikeBaseVisitor.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ public partial class VBALikeBaseVisitor<Result> : AbstractParseTreeVisitor<Resul
5454
/// <return>The visitor result.</return>
5555
public virtual Result VisitCompilationUnit([NotNull] VBALikeParser.CompilationUnitContext context) { return VisitChildren(context); }
5656

57+
/// <summary>
58+
/// Visit a parse tree produced by <see cref="VBALikeParser.likePatternCharlistChar"/>.
59+
/// <para>
60+
/// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
61+
/// on <paramref name="context"/>.
62+
/// </para>
63+
/// </summary>
64+
/// <param name="context">The parse tree.</param>
65+
/// <return>The visitor result.</return>
66+
public virtual Result VisitLikePatternCharlistChar([NotNull] VBALikeParser.LikePatternCharlistCharContext context) { return VisitChildren(context); }
67+
5768
/// <summary>
5869
/// Visit a parse tree produced by <see cref="VBALikeParser.likePatternCharlist"/>.
5970
/// <para>
@@ -76,6 +87,17 @@ public partial class VBALikeBaseVisitor<Result> : AbstractParseTreeVisitor<Resul
7687
/// <return>The visitor result.</return>
7788
public virtual Result VisitLikePatternElement([NotNull] VBALikeParser.LikePatternElementContext context) { return VisitChildren(context); }
7889

90+
/// <summary>
91+
/// Visit a parse tree produced by <see cref="VBALikeParser.likePatternChar"/>.
92+
/// <para>
93+
/// The default implementation returns the result of calling <see cref="AbstractParseTreeVisitor{Result}.VisitChildren(IRuleNode)"/>
94+
/// on <paramref name="context"/>.
95+
/// </para>
96+
/// </summary>
97+
/// <param name="context">The parse tree.</param>
98+
/// <return>The visitor result.</return>
99+
public virtual Result VisitLikePatternChar([NotNull] VBALikeParser.LikePatternCharContext context) { return VisitChildren(context); }
100+
79101
/// <summary>
80102
/// Visit a parse tree produced by <see cref="VBALikeParser.likePatternCharlistElement"/>.
81103
/// <para>

Rubberduck.Parsing/Preprocessing/VBALikeLexer.cs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace Rubberduck.Parsing.Like {
2828
public partial class VBALikeLexer : Lexer {
2929
public const int
3030
QUESTIONMARK=1, HASH=2, STAR=3, L_SQUARE_BRACKET=4, R_SQUARE_BRACKET=5,
31-
DASH=6, EXCLAMATION=7, NORMALCHAR=8;
31+
DASH=6, EXCLAMATION=7, ANYCHAR=8;
3232
public static string[] modeNames = {
3333
"DEFAULT_MODE"
3434
};
@@ -39,7 +39,7 @@ public const int
3939
};
4040
public static readonly string[] ruleNames = {
4141
"QUESTIONMARK", "HASH", "STAR", "L_SQUARE_BRACKET", "R_SQUARE_BRACKET",
42-
"DASH", "EXCLAMATION", "NORMALCHAR"
42+
"DASH", "EXCLAMATION", "ANYCHAR"
4343
};
4444

4545

@@ -64,16 +64,15 @@ public VBALikeLexer(ICharStream input)
6464
"\x2\x4\x3\t\x3\x4\x4\t\x4\x4\x5\t\x5\x4\x6\t\x6\x4\a\t\a\x4\b\t\b\x4\t"+
6565
"\t\t\x3\x2\x3\x2\x3\x3\x3\x3\x3\x4\x3\x4\x3\x5\x3\x5\x3\x6\x3\x6\x3\a"+
6666
"\x3\a\x3\b\x3\b\x3\t\x3\t\x2\x2\x2\n\x3\x2\x3\x5\x2\x4\a\x2\x5\t\x2\x6"+
67-
"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\x2\x2\x2\x13\x14\a\x41\x2\x2\x14"+
72+
"\x4\x3\x2\x2\x2\x15\x16\a%\x2\x2\x16\x6\x3\x2\x2\x2\x17\x18\a,\x2\x2\x18"+
73+
"\b\x3\x2\x2\x2\x19\x1A\a]\x2\x2\x1A\n\x3\x2\x2\x2\x1B\x1C\a_\x2\x2\x1C"+
74+
"\f\x3\x2\x2\x2\x1D\x1E\a/\x2\x2\x1E\xE\x3\x2\x2\x2\x1F \a#\x2\x2 \x10"+
75+
"\x3\x2\x2\x2!\"\v\x2\x2\x2\"\x12\x3\x2\x2\x2\x3\x2\x2";
7776
public static readonly ATN _ATN =
7877
new ATNDeserializer().Deserialize(_serializedATN.ToCharArray());
7978
}

Rubberduck.Parsing/Preprocessing/VBALikeListener.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ public interface IVBALikeListener : IParseTreeListener {
5151
/// <param name="context">The parse tree.</param>
5252
void ExitCompilationUnit([NotNull] VBALikeParser.CompilationUnitContext context);
5353

54+
/// <summary>
55+
/// Enter a parse tree produced by <see cref="VBALikeParser.likePatternCharlistChar"/>.
56+
/// </summary>
57+
/// <param name="context">The parse tree.</param>
58+
void EnterLikePatternCharlistChar([NotNull] VBALikeParser.LikePatternCharlistCharContext context);
59+
/// <summary>
60+
/// Exit a parse tree produced by <see cref="VBALikeParser.likePatternCharlistChar"/>.
61+
/// </summary>
62+
/// <param name="context">The parse tree.</param>
63+
void ExitLikePatternCharlistChar([NotNull] VBALikeParser.LikePatternCharlistCharContext context);
64+
5465
/// <summary>
5566
/// Enter a parse tree produced by <see cref="VBALikeParser.likePatternCharlist"/>.
5667
/// </summary>
@@ -73,6 +84,17 @@ public interface IVBALikeListener : IParseTreeListener {
7384
/// <param name="context">The parse tree.</param>
7485
void ExitLikePatternElement([NotNull] VBALikeParser.LikePatternElementContext context);
7586

87+
/// <summary>
88+
/// Enter a parse tree produced by <see cref="VBALikeParser.likePatternChar"/>.
89+
/// </summary>
90+
/// <param name="context">The parse tree.</param>
91+
void EnterLikePatternChar([NotNull] VBALikeParser.LikePatternCharContext context);
92+
/// <summary>
93+
/// Exit a parse tree produced by <see cref="VBALikeParser.likePatternChar"/>.
94+
/// </summary>
95+
/// <param name="context">The parse tree.</param>
96+
void ExitLikePatternChar([NotNull] VBALikeParser.LikePatternCharContext context);
97+
7698
/// <summary>
7799
/// Enter a parse tree produced by <see cref="VBALikeParser.likePatternCharlistElement"/>.
78100
/// </summary>

0 commit comments

Comments
 (0)