Beispiel #1
0
def test_recognize():
    d = DFA([{"a": 1}, {"b": 0}], [False, True])
    assert d.recognize("ababab") == 5
    assert d.recognize("c") == -1

    d = DFA([{"a": 1}, {DEFAULT: 0}], [False, True])
    assert d.recognize("a,a?ab") == 5
    assert d.recognize("c") == -1

    d = NonGreedyDFA([{"a": 1}, {"b": 0}], [False, True])
    assert d.recognize("ababab") == 1
    assert d.recognize("c") == -1

    d = NonGreedyDFA([{"a": 1}, {DEFAULT: 0}], [False, True])
    assert d.recognize("a,a?ab") == 1
    assert d.recognize("c") == -1
Beispiel #2
0
def test_nonascii():
    d = DFA([{"a": 1}, {NON_ASCII: 1}], [False, True])
    input = u"aüüüü".encode("utf-8")
    assert d.recognize(input) == len(input)
    assert d.recognize("c") == -1
    assert d.recognize("ü") == -1

    d = NonGreedyDFA([{NON_ASCII: 0, "b": 1}, {"b": 0}], [False, True])
    input = u"üübbbb".encode("utf-8")
    assert d.recognize(input) == len(u"üüb".encode("utf-8"))
    assert d.recognize("c") == -1

    pytest.raises(ValueError, DFA, [{"\x81": 2}], [True])
Beispiel #3
0
def makePyEndDFAMap():
    states = []
    single = chain(
        states, any(states, notGroupStr(states, "'\\")),
        any(
            states,
            chain(states, newArcPair(states, "\\"),
                  newArcPair(states, DEFAULT),
                  any(states, notGroupStr(states, "'\\")))),
        newArcPair(states, "'"))
    singleDFA = DFA(*nfaToDfa(states, *single))
    states = []
    double = chain(
        states, any(states, notGroupStr(states, '"\\')),
        any(
            states,
            chain(states, newArcPair(states, "\\"),
                  newArcPair(states, DEFAULT),
                  any(states, notGroupStr(states, '"\\')))),
        newArcPair(states, '"'))
    doubleDFA = DFA(*nfaToDfa(states, *double))
    states = []
    single3 = chain(
        states, any(states, notGroupStr(states, "'\\")),
        any(
            states,
            chain(
                states,
                group(
                    states,
                    chain(states, newArcPair(states, "\\"),
                          newArcPair(states, DEFAULT)),
                    chain(states, newArcPair(states, "'"),
                          notChainStr(states, "''"))),
                any(states, notGroupStr(states, "'\\")))),
        chainStr(states, "'''"))
    single3DFA = NonGreedyDFA(*nfaToDfa(states, *single3))
    states = []
    double3 = chain(
        states, any(states, notGroupStr(states, '"\\')),
        any(
            states,
            chain(
                states,
                group(
                    states,
                    chain(states, newArcPair(states, "\\"),
                          newArcPair(states, DEFAULT)),
                    chain(states, newArcPair(states, '"'),
                          notChainStr(states, '""'))),
                any(states, notGroupStr(states, '"\\')))),
        chainStr(states, '"""'))
    double3DFA = NonGreedyDFA(*nfaToDfa(states, *double3))
    map = {
        "'": singleDFA,
        '"': doubleDFA,
        "r": None,
        "R": None,
        "u": None,
        "U": None,
        "b": None,
        "B": None
    }
    for uniPrefix in (
            "",
            "u",
            "U",
            "b",
            "B",
    ):
        for rawPrefix in ("", "r", "R"):
            prefix = uniPrefix + rawPrefix
            map[prefix + "'''"] = single3DFA
            map[prefix + '"""'] = double3DFA
    return map
Beispiel #4
0
def makePyEndDFAMap():
    states = []
    single = chain(
        states, any(states, notGroupStr(states, "'\\")),
        any(
            states,
            chain(states, newArcPair(states, "\\"),
                  newArcPair(states, DEFAULT),
                  any(states, notGroupStr(states, "'\\")))),
        newArcPair(states, "'"))
    states, accepts = nfaToDfa(states, *single)
    singleDFA = DFA(states, accepts)
    states_singleDFA = states
    states = []
    double = chain(
        states, any(states, notGroupStr(states, '"\\')),
        any(
            states,
            chain(states, newArcPair(states, "\\"),
                  newArcPair(states, DEFAULT),
                  any(states, notGroupStr(states, '"\\')))),
        newArcPair(states, '"'))
    states, accepts = nfaToDfa(states, *double)
    doubleDFA = DFA(states, accepts)
    states_doubleDFA = states
    states = []
    single3 = chain(
        states, any(states, notGroupStr(states, "'\\")),
        any(
            states,
            chain(
                states,
                group(
                    states,
                    chain(states, newArcPair(states, "\\"),
                          newArcPair(states, DEFAULT)),
                    chain(states, newArcPair(states, "'"),
                          notChainStr(states, "''"))),
                any(states, notGroupStr(states, "'\\")))),
        chainStr(states, "'''"))
    states, accepts = nfaToDfa(states, *single3)
    single3DFA = NonGreedyDFA(states, accepts)
    states_single3DFA = states
    states = []
    double3 = chain(
        states, any(states, notGroupStr(states, '"\\')),
        any(
            states,
            chain(
                states,
                group(
                    states,
                    chain(states, newArcPair(states, "\\"),
                          newArcPair(states, DEFAULT)),
                    chain(states, newArcPair(states, '"'),
                          notChainStr(states, '""'))),
                any(states, notGroupStr(states, '"\\')))),
        chainStr(states, '"""'))
    states, accepts = nfaToDfa(states, *double3)
    double3DFA = NonGreedyDFA(states, accepts)
    states_double3DFA = states
    return {
        "'": (singleDFA, states_singleDFA),
        '"': (doubleDFA, states_doubleDFA),
        "'''": (single3DFA, states_single3DFA),
        '"""': (double3DFA, states_double3DFA)
    }