sprache - 使用 Sprache 解析文件时出现异常 "Parsing failure: Unexpected end of input reached; expected ="

标签 sprache

我想解析下面的文件,

first=The_First_Step
{
    {
        value=First.Value,
    }
}

second=The_Second_Step
{
    {
        another = Second_Value,
        more =  Yet.More,
    }
}

我将语法写为,

public static NGSection ParseSections(string ngServerConfig)
{
    return Sections.End().Parse(ngServerConfig);
}

internal static Parser<string> ValueText = Parse.LetterOrDigit.AtLeastOnce().Text().Token();

internal static Parser<string> Identifier = Parse.AnyChar.AtLeastOnce().Text().Token();

internal static Parser<Config> Config =
      from id in Identifier
      from equal in Parse.Char('=').Token()
      from value in ValueText
      from comma in Parse.Char(',').Token()
      select new Config(id, value);

internal static Parser<Section> Section =
      from id in Identifier
      from equal in Parse.Char('=').Token()
      from title in ValueText
      from lbracket in Parse.Char('{').Token()
      from inbracket in Parse.Char('{').Token()
      from configs in Config.AtLeastOnce()
      from outbracket in Parse.Char('}').Token()
      from rbracket in Parse.Char('}').Token()
      select new Section(id, title, configs);

internal static Parser<NGSection> Sections =
     from sections in Section.AtLeastOnce()
     select new NGSection(sections);

我遇到异常

解析失败:意外到达输入结尾;预期=(第13行,第2列);最近消耗:矿石 } }

任何线索都会有帮助。

最佳答案

两个问题:首先,在您的示例中,值可以包含 _.,因此 LetterOrDigit 不会覆盖它。应该是:

internal static Parser<string> ValueText =
  Parse.LetterOrDigit.Or(Parse.Chars('_', '.')).AtLeastOnce().Text().Token();

接下来,Identifier解析器的AnyChar太贪婪了;您需要排除 =,否则它将被视为标识符的一部分:

internal static Parser<string> Identifier =
  Parse.CharExcept('=').AtLeastOnce().Text().Token();

关于sprache - 使用 Sprache 解析文件时出现异常 "Parsing failure: Unexpected end of input reached; expected =",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45913463/

相关文章:

c# - 使用 Sprache 解析文本时,我可以确定原始字符串中的当前索引吗?

c# - Sprache 中的文本查询解析

c# - Sprache:语法中的左递归

c# - 使用 Sprache 从标识符解析枚举?

parsing - 如何使用解析器组合器处理 'line-continuation'

c# - Sprache 中的递归表达式解析

c# - 如何使用 Sprache 解析可以以任何顺序出现的行?

c# - 如何改进缺少右大括号的 Sprache 解析器错误消息?