Giter Club home page Giter Club logo

Comments (10)

AndrewSav avatar AndrewSav commented on June 25, 2024 1

Perhaps something along these lines:

public static TokenListParser<TKind, T> OneOf<TKind, T>(bool backtrack = false, params TokenListParser<TKind, T>[] parsers)
{
    if (parsers == null) throw new ArgumentNullException(nameof(parsers));

    if (parsers.Length == 0)
    {
        return i => TokenListParserResult.Empty<TKind, T>(TokenList<TKind>.Empty);
    }

    TokenListParser<TKind, T> c = parsers[0];
    for (int i = 1; i < parsers.Length; i++)
    {
        if (backtrack)
        {
            c = c.Try();
        }
        c = c.Or(parsers[i]);
    }

    return c;
}

public static TextParser<T> OneOf<T>(bool backtrack = false, params TextParser<T>[] parsers)
{
    if (parsers == null) throw new ArgumentNullException(nameof(parsers));

    if (parsers.Length == 0)
    {
        return i => Result.Empty<T>(TextSpan.None);
    }

    TextParser<T> c = parsers[0];
    for (int i = 1; i < parsers.Length; i++)
    {
        if (backtrack)
        {
            c = c.Try();
        }
        c = c.Or(parsers[i]);
    }

    return c;
}

from superpower.

nblumhardt avatar nblumhardt commented on June 25, 2024 1

I think this is an interesting feature but it's mostly sugar on top of the current API's capabilities, and makes it tempting to apply a blanket 'backtrack' when doing it surgically would provide better error reporting.

from superpower.

AndrewSav avatar AndrewSav commented on June 25, 2024 1

@nblumhardt Backtrack has never been exposed as public API before, so what you are saying is fair. Handling it with Try outside of OneOf also makes sense.

from superpower.

nblumhardt avatar nblumhardt commented on June 25, 2024

Apologies for the delayed response, I'll take a look 👍

from superpower.

AndrewSav avatar AndrewSav commented on June 25, 2024

@nblumhardt this is disappointing, especially because in the absence of examples it's difficult to understand what you mean.

from superpower.

nblumhardt avatar nblumhardt commented on June 25, 2024

Hi @AndrewSav - sorry about the rushed reply and close; had a few minutes to try getting the build working and issue tracker cleaned up.

Taking a look again, I think it's only the backtrack flag I'm not sold on. Backtracking can reduce the quality of error reporting if used when not necessary.

I think, though, OneOf without the flag would still be quite ergonomic:

var p = Parse.OneOf(
  First,
  Second.Try(),
  Third);

What do you think?

from superpower.

nblumhardt avatar nblumhardt commented on June 25, 2024

Great 👍 - If I get a chance I'll give it a shot; marking as up-for-grabs in case anyone else can take a look.

from superpower.

chrisspre avatar chrisspre commented on June 25, 2024

Will the OneOf combinator still be useful/applicable if the individual parsers are of different subtypes?

interface IStuff {}
class RedStuff : IStuff {}
class GreenStuff : IStuff {}
class BlueStuff: IStuff {}

TokenListParser<TKind, RedStuff> First;
TokenListParser<TKind, GreenStuff> Second;
TokenListParser<TKind, BlueStuff> Third;

var p = Parse.OneOf(
  First,
  Second.Try(),
  Third);

Can (or should) Parse.OneOf take care of the cast and therefore return a TokenListParser<TKind, IStuff >?
Or will it be necessary to cast each one individually (which would make the call a bit more verbose).

from superpower.

nblumhardt avatar nblumhardt commented on June 25, 2024

Hi @chrisspre - as it's currently envisaged, no - OneOf won't accept different parser types in its params array of alternatives. It might be possible to create various overloads: OneOf<T,U>(), OneOf<T,U,V>(), etc. to get a little closer to what you're suggesting, but there would be some fairly severe ergonomic quirks.

var p = Parse.OneOf(
  First.Cast<IStuff>(),
  Second.Cast<IStuff>().Try(),
  Third.Cast<IStuff>());

seems like the best option, as far as I can tell. HTH!

from superpower.

nblumhardt avatar nblumhardt commented on June 25, 2024

This feature was added in #123.

from superpower.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.