Exemplo n.º 1
0
    def test_CorrectlyHandlesDifferentNumberOfArguments(self):
        # Arrange
        txt = 'lorem ipsum'
        parser_l = bpr.LParser('l')
        parser_o = bpr.LParser('o')
        parser_r = bpr.LParser('r')
        parser_e = bpr.LParser('e')

        parser1 = cmb.and_then(parser_l)
        parser2 = cmb.and_then(parser_l, parser_o)
        parser3 = cmb.and_then(parser_l, parser_o, parser_r)
        parser4 = cmb.and_then(parser_l, parser_o, parser_r, parser_e)
        # Act
        result1 = parser1(txt)
        result2 = parser2(txt)
        result3 = parser3(txt)
        result4 = parser4(txt)
        expected1 = res.Success(('l', 'orem ipsum'))
        expected2 = res.Success(('lo', 'rem ipsum'))
        expected3 = res.Success(('lor', 'em ipsum'))
        expected4 = res.Success(('lore', 'm ipsum'))
        # Assert
        self.assertEqual(result1, expected1)
        self.assertEqual(result2, expected2)
        self.assertEqual(result3, expected3)
        self.assertEqual(result4, expected4)
Exemplo n.º 2
0
def parse_float(label='FLOAT'):
    """
    returns parser for floating point number
    """
    sign = cmb.opt(bp.LParser('-'))
    coma = bp.LParser('.')
    e = cmb.or_else(bp.LParser('e'), bp.LParser('E'))
    exponent = cmb.opt(cmb.and_then(e, parse_integer()))
    integer = parse_integer()
    return cmb.and_then(sign, cmb.opt(integer), coma, parse_digits(), exponent, label=label)
Exemplo n.º 3
0
 def test_AddingLabel(self):
     # Arrange
     parser1 = bpr.LParser('l')
     parser2 = bpr.LParser('o')
     parser3 = bpr.LParser('r')
     parser = cmb.and_then(parser1, parser2, parser3, label='Parser: lor')
     parser_default_label = cmb.and_then(parser1, parser2, parser3)
     # Assert
     self.assertEqual(parser.label, 'Parser: lor')
     self.assertEqual(parser_default_label.label, 'lor')
Exemplo n.º 4
0
def parse_string(string, label=None):
    """
    returns parser of a given string
    """
    if not isinstance(string, str):
        raise Exception('Expected string')
    return cmb.and_then(*[bp.LParser(char) for char in string], label=label)
Exemplo n.º 5
0
 def test_WhenFirstLettersAreIncorrect_ReturnFailure(self):
     # Arrange
     txt = 'lorem ipsum'
     parser1 = bpr.LParser('l')
     parser2 = bpr.LParser('x')
     parser3 = bpr.LParser('r')
     parser = cmb.and_then(parser1, parser2, parser3)
     # Act
     result = parser(txt)
     expected = res.Failure('error')
     # Assert
     self.assertEqual(result, expected)
Exemplo n.º 6
0
 def test_WhenFirstLettersAreCorrect_ReturnSuccess(self):
     # Arrange
     txt = 'lorem ipsum'
     parser1 = bpr.LParser('l')
     parser2 = bpr.LParser('o')
     parser3 = bpr.LParser('r')
     parser = cmb.and_then(parser1, parser2, parser3)
     # Act
     result = parser(txt)
     expected = res.Success(('lor', 'em ipsum'))
     # Assert
     self.assertEqual(result, expected)
Exemplo n.º 7
0
def parse_unsignedinteger(label='UINTEGER'):
    """
    returns parser for unsigned integer
    """
    digits_09 = parse_digit('0123456789')
    digits_19 = parse_character('123456789')
    nonzero = cmb.and_then(digits_19, cmb.many(digits_09))
    zero = bp.LParser('0')

    def internal(txt):
        fst_digit = zero(txt)
        if fst_digit.isSuccess:
            snd = digits_09(fst_digit.value[1])
            if snd.isSuccess:
                return res.Failure('error')
            else:
                return fst_digit
        return nonzero(txt)

    return bp.LParser(internal, label)
Exemplo n.º 8
0
def parse_integer(label='INTEGER'):
    """
    returns parser for integer
    """
    sign = cmb.opt(bp.LParser('-'))
    return cmb.and_then(sign, parse_unsignedinteger(), label=label)