Comments (6)
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.
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.
I've added tests for both variations and everything is as expected: 62da932.
from dart-petitparser.
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.
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.
Thanks, I understand it now
from dart-petitparser.
Related Issues (20)
- Translate JS/TS to Dart? HOT 5
- Alternative to trim parser or how to discard HOT 2
- Relevance of `refX`? HOT 1
- Broken link for linter rules HOT 1
- Eliminate dynamic calls
- How to upgrade? HOT 7
- Getting line and colone in the code. HOT 2
- Improve error reporting of `ExpressionPaser` HOT 1
- Consider rolling meta back to ^1.8.0 HOT 1
- Greedy parse problem HOT 2
- Higher Unicode planes support? HOT 2
- Collecting Parsed result and passing it to next product HOT 1
- Possible alternative use of switch that might be fast enough HOT 3
- ReferenceParser prevents repeatString optimization HOT 1
- Projects that are using PetitParser HOT 1
- Parsing Latin1 characters HOT 4
- Alternative to deprecated `separatedBy(separator, includeSeparators: false)` HOT 2
- Choice parser behavior HOT 2
- use petitparser to recognize jsonDecode output HOT 1
- Question: parse any word or term HOT 2
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 dart-petitparser.