-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #249 add GUIDE.md #254
Conversation
Fixes #249, adding a guide for using Parsimmon
@ivan-kleshnin, @anko, are either of you interesting in looking at what I've got here so far? No pressure |
Thanks! I'll review this, for sure. |
GUIDE.md
Outdated
|
||
> Some performance tips. Which constructions should be considered dangerous performance-wise? | ||
|
||
I want to answer this one, but honestly don't even really know where to start. It's not something I look at much. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be worth mentioning that for parsing efficiency it is good to have the preceding parser to eat all the trailing whitespace, so the next one doesn't have to munch through it again.
const token = Parsimmon.regex(/[a-Z]+/)
const whitespace1 = Parsimmon.regex(/[ \t]+/)
const parser = Parsimmon.seq(
token.skip(whitespace1),
Parsimmon.alt(
Parsimmon.string('World'),
// Other parsers.
)
)
parser.parse('Hello World!')
// ☝️ all the whitespace is gone after `token` is parsed,
// instead of munching through it in every parser in `Parsimmon.alt`
This is not very useful for the example above, but in general, I feel like it's a fairly common optimization technique.
|
||
Additionally, what if the underlying parser _doesn't_ match? What would the `not` version return there? And more importantly, how much input would it consume? The only reasonable answer I can find to this question is it returns `null` or `undefined` and consumes zero characters, but this isn't any different from `Parsimmon.notFollowedBy`. | ||
|
||
So if you want avoid parsing things you can use `Parsimmon.notFollowedBy` or `.notFollowedBy`, but you still need to specify _what_ you actually want to parse. It may be possible to prove things by contradiction, but for parsing we still need some kind of positive construction in order to consume the characters and yield a result. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a good place to put the references to the API docs 🙂
Fixes #249, adding a guide for using Parsimmon