pythonpegpypeg

How to match unordered things in pyPEG?


I have the following file:

orange
apple
orange
apple
apple 
lime
banana

Each type of fruit has a class to match it:

class Banana:
    grammar = ....

class Apple:
    ...

I have to match each fruit unordered, I cannot tell upfront what will be the order. How can I setup a FruitBasket grammar to match'em?

class FruitBasket:
    grammar = ?????

ps: The fruits are actually complicated rules, each fruit type stands for a different code block, such as a function, prototype, global variable, etc.


Solution

  • Use a list:

    A list instance which is not derived from pypeg2.Concat represents different options. They're tested in their sequence. The first option which parses is chosen, the others are not tested any more. If none matches, a SyntaxError is raised.

    For example:

    from pypeg2 import K, List, maybe_some, parse
    
    class Apple:
        grammar = K('apple')
    
    class Banana:
        grammar = K('banana')
    
    class Lime:
        grammar = K('lime')
    
    class Orange:
        grammar = K('orange')
    
    class FruitBasket(List):
        '''An unordered collection of zero or more fruits'''
        grammar = maybe_some([Apple, Banana, Lime, Orange])
    
    
    if __name__ == '__main__':
    
        fruit = '''
            orange
            apple
            orange
            apple
            apple 
            lime
            banana
        '''
    
        print(parse(fruit, FruitBasket))
    

    Output:

    FruitBasket([<__main__.Orange object at 0x7ffa4991d8d0>, <__main__.Apple object at 0x7ffa49922c18>, <__main__.Orange object at 0x7ffa49927a58>, <__main__.Apple object at 0x7ffa499279b0>, <__main__.Apple object at 0x7ffa49927908>, <__main__.Lime object at 0x7ffa421ad828>, <__main__.Banana object at 0x7ffa421ad8d0>])