"""
Name: Automaton.py
Author: Radovan Klembara

Implementation of class representing common parts of deep pushdown automaton, parallel deep pushdown automaton amd tuple
deep pushdown automaton.
"""

from Helpers import check_empty_intersection
from Error import Error


def __split_data__(string: str) -> [str]:
    """
    Splits data on space symbol and deletes new line symbol.

    :param string: Data to be split.
    :return: List of split data.
    """
    return string\
        .replace("\n", "")\
        .split(" ")


class Automaton:
    """
    Basic deep pushdown automaton class with common attributes.
    """
    def __init__(self,
                 depth: int,
                 states: [str],
                 input_alphabet: [str],
                 pushdown_alphabet: [str],
                 start_state: str,
                 final_states: [str]):
        """
        Creates part of automaton that is common for deep pushdown automaton, parallel deep pushdown automaton and tuple
        parallel deep pushdown automaton.

        :param depth: The maximum depth of automaton.
        :param states: String that represents the list of states.
        :param input_alphabet: String that represents the list of input symbols.
        :param pushdown_alphabet: String that represents the list of pushdown symbols.
        :param start_state: String that represents the starting state.
        :param final_states: String that represents the list of final states.
        """
        self.depth = depth
        self.states = __split_data__(states)
        self.input_alphabet = __split_data__(input_alphabet)
        pushdown_alphabet = __split_data__(pushdown_alphabet)
        if not set(pushdown_alphabet).issuperset(set(self.input_alphabet)):
            print("Error: Pushdown alphabet has to be superset of input alphabet!")
            exit(Error.FORMAL)
        self.start_state = start_state.replace("\n", "")
        if check_empty_intersection(states, [start_state]):
            print("Error: Unknown start state!")
            exit(Error.FORMAL)
        self.nonterminals = list(set(pushdown_alphabet).difference(self.input_alphabet))
        self.final_states = __split_data__(final_states)
        if not set(states).issuperset(final_states):
            print("Error: Final states have to be subset of all states!")
            exit(Error.FORMAL)
