예제 #1
0
    def _assemble(self)->nfa.EpsilonNFA:
        if isinstance(self._item, str):
            item_enfa = nfa.EpsilonNFA.factory(
                """ks0,ks1
{0}
ks1
ks0
ks0,{0}->ks1""".format(*helper.escape_string(self._item)),
            generator.StandardFormatGenerator())
        elif isinstance(self._item, Operator):
            item_enfa = self._item._assemble()
        return item_enfa.kleene_operator()
예제 #2
0
    def _assemble(self)->nfa.EpsilonNFA:
        if isinstance(self._item, str):
            enfa = nfa.EpsilonNFA.factory(
                """s0,s1
{0}
s1
s0
s0,{0}->s1""".format(*helper.escape_string(self._item)),
            generator.StandardFormatGenerator())
        elif isinstance(self._item, Operator):
            enfa = self._item._assemble()
        return enfa
예제 #3
0
    def _assemble(self)->nfa.EpsilonNFA:
        if isinstance(self._item, str):
            # todo: don't call it through this.
            enfa = nfa.EpsilonNFA.factory(
                """kp0,kp1
{0}
kp1
kp0
kp0,{0}->kp1""".format(*helper.escape_string(self._item)),
            generator.StandardFormatGenerator())
        elif isinstance(self._item, Operator):
            enfa = self._item._assemble()
        # else is not needed because OperatorInputTypeError would already have been raised
        # if item is not a string or an Operator.
        return enfa.deepcopy() * enfa.kleene_operator()
예제 #4
0
    def _assemble(self)->nfa.EpsilonNFA:
        if isinstance(self._item, str):
            item_enfa = nfa.EpsilonNFA.factory(
                """qm0,qm1
{0}
qm1
qm0
qm0,{0}->qm1
qm0,$->qm1""".format(*helper.escape_string(self._item)),
            generator.StandardFormatGenerator())
        elif isinstance(self._item, Operator):
            start_enfa = nfa.EpsilonNFA.factory(
                """qm\n\nqm\nqm\n""",
            generator.StandardFormatGenerator())
            end_enfa = start_enfa.deepcopy()
            item_enfa = start_enfa*end_enfa + self._item._assemble()
        return item_enfa
예제 #5
0
    def _assemble(self)->nfa.EpsilonNFA:
        result = ''
        inputs = ''
        for char in self.all_characters:
            result += 'c0,{}->c1\n'.format(*helper.escape_string(char))
            inputs += char + ','
        result = result[:-1]
        inputs = inputs[:-1]

        enfa = nfa.EpsilonNFA.factory(
            """c0,c1
{}
c1
c0
{}""".format(inputs, result),
        generator.StandardFormatGenerator())
        return enfa
예제 #6
0
    def _assemble(self)->nfa.EpsilonNFA:
        enfas = []
        for item in self._items:
            if isinstance(item, str):
                enfas.append(nfa.EpsilonNFA.factory(
                    """c0,c1
{0}
c1
c0
c0,{0}->c1""".format(*helper.escape_string(item)),
                    generator.StandardFormatGenerator()))
            elif isinstance(item, Operator):
                enfas.append(item._assemble())

        result = enfas[0]
        for i in range(1, len(enfas)):
            result = result * enfas[i]
        return result
예제 #7
0
 def test_escaping(self):
     self.assertEqual(helper.escape_string('test'), ['\\t\\e\\s\\t'])
     self.assertEqual(helper.de_escape_string('\\t\\e\\st'), ['test'])