2

I want to parse below file,

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

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

I have written grammar as,

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);

I am getting exception as

Parsing failure: Unexpected end of input reached; expected = (Line 13, Column 2); recently consumed: ore } }

Any clue will be helpful.

Rahul Techie
  • 363
  • 2
  • 8
  • Just curious, why the "close" vote on this? It's a programming question, with source code, example input, and error message... Any suggestion for improvement? – Nicholas Blumhardt Aug 28 '17 at 23:22

1 Answers1

2

Two problems: first, values can contain _ or . in your example, so LetterOrDigit won't cover it. Should be:

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

Next, the Identifier parser's AnyChar is too greedy; you need to exclude the =, or it will be considered part of the identifier:

internal static Parser<string> Identifier =
  Parse.CharExcept('=').AtLeastOnce().Text().Token();
Nicholas Blumhardt
  • 30,271
  • 4
  • 90
  • 101