如何在Sprache解析器中使用Token()方法

问题描述 投票:0回答:1

我正在使用'Token()'方法丢弃前导和尾随空格,但它不会,此测试失败,消息Expected string to be "token", but it has unexpected whitespace at the end.

我试图在方法Token()之前调用方法Text(),但它也没有帮助。 Parse.AnyChar.Many().Token().Text()

如何以正确的方式使用方法Token()

[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
}
sprache
1个回答
1
投票

Parse.AnyChar修饰符发挥作用之前,Token消耗了尾随空格。

要修复解析器,请排除这样的空格:

[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
}
© www.soinside.com 2019 - 2024. All rights reserved.