Esempio n. 1
0
    def test_prio(self):
        k_hi = Keyword('hi')
        k_bye = Keyword('bye')
        k_and = Keyword('and')
        k_or = Keyword('or')
        prio = Prio(
            k_hi,
            k_bye,
            Sequence('(', THIS, ')'),
            Sequence(THIS, k_and, THIS),
            Sequence(THIS, k_or, THIS),
        )
        grammar = create_grammar(prio)

        self.assertTrue(grammar.parse('hi').is_valid)
        self.assertTrue(grammar.parse('(bye)').is_valid)
        self.assertTrue(grammar.parse('(hi and bye)').is_valid)
        self.assertTrue(grammar.parse('(hi or hi) and (hi or hi)').is_valid)
        self.assertTrue(grammar.parse('(hi or (hi and bye))').is_valid)
        self.assertFalse(grammar.parse('').is_valid)
        self.assertFalse(grammar.parse('(hi').is_valid)
        self.assertFalse(grammar.parse('()').is_valid)
        self.assertFalse(grammar.parse('(hi or hi) and').is_valid)
        with self.assertRaises(MaxRecursionError):
            grammar.parse(
                '(((((((((((((((((((((((((((((((((((((((((((((((((((hi'
                ')))))))))))))))))))))))))))))))))))))))))))))))))))')
Esempio n. 2
0
    def test_token(self):
        dot = Token('.')
        grammar = create_grammar(dot)

        self.assertTrue(grammar.parse('.').is_valid)
        self.assertFalse(grammar.parse('..').is_valid)
        self.assertFalse(grammar.parse('').is_valid)
        self.assertEqual(str(grammar.parse('')),
                         'error at position 0, expecting: .')
Esempio n. 3
0
    def test_keyword_ign_case(self):
        k_hi = Keyword('hi', ign_case=True)
        grammar = create_grammar(k_hi)

        self.assertTrue(k_hi.ign_case)
        self.assertTrue(grammar.parse('hi').is_valid)
        self.assertTrue(grammar.parse('Hi').is_valid)
        self.assertFalse(grammar.parse('hello').is_valid)
        self.assertEqual('error at position 2, expecting: end_of_statement',
                         str(grammar.parse('Hi Iris')))
Esempio n. 4
0
    def test_sequence(self):
        k_hi = Keyword('hi')
        k_iris = Keyword('iris')
        seq = Sequence(k_hi, k_iris)
        grammar = create_grammar(seq)

        self.assertTrue(grammar.parse('hi iris').is_valid)
        self.assertFalse(grammar.parse(' hi sasha ').is_valid)
        self.assertEqual(str(grammar.parse('hi sasha')),
                         'error at position 3, expecting: iris')
Esempio n. 5
0
    def test_choice_first_match(self):
        k_hi = Keyword('hi')
        k_iris = Keyword('iris')
        seq = Sequence(k_hi, k_iris)
        choice = Choice(k_hi, seq, most_greedy=False)
        grammar = create_grammar(choice)

        self.assertFalse(choice.most_greedy)
        self.assertTrue(grammar.parse('hi').is_valid)
        self.assertFalse(grammar.parse(' hi iris ').is_valid)
        self.assertFalse(grammar.parse(' hi sasha ').is_valid)
Esempio n. 6
0
    def test_keyword(self):
        k_hi = Keyword('hi')
        grammar = create_grammar(k_hi)

        self.assertFalse(k_hi.ign_case)
        self.assertTrue(grammar.parse('hi').is_valid)
        self.assertTrue(grammar.parse(' hi ').is_valid)
        self.assertFalse(grammar.parse('Hi').is_valid)
        self.assertFalse(grammar.parse('hello').is_valid)
        self.assertEqual('error at position 0, expecting: hi',
                         str(grammar.parse('hello')))
        self.assertEqual('parsed successfully', str(grammar.parse('hi')))
Esempio n. 7
0
    def test_ref(self):
        ref = Ref()
        k_hi = Keyword('HI')
        ref = k_hi
        grammar = create_grammar(ref)

        self.assertTrue(grammar.parse('HI').is_valid)
        self.assertFalse(grammar.parse('hi').is_valid)
        self.assertFalse(grammar.parse('').is_valid)
        self.assertEqual(
            str(grammar.parse('ha')),
            'error at position 0, expecting: HI',
        )
Esempio n. 8
0
    def test_tokens(self):
        spaced = '== != >= <= > <'
        tokens = Tokens('== > !=  <  >=  <= ')
        grammar = create_grammar(tokens)

        self.assertEqual(spaced, str(tokens))
        self.assertTrue(grammar.parse('==').is_valid)
        self.assertTrue(grammar.parse('<=').is_valid)
        self.assertTrue(grammar.parse('>').is_valid)
        self.assertFalse(grammar.parse('').is_valid)
        self.assertFalse(grammar.parse('=').is_valid)
        self.assertEqual(str(grammar.parse('')),
                         'error at position 0, expecting: == != >= <= > <')
Esempio n. 9
0
    def test_regex(self):
        regex = Regex('^(?:\'(?:[^\']*)\')+')
        grammar = create_grammar(regex)

        self.assertTrue(grammar.parse("'hi'").is_valid)
        self.assertTrue(grammar.parse("'this is ''SiriDB'''").is_valid)
        self.assertFalse(grammar.parse("'Hi !").is_valid)
        self.assertFalse(grammar.parse("'hello''").is_valid)
        self.assertFalse(grammar.parse("").is_valid)
        self.assertEqual(
            grammar.parse('hi.').as_str(
                translate=lambda elem: 'single_quoted_string'
                if elem is regex else None),
            'error at position 0, expecting: single_quoted_string')
Esempio n. 10
0
    def test_list(self):
        k_hi = Keyword('hi')
        list_ = List(k_hi)
        grammar = create_grammar(list_)

        self.assertEqual(list_.min, 0)
        self.assertEqual(list_.max, None)
        self.assertFalse(list_.opt_closing)
        self.assertTrue(grammar.parse('hi, hi, hi').is_valid)
        self.assertTrue(grammar.parse('hi').is_valid)
        self.assertTrue(grammar.parse('').is_valid)
        self.assertFalse(grammar.parse('hi,').is_valid)
        self.assertEqual(
            str(grammar.parse('hi.')),
            'error at position 2, expecting: , or end_of_statement')
Esempio n. 11
0
    def test_optional(self):
        k_hi = Keyword('hi')
        optional = Optional(k_hi)
        grammar = create_grammar(optional)

        self.assertTrue(grammar.parse('hi').is_valid)
        self.assertTrue(grammar.parse('').is_valid)
        self.assertFalse(grammar.parse('hello').is_valid)

        self.assertEqual(
            str(grammar.parse('hello')),
            'error at position 0, expecting: end_of_statement or hi'
        )
        self.assertEqual(
            str(grammar.parse('hi hi')),
            'error at position 2, expecting: end_of_statement'
        )
Esempio n. 12
0
    def test_repeat(self):
        k_hi = Keyword('hi')
        repeat = Repeat(k_hi)
        grammar = create_grammar(repeat)

        self.assertEqual(repeat.min, 0)
        self.assertEqual(repeat.max, None)
        self.assertTrue(grammar.parse('hi hi hi').is_valid)
        self.assertTrue(grammar.parse('hi').is_valid)
        self.assertTrue(grammar.parse('').is_valid)
        self.assertFalse(grammar.parse('hihi').is_valid)
        self.assertFalse(grammar.parse('ha,').is_valid)
        self.assertEqual(
            grammar.parse('hi.').as_str(),
            'error at position 2, expecting: end_of_statement or hi')
        self.assertEqual(
            grammar.parse('hi.').as_str(translate=lambda elem: ''),
            'error at position 2')
Esempio n. 13
0
    def test_repeat_all_options(self):
        k_hi = Keyword('hi')
        repeat = Repeat(k_hi, mi=1, ma=3)
        grammar = create_grammar(repeat)

        self.assertEqual(repeat.min, 1)
        self.assertEqual(repeat.max, 3)
        self.assertTrue(grammar.parse('hi hi hi').is_valid)
        self.assertTrue(grammar.parse('hi').is_valid)
        self.assertFalse(grammar.parse('').is_valid)
        self.assertFalse(grammar.parse('hi hi hi hi').is_valid)
        self.assertEqual(
            grammar.parse('hi hi hi hi hi.').as_str(),
            'error at position 8, expecting: end_of_statement')
        self.assertEqual(
            grammar.parse('hi.').as_str(),
            'error at position 2, expecting: end_of_statement or hi')
        self.assertEqual(
            grammar.parse('').as_str(), 'error at position 0, expecting: hi')
Esempio n. 14
0
    def test_list_all_options(self):
        k_hi = Keyword('hi')
        list_ = List(k_hi, delimiter='-', mi=1, ma=3, opt=True)
        grammar = create_grammar(list_)

        self.assertEqual(list_.min, 1)
        self.assertEqual(list_.max, 3)
        self.assertTrue(list_.opt_closing)

        self.assertTrue(grammar.parse('hi - hi - hi').is_valid)
        self.assertTrue(grammar.parse('hi-hi-hi-').is_valid)
        self.assertTrue(grammar.parse('hi-').is_valid)
        self.assertTrue(grammar.parse('hi').is_valid)
        self.assertFalse(grammar.parse('').is_valid)
        self.assertFalse(grammar.parse('-').is_valid)
        self.assertFalse(grammar.parse('hi-hi-hi-hi').is_valid)
        self.assertEqual(str(grammar.parse('hi-hi-hi-hi-hi')),
                         'error at position 9, expecting: end_of_statement')
        self.assertEqual(
            str(grammar.parse('hi.')),
            'error at position 2, expecting: - or end_of_statement')
        self.assertEqual(str(grammar.parse('')),
                         'error at position 0, expecting: hi')
Esempio n. 15
0
 def test_keyword_alt_regkw(self):
     grammar = create_grammar(Choice(Keyword('hi'), Token('HI')), r'[a-z]+')
     self.assertTrue(grammar.parse('hi').is_valid)
     self.assertTrue(grammar.parse('HI').is_valid)
     self.assertFalse(grammar.parse('').is_valid)
     self.assertFalse(grammar.parse('Hi').is_valid)