Example #1
0
def test_Choice():
    assert (Choice(Literal('a'),
                   Dot()) == Def(Op.CHC, ([Literal('a'), Dot()], )))
    assert Choice('foo', 'bar') == Choice(Literal('foo'), Literal('bar'))
    # simple optimizations
    assert Choice(Dot()) == Dot()
    assert Choice(Choice('a', 'b'), 'c') == Choice('a', 'b', 'c')
Example #2
0
def test_Sequence():
    assert (Sequence(Literal('a'),
                     Dot()) == Def(Op.SEQ, ([Literal('a'), Dot()], )))
    assert Sequence('foo', 'bar') == Sequence(Literal('foo'), Literal('bar'))
    # simple optimizations
    assert Sequence(Dot()) == Dot()
    assert Sequence(Sequence('a', 'b'), 'c') == Sequence('a', 'b', 'c')
Example #3
0
from pe._py_machine import MachineParser as PyMachineParser
try:
    from pe._cy_machine import MachineParser as CyMachineParser
except ImportError:
    CyMachineParser = None

# don't reuse these in value-changing operations like Bind
abc = Cls('abc')
xyz = Cls('xyz')
abseq = Seq('a', 'b')

_blank = ((), {}, None)

data = [  # noqa: E127
    # id     definition       input,  start,end, (groups, groupdict, value)
    ('Dot0', Dot(), 'aaa', 0, 1, _blank),
    ('Dot1', Dot(), '\n', 0, 1, _blank),
    ('Dot2', Dot(), '', 0, FAIL, None),
    ('Lit0', Lit('a', ), 'a', 0, 1, _blank),
    ('Lit1', Lit('a', ), 'aa', 0, 1, _blank),
    ('Lit2', Lit('a', ), 'b', 0, FAIL, None),
    ('Lit3', Lit('a', ), 'a', 1, FAIL, None),
    ('Lit4', Lit('a', ), 'ab', 1, FAIL, None),
    ('Lit5', Lit('b', ), 'ab', 0, FAIL, None),
    ('Lit6', Lit('b', ), 'ab', 1, 2, _blank),
    ('Lit7', Lit('abc', ), 'abcabc', 0, 3, _blank),
    ('Lit8', Lit('abc', ), 'abcabc', 1, FAIL, None),
    ('Lit9', Lit('abc', ), 'abcabc', 3, 6, _blank),
    ('Cls0', Cls('ab', ), 'a', 0, 1, _blank),
    ('Cls1', Cls('ab', ), 'aa', 0, 1, _blank),
    ('Cls2', Cls('ab', ), 'b', 0, 1, _blank),
Example #4
0
def test_Bind():
    assert Bind(Dot(), name='x') == Def(Op.BND, (Dot(), 'x'))
    assert Bind('foo', name='bar') == Bind(Literal('foo'), name='bar')
Example #5
0
def test_Capture():
    assert Capture(Dot()) == Def(Op.CAP, (Dot(), ))
    assert Capture('foo') == Capture(Literal('foo'))
Example #6
0
def test_Not():
    assert Not(Dot()) == Def(Op.NOT, (Dot(), ))
    assert Not('foo') == Not(Literal('foo'))
Example #7
0
def test_And():
    assert And(Dot()) == Def(Op.AND, (Dot(), ))
    assert And('foo') == And(Literal('foo'))
Example #8
0
def test_Plus():
    assert Plus(Dot()) == Def(Op.PLS, (Dot(), ))
    assert Plus('foo') == Plus(Literal('foo'))
Example #9
0
def test_Star():
    assert Star(Dot()) == Def(Op.STR, (Dot(), ))
    assert Star('foo') == Star(Literal('foo'))
Example #10
0
def test_Optional():
    assert Optional(Dot()) == Def(Op.OPT, (Dot(), ))
    assert Optional('foo') == Optional(Literal('foo'))
Example #11
0
def test_Dot():
    assert Dot() == Def(Op.DOT, ())
Example #12
0
def test_loads_dot():
    assert eloads('.') == Dot()
    assert eloads('.  # comment') == Dot()
Example #13
0
V.Class = Sequence('[', Capture(Star(Sequence(Not(']'), V.Range))), ']',
                   V.Spacing)

# Non-recursive patterns

# V.Operator = Choice(V.LEFTARROW)
V.Special = Class('tnvfr"\'[]\\\\')
V.Oct = Class('0-7')
V.Hex = Class('0-9a-fA-F')
V.Octal = Sequence(V.Oct, Optional(V.Oct), Optional(V.Oct))
V.UTF8 = Sequence('x', *([V.Hex] * 2))
V.UTF16 = Sequence('u', *([V.Hex] * 4))
V.UTF32 = Sequence('U', *([V.Hex] * 8))
V.Char = Choice(
    Sequence('\\', Choice(V.Special, V.Octal, V.UTF8, V.UTF16, V.UTF32)),
    Sequence(Not('\\'), Dot()))
V.RangeEndWarn = Literal(']')
V.Range = Choice(Sequence(V.Char, '-', Choice(V.RangeEndWarn, V.Char)), V.Char)
V.IdentStart = Class('a-zA-Z_')
V.IdentCont = Class('a-zA-Z_0-9')
V.Identifier = Sequence(Capture(Sequence(V.IdentStart, Star(V.IdentCont))),
                        V.Spacing)

# Tokens

V.LEFTARROW = Sequence('<-', V.Spacing)
V.SLASH = Sequence('/', V.Spacing)
V.AND = Sequence('&', V.Spacing)
V.NOT = Sequence('!', V.Spacing)
V.TILDE = Sequence('~', V.Spacing)
V.QUESTION = Sequence('?', V.Spacing)