获取例外,同时解析使用SPRACHE文件“分析失败:达到输入意外结束;预计=”

问题描述:

我想分析如下文件,获取例外,同时解析使用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栏);最近消耗:矿石 } }

任何线索都会有帮助。

+0

只是好奇,为什么“关闭”投票呢?这是一个编程问题,包含源代码,示例输入和错误消息......任何有待改进的建议? –

两个问题:首先,在您的示例中,值可以包含_.,因此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(); 
+0

谢谢,这解决了我的问题。 –