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

Please more examples ... #243

Closed
heilingbrunner opened this issue May 12, 2022 · 12 comments · Fixed by #276
Closed

Please more examples ... #243

heilingbrunner opened this issue May 12, 2022 · 12 comments · Fixed by #276

Comments

@heilingbrunner
Copy link

Please provide more simple examples regarding to the documentation. The section 'Parsing Expression Types' could be better if the are some simple examples to get into the usage of the expression.

Thank you.

@hildjj
Copy link
Contributor

hildjj commented May 31, 2022

I'm imagining an example for each type, showing a matching input and a non-matching input. For example:

"literal"
'literal'
Match exact literal string and return it. The string syntax is the same as in JavaScript. Appending i right after the literal makes the match case-insensitive.

Example: top = "foo"i
Matches: foo, FOO, FoO
Doesn't match: fo, fooo, bar

@Mingun
Copy link
Member

Mingun commented Jun 1, 2022

Strongly speaking, fooo is matched by this rule, but the whole grammar does not match (because grammar shouldn't leave unconsumed input)

@hildjj
Copy link
Contributor

hildjj commented Jun 1, 2022

Let's decide if these are single rules or full grammars, I guess. Either works for me as long as we're consistent.

@Mingun
Copy link
Member

Mingun commented Jun 1, 2022

My vote for single rules with an explanation that examples just for rules, to see them in action you should pretend that actual grammar is

grammar = rule .*;
rule = <whatever>;

Links to live examples (or even embedded examples) also would be useful, but that the task for a web-site.

@hildjj
Copy link
Contributor

hildjj commented Jun 1, 2022

I like the live playground idea, and since the docs are web-only now, that should work. We could have one large grammar that has a rule for each section, with each rule in the allowedStartRules.

Maybe let's try that, and see what it feels like in practice before we finalize the .* approach?

@Mingun
Copy link
Member

Mingun commented Jun 1, 2022

The .* approach is for dealing with situations like

foo = 'foo'i;

and input

foooo

The rule itself will match the beginning of this string, but to show this in action we should append .* to the end of rule, or just use grammar rule shown above. I think, it is important to highlight this particularity, because otherwise people can have a wrong impression that to match a keyword the grammar

keyword = 'keyword';

is enough (spoiler: it isn't, the right way to do that is to check that after a keyword there is a delimiter symbol or not an identifier symbol:

keyword = 'keyword' !identifierPart;

)

I think more about supporting GET parameters in the https://peggyjs.org/online to embed a grammar and adding that links to a docs. I don't think that this should be a hard to implement.

@hildjj
Copy link
Contributor

hildjj commented Jun 1, 2022

Can you take a look at https://raw.githack.com/hildjj/peggy/examples/docs/documentation.html please, and see what you think before I finish? Go down to "Parsing Expression Types", and look at the first two.

@heilingbrunner
Copy link
Author

These two examples are very simple. The inline try-test is nice, but not really required. I would like to see examples for the '&/! expression */+/?' and '&{predicate}'.
I used your online version too. This is a very good learning tool.

@hildjj
Copy link
Contributor

hildjj commented Jun 2, 2022

Please refresh the link above and see what you think? I have now agreed with @Mingun about just matching the beginning, and have started returning { match: "stuff that matched", rest: "stuff after the match" }

@hildjj
Copy link
Contributor

hildjj commented Jun 3, 2022

This will show up with the next release. I don't want to cherry-pick it onto the docs branch because the tooling needed to build it is only available after the next release.

@hildjj
Copy link
Contributor

hildjj commented Jun 3, 2022

In the meantime, you can use https://raw.githack.com/peggyjs/peggy/main/docs/documentation.html

@heilingbrunner
Copy link
Author

I looked at the examples and understood. They are very short, but now I understand how I can use e.g. predicates. I hadn't used that yet. Thank you.

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 a pull request may close this issue.

3 participants