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

Add an example on how to implement a parser for interspersed input #468

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

daddykotex
Copy link

@daddykotex daddykotex commented Dec 7, 2022

While looking at the documentation I found some things that I could be improved so here is a PR for that.

I was specifically looking to see how I could implement something to parse "a,b,c" and extract a, b and c. I found that soft could be used for that and because I think it's a common pattern in parsers, I figured it could be useful to have an example in the documentation.

On top of that, I get feedback from experts to tell me if that solution is bad and what would be a better alternative, win-win!


One annoying thing about the docs/index.md is that yes, it's compiled but it's not the actual output of mdoc and so the result in there are hard coded. I found it annoying to go into site/target/mdoc/index.md and retrieve the output and format it to fit with the rest of the document. Is there a better way?

@armanbilge
Copy link
Member

t's compiled but it's not the actual output of mdoc and so the result in there are hard coded ... Is there a better way?

mdoc already renders expressions automatically (but not statements like val declarations).

In fact I think in some places there is duplicate output.
https://typelevel.org/cats-parse/


But you can use, `soft` along with `|` to implement that:
```scala mdoc
val interspersed = ((p4.soft <* pchar(',')) | p4).rep
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this example would also parse abc which maybe what you want but might be a bit confusing for newcomers.

here is an example of how I parse lists in bosatsu:
https://github.com/johnynek/bosatsu/blob/c58b55785b6ac72e59b98afe3149a042d623a902/core/src/main/scala/org/bykn/bosatsu/Parser.scala#L302

Copy link
Collaborator

@johnynek johnynek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for sending the PR.

I think we should add some notes to the example you added because I think it accepts more than most people would expect with a list example (making the , fully optional).

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

3 participants