Giter Club home page Giter Club logo

Comments (6)

renggli avatar renggli commented on August 10, 2024

Order matters. The following parser should do what you want:

word().plus().flatten().trim()

Have a look at the documentation of flatten() and trim() and check out the implementation of the two parsers to understand why that is.

from dart-petitparser.

freewind avatar freewind commented on August 10, 2024

It seems doesn't work:

word().plus().flatten().trim()

The whitespaces are still there. I have to write it like:

word().plus().flatten().map((x) => x.trim());

from dart-petitparser.

renggli avatar renggli commented on August 10, 2024

I've added tests for both variations and everything is as expected: 62da932.

from dart-petitparser.

freewind avatar freewind commented on August 10, 2024

What about a complex one?

    Parser whitespaceInLine() => char('\n').not() & whitespace();
    var ppp = word().plus().trim(whitespaceInLine()).flatten().trim();

    var rs = ppp.parse("   abc  ");
    print("[${rs.value}]");

It prints:

[abc  ]

I think if we can change the flatten() method as:

flatten([bool trim = false])

Because I think trim a string is a very common requirement.

PS: I love your PetitParserDart very much, that I can't write my Dart code without it, thank you !

from dart-petitparser.

renggli avatar renggli commented on August 10, 2024

Again, flatten() does not work on the result of the inner parser, but it discards that result and returns the the range in the input buffer the inner parser consumed (see the documentation and the implementation of the FlattenParser). If there is a trim() parser inside the flatten(), then you end up with the whitespace that the trimmer consumed as well. If trim() is outside the flatten(), the whitespace are consumed before and after the flatten() kicks in.

So you need to flatten() where you want the input to be flattened. That is, in your example directly after word().plus(). If you change your example to

var ppp = word().plus().flatten().trim(whitespaceInLine());

it works.

from dart-petitparser.

freewind avatar freewind commented on August 10, 2024

Thanks, I understand it now

from dart-petitparser.

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.