Example #1
0
 def test_consumed_by(self):
     """
     L{OMetaBase.consumed_by} return the full matched string, not each matched parts
     """
     o = OMetaBase("aax")
     v, e = o.consumed_by(lambda: o.many(lambda: o.exactly("a")))
     self.assertEqual((v, e), ("aa", _MaybeParseError(2, [('expected', None, 'a')])))
Example #2
0
 def test_range(self):
     """
     L{OMetaBase.range} matches range of characters
     """
     o = OMetaBase("2")
     v, e = o.range('0', '9')
     self.assertEqual((v, e), ("2", [0, None]))
Example #3
0
 def findInGrammar(self, expr):
     """
     L{OMeta.pythonExpr()} can extract a single Python expression from a
     string, ignoring the text following it.
     """
     o = OMetaBase(expr + "\nbaz ::= ...\n")
     self.assertEqual(o.pythonExpr()[0][0], expr)
Example #4
0
 def test_listpattern(self):
     """
     L{OMetaBase.rule_listpattern} matches contents of lists.
     """
     o = OMetaBase([["a"]])
     v, e = o.listpattern(lambda: o.exactly("a"))
     self.assertEqual((v, e), (["a"], [0, None]))
Example #5
0
 def test_listpattern(self):
     """
     L{OMetaBase.rule_listpattern} matches contents of lists.
     """
     o = OMetaBase([["a"]])
     v, e = o.listpattern(lambda: o.exactly("a"))
     self.assertEqual((v, e), (["a"], [0, None]))
Example #6
0
 def findInGrammar(self, expr):
     """
     L{OMeta.pythonExpr()} can extract a single Python expression from a
     string, ignoring the text following it.
     """
     o = OMetaBase(expr + "\nbaz = ...\n")
     self.assertEqual(o.pythonExpr()[0][0], expr)
Example #7
0
    def test_predSuccess(self):
        """
        L{OMetaBase.pred} returns True and empty error info on success.
        """

        o = OMetaBase("")
        v, e = o.pred(lambda: (True, _MaybeParseError(0, None)))
        self.assertEqual((v, e), (True, _MaybeParseError(0, None)))
Example #8
0
    def test_keyword(self):
        """
        L{OMetaBase.rule_keyword} matches all the characters in the given string.
        """

        o = OMetaBase("foo bar")
        v, e = o.rule_keyword("foo")
        self.assertEqual(v, { 'keyword':"foo", 'prefix':[] })
Example #9
0
    def test_predSuccess(self):
        """
        L{OMetaBase.pred} returns True and empty error info on success.
        """

        o = OMetaBase("")
        v, e = o.pred(lambda: (True, _MaybeParseError(0, None)))
        self.assertEqual((v, e), (True, _MaybeParseError(0, None)))
Example #10
0
 def test_digit(self):
     """
     L{OMetaBase.rule_digit} matches digits.
     """
     o = OMetaBase("1a")
     v, e = o.rule_digit()
     self.assertEqual((v, e), ("1", [0, None]))
     e = self.assertRaises(ParseError, o.rule_digit)
     self.assertEqual(e, ParseError(1, expected("digit")))
Example #11
0
    def test_many(self):
        """
        L{OMetaBase.many} returns a list of parsed values and the error that
        caused the end of the loop.
        """

        data = "ooops"
        o  = OMetaBase(data)
        self.assertEqual(o.many(lambda: o.rule_exactly('o')), (['o'] * 3, ParseError(3, expected(None, 'o'))))
Example #12
0
 def test_digit(self):
     """
     L{OMetaBase.rule_digit} matches digits.
     """
     o = OMetaBase("1a")
     v, e = o.rule_digit()
     self.assertEqual((v, e), ("1", [0, None]))
     e = self.assertRaises(ParseError, o.rule_digit)
     self.assertEqual(e, ParseError(1, expected("digit")))
Example #13
0
    def test_spaces(self):
        """
        L{OMetaBase.rule_spaces} provides error information.
        """

        data = "  xyz"
        o = OMetaBase(data)
        v, e = o.rule_spaces()

        self.assertEqual(e[0], 2)
Example #14
0
    def test_letter(self):
        """
        L{OMetaBase.rule_letter} matches letters.
        """

        o = OMetaBase("a1")
        v, e = o.rule_letter()
        self.assertEqual((v, e), ("a", [0, None]))
        e = self.assertRaises(ParseError, o.rule_letter)
        self.assertEqual(e, ParseError(1, expected("letter")))
Example #15
0
 def test_end(self):
     """
     L{OMetaBase.rule_end} matches the end of input and raises L{ParseError}
     if input is left.
     """
     o = OMetaBase("abc")
     e = self.assertRaises(ParseError, o.rule_end)
     self.assertEqual(e, ParseError(1, None))
     o.many(o.rule_anything)
     self.assertEqual(o.rule_end(), (True, [3, None]))
Example #16
0
    def test_notError(self):
        """
        When L{OMetaBase._not} fails, its error contains the current input position and no error info.
        """

        data = "xy"
        o = OMetaBase(data)
        e = self.assertRaises(ParseError, o._not, lambda: o.exactly("x"))
        self.assertEqual(e[0], 1)
        self.assertEqual(e[1], None)
Example #17
0
 def test_end(self):
     """
     L{OMetaBase.rule_end} matches the end of input and raises L{ParseError}
     if input is left.
     """
     o = OMetaBase("abc")
     e = self.assertRaises(ParseError, o.rule_end)
     self.assertEqual(e, ParseError(1, None))
     o.many(o.rule_anything)
     self.assertEqual(o.rule_end(), (True, [3, None]))
Example #18
0
    def test_letter(self):
        """
        L{OMetaBase.rule_letter} matches letters.
        """

        o = OMetaBase("a1")
        v, e = o.rule_letter()
        self.assertEqual((v, e), ("a", [0, None]))
        e = self.assertRaises(ParseError, o.rule_letter)
        self.assertEqual(e, ParseError(1, expected("letter")))
Example #19
0
 def test_end(self):
     """
     L{OMetaBase.rule_end} matches the end of input and raises L{_MaybeParseError}
     if input is left.
     """
     o = OMetaBase("abc")
     try:
         o.rule_end()
     except _MaybeParseError, e:
         self.assertEqual(e, _MaybeParseError(1, None))
Example #20
0
    def test_many(self):
        """
        L{OMetaBase.many} returns a list of parsed values and the error that
        caused the end of the loop.
        """

        data = "ooops"
        o = OMetaBase(data)
        self.assertEqual(o.many(lambda: o.rule_exactly('o')),
                         (['o'] * 3, _MaybeParseError(3, expected(None, 'o'))))
Example #21
0
    def test_spaces(self):
        """
        L{OMetaBase.rule_spaces} provides error information.
        """

        data = "  xyz"
        o = OMetaBase(data)
        v, e = o.rule_spaces()

        self.assertEqual(e[0], 2)
Example #22
0
    def test_predFailure(self):
        """
        L{OMetaBase.pred} returns True and empty error info on success.
        """

        o = OMetaBase("")
        try:
            o.pred(lambda: (False, _MaybeParseError(0, None)))
        except _MaybeParseError, e:
            self.assertEqual(e, _MaybeParseError(0, None))
Example #23
0
 def test_interleave(self):
     """
     L{OMetaBase.interleave} matched randomly mixed parts
     """
     o = OMetaBase("ab")
     d = dict()
     v, e = o._interleave(d, '1', lambda: o.exactly('a'), None, '*', lambda: o.exactly('b'), 'x')
     self.assertEqual((v, e), (['a', ['b']], [1, None]))
     self.assertIn('x', d)
     self.assertEqual(d['x'], ['b'])
Example #24
0
    def test_notError(self):
        """
        When L{OMetaBase._not} fails, its error contains the current input position and no error info.
        """

        data = "xy"
        o = OMetaBase(data)
        e = self.assertRaises(ParseError, o._not, lambda: o.exactly("x"))
        self.assertEqual(e[0], 1)
        self.assertEqual(e[1], None)
Example #25
0
    def test_anything(self):
        """
        L{OMetaBase.rule_anything} returns each item from the input along with its position.
        """

        data = "foo"
        o = OMetaBase(data)

        for i, c in enumerate(data):
            v, e = o.rule_anything()
            self.assertEqual((c, i), (v, e[0]))
Example #26
0
    def test_anything(self):
        """
        L{OMetaBase.rule_anything} returns each item from the input along with its position.
        """

        data = "foo"
        o = OMetaBase(data)

        for i, c in enumerate(data):
            v, e = o.rule_anything()
            self.assertEqual((c, i), (v, e[0]))
Example #27
0
 def test_letterOrDigit(self):
     """
     L{OMetaBase.rule_letterOrDigit} matches alphanumerics.
     """
     o = OMetaBase("a1@")
     v, e = o.rule_letterOrDigit()
     self.assertEqual((v, e), ("a", [0, None]))
     v, e = o.rule_letterOrDigit()
     self.assertEqual((v, e), ("1", [1, None]))
     e = self.assertRaises(ParseError, o.rule_letterOrDigit)
     self.assertEqual(e, ParseError(2, expected("letter or digit")))
Example #28
0
 def test_letterOrDigit(self):
     """
     L{OMetaBase.rule_letterOrDigit} matches alphanumerics.
     """
     o = OMetaBase("a1@")
     v, e = o.rule_letterOrDigit()
     self.assertEqual((v, e), ("a", [0, None]))
     v, e = o.rule_letterOrDigit()
     self.assertEqual((v, e), ("1", [1, None]))
     e = self.assertRaises(ParseError, o.rule_letterOrDigit)
     self.assertEqual(e, ParseError(2, expected("letter or digit")))
Example #29
0
    def test_exactly(self):
        """
        L{OMetaBase.rule_exactly} returns the requested item from the input
        string along with its position, if it's there.
        """

        data = "foo"
        o = OMetaBase(data)
        v, e = o.rule_exactly("f")
        self.assertEqual(v, "f")
        self.assertEqual(e[0], 0)
Example #30
0
 def test_digit(self):
     """
     L{OMetaBase.rule_digit} matches digits.
     """
     o = OMetaBase("1a")
     v, e = o.rule_digit()
     self.assertEqual((v, e), ("1", [0, None]))
     try:
         o.rule_digit()
     except _MaybeParseError, e:
         self.assertEqual(e, _MaybeParseError(1, expected("digit")))
Example #31
0
    def test_exactly(self):
        """
        L{OMetaBase.rule_exactly} returns the requested item from the input
        string along with its position, if it's there.
        """

        data = "foo"
        o = OMetaBase(data)
        v, e = o.rule_exactly("f")
        self.assertEqual(v, "f")
        self.assertEqual(e[0], 0)
Example #32
0
    def test_anything(self):
        """
        L{OMetaBase.rule_anything} returns each item from the input along with its position.
        """
        #TODO test more unicode chars
        
        data = u"foo123!@£©™☺☹℅☐☑☒✓❍❖⁰¹²³⁴₀₁₂₃★☆€¥¢!¡?¿–_—·•%‰‱˚˛"
        o = OMetaBase(data)

        for i, c in enumerate(data):
            v, e = o.rule_anything()
            self.assertEqual((c, i), (v, e[0]))
Example #33
0
    def test_letter(self):
        """
        L{OMetaBase.rule_letter} matches letters.
        """

        o = OMetaBase("a1")
        v, e = o.rule_letter()
        self.assertEqual((v, e), ("a", [0, None]))
        try:
            o.rule_letter()
        except _MaybeParseError, e:
            self.assertEqual(e, _MaybeParseError(1, expected("letter")))
Example #34
0
    def test_predFailure(self):
        """
        L{OMetaBase.pred} returns True and empty error info on success.
        """

        o = OMetaBase("")
        try:
            o.pred(lambda: (False, _MaybeParseError(0, None)))
        except _MaybeParseError as e:
            self.assertEqual(e, _MaybeParseError(0, None))
        else:
            self.fail('_MaybeParseError not raised')
Example #35
0
    def test_notError(self):
        """
        When L{OMetaBase._not} fails, its error contains the current input position and no error info.
        """

        data = "xy"
        o = OMetaBase(data)
        try:
            o._not(lambda: o.exactly("x"))
        except _MaybeParseError, e:
            self.assertEqual(e[0], 1)
            self.assertEqual(e[1], None)
Example #36
0
 def test_tokenFailed(self):
     """
     On failure, L{OMetaBase.rule_token} produces an error indicating the
     position where match failure occurred and the expected character.
     """
     data = "foozle"
     o = OMetaBase(data)
     try:
         o.rule_token('fog')
     except _MaybeParseError, e:
         self.assertEqual(e[0], 2)
         self.assertEqual(e[1], expected("token", "fog"))
Example #37
0
 def test_digit(self):
     """
     L{OMetaBase.rule_digit} matches digits.
     """
     o = OMetaBase("1a")
     v, e = o.rule_digit()
     self.assertEqual((v, e), ("1", [0, None]))
     try:
         o.rule_digit()
     except _MaybeParseError as e:
         self.assertEqual(e, _MaybeParseError(1, expected("digit")))
     else:
         self.fail('_MaybeParseError not raised')
Example #38
0
 def test_letterOrDigit(self):
     """
     L{OMetaBase.rule_letterOrDigit} matches alphanumerics.
     """
     o = OMetaBase("a1@")
     v, e = o.rule_letterOrDigit()
     self.assertEqual((v, e), ("a", [0, None]))
     v, e = o.rule_letterOrDigit()
     self.assertEqual((v, e), ("1", [1, None]))
     try:
         o.rule_letterOrDigit()
     except _MaybeParseError, e:
         self.assertEqual(e, _MaybeParseError(2, expected("letter or digit")))
Example #39
0
    def test_orErrorTie(self):
        """
        When branches of L{OMetaBase._or} produce errors that tie for rightmost
        position, they are merged.
        """

        data = "foozle"
        o = OMetaBase(data)

        v, e = o._or([lambda: o.token("fog"),
                      lambda: o.token("foz"),
                      lambda: o.token("f")])
        self.assertEqual(e[0], 2)
        self.assertEqual(e[1], [expected("token", "fog")[0], expected("token", "foz")[0]])
Example #40
0
    def test_token(self):
        """
        L{OMetaBase.rule_token} matches all the characters in the given string
        plus any preceding whitespace.
        """

        data = "  foo bar"
        o = OMetaBase(data)
        v, e = o.rule_token("foo")
        self.assertEqual(v, "foo")
        self.assertEqual(e[0], 4)
        v, e = o.rule_token("bar")
        self.assertEqual(v, "bar")
        self.assertEqual(e[0], 8)
Example #41
0
 def TODO_test_keywordFailed(self):
     """
     On failure, L{OMetaBase.rule_keyword} produces an error indicating the
     position where match failure occurred and the expected character.
     """
     data = "foozle"
     o = OMetaBase(data)
     try:
         o.rule_keyword('foo')
     except _MaybeParseError, e:
         self.assertEqual(e[0], 4)
         self.assertEqual(e[1], expected("keyword", "foo"))
         self.assertEqual(o.input.position, 0)
         self.assertEqual(o.input.head()[0], 'f')
Example #42
0
    def test_orErrorTie(self):
        """
        When branches of L{OMetaBase._or} produce errors that tie for rightmost
        position, they are merged.
        """

        data = "foozle"
        o = OMetaBase(data)

        v, e = o._or([lambda: o.token("fog"),
                      lambda: o.token("foz"),
                      lambda: o.token("f")])
        self.assertEqual(e[0], 2)
        self.assertEqual(e[1], [expected("token", "fog")[0], expected("token", "foz")[0]])
Example #43
0
    def test_orFalseSuccess(self):
        """
        When a failing branch of L{OMetaBase._or} gets further than a
        succeeding one, its error is returned instead of the success branch's.
        """

        data = "foozle"
        o = OMetaBase(data)

        v, e = o._or([lambda: o.token("fog"),
                      lambda: o.token("foozik"),
                      lambda: o.token("f")])
        self.assertEqual(e[0], 4)
        self.assertEqual(e[1], expected("token", "foozik"))
Example #44
0
    def test_letter(self):
        """
        L{OMetaBase.rule_letter} matches letters.
        """

        o = OMetaBase("a1")
        v, e = o.rule_letter()
        self.assertEqual((v, e), ("a", [0, None]))
        try:
            o.rule_letter()
        except _MaybeParseError as e:
            self.assertEqual(e, _MaybeParseError(1, expected("letter")))
        else:
            self.fail('_MaybeParseError not raised')
Example #45
0
 def test_tokenFailed(self):
     """
     On failure, L{OMetaBase.rule_token} produces an error indicating the
     position where match failure occurred and the expected character.
     """
     data = "foozle"
     o = OMetaBase(data)
     try:
         o.rule_token('fog')
     except _MaybeParseError as e:
         self.assertEqual(e[0], 2)
         self.assertEqual(e[1], expected("token", "fog"))
     else:
         self.fail('_MaybeParseError not raised')
Example #46
0
    def test_token(self):
        """
        L{OMetaBase.rule_token} matches all the characters in the given string
        plus any preceding whitespace.
        """

        data = "  foo bar"
        o = OMetaBase(data)
        v, e = o.rule_token("foo")
        self.assertEqual(v, "foo")
        self.assertEqual(e[0], 4)
        v, e = o.rule_token("bar")
        self.assertEqual(v, "bar")
        self.assertEqual(e[0], 8)
Example #47
0
    def test_exactlyFail(self):
        """
        L{OMetaBase.rule_exactly} raises L{_MaybeParseError} when the requested item
        doesn't match the input. The error contains info on what was expected
        and the position.
        """

        data = "foo"
        o = OMetaBase(data)
        try:
            o.rule_exactly("g")
        except _MaybeParseError, e:
            self.assertEquals(e[1], expected(None, "g"))
            self.assertEquals(e[0], 0)
Example #48
0
    def test_notError(self):
        """
        When L{OMetaBase._not} fails, its error contains the current input position
        and no error info.
        """

        data = "xy"
        o = OMetaBase(data)
        try:
            o._not(lambda: o.exactly("x"))
        except _MaybeParseError as e:
            self.assertEqual(e[0], 1)
            self.assertEqual(e[1], None)
        else:
            self.fail('_MaybeParseError not raised')
Example #49
0
    def test_orSimpleFailure(self):
        """
        When none of the alternatives passed to L{OMetaBase._or} succeed, the
        one that got the furthest is returned.
        """

        data = "foozle"
        o = OMetaBase(data)

        e = self.assertRaises(ParseError, o._or,
                              [lambda: o.token("fog"),
                               lambda: o.token("foozik"),
                               lambda: o.token("woozle")])
        self.assertEqual(e[0], 4)
        self.assertEqual(e[1], expected("token",  "foozik"))
Example #50
0
    def test_orSimpleFailure(self):
        """
        When none of the alternatives passed to L{OMetaBase._or} succeed, the
        one that got the furthest is returned.
        """

        data = "foozle"
        o = OMetaBase(data)

        e = self.assertRaises(ParseError, o._or, [
            lambda: o.token("fog"), lambda: o.token("foozik"),
            lambda: o.token("woozle")
        ])
        self.assertEqual(e[0], 4)
        self.assertEqual(e[1], expected("token", "foozik"))
Example #51
0
    def test_exactlyFail(self):
        """
        L{OMetaBase.rule_exactly} raises L{_MaybeParseError} when the requested item
        doesn't match the input. The error contains info on what was expected
        and the position.
        """

        data = "foo"
        o = OMetaBase(data)
        try:
            o.rule_exactly("g")
        except _MaybeParseError as e:
            self.assertEqual(e[1], expected(None, "g"))
            self.assertEqual(e[0], 0)
        else:
            self.fail('_MaybeParseError not raised')
Example #52
0
    def test_predFailure(self):
        """
        L{OMetaBase.pred} returns True and empty error info on success.
        """

        o = OMetaBase("")
        e = self.assertRaises(ParseError, o.pred, lambda:
                              (False, ParseError(0, None)))
        self.assertEqual(e, ParseError(0, None))
Example #53
0
    def test_expressions(self):
        """
        L{OMeta.pythonExpr()} can recognize various paired delimiters properly
        and include newlines in expressions where appropriate.
        """
        self.findInGrammar("x")
        self.findInGrammar("(x + 1)")
        self.findInGrammar("{x: (y)}")
        self.findInGrammar("x, '('")
        self.findInGrammar('x, "("')
        self.findInGrammar('x, """("""')
        self.findInGrammar('(x +\n 1)')
        self.findInGrammar('[x, "]",\n 1]')
        self.findInGrammar('{x: "]",\ny: "["}')

        o = OMetaBase("foo(x[1]])\nbaz = ...\n")
        self.assertRaises(_MaybeParseError, o.pythonExpr)
        o = OMetaBase("foo(x[1]\nbaz = ...\n")
        self.assertRaises(_MaybeParseError, o.pythonExpr)
Example #54
0
 def test_tokenFailed(self):
     """
     On failure, L{OMetaBase.rule_token} produces an error indicating the
     position where match failure occurred and the expected character.
     """
     data = "foozle"
     o = OMetaBase(data)
     e = self.assertRaises(ParseError, o.rule_token, "fog")
     self.assertEqual(e[0], 2)
     self.assertEqual(e[1], expected("token", "fog"))
Example #55
0
    def test_exactlyFail(self):
        """
        L{OMetaBase.rule_exactly} raises L{ParseError} when the requested item
        doesn't match the input. The error contains info on what was expected
        and the position.
        """

        data = "foo"
        o = OMetaBase(data)
        e = self.assertRaises(ParseError, o.rule_exactly, "g")
        self.assertEquals(e[1], expected(None, "g"))
        self.assertEquals(e[0], 0)
Example #56
0
    def test_or(self):
        """
        L{OMetaBase._or} returns the result of the first of its arguments to succeed.
        """

        data = "a"

        o = OMetaBase(data)
        called = [False, False, False]
        targets = ['b', 'a', 'c']
        matchers = []
        for i, m in enumerate(targets):
            def match(i=i, m=m):
                called[i] = True
                return o.exactly(m)
            matchers.append(match)

        v, e = o._or(matchers)
        self.assertEqual(called, [True, True, False])
        self.assertEqual(v, 'a')
        self.assertEqual(e[0], 0)
Example #57
0
 def test_end(self):
     """
     L{OMetaBase.rule_end} matches the end of input and raises L{_MaybeParseError}
     if input is left.
     """
     o = OMetaBase("abc")
     try:
         o.rule_end()
     except _MaybeParseError as e:
         self.assertEqual(e, _MaybeParseError(1, None))
     else:
         self.fail('_MaybeParseError not raised')
     o.many(o.rule_anything)
     self.assertEqual(o.rule_end(), (True, _MaybeParseError(3, None)))
Example #58
0
    def test_orFalseSuccess(self):
        """
        When a failing branch of L{OMetaBase._or} gets further than a
        succeeding one, its error is returned instead of the success branch's.
        """

        data = "foozle"
        o = OMetaBase(data)

        v, e = o._or([lambda: o.token("fog"),
                      lambda: o.token("foozik"),
                      lambda: o.token("f")])
        self.assertEqual(e[0], 4)
        self.assertEqual(e[1], expected("token", "foozik"))
Example #59
0
 def test_letterOrDigit(self):
     """
     L{OMetaBase.rule_letterOrDigit} matches alphanumerics.
     """
     o = OMetaBase("a1@")
     v, e = o.rule_letterOrDigit()
     self.assertEqual((v, e), ("a", [0, None]))
     v, e = o.rule_letterOrDigit()
     self.assertEqual((v, e), ("1", [1, None]))
     try:
         o.rule_letterOrDigit()
     except _MaybeParseError as e:
         self.assertEqual(e, _MaybeParseError(2, expected("letter or digit")))
     else:
         self.fail('_MaybeParseError not raised')
Example #60
0
    def test_orSimpleFailure(self):
        """
        When none of the alternatives passed to L{OMetaBase._or} succeed, the
        one that got the furthest is returned.
        """

        data = "foozle"
        o = OMetaBase(data)

        try:
            o._or([lambda: o.token("fog"),
                   lambda: o.token("foozik"),
                   lambda: o.token("woozle")])
        except _MaybeParseError as e:
            self.assertEqual(e[0], 4)
            self.assertEqual(e[1], expected("token", "foozik"))
        else:
            self.fail('_MaybeParseError not raised')