コード例 #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)])
コード例 #2
0
ファイル: test_compiler.py プロジェクト: cben/re2
def test_builtin_macros():
    assert compile(Macro('#any')) == asm.ANY
    not_linefeed = asm.CharacterClass([r'\n'], inverted=True)
    assert compile(Macro('#not_linefeed')) == not_linefeed
    assert compile(Macro('#nlf')) == not_linefeed
    assert compile(Macro('#crlf')) == asm.Literal('\r\n')
    assert compile(Concat([Macro('#sl'),
                           Literal('yo'),
                           Macro('#el')])) == asm.Concat([
                               asm.Boundary('^', None),
                               asm.Literal('yo'),
                               asm.Boundary('$', None)
                           ])
コード例 #3
0
def test_builtin_macros():
    assert compile(Macro('#any')) == asm.ANY
    not_linefeed = asm.CharacterClass([r'\n'], inverted=True)
    assert compile(Macro('#not_linefeed')) == not_linefeed
    assert compile(Macro('#not_linefeed')) == not_linefeed
    assert compile(Macro('#windows_newline')) == asm.Literal('\r\n')
    assert compile(Concat([Macro('#sl'),
                           Literal('yo'),
                           Macro('#el')])) == asm.Concat([
                               asm.Boundary('^', None),
                               asm.Literal('yo'),
                               asm.Boundary('$', None)
                           ])
    assert compile(Macro('#quote')) == asm.Literal("'")
    assert compile(Macro('#double_quote')) == asm.Literal('"')
    assert compile(Macro('#left_brace')) == asm.Literal("[")
    assert compile(Macro('#right_brace')) == asm.Literal(']')
コード例 #4
0
def test_def_scoping():
    assert compile(Concat([Concat([Macro('#x')]),
                           Def('#x', Literal('x'))])) == asm.Literal('x')
    with pytest.raises(CompileError):
        compile(Concat([Concat([Def('#x', Literal('x'))]), Macro('#x')]))
コード例 #5
0
def test_def():
    assert compile(Concat([Def('#x', Literal('x')),
                           Macro('#x')])) == asm.Literal('x')
    assert compile(Concat([Macro('#x'),
                           Def('#x', Literal('x'))])) == asm.Literal('x')
コード例 #6
0
def test_nothing():
    assert compile(Nothing()) == asm.Literal('')
    assert compile(Concat([Nothing(), Nothing()])) == asm.Literal('')
コード例 #7
0
def test_concat():
    assert compile(Concat(map(Literal, 'abc'))) == asm.Concat(
        [asm.Literal('a'),
         asm.Literal('b'),
         asm.Literal('c')])