首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Sprache解析文件时出现异常“解析失败:到达输入的意外结尾;预期=”

使用Sprache解析文件时出现异常“解析失败:到达输入的意外结尾;预期=”
EN

Stack Overflow用户
提问于 2017-08-28 15:06:05
回答 1查看 630关注 0票数 1

我想解析下面的文件,

代码语言:javascript
复制
first=The_First_Step
{
    {
        value=First.Value,
    }
}

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

我把语法写成,

代码语言:javascript
复制
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列);最近使用: ore }}

任何线索都会有帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-29 07:31:01

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

代码语言:javascript
复制
internal static Parser<string> ValueText =
  Parse.LetterOrDigit.Or(Parse.Chars('_', '.')).AtLeastOnce().Text().Token();

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

代码语言:javascript
复制
internal static Parser<string> Identifier =
  Parse.CharExcept('=').AtLeastOnce().Text().Token();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45913463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档