lark icon indicating copy to clipboard operation
lark copied to clipboard

Black formatter breaks Lark standalone parser generation

Open marsninja opened this issue 7 months ago • 0 comments

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.

marsninja avatar Nov 12 '23 14:11 marsninja