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 possibility to include another grammar in a grammar file #531

Closed
jhoobergs opened this issue Aug 12, 2021 · 3 comments
Closed

Add possibility to include another grammar in a grammar file #531

jhoobergs opened this issue Aug 12, 2021 · 3 comments

Comments

@jhoobergs
Copy link

jhoobergs commented Aug 12, 2021

A little context

I am currently developing a parser that can parse certain mathematical expressions. On top of that, I want to be able to parse these expressions within html files. To do this, I copied a pest grammar for HTML and copied it into my existing grammar. The main problem that I am having with this is the fact that the COMMENT (and also WHITESPACE) rules are now unusable for me because the HTML and the mathematical expression have different comment and whitespacing needs. I also don't like the fact that my grammar does now contain multiple languages without a clear separation.

My proposal:

  • Allow including one pest grammar in another (e.g. html = include html.pest)
  • All rules of the included grammar are usable in the other grammar, preferably with a prefix so there are no duplicate names (e.g. html:node instead of just node (which is defined in html.pest)
  • Both grammars do still use the COMMENT and WHITESPACE rules as described in their own file

Other cases

  • An html parser might for example also want to use a pest grammar for css and js, and that would be a lot easier with the include function.

Alternative

  • Including all rules might be a bit to much and give problems with contradicting COMMENT and WHITESPACE values, but just including one rule like expression or html_document or js_code or css_style from another grammar would help. The other rules defined in the subgrammars would still exist and be usable in the parsing code, but not in the main pest file itself.

Current solution

I currently parse the html with an HTML parser (with its own grammar) and have special rule expression. Each expression that I parse, is than parse with to parser for mathematical expressions. This works, but has some drawbacks like much harder line number detection etc.

What do you think? Is this something that seems useful or am I missing something?

@Abdillah
Copy link

FYI, This has been discussed in #197.

@jhoobergs
Copy link
Author

It does seem that the modules as described in #333 are indeed the thing I want. µ

I am missing information about how COMMENT and WHITESPACE would work?

@tomtau
Copy link
Contributor

tomtau commented Jul 14, 2022

it looks like this is a duplicate of #197

@tomtau tomtau closed this as not planned Won't fix, can't repro, duplicate, stale Jul 14, 2022
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

No branches or pull requests

3 participants