Giter Club home page Giter Club logo

Comments (7)

nblumhardt avatar nblumhardt commented on July 24, 2024

Hi! I think there are two possible approaches. I would tend towards enforcing this by post-processing the resulting AST, but it also should be possible to use something like:

public static readonly TokenListParser<Token, Expression> Conditional =
    Parse.Chain(Boolean, Operand, MakeBinary)
        .Where(e => e is BinaryExpression);

Alternatively, you could modify Chain() to create your own combinator ChainAtLeastOnce().

Do any of the above help? Cheers!

from superpower.

RPiAwesomeness avatar RPiAwesomeness commented on July 24, 2024

@nblumhardt Sorry about the delay in getting back to you, I did try to do something similar to both of those, but I was hoping there would be a way that in either of those methods I would be able to have access to the character/line number for the error message. The latter method does throw an ParseException automatically, but the message isn't helpful, and I haven't figured out how to get access to the positions within the different parts during post-processing.

Is it possible to do either of those? Preferably I would have it generate the error message, so it can happen earlier on during the parsing, but it would also be useful to know how to access the line/char/abs position after the parser runs.

If I were to modify Chain(), how would you suggest I go about that? I'm still fairly new to working with C# and libraries.

from superpower.

nblumhardt avatar nblumhardt commented on July 24, 2024

Hi @RPiAwesomeness - some experimentation might be needed, but I think it should be enough to add a line at: https://github.com/datalust/superpower/blob/dev/src/Superpower/Parse.cs#L135

if (!operatorResult.HasValue) return operatorResult;

This would be before the while loop begins, and would have the effect of failing the parse if the first attempt to parse an operator fails.

HTH!

from superpower.

RPiAwesomeness avatar RPiAwesomeness commented on July 24, 2024

So I could just edit that file in my local copy of Superpower? How would that work with future releases?

If I were to add a combinator like you mentioned (ex. ChainAtLeastOnce) how best should I go about submitting the pull request/handling testing/and all that?

from superpower.

nblumhardt avatar nblumhardt commented on July 24, 2024

Hi! You'd just add ChainAtLeastOnce() to a new static class in your own project. As long as future Superpower versions don't break the signatures of underlying parser/result types, this would just keep working.

I don't think we'd accept this via a PR currently, as although it's the right thing to do in your scenario, it's probably not general enough to warrant adding to the core library right now (there's a huge space of possible parsing tactics, so it'd be easy to bloat Superpower out unintentionally).

HTH, and let me know if you need a hand getting ChainAtLeastOnce() up and running! 👍

from superpower.

RPiAwesomeness avatar RPiAwesomeness commented on July 24, 2024

I finally have had time to get back around to working on this again and in trying what you suggested I ran into two issues when I attempted to build it:

1>Parser\Mixins.cs(60,54,60,68): error CS0029: Cannot implicitly convert type 'Superpower.Model.TokenListParserResult<TKind, TOperator>' to 'Superpower.Model.TokenListParserResult<TKind, T>'
1>Parser\Mixins.cs(60,54,60,68): error CS1662: Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type
1>Parser\Mixins.cs(62,66,62,75): error CS1061: 'TokenListParserResult<TKind, TOperator>' does not contain a definition for 'IsPartial' and no accessible extension method 'IsPartial' accepting a first argument of type 'TokenListParserResult<TKind, TOperator>' could be found (are you missing a using directive or an assembly reference?)

I wasn't sure exactly what you meant, so I tried copying the Chain() function code directly and adding the line as you suggested. I assume there is a better way to approach this, but that made the most sense to me as the first thing to try.

The first error makes sense to me, I just need to be able to return the correct type when I return early and the result from @operator() doesn't match the second type.

As for the second error, is this something that I can resolve by adding a reference to a certain part of Superpower? According to Visual Studio it's an internal method that I don't have access to in my code.

from superpower.

nblumhardt avatar nblumhardt commented on July 24, 2024

Hope you found a solution to this; closing as the thread's now a bit stale 👋

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.