benjamin-hodgson / Pidgin

A lightweight and fast parsing library for C#.
https://www.benjamin.pizza/Pidgin/
MIT License
914 stars 70 forks source link

[Question] Parse until exact string #176

Closed Platapoop closed 2 months ago

Platapoop commented 2 months ago

Hello,

I have an issue where I want to parse until i see a =>. For example,

parser.ParseOrThrow("abc=>def") returns "abc".

However, I also want to do this parser.ParseOrThrow("a=b=c=>def") returns "a=b=c". But things that I write always seems to fail because once it sees an =, it then expects a >

Here's a very basic example code of one of the things that I've tried.

        var splitParser = Map((id, targets) => (string.Concat(id), targets),
            Any.Until(String("=>")),
            Any.ManyString());
            string ids;
            string targets;

        // fails saying Parse error. unexpected b, expected =>
        (ids, targets) = splitParser.ParseOrThrow("a=b=c=>def");

Thanks!

Platapoop commented 2 months ago

I think I somehow never thought about trying Any.Until(Try(String("=>"))) until right after I posted this issue. Anyways, it looks like that solved my problem.