lark icon indicating copy to clipboard operation
lark copied to clipboard

Load files once

Open MegaIng opened this issue 4 years ago • 14 comments
trafficstars

The changes implemented during the discussion of #992, and a basic implementation of %include (typo in commit message :-/). Should I also implement more complex load once stuff in this PR?

MegaIng avatar Sep 14 '21 12:09 MegaIng

@MegaIng say i have 2 grammars A and B that both include a grammar C. now if i include grammars A and B in a grammar D, i get conflict errors as rules defined in grammar C are declared twice. is that wanted? in fact (not exclusive to %include), why prohibit defining a rule more than once, instead of using the last definition?

ornariece avatar Sep 25 '21 13:09 ornariece

@ornariece Without a major rewrite of GrammarLoader, that is not avoidable. I will take %include out of this PR, and start on a rework. The rest of this PR works.

The logic is that you can manually specify %extend or %override which might both be correct behavior for a repeated definition.

MegaIng avatar Sep 25 '21 13:09 MegaIng

@erezsh This PR is now done. I will put %include into a new one.

MegaIng avatar Sep 25 '21 14:09 MegaIng

@MegaIng you added %include back

ornariece avatar Sep 25 '21 14:09 ornariece

HOW? I completely reverted those changes (they aren't even in my local files). I am not sure where git got those changes from...

MegaIng avatar Sep 25 '21 14:09 MegaIng

@MegaIng (currently tweaking my grammars to use %include) %extend can't be used on a rule that got defined through an %include statement (ie. not through direct definition or %import). the error that is raised is lark.exceptions.GrammarError: Can't extend rule name as it wasn't defined before

ornariece avatar Sep 26 '21 16:09 ornariece

@ornariece That is part of the reason why I removed this implementation of %include.

MegaIng avatar Sep 27 '21 11:09 MegaIng

@MegaIng I'm probably missing something, but why not just make it use the same implementation of %import but just with an empty namespace?

erezsh avatar Sep 27 '21 12:09 erezsh

@erezsh That leads exactly to the problem @ornariece describes, since imports (and then includes) are added before anything in the grammar is done, meaning that an included grammar could not extend symbols in the main grammar. Also, that would still create a full GrammarLoader instance for each included grammar, meaning that no actual import de duplication would happen.

MegaIng avatar Sep 27 '21 12:09 MegaIng

an included grammar could not extend symbols in the main grammar

I think that's the desirable behavior.

Perhaps these symbols should be refactored into a shared.lark, from which they can be extended.

erezsh avatar Sep 27 '21 12:09 erezsh

I think that's the desirable behavior.

I disagree. Including a grammar should behave no different then just pasting the entire content at that place.

MegaIng avatar Sep 27 '21 13:09 MegaIng

For that behavior, it has to be processed before all other directives.

erezsh avatar Sep 27 '21 13:09 erezsh

@erezsh That is the plan, but that is interdependent of this PR. I will create one for those changes.

MegaIng avatar Sep 27 '21 13:09 MegaIng

Sorry I'm only getting to this now. Any chance you can fix the PR for the recent changes?

It might even need a few more changes for allowing abnf and such? (maybe not. I didn't look that deeply yet)

erezsh avatar Oct 17 '21 09:10 erezsh