Comments (6)
Hi! The problem here is that once Superpower has matched part of a rule, it won't "backtrack" to try another rule. The numeric part of the first rule is matching, so the second rule isn't tried.
You could use Minutes.Try().Or(Seconds)
, or an approach something like:
public static TextParser<TimeSpan> TimeSpan { get; } = from number in Numerics.IntegerInt32
from _ in Character.WhiteSpace.IgnoreMany()
from units in Span.EqualToIgnoreCase("minutes")
.Or(Span.EqualToIgnoreCase("seconds))"
select units.ToStringValue == "minutes" ?
System.TimeSpan.FromMinutes(number) :
TimeSpan.FromSeconds(number);
HTH!,
Nick
from superpower.
@nblumhardt thanks, that was a big help. One more question if you don't mind? Can you help with the following:
[TestClass]
public class BugTests
{
private static readonly TextParser<TextSpan> Word = Identifier.CStyle;
public enum BugToken
{
NumberRange,
Word
}
public static TextParser<string> CommaRange { get; } = from _ in Character.EqualTo(',')
from __ in Character.WhiteSpace.IgnoreMany()
from number1 in Numerics.IntegerInt32
select $", {number1}";
public static TextParser<string> DashRange { get; } = from _ in Character.EqualTo('-')
from __ in Character.WhiteSpace.IgnoreMany()
from number1 in Numerics.IntegerInt32
from ___ in Character.WhiteSpace.IgnoreMany()
from step in BugTests.StepRange.OptionalOrDefault()
select $"-{number1}{step?.Trim()}";
public static TextParser<RepRange> Range { get; } = from number1 in Numerics.IntegerInt32
from __ in Character.WhiteSpace.IgnoreMany()
from subranges in WorkoutTextParser.CommaRange.Try().Or(BugTests.DashRange).AtLeastOnce()
select new RepRange(number1 + string.Join(string.Empty, subranges));
public static TextParser<string> StepRange { get; } = from _ in Character.EqualToIgnoreCase('x')
from __ in Character.WhiteSpace.IgnoreMany()
from number1 in Numerics.IntegerInt32
select $" x{number1}";
[TestMethod]
public void Bug()
{
var tokens = BugTests.CreateTokenizer().Tokenize("15-20 kettlebell swings");
tokens.Count().Should().Be(3);
tokens.ElementAt(0).Kind.Should().Be(BugToken.NumberRange);
tokens.ElementAt(0).ToStringValue().Should().Be("15-20");
}
public static Tokenizer<BugToken> CreateTokenizer()
{
var builder = new TokenizerBuilder<BugToken>();
return builder.Match(BugTests.Range, BugToken.NumberRange).Match(BugTests.Word, BugToken.Word).Ignore(Span.WhiteSpace).Build();
}
}
I don't know why the token has the trailing space. Any ideas?
from superpower.
Hi @powerdude - glad it helped 👍
I'm short on time to dig through the second example, but one tip you might consider - it's normally the job of the tokenizer to deal with whitespace. E.g. if the comma range rule allows embedded whitespace, then basing it on Comma
and Number
tokens should do the job. Likewise with the other rules.
(Minor note, Character.WhiteSpace.IgnoreMany()
can just as efficiently be written Span.WhiteSpace()
.)
HTH!
from superpower.
hi @nblumhardt. thanks for the "minor note" and i've sorted things out.
I tried using it but I still had to use Optional
also. Is there a benefit of using one over the other?
from superpower.
Not 100% clear on what you mean, here - if you mean Optional()
vs Try()
, they're quite different and sometimes do need to be used together. HTH
from superpower.
I was just pointing out that your suggestion didn't work was suggested. I couldn't replace Character.Whitespace.IgnoreMany()
with Span.Whitespace()
. It had to be Span.Whitespace.Optional
.
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.