# Path: mom/Policy/Parser.py
# class Parser(GenericParser):
#     def __init__(self, start='value'):
#         GenericParser.__init__(self, start)
# 
#     def mklist(self, args):
#         if len(args) == 2:
#             return []
#         return args[1]
# 
#     def p_value_list(self, args):
#         '''
#           value_list ::= value
#           value_list ::= value_list value
#         '''
#         if len(args) == 1:
#             return [args[0]]
#         return args[0] + [args[1]]
# 
#     def p_list(self, args):
#         '''
#           list ::= ( )
#           list ::= ( value_list )
#         '''
#         return self.mklist(args)
# 
#     def p_bracket_list(self, args):
#         '''
#           bracket_list ::= [ ]
#           bracket_list ::= [ value_list ]
#         '''
#         return self.mklist(args)
# 
#     def p_curly_list(self, args):
#         '''
#           curly_list ::= { }
#           curly_list ::= { value_list }
#         '''
#         return [Token('symbol', 'eval')] + self.mklist(args)
# 
#     def p_value(self, args):
#         '''
#           value ::= operator
#           value ::= number
#           value ::= operator
#           value ::= symbol
#           value ::= string
#           value ::= single_quote_string
#           value ::= list
#           value ::= curly_list
#           value ::= bracket_list
#          '''
#         return args[0]


import unittest
from mom.Policy import Parser

    def test_externals(self):
        pol = """
        (+ (abs -21) (abs 21))
        """
        self.verify(pol, [ 42 ])

    def test_with(self):
        class Guest(object):
            def __init__(self, num):
                self.num = num
            def name(self):
                return "Guest-%i" % self.num
        guest_list = [ Guest(1), Guest(2), Guest(4) ]
        self.e.stack.set('Guests', guest_list, True)
        pol = """
        (def guestName (guest) (+ "This guest's name is " (guest.name)))
        (with Guests guest (guestName guest))
        """
        # The results of 'with' are returned in their own list
        # This means that (with ...) cannot be evaluated yet
        self.verify(pol, [ "guestName",
                           [ "This guest's name is Guest-1",
                             "This guest's name is Guest-2",
                             "This guest's name is Guest-4" ] ])

    def test_syntax_error(self):
        pol = """
        (+ 2 2
        """

# Path: mom/Policy/Parser.py
# class Parser(GenericParser):
#     def __init__(self, start='value'):
#         GenericParser.__init__(self, start)
# 
#     def mklist(self, args):
#         if len(args) == 2:
#             return []
#         return args[1]
# 
#     def p_value_list(self, args):
#         '''
#           value_list ::= value
#           value_list ::= value_list value
#         '''
#         if len(args) == 1:
#             return [args[0]]
#         return args[0] + [args[1]]
# 
#     def p_list(self, args):
#         '''
#           list ::= ( )
#           list ::= ( value_list )
#         '''
#         return self.mklist(args)
# 
#     def p_bracket_list(self, args):
#         '''
#           bracket_list ::= [ ]
#           bracket_list ::= [ value_list ]
#         '''
#         return self.mklist(args)
# 
#     def p_curly_list(self, args):
#         '''
#           curly_list ::= { }
#           curly_list ::= { value_list }
#         '''
#         return [Token('symbol', 'eval')] + self.mklist(args)
# 
#     def p_value(self, args):
#         '''
#           value ::= operator
#           value ::= number
#           value ::= operator
#           value ::= symbol
#           value ::= string
#           value ::= single_quote_string
#           value ::= list
#           value ::= curly_list
#           value ::= bracket_list
#          '''
#         return args[0]


# Path: tests/ParserTests.py
import unittest
from mom.Policy import Parser


    def test_externals(self):
        pol = """
        (+ (abs -21) (abs 21))
        """
        self.verify(pol, [ 42 ])

    def test_with(self):
        class Guest(object):
            def __init__(self, num):
                self.num = num
            def name(self):
                return "Guest-%i" % self.num
        guest_list = [ Guest(1), Guest(2), Guest(4) ]
        self.e.stack.set('Guests', guest_list, True)
        pol = """
        (def guestName (guest) (+ "This guest's name is " (guest.name)))
        (with Guests guest (guestName guest))
        """
        # The results of 'with' are returned in their own list
        # This means that (with ...) cannot be evaluated yet
        self.verify(pol, [ "guestName",
                           [ "This guest's name is Guest-1",
                             "This guest's name is Guest-2",
                             "This guest's name is Guest-4" ] ])

    def test_syntax_error(self):
        pol = """
        (+ 2 2
        """

        self.assertRaises(Parser.PolicyError, Parser.eval, self.e, pol)