Recursive IndentedBlock with optional contents

37 views Asked by At

IndentedBlock from pyparsing not dedenting

I am trying to write a parser in pyparsing that has a container that has optional contents, and I'm not getting the results I'm expecting.

Here's the simplest version that demonstrates my problem I could come up with:

from pyparsing import *
ident = Word(alphas)
value = Word(nums)
container = Forward()
container << (Group(ident + IndentedBlock(container | value)) | ident)

test = """\
A
    1
    B
    C
        2
"""

container.parse_string(test, parse_all=True).pprint()

What I'm getting is this:

[['A', ['1', ['B', [['C', ['2']]]]]]]

Which shows that the C is being treated as indented underneath the B, but it should be listed underneath the A.

What I'm expecting is this:

[['A', ['1', ['B'], ['C', ['2']]]]]

Am I doing this wrong? How could I get a container with optional contents in pyparsing using IndentedBlock?

I am using pyparsing 3.1.1

0

There are 0 answers