Skip to content

Commit

Permalink
Support case sensitivity with CharRangeTerminal
Browse files Browse the repository at this point in the history
For #49
  • Loading branch information
cwensley committed May 23, 2021
1 parent 5dc4557 commit 63f8918
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
48 changes: 48 additions & 0 deletions Eto.Parse.Tests/Parsers/CharRangeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Eto.Parse.Parsers;
using NUnit.Framework;

namespace Eto.Parse.Tests.Parsers
{
[TestFixture]
public class CharRangeTests
{
[TestCase(true, false)]
[TestCase(false, false)]
[TestCase(null, false)]
[TestCase(true, true)]
[TestCase(false, true)]
public void CaseSensitiveShouldBeCorrect(bool? caseSensitive, bool inherit)
{
var parser = new CharRangeTerminal();
parser.Start = 'a';
parser.End = 'f';
var grammar = new Grammar { Inner = parser };

if (inherit)
grammar.CaseSensitive = caseSensitive.Value;
else
parser.CaseSensitive = caseSensitive;

Assert.IsTrue(grammar.Match("a").Success, "1.1");
Assert.IsTrue(grammar.Match("b").Success, "1.2");
Assert.IsTrue(grammar.Match("f").Success, "1.3");

if (caseSensitive != false)
{
Assert.IsFalse(grammar.Match("A").Success, "2.1");
Assert.IsFalse(grammar.Match("B").Success, "2.2");
Assert.IsFalse(grammar.Match("F").Success, "2.3");
}
else
{
Assert.IsTrue(grammar.Match("A").Success, "3.1");
Assert.IsTrue(grammar.Match("B").Success, "3.2");
Assert.IsTrue(grammar.Match("F").Success, "3.3");
}

// out of range
Assert.IsFalse(grammar.Match("g").Success, "4.1");
Assert.IsFalse(grammar.Match("G").Success, "4.2");
}
}
}
4 changes: 2 additions & 2 deletions Eto.Parse.Tests/Parsers/CharSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public void CaseSensitiveShouldBeCorrect(bool? caseSensitive, bool inherit)
{
Assert.IsFalse(grammar.Match("A").Success, "2.1");
Assert.IsFalse(grammar.Match("B").Success, "2.2");
Assert.IsFalse(grammar.Match("C").Success, "2.3");
Assert.IsFalse(grammar.Match("F").Success, "2.3");
}
else
{
Assert.IsTrue(grammar.Match("A").Success, "3.1");
Assert.IsTrue(grammar.Match("B").Success, "3.2");
Assert.IsTrue(grammar.Match("C").Success, "3.3");
Assert.IsTrue(grammar.Match("F").Success, "3.3");
}

// out of range
Expand Down
16 changes: 15 additions & 1 deletion Eto.Parse/Parsers/CharRangeTerminal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class CharRangeTerminal : CharTerminal
public char Start { get; set; }

public char End { get; set; }

protected CharRangeTerminal(CharRangeTerminal other, ParserCloneArgs args)
: base(other, args)
{
Expand All @@ -28,6 +28,9 @@ public CharRangeTerminal(char start, char end)

protected override bool Test(char ch)
{
if (!TestCaseSensitive)
ch = char.ToLowerInvariant(ch);

return ch >= Start && ch <= End;
}

Expand All @@ -40,5 +43,16 @@ public override Parser Clone(ParserCloneArgs args)
{
return new CharRangeTerminal(this, args);
}

protected override void InnerInitialize(ParserInitializeArgs args)
{
base.InnerInitialize(args);
if (!TestCaseSensitive)
{
Start = char.ToLowerInvariant(Start);
End = char.ToLowerInvariant(End);
}
}

}
}

0 comments on commit 63f8918

Please sign in to comment.