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

Black formatter breaks Lark standalone parser generation #1362

Open
marsninja opened this issue Nov 12, 2023 · 0 comments
Open

Black formatter breaks Lark standalone parser generation #1362

marsninja opened this issue Nov 12, 2023 · 0 comments

Comments

@marsninja
Copy link

marsninja commented Nov 12, 2023

Describe the bug

The functionality of Lark standalone parser generator breaks if lark codebase is run through the black formatter tool.

This error is produced:

jaclang/vendor/lark/tools/standalone.py:217: in main
    gen_standalone(lark_inst, out=out, compress=ns.compress)
jaclang/vendor/lark/tools/standalone.py:170: in gen_standalone
    code = extract_sections(f)["standalone"]
E   KeyError: 'standalone'

The culpret comes from the extract_sections function on line 96 (post black formatting):

def extract_sections(lines):
    section = None
    text = []
    sections = defaultdict(list)
    for line in lines:
        if line.startswith("###"):
            if line[3] == "{":
                section = line[4:].strip()
            elif line[3] == "}":
                sections[section] += text
                section = None
                text = []
            else:
                raise ValueError(line)
        elif section:
            text.append(line)

A fixed version is

def extract_sections(lines):
    section = None
    text = []
    sections = defaultdict(list)
    for line in lines:
        clean_line = line.strip()
        if clean_line.startswith("###"):
            if clean_line[3] == "{":
                section = clean_line[4:].strip()
            elif clean_line[3] == "}":
                sections[section] += text
                section = None
                text = []
            else:
                raise ValueError(line)
        elif section:
            text.append(line)

Will make a pr.

To Reproduce

Run lark code base through black formatter, then try to create a standalone parser.

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

1 participant