Example #1
0
def test_multiple_subexpression():
    assert assemble(Multiple(0, 1, True, Literal('abc'))) == '(?:abc)?'
    assert assemble(Multiple(0, None, True, Literal('abc'))) == '(?:abc)*'
    assert assemble(Multiple(1, None, True, Literal('abc'))) == '(?:abc)+'
    assert assemble(Multiple(0, 1, True,
                             Multiple(2, 3, True,
                                      Literal('a')))) == '(?:a{2,3})?'
Example #2
0
def test_capture():
    assert assemble(Capture(None, DIGIT)) == r'(\d)'
    assert assemble(
        Concat([
            Literal('No. '),
            Capture('number', Multiple(1, None, True, DIGIT))
        ])) == r'No\.\ (?P<number>\d+)'
Example #3
0
File: test_asm.py Project: cben/re2
def test_boundary():
    assert assemble(Boundary(r'\b', r'\B')) == r'\b'
    assert Boundary(r'\b', r'\B').invert() == Boundary(r'\B', r'\b')
    with pytest.raises(ValueError): Boundary(r'\A', None).invert()
    assert assemble(START_LINE) == r'^'
    assert assemble(START_STRING) == r'\A'
    assert assemble(WORD_BOUNDARY) == r'\b'
Example #4
0
def test_boundary():
    assert assemble(Boundary(r'\b', r'\B')) == r'\b'
    assert Boundary(r'\b', r'\B').invert() == Boundary(r'\B', r'\b')
    with pytest.raises(ValueError):
        Boundary(r'\A', None).invert()
    assert assemble(START_LINE) == r'^'
    assert assemble(START_STRING) == r'\A'
    assert assemble(WORD_BOUNDARY) == r'\b'
Example #5
0
def test_either():
    assert assemble(Either(map(Literal, 'abc'))) == 'a|b|c'
    assert assemble(Either(map(Literal, ['123', '45', '']))) == '123|45|'
    assert assemble(
        Multiple(1, None, True,
                 Either(map(Literal, ['123', '45', ''])))) == '(?:123|45|)+'
    assert assemble(
        Concat([Literal('a'),
                Either([Literal('b'), Literal('c')])])) == 'a(?:b|c)'
    assert assemble(Concat([Either([Literal('b'), Literal('c')])])) == 'b|c'
Example #6
0
def test_multiple():
    assert assemble(Multiple(0, 1, True, Literal('a'))) == 'a?'
    assert assemble(Multiple(0, None, True, Literal('a'))) == 'a*'
    assert assemble(Multiple(1, None, True, Literal('a'))) == 'a+'
    assert assemble(Multiple(2, 2, True, Literal('a'))) == 'a{2}'
    assert assemble(Multiple(None, 2, True, Literal('a'))) == 'a{,2}'
    assert assemble(Multiple(2, None, True, Literal('a'))) == 'a{2,}'
    assert assemble(Multiple(2, 5, True, Literal('a'))) == 'a{2,5}'
Example #7
0
File: test_asm.py Project: cben/re2
def test_multiple():
    assert assemble(Multiple(0, 1, True, Literal('a'))) == 'a?'
    assert assemble(Multiple(0, None, True, Literal('a'))) == 'a*'
    assert assemble(Multiple(1, None, True, Literal('a'))) == 'a+'
    assert assemble(Multiple(2, 2, True, Literal('a'))) == 'a{2}'
    assert assemble(Multiple(None, 2, True, Literal('a'))) == 'a{,2}'
    assert assemble(Multiple(2, None, True, Literal('a'))) == 'a{2,}'
    assert assemble(Multiple(2, 5, True, Literal('a'))) == 'a{2,5}'
Example #8
0
def re(re2):
    try:
        ast = parser.parse(re2)
    except ParsimoniousParseError:
        # we want to raise the nice parsimonious ParseError with all the explanation,
        # but we also want to raise something that isinstance(x, re.error)...
        # so we defined a doubly-inheriting class ParseError and we convert the exception
        # to that, taking care to keep the traceback (as described in
        # http://www.ianbicking.org/blog/2007/09/re-raising-exceptions.html)
        exc_class, exc, tb = sys.exc_info()
        exc = ParseError(exc.text, exc.pos, exc.expr)
        raise exc.__class__, exc, tb
    compiled = compiler.compile(ast)
    return asm.assemble(compiled)
Example #9
0
File: test_asm.py Project: cben/re2
def test_multiple_nongreedy():
    assert assemble(Multiple(0, 1, False, Literal('a'))) == 'a??'
    assert assemble(Multiple(0, None, False, Literal('a'))) == 'a*?'
    assert assemble(Multiple(1, None, False, Literal('a'))) == 'a+?'
    assert assemble(Multiple(2, 2, False, Literal('a'))) == 'a{2}?'
Example #10
0
def re(re2):
    return asm.assemble(compiler.compile(parser.parse(re2)))
Example #11
0
File: test_asm.py Project: cben/re2
def test_either():
    assert assemble(Either(map(Literal, 'abc'))) == 'a|b|c'
    assert assemble(Either(map(Literal, ['123', '45', '']))) == '123|45|'
Example #12
0
def test_setting():
    assert assemble(Setting('m', Literal('a'))) == '(?m)a'
    assert assemble(Multiple(0, 1, True,
                             Setting('m', Literal('ab')))) == '(?m)(?:ab)?'
Example #13
0
def test_character_class():
    assert assemble(DIGIT) == r'\d'
    assert assemble(Concat([DIGIT, DIGIT])) == r'\d\d'
    assert assemble(Concat([DIGIT, Multiple(0, None, True,
                                            DIGIT)])) == r'\d\d*'
    assert assemble(CharacterClass(['a'], False)) == r'a'
    assert assemble(CharacterClass(['a', 'b'], False)) == r'[ab]'
    assert assemble(CharacterClass(['b', 'a'], False)) == r'[ab]'
    assert assemble(CharacterClass(['a'], inverted=True)) == r'[^a]'
    assert assemble(CharacterClass(['a', 'b'], inverted=True)) == r'[^ab]'
    assert assemble(CharacterClass([['a', 'z']], False)) == r'[a-z]'
    assert assemble(CharacterClass([['a', 'z'], ['0', '5']],
                                   False)) == r'[0-5a-z]'
    assert assemble(CharacterClass([['a', 'z'], ['0', '5'], 'X'],
                                   False)) == r'[0-5Xa-z]'
    assert assemble(CharacterClass([['a', 'z']], inverted=True)) == r'[^a-z]'
    assert assemble(CharacterClass([['a', 'z'], ['0', '5']],
                                   inverted=True)) == r'[^0-5a-z]'
Example #14
0
def test_literal():
    assert assemble(Literal('abc')) == 'abc'
    assert assemble(Literal('^[a](b)$')) == r'\^\[a\]\(b\)\$'
Example #15
0
def test_either():
    assert assemble(Either(map(Literal, 'abc'))) == 'a|b|c'
    assert assemble(Either(map(Literal, ['123', '45', '']))) == '123|45|'
Example #16
0
def test_concat():
    assert assemble(Concat(map(Literal, 'abc'))) == 'abc'
    assert assemble(Concat(map(Literal, ['123', '45', '']))) == '12345'
    assert assemble(
        Concat([Literal('123'),
                Multiple(0, 1, True, Literal('abc'))])) == '123(?:abc)?'
Example #17
0
File: test_asm.py Project: cben/re2
def test_concat():
    assert assemble(Concat(map(Literal, 'abc'))) == 'abc'
    assert assemble(Concat(map(Literal, ['123', '45', '']))) == '12345'
    assert assemble(Concat([Literal('123'), Multiple(0, 1, True, Literal('abc'))])) == '123(?:abc)?'
Example #18
0
def test_multiple_nongreedy():
    assert assemble(Multiple(0, 1, False, Literal('a'))) == 'a??'
    assert assemble(Multiple(0, None, False, Literal('a'))) == 'a*?'
    assert assemble(Multiple(1, None, False, Literal('a'))) == 'a+?'
    assert assemble(Multiple(2, 2, False, Literal('a'))) == 'a{2}?'
Example #19
0
File: test_asm.py Project: cben/re2
def test_multiple_subexpression():
    assert assemble(Multiple(0, 1, True, Literal('abc'))) == '(?:abc)?'
    assert assemble(Multiple(0, None, True, Literal('abc'))) == '(?:abc)*'
    assert assemble(Multiple(1, None, True, Literal('abc'))) == '(?:abc)+'
    assert assemble(Multiple(0, 1, True, Multiple(2, 3, True, Literal('a')))) == '(?:a{2,3})?'
Example #20
0
File: test_asm.py Project: cben/re2
def test_character_class():
    assert assemble(DIGIT) == r'\d'
    assert assemble(Concat([DIGIT, DIGIT])) == r'\d\d'
    assert assemble(Concat([DIGIT, Multiple(0, None, True, DIGIT)])) == r'\d\d*'
    assert assemble(CharacterClass(['a'], False)) == r'a'
    assert assemble(CharacterClass(['a', 'b'], False)) == r'[ab]'
    assert assemble(CharacterClass(['b', 'a'], False)) == r'[ab]'
    assert assemble(CharacterClass(['a'], inverted=True)) == r'[^a]'
    assert assemble(CharacterClass(['a', 'b'], inverted=True)) == r'[^ab]'
    assert assemble(CharacterClass([['a', 'z']], False)) == r'[a-z]'
    assert assemble(CharacterClass([['a', 'z'], ['0', '5']], False)) == r'[0-5a-z]'
    assert assemble(CharacterClass([['a', 'z'], ['0', '5'], 'X'], False)) == r'[0-5Xa-z]'
    assert assemble(CharacterClass([['a', 'z']], inverted=True)) == r'[^a-z]'
    assert assemble(CharacterClass([['a', 'z'], ['0', '5']], inverted=True)) == r'[^0-5a-z]'
Example #21
0
File: test_asm.py Project: cben/re2
def test_capture():
    assert assemble(Capture(None, DIGIT)) == r'(\d)'
    assert assemble(Concat([Literal('No. '), Capture('number', Multiple(1, None, True, DIGIT))])) == r'No\.\ (?P<number>\d+)'
Example #22
0
File: test_asm.py Project: cben/re2
def test_literal():
    assert assemble(Literal('abc')) == 'abc'
    assert assemble(Literal('^[a](b)$')) == r'\^\[a\]\(b\)\$'
Example #23
0
File: test_asm.py Project: cben/re2
def test_setting():
    assert assemble(Setting('m', Literal('a'))) == '(?m)a'
    assert assemble(Multiple(0, 1, True, Setting('m', Literal('ab')))) == '(?m)(?:ab)?'