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

Import lark grammar written in one python project into another #1397

Open
manjrekarom opened this issue Mar 26, 2024 · 2 comments
Open

Import lark grammar written in one python project into another #1397

manjrekarom opened this issue Mar 26, 2024 · 2 comments
Labels

Comments

@manjrekarom
Copy link

manjrekarom commented Mar 26, 2024

What is your question?

How do I import lark grammar written in one python project into another.
E.g.
project A has A/grammar.lark file
project B can import A
But

// file called B/superset.py with inline grammar
...
%import A.grammar.SOME_TERMINAL

fails with FileNotFoundError: [Errno 2] No such file or directory: 'A/grammar.lark'

Explain what you're trying to do, and what is obstructing your progress.
I am not sure if there is a nice way to do this. We are able to %import common.<TERMINAL> so I would like to think there should be a way to do this.

@manjrekarom manjrekarom changed the title Import lark grammar written in one project into another Import lark grammar written in one python project into another Mar 26, 2024
@MegaIng
Copy link
Member

MegaIng commented Mar 26, 2024

lark has facilities for this, namely import_paths.

If "project A" means an importable package, you can use the FromPackageLoader helper class:

from lark.load_grammar import FromPackageLoader

Lark(... , import_paths=[FromPackageLoader('A', [''])

'' can be replaced by a relative path if your grammar files aren't in the top level of the package's folder. You would then import this with %import grammar.SOME_TERMINAL, not %import A.grammar.SOME_TERMINAL.

If A is just a folder somewhere, you can instead provide a path inside of import_paths: Lark(..., import_paths['/some/absolute/path'])

@manjrekarom
Copy link
Author

Thanks, this works. Is there a way to also import all symbols from the grammar?
E.g. replace this

%import grammar.ABC
%import grammar.DEF

with

%import grammar.*

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants