def test_op(): assert compile(Operator('capture', Literal('Yo'))) == asm.Capture( None, asm.Literal('Yo')) assert compile(Operator('0-1', Literal('Yo'))) == asm.Multiple( 0, 1, True, asm.Literal('Yo')) assert compile(Operator('1+', Literal('Yo'))) == asm.Multiple( 1, None, True, asm.Literal('Yo'))
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)])
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) ])
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(']')
import re from re2.parser import Concat, Either, Def, Operator, Macro, Literal, Nothing from re2 import asm EMPTY = asm.Literal('') EMPTY_CONCAT = asm.Concat([]) class CompileError(Exception): pass builtin_macros = { '#any': asm.ANY, '#linefeed': asm.LINEFEED, '#carriage_return': asm.CARRIAGE_RETURN, '#windows_newline': asm.Literal('\r\n'), '#tab': asm.TAB, '#digit': asm.DIGIT, '#letter': asm.LETTER, '#lowercase': asm.LOWERCASE, '#uppercase': asm.UPPERCASE, '#space': asm.SPACE, '#word_character': asm.WORD_CHARACTER, '#start_string': asm.START_STRING, '#end_string': asm.END_STRING, '#start_line': asm.START_LINE, '#end_line': asm.END_LINE, '#word_boundary': asm.WORD_BOUNDARY, '#quote': asm.Literal("'"), '#double_quote': asm.Literal('"'), }
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')]))
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')
def test_nothing(): assert compile(Nothing()) == asm.Literal('') assert compile(Concat([Nothing(), Nothing()])) == asm.Literal('')
def test_either(): assert compile(Either([Literal('abc'), Literal('def')])) == asm.Either( [asm.Literal('abc'), asm.Literal('def')])
def test_concat(): assert compile(Concat(map(Literal, 'abc'))) == asm.Concat( [asm.Literal('a'), asm.Literal('b'), asm.Literal('c')])
def test_literal(): assert compile(Literal('abc')) == asm.Literal('abc')
import re from re2.parser import Parser, Concat, Either, Def, Operator, Macro, Range, Literal, Nothing from re2 import asm from re2.errors import CompileError parser = Parser() EMPTY = asm.Literal('') EMPTY_CONCAT = asm.Concat([]) builtin_macros = { '#any': asm.ANY, '#linefeed': asm.LINEFEED, '#carriage_return': asm.CARRIAGE_RETURN, '#windows_newline': asm.Literal('\r\n'), '#tab': asm.TAB, '#digit': asm.DIGIT, '#letter': asm.LETTER, '#lowercase': asm.LOWERCASE, '#uppercase': asm.UPPERCASE, '#space': asm.SPACE, '#token_character': asm.TOKEN_CHARACTER, '#start_string': asm.START_STRING, '#end_string': asm.END_STRING, '#start_line': asm.START_LINE, '#end_line': asm.END_LINE, '#word_boundary': asm.WORD_BOUNDARY, '#quote': asm.Literal("'"), '#double_quote': asm.Literal('"'), '#left_brace': asm.Literal('['),