3

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 do I enforce that the lists opening and closing characters must match?

Kimvais
  • 38,306
  • 16
  • 108
  • 142

1 Answers1

4

You make a list a choice between two rules: one for parentheses and one for square brackets. Thanks for bringing up pyparsing. I like it. My answer for your question is:

delim_value = Group(delimitedList(string_value | int_value))("list")
list_value = Or( (Suppress("[") + delim_value + Suppress("]"),
                  Suppress("(") + delim_value + Suppress(")")) )
minopret
  • 4,726
  • 21
  • 34
  • This gives me an (seemingly unrelated) error when I try to parse a list surrounded with `(`'s – Kimvais Aug 17 '12 at 05:19
  • Works for me: `list_value.parseString("(1,2,3,'slkfj')").asList()` gives `[['1', '2', '3', "'slkfj'"]]`. Perhaps there is something wrong with your definition of string_value or int_value (I had to guess at them in my simulation). In fact, if string_value is something like `Word(printables)`, then this is going to cause problems in matching the enclosing ()'s; they will get slurped up into such a broadly-defined string_value. Pyparsing does not do backtracking like regexen do, but works strictly left-to-right. Add `.setDebug()` to your definition of string_value and see what you match with it. – PaulMcG Aug 29 '12 at 13:18