GithubHelp home page GithubHelp logo

Simple tokenizer question about superpower HOT 5 CLOSED

dhowe avatar dhowe commented on July 17, 2024
Simple tokenizer question

from superpower.

Comments (5)

nblumhardt avatar nblumhardt commented on July 17, 2024

Hi!

The tokenizer will consider the whole span matched by the parser to be the token, regardless of the value that's returned, but, you can use .Apply(HashIdent) later on in the parsing stage to get the value, i.e. Token.EqualTo(Tokens.Identifier).Apply(HashIdent).

For this to work you need to drop off the Value(Unit.Value) piece:

static TextParser<Unit> HashIdent { get; } = Character.EqualTo('#').IgnoreThen(Identifier.CStyle)

HTH!

from superpower.

dhowe avatar dhowe commented on July 17, 2024

Thanks, thats very helpful (I was just wondering what the apply was doing in your example code)!

When I do that, I get a compile error... do I need to change the generic type to TextSpan?

from superpower.

dhowe avatar dhowe commented on July 17, 2024

Also, how do I handle the opposite case, where I want to ignore the last character?

        static TextParser<Unit> Actor { get; } =
            from name in Character.LetterOrDigit.Many()
            from last in Character.EqualTo(':')
            select Unit.Value;

from superpower.

AndrewSav avatar AndrewSav commented on July 17, 2024

Yes, dropping Unit means that you are returning a different value now. If value is not needed for performance reasons it's better to use unit, but in your case change it to TextSpan as you suggested.

As for your second question something like this would probably work:

static TextParser<string> Actor { get; } =
			from name in Character.LetterOrDigit.Many()
			from last in Character.EqualTo(':')
			select new string(name);

from superpower.

nblumhardt avatar nblumhardt commented on July 17, 2024

I guess this water is long under the bridge - hope you found a good solution, @dhowe - closing as stale, but keep us posted :-)

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.