Example #1
0
    def format(self, code_line):

        if code_line.startswith(self.COMMENT_SYMBOL):  # Comment
            return code_line

        statement = ""

        for idx, symbol in enumerate(code_line):

            if symbol.isdigit() or symbol.isalpha():
                statement += symbol

                char = 1
                while idx + char < len(code_line) and code_line[idx +
                                                                char] == " ":
                    self.skip_token(code_line[idx + char], " ")
                    char += 1

                    if any((code_line[idx + char].isdigit(),
                            code_line[idx + char].isalpha())):
                        raise SyntaxException(
                            "Spaces in Identifier and between numbers not allowed"
                        )

            elif symbol == " ":
                continue
            else:
                statement += symbol

        self.check_formatted_expression(statement)

        return statement
Example #2
0
    def check_formatted_expression(cls, expression):

        for idx in range(len(expression)):
            if expression[idx] == ",":
                try:
                    if not all((expression[idx - 1].isdigit(),
                                expression[idx + 1].isdigit())):
                        raise SyntaxException("Missing Number in set")
                except Exception as e:
                    raise SyntaxException(str(e))
            elif expression[idx] == '0':
                if all((
                        not expression[idx - 1].isdigit(),
                        expression[idx + 1].isdigit(),
                )):
                    raise SyntaxException("Invalid number given")
Example #3
0
    def parse_identifier(cls, identifier):
        identifier = Identifier(identifier)

        if not identifier.is_valid():
            raise SyntaxException("Identifier has not valid format.")

        return identifier
Example #4
0
    def parse_factor(self, factor) -> set:
        result, open_complex_factors, set_values = set(), 0, ""

        idx, iter_factor = 0, iter(factor)

        while True:
            symbol = next(iter_factor, None)
            idx += 1

            if symbol is None:
                break

            if symbol == "(":
                open_complex_factors += 1

                while open_complex_factors != 0:
                    symbol = next(iter_factor, None)
                    if symbol == "(":
                        open_complex_factors += 1
                        set_values += symbol
                    elif symbol == ")":
                        open_complex_factors -= 1

                        if open_complex_factors == 0:
                            symbol = next(iter_factor, None)
                            if symbol is not None:
                                raise SyntaxException("Invalid token detected")
                        else:
                            set_values += symbol
                    else:
                        if symbol is None:  # ( not closed
                            raise SyntaxException("Invalid token detected")
                        set_values += symbol

                # print(set_values)
                result = self.parse_expression(set_values)

            elif symbol.isalpha():
                set_values += symbol

                while True:
                    symbol = next(iter_factor, None)
                    if symbol and (symbol.isdigit() or symbol.isalpha()):
                        set_values += symbol
                    else:
                        break

                identifier = self.parse_identifier(set_values)
                if self._set_collection.get(identifier):
                    result = self._set_collection[identifier]
                else:
                    raise SyntaxException(
                        "Identifier '{}' does not correspond to a Set".format(
                            str(identifier)))

            elif symbol == "{":
                symbol = next(iter_factor, None)

                if symbol == ",":
                    raise SyntaxException("Number missing in set")

                while True:
                    if symbol and symbol != "}":
                        set_values += symbol
                        symbol = next(iter_factor, None)
                    else:
                        break

                if symbol != "}":
                    raise SyntaxException("Invalid token in set")

                if iter_factor.__length_hint__():
                    raise SyntaxException("Operator or end of line missing")

                result = self.parse_set(set_values)
            else:
                raise SyntaxException("Invalid statement detected")

        if open_complex_factors != 0:
            raise SyntaxException("Missing parenthesis detected")

        return result
Example #5
0
 def skip_token(cls, _input, char):
     if not _input.startswith(char):
         raise SyntaxException("Missing token : {}".format(char))
Example #6
0
 def parse_set(cls, set_values):
     set_values = set_values.split(",")
     if not all(map(lambda x: x.isdigit(), set_values)):
         raise SyntaxException("Only digits in set allowed")
     return set(map(int, set_values))