How do I use Token() method in Sprache parser

294 Views Asked by At

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
}
1

There are 1 best solutions below

0
yallie On

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
}