Skip to content
Browse files

Pattern and TokenRegex support optional RegexOptions enum values.

  • Loading branch information...
1 parent 2d11375 commit c33c63bf870253be503f900482a6d3a65f50180a @plioi committed Dec 27, 2012
View
8 src/Parsley.Test/TokenKindTests.cs
@@ -1,4 +1,5 @@
using System;
+using System.Text.RegularExpressions;
using Should;
using Xunit;
@@ -8,12 +9,14 @@ public class TokenKindTests
{
private readonly TokenKind lower;
private readonly TokenKind upper;
+ private readonly TokenKind caseInsensitive;
private readonly Text abcDEF;
public TokenKindTests()
{
lower = new Pattern("Lowercase", @"[a-z]+");
upper = new Pattern("Uppercase", @"[A-Z]+");
+ caseInsensitive = new Pattern("Case Insensitive", @"[a-z]+", RegexOptions.IgnoreCase);
abcDEF = new Text("abcDEF");
}
@@ -36,20 +39,25 @@ public void ProducesTokenUponSuccessfulMatch()
upper.TryMatch(abcDEF.Advance(3), out token).ShouldBeTrue();
token.ShouldEqual(upper, "DEF", 1, 4);
+
+ caseInsensitive.TryMatch(abcDEF, out token).ShouldBeTrue();
+ token.ShouldEqual(caseInsensitive, "abcDEF", 1, 1);
}
[Fact]
public void HasDescriptiveName()
{
lower.Name.ShouldEqual("Lowercase");
upper.Name.ShouldEqual("Uppercase");
+ caseInsensitive.Name.ShouldEqual("Case Insensitive");
}
[Fact]
public void UsesDescriptiveNameForToString()
{
lower.ToString().ShouldEqual("Lowercase");
upper.ToString().ShouldEqual("Uppercase");
+ caseInsensitive.ToString().ShouldEqual("Case Insensitive");
}
[Fact]
View
17 src/Parsley.Test/TokenRegexTests.cs
@@ -1,4 +1,5 @@
-using Should;
+using System.Text.RegularExpressions;
+using Should;
using Xunit;
namespace Parsley
@@ -34,6 +35,20 @@ public void CanMatchMultilineAndCommentedRegexes()
}
[Fact]
+ public void SupportsOptionalRegexOptions()
+ {
+ var regex = new TokenRegex(@"[a-z]+", RegexOptions.IgnoreCase);
+
+ regex.Match("123aBc0", 0).Success.ShouldBeFalse();
+
+ regex.Match("123aBc0", 3).Success.ShouldBeTrue();
+ regex.Match("123aBc0", 3).Value.ShouldEqual("aBc");
+
+ regex.Match("123aBc0", 4).Success.ShouldBeTrue();
+ regex.Match("123aBc0", 4).Value.ShouldEqual("Bc");
+ }
+
+ [Fact]
public void HasStringRepresentation()
{
new TokenRegex(@"[a-zA-Z0-9]*").ToString().ShouldEqual(@"[a-zA-Z0-9]*");
View
10 src/Parsley/TokenKind.cs
@@ -1,5 +1,6 @@
using System;
using System.Linq;
+using System.Text.RegularExpressions;
namespace Parsley
{
@@ -53,10 +54,15 @@ public class Pattern : TokenKind
{
private readonly TokenRegex regex;
- public Pattern(string name, string pattern, bool skippable = false)
+ public Pattern(string name, string pattern, params RegexOptions[] regexOptions)
+ : this(name, pattern, false, regexOptions)
+ {
+ }
+
+ public Pattern(string name, string pattern, bool skippable, params RegexOptions[] regexOptions)
: base(name, skippable)
{
- regex = new TokenRegex(pattern);
+ regex = new TokenRegex(pattern, regexOptions);
}
protected override MatchResult Match(Text text)
View
9 src/Parsley/TokenRegex.cs
@@ -7,12 +7,17 @@ public class TokenRegex
private readonly string pattern;
private readonly Regex regex;
- public TokenRegex(string pattern)
+ public TokenRegex(string pattern, params RegexOptions[] regexOptions)
{
+ var options = RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace;
+
+ foreach (var additionalOption in regexOptions)
+ options |= additionalOption;
+
this.pattern = pattern;
regex = new Regex(@"\G(
" + pattern + @"
- )", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
+ )", options);
}
public MatchResult Match(string input, int index)

0 comments on commit c33c63b

Please sign in to comment.
Something went wrong with that request. Please try again.