Exemplo n.º 1
0
    def __validate_command(value: (str, list)):
        command, params = value

        try:
            command = DataProvider.get_service_command(command)
        except KeyError:
            raise Validator.ValidationError(f"Wrong command name: `{command}`")

        if command.get_name() != Semantic.get_symbol(
                'var') and command.get_attr_count() != len(params):
            raise Validator.ValidationError(
                f"Wrong params count: found {len(params)}"
                f" instead of {command.get_attr_count()}")

        if command.get_name() == Semantic.get_symbol('import'):
            Validator.__validate_import(params[0])
            Validator.__import_module(params[0])
            return

        param_types = command.get_param_types()
        for idx, param in enumerate(params):
            try:
                if ModuleManager.module_was_loaded('CORE'):
                    if CommonValidator.looks_like_variable(param):
                        raise ValueError

                param_types[idx](param)
            except ValueError:
                if ModuleManager.module_was_loaded('CORE'):
                    if CommonValidator.looks_like_variable(param):
                        CommonValidator.check_if_variable_exists(param)
                        continue

                raise Validator.ValidationError(
                    f"Wrong param type. Param #{idx + 1}: `{param}` "
                    f"cannot be casted to {param_types[idx]}. ")
            except TypeError:
                new_params = params[idx + 1:]
                Validator.__validate_command((param, new_params))
                break

        previous_command = DataProvider.peek_command_stack()
        if DataProvider.is_closing_command(command.get_name()) and \
                not DataProvider.is_pair(previous_command,
                                 command.get_name()):
            raise Validator.ValidationError(
                f"Wrong command pair: [{previous_command}, {command.get_name()}]."
            )

        try:
            command.get_validation_func()(params)
        except TypeError as ignored:
            # some commands may have no validation function
            pass
Exemplo n.º 2
0
    def __preprocess_line(self, line, w):
        # comments
        line = line.split('#')[0]
        parts = line.strip().split()

        # no action in this string
        if len(parts) == 0:
            w.write('\n')
            return

        command, *params = parts

        if ModuleManager.module_was_loaded('CORE'):
            try:
                CommonValidator.check_if_variable_exists(command)

                leading_spaces = self.__get_leading_spaces(line)
                line = " ".join([
                    ' ' * leading_spaces,
                    Semantic.get_symbol('var'),
                    line.lstrip()
                ])

                parts = line.strip().split()
                command, *params = parts
            except Exception as ignored:
                pass

        try:
            self.__validator.validate('command', (command, params))
        except Validator.ValidationError as exception:
            raise Preprocessor.ParseException(f"Command: `{command}`. " +
                                              str(exception))
        if command == Semantic.get_symbol('var'):
            DataProvider.set_variable_value(params[0], None)
        elif DataProvider.is_opening_command(command):
            DataProvider.append_command_stack(command)
        elif DataProvider.is_closing_command(command):
            DataProvider.pop_command_stack()

        try:
            self.__command_to_func[command](line, w)
        except KeyError:
            w.write(line + '\n')