I have a grammar defined in pyparsing
OCB, CCB, SQ = map(Suppress, "{}'")
name = SQ + Word(printables, excludeChars="{},'") + SQ
_name = CaselessKeyword("name").suppress()
_interface = Keyword("Component").suppress()
interface = Group(_interface + OCB + _name + name("interface_name") + CCB)
system = OneOrMore(interface + Optional(",").suppress())("interfaces")
If I have an input string:
model = "Component { name '/comp1' },
Component { name '/comp2' }"
result = system.parseString(model)
print(result.dump())
The parsing result is as expected:
[['/comp1'], ['/comp2']]
- interfaces: [['/comp1'], ['/comp2']]
[0]:
['/comp1']
- interface_name: ['/comp1']
[1]:
['/comp2']
- interface_name: ['/comp2']
I want to know if there is a way to generate a string based on the grammar mentioned above. Since the only "variables" are comp1 and comp2, I need a function that generates the text:
def generate_string(comps: list):
# do something
return result
And the result of generate_string (maybe using originalTextFor?) should be:
"Component { name '/comp1' }, Component { name '/comp2' }"
I have seen examples to edit and to manually insert in ParseResults. But they don't make use of the grammar