Esempio n. 1
0
def test_comment():
    assert compile(Operator('comment', Either([]))) == asm.Literal('')
    assert compile(Operator('comment', Literal('a'))) == asm.Literal('')
    assert compile(Operator('comment',
                            Either([Literal('a'),
                                    Literal('b')]))) == asm.Literal('')
    assert compile(
        Concat([
            Macro('#sl'),
            Operator('comment', Literal('yo')),
            Macro('#el')
        ])) == asm.Concat([asm.Boundary('^', None),
                           asm.Boundary('$', None)])
Esempio n. 2
0
def test_invert():
    assert compile(Operator('not',
                            Literal('a'))) == asm.CharacterClass(['a'],
                                                                 inverted=True)
    assert compile(Operator('not',
                            Either([Literal('a'), Literal('b')
                                    ]))) == asm.CharacterClass(['a', 'b'],
                                                               inverted=True)
    assert compile(Operator('not',
                            Either([Literal('a'), Macro('#d')
                                    ]))) == asm.CharacterClass(['a', r'\d'],
                                                               inverted=True)
    assert compile(Operator('not', Either(
        [Literal('a'),
         Macro('#l')]))) == asm.CharacterClass(['a', ['a', 'z'], ['A', 'Z']],
                                               inverted=True)
Esempio n. 3
0
def test_character_class():
    assert compile(Either([Literal('a'),
                           Literal('b')])) == asm.CharacterClass(['a', 'b'],
                                                                 False)
    assert compile(Operator('not',
                            Either([Literal('a'), Literal('b')
                                    ]))) == asm.CharacterClass(['a', 'b'],
                                                               inverted=True)
    with pytest.raises(CompileError):
        compile(Operator('not', Either([Literal('a'), Literal('bc')])))
    assert compile(Either([Literal('a'),
                           Literal('b'),
                           Literal('0')
                           ])) == asm.CharacterClass(['a', 'b', '0'], False)
    assert compile(Either([Literal('a'),
                           Macro('#d')])) == asm.CharacterClass(['a', r'\d'],
                                                                False)
Esempio n. 4
0
def test_range_macros():
    assert compile(Range('a', 'f')) == asm.CharacterClass([('a', 'f')], False)
    assert compile(Range('B', 'Z')) == asm.CharacterClass([('B', 'Z')], False)
    assert compile(Range('2', '6')) == asm.CharacterClass([('2', '6')], False)
    assert compile(Either([Range('a', 'f'), Macro('#digit')
                           ])) == asm.CharacterClass([('a', 'f'), r'\d'],
                                                     False)
    with pytest.raises(CompileError):
        compile(Range('a', '5'))
    with pytest.raises(CompileError):
        compile(Range('a', 'F'))
    with pytest.raises(CompileError):
        compile(Range('c', 'a'))
    with pytest.raises(CompileError):
        compile(Range('a', 'a'))
    with pytest.raises(AssertionError):
        compile(Range('!', ','))
Esempio n. 5
0
def test_either():
    assert compile(Either([Literal('abc'), Literal('def')])) == asm.Either(
        [asm.Literal('abc'), asm.Literal('def')])