Skip to content

Conversation

karloscarweber
Copy link
Contributor

Hi Friends.

This PR improves the documentation around Instantiating Grammars somewhat by providing verbose examples. We could simplify the examples to save space, and add clarity, but I think verbose examples are usually helpful.

The desire to add this came about because I was a bit confused about how to actually instantiate a grammar that inherits from another. The documentation is clear, but I had to read it thoroughly a couple of times, and ask for some help, before I understood what was going on.

@karloscarweber
Copy link
Contributor Author

Tests are failing. It's stemming from the code examples. I'll add some markscript checks and run through the test suite locally to ensure that it will pass. Then upload my changes.

Copy link
Contributor

@pdubroy pdubroy 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 this! I think it will make a nice addition to the docs. I'd just suggest simplifying it a bit to keep the signal-to-noise ratio high.

(And sorry about the Markscript stuff, it's a bit of a pain. I implemented it a long time ago as a way of making sure that all the code examples in the docs are actually valid, but it's a bit tricky to understand.)

Comment on lines 21 to 42
Arithmetic {
Exp
= AddExp
AddExp
= AddExp "+" PriExp -- plus
| AddExp "-" PriExp -- minus
| PriExp
PriExp
= "(" Exp ")" -- paren
| "+" PriExp -- pos
| "-" PriExp -- neg
| ident
| number
ident (an identifier)
= letter alnum*
number (a number)
= digit* "." digit+ -- fract
| digit+ -- whole
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is the API reference for ohm.grammars, I think it's best if we reduce this to a minimal grammar, e.g.:

const parentDef = String.raw`
  Parent {
    start = "parent"
  }
`;

With an arithmetic grammar, there's a whole bunch of detail that distracts from what we're trying to show — and a reader likely doesn't now what's relevant and what's not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good idea. I've greatly simplified the examples in accordance with your instructions and added mark script tests inline to ensure it all works.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I really like mark script. It's really cool.

Comment on lines 66 to 70
// combinedGrammars:
// {
// Arithmetic: object,
// BetterArithmetic: object
// }
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you mean to show what childGrammar is here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I fixed this.

@karloscarweber
Copy link
Contributor Author

Please hold off on approving this PR. For some reason my commits aren't attached correctly to my identity. I've gotta figure that out.

@karloscarweber karloscarweber force-pushed the docs/improve-documentation-around-inheriting-grammars branch from 7a4e1a2 to 9f6c173 Compare September 18, 2025 14:56
@karloscarweber
Copy link
Contributor Author

Please hold off on approving this PR. For some reason my commits aren't attached correctly to my identity. I've gotta figure that out.

Alright I fixed that. Good to go.

@pdubroy pdubroy merged commit ac3addb into ohmjs:main Sep 18, 2025
3 checks passed
@pdubroy
Copy link
Contributor

pdubroy commented Sep 18, 2025

Thanks again!

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.

2 participants