Comments (7)
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.
@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.
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.
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.
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.
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.
Hope you found a solution to this; closing as the thread's now a bit stale 👋
from superpower.
Related Issues (20)
- Zero allocation parsing? HOT 1
- Unit Testing? HOT 1
- Is it normal that NaturalUInt32 raise System.OverflowException : Value was either too large or too small for a UInt32 ? HOT 2
- [Request] Need a new introductory blog for v2 (or v3) HOT 1
- Questions: My On Going Questions HOT 4
- Parsing confusion: Zero-width parsers, what rewinds and when, and properly returning a "failed" parse? HOT 4
- [Question] New Release? HOT 1
- Thank you for writing superpower HOT 1
- Better `Message` HOT 5
- Improve error reporting for some failed match scenarios HOT 9
- Using backslash in CStyle QuotedString causes syntax error HOT 1
- `OptionalOrNull()` HOT 2
- Send parser value into another parser HOT 1
- Tokenizer and Parser Assistance HOT 3
- TokenizerBuilder and Mapping CLR Types HOT 2
- Dynamically referencing Parsers via Decorators for TokenizerBuilder HOT 2
- Some way to check the next token HOT 3
- Missing: Cheat sheet for upgrading from Sprache to Superpower HOT 1
- How to handle consecutive delimiters HOT 6
- dotnet 8 upgrade? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from superpower.