Skip to content
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

Merged
merged 8 commits into from
Oct 2, 2018
Merged

Issue #249 add GUIDE.md #254

merged 8 commits into from
Oct 2, 2018

Conversation

wavebeem
Copy link
Collaborator

Fixes #249, adding a guide for using Parsimmon

Fixes #249, adding a guide for using Parsimmon
@wavebeem wavebeem mentioned this pull request Jun 13, 2018
@coveralls
Copy link

coveralls commented Jun 13, 2018

Coverage Status

Coverage remained the same at 100.0% when pulling 5d9c212 on issues/249 into 6690c37 on master.

@wavebeem
Copy link
Collaborator Author

@ivan-kleshnin, @anko, are either of you interesting in looking at what I've got here so far? No pressure

@ivan-kleshnin
Copy link
Contributor

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.
Copy link
Contributor

@halfzebra halfzebra Jul 28, 2018

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.
Copy link
Contributor

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 🙂

@wavebeem wavebeem merged commit 9bed271 into master Oct 2, 2018
@wavebeem wavebeem deleted the issues/249 branch October 2, 2018 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants