Search code examples
c#parsingsprache

sprache parsing of text using a delimiter


I am trying to use sprache to parse the string accept-version:V1.2

so that I extract there 2 strings out of it: accept-version and V1.2

But I get a parsing failure error:

Parsing failure: Unexpected end of input reached; expected : (Line 1, Column 20); recently consumed: rsion:V1.2

How do I parse the given string? Sample code from my attempt is:

void Main()
{
    string message = "accept-version:V1.2";
    StompFrame_Grammar.Parse_StompFrame(message).Dump();
}

public class StompFrame_Grammar
{
    public static Parser<char> Colon = Parse.Char(':').Token();

    public static Parser<string> Text =
        (from content in Parse.CharExcept('"').Many().Text()
         select content).Token();

    public static Parser<Foo> StompFrame_Headers_Parser =
            from headerStr in Text
            from colon in Colon
            from headerVal in Text

            select new Foo(headerStr, headerVal);

    public static Foo Parse_StompFrame(string frameContents)
    {
        return StompFrame_Headers_Parser.End().Parse(frameContents);
    }
}

public struct Foo
{
    string header;
    string value;

    public Foo(string hdr, string val)
    {
        header = hdr;
        value = val;
    }
}


Solution

  • In your code

    public static Parser<string> Text =
        (from content in Parse.CharExcept('"').Many().Text()
         select content).Token();
    

    Why do you pass a double quote to CharExcept? You want to use colon as a separator, so use a colon instead of double quote.

    public static Parser<string> Text =
        (from content in Parse.CharExcept(':').Many().Text()
         select content).Token();