lark icon indicating copy to clipboard operation
lark copied to clipboard

Can Lark parse rpm spec file format?

Open nikableh opened this issue 1 year ago • 4 comments

What is your question?

Can Lark parse files like this one? How?

I've tried implementing the grammar, but can't figure out how to do these two things:

  • Parse until the next section
  • Parse based on current state

Spec file's "sections" (like%description, %build, %prep, etc.) don't have concrete begin and end markers (like { and } in Java). I couldn't figure out how to parse until the next known section either.

Thanks in advance! 🩵

nikableh avatar Dec 11 '24 17:12 nikableh

I also couldn't find a way to store the current state during the lexing stage. This is how PLY does it, for example: "4.19 Conditional lexing and start conditions".

nikableh avatar Dec 11 '24 17:12 nikableh

Lark by default switches the lexer context automatically, so usually you won't need to do any manual changes like in PLY.

But if you need more control over the parsing process, you should be able to get everything you need by using the interactive parser (see Lark.parse_interactive())

erezsh avatar Dec 11 '24 18:12 erezsh

So, do you suggest that interactive mode is the only viable option to parse such file format?

Input:

%foo
12345

%bar
67890

Output:

result = {
    "foo": 12345,
    "bar": 67890,
}

nikableh avatar Dec 11 '24 18:12 nikableh

The example in this reply is something lark can handle easily, without any special techniques.

erezsh avatar Dec 11 '24 19:12 erezsh