Permalink
Browse files

Add TokenKind subclass, Empty, to represent tokens that do not match …

…literals from the input.
  • Loading branch information...
1 parent ebc5f87 commit 23908a1a781d845fe73e4963a71778cd2f743b9e @plioi committed Mar 31, 2012
Showing with 26 additions and 1 deletion.
  1. +14 −0 src/Parsley.Test/TokenKindTests.cs
  2. +12 −1 src/Parsley/TokenKind.cs
@@ -111,5 +111,19 @@ public void ProvidesConvenienceSubclassForDefiningOperators()
doubleStar.TryMatch(new Text("**"), out token).ShouldBeTrue();
token.ShouldBe(doubleStar, "**", 1, 1);
}
+
+ [Fact]
+ public void ProvidesConvenienceSubclassForTokensThatDoNotMatchLiteralsFromTheInput()
+ {
+ Token token;
+
+ TokenKind.EndOfInput.ShouldBeType<Empty>();
+
+ TokenKind.EndOfInput.Name.ShouldEqual("end of input");
+ TokenKind.EndOfInput.Skippable.ShouldBeFalse();
+
+ TokenKind.EndOfInput.TryMatch(new Text(""), out token).ShouldBeFalse();
+ TokenKind.EndOfInput.TryMatch(new Text("foo"), out token).ShouldBeFalse();
+ }
}
}
@@ -5,7 +5,7 @@ namespace Parsley
{
public abstract class TokenKind
{
- public static readonly TokenKind EndOfInput = new Pattern("end of input", @"$");
+ public static readonly TokenKind EndOfInput = new Empty("end of input");
public static readonly TokenKind Unknown = new Pattern("Unknown", @".+");
private readonly string name;
@@ -95,4 +95,15 @@ protected override MatchResult Match(Text text)
return MatchResult.Fail();
}
}
+
+ public class Empty : TokenKind
+ {
+ public Empty(string name)
+ : base(name) { }
+
+ protected override MatchResult Match(Text text)
+ {
+ return MatchResult.Fail();
+ }
+ }
}

0 comments on commit 23908a1

Please sign in to comment.