Giter Club home page Giter Club logo

Comments (6)

nblumhardt avatar nblumhardt commented on July 4, 2024

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.

powerdude avatar powerdude commented on July 4, 2024

@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.

nblumhardt avatar nblumhardt commented on July 4, 2024

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.

powerdude avatar powerdude commented on July 4, 2024

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.

nblumhardt avatar nblumhardt commented on July 4, 2024

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.

powerdude avatar powerdude commented on July 4, 2024

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)

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.