How to combine brackets / brackets in pyparsing

I have a grammar token specified as:

list_value = Suppress(oneOf("[ (")) + Group(
    delimitedList(string_value | int_value))("list") + Suppress(oneOf("] )"))

However, this obviously allows (foo, bar]

How to ensure that the symbols for opening and closing lists are consistent?

+3
source share
1 answer

You make the list a choice between two rules: one for parentheses and one for square brackets. Thanks for picking up the piraping. I like it. My answer to your question:

delim_value = Group(delimitedList(string_value | int_value))("list")
list_value = Or( (Suppress("[") + delim_value + Suppress("]"),
                  Suppress("(") + delim_value + Suppress(")")) )
+3
source

All Articles