简体   繁体   中英

How do I use Token() method in Sprache parser

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
}

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM