0

I'm using 'Token()' method to discard leading and trailing whitespaces but it won't, this test fails with message Expected string to be "token", but it has unexpected whitespace at the end.

I tried to call method Token() before method Text() but it won't help too. Parse.AnyChar.Many().Token().Text()

How do I use method Token() in a right way?

[Test]
public void Test()
{
  Parser<string> parser = Parse.AnyChar.Many().Text().Token();
  var actual = parser.Parse(" token ");

  actual.Should().Be("token"); // without leading and trailing whitespaces
}
Ed Pavlov
  • 2,353
  • 2
  • 19
  • 25

1 Answers1

2

Parse.AnyChar consumes the trailing whitespace before the Token modifier comes into play.

To fix the parser, exclude the whitespace like this:

[Test]
public void Test()
{
    var parser = Parse.AnyChar.Except(Parse.WhiteSpace).Many().Text().Token();
    var actual = parser.Parse(" token ");

    actual.Should().Be("token"); // without leading and trailing whitespaces
}
yallie
  • 2,200
  • 1
  • 28
  • 26