Exemple #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
Exemple #2
0
    def __run_command(self, command, params):
        command = DataProvider.get_service_command(command)

        if ModuleManager.module_was_loaded('CORE'):
            for idx, param in enumerate(params):
                try:
                    CommonValidator.validate_name_variable(param)
                    CommonValidator.check_if_variable_exists(param)
                    variable = param[1:]
                    params[idx] = DataProvider.get_variable_value(variable)
                except Exception:
                    pass

        try:
            self.__validator.validate('command', (command.get_name(), params))
        except (Validator.ValidationError,
                CommonValidator.ValidationError) as exception:
            raise Interpreter.RunTimeError(
                f'Command: `{command.get_name()}`. ' + str(exception))

        param_types = command.get_param_types()
        for idx, param in enumerate(params):
            if inspect.isclass(param_types[idx]):
                params[idx] = param_types[idx](params[idx])
            else:
                break

        try:
            self.__validator.validate('command', (command.get_name(), params))
        except (Validator.ValidationError,
                CommonValidator.ValidationError) as exception:
            raise Interpreter.RunTimeError(
                f'Command: `{command.get_name()}`. ' + str(exception))

        if command.get_name() == Semantic.get_symbol('var'):
            new_command, *new_params = params[2:]
            self.__run_command(new_command, new_params)

            params = params[:2]
            params.append(DataProvider.get_returned_value())
            DataProvider.return_value(None)

        if command.get_name() == Semantic.get_symbol('import'):
            return

        try:
            command.get_exec_func()(*params)
        except CommonLogic.RunTimeError as exception:
            raise Interpreter.RunTimeError(f'Command: {command.get_name()}. '
                                           f'{str(exception)}')
Exemple #3
0
    def parse_file(self, filename):
        filename = copy.deepcopy(filename)
        ModuleManager.download_default_modules()

        Logger.log("Compiling...")

        try:
            filename = self.__preproccessor.preprocess_file(filename)
        except Preprocessor.ParseException as exception:
            raise Interpreter.CompilationError("Compilation error: " +
                                               str(exception))

        Logger.log("Compilation succeed. Running...")

        with open(filename) as f:
            lines = [line.rstrip() for line in f]

        while True:
            idx = DataProvider.get_current_line()
            try:
                line = lines[idx]
            except IndexError:
                break

            if len(line) == 0:
                DataProvider.inc_current_line()
                continue

            try:
                self.__parse_line(line)
            except Interpreter.RunTimeError as exception:
                raise Interpreter.RunTimeError(
                    f"Runtime error. Line: {idx + 1}. " + str(exception))

            DataProvider.inc_current_line()

        Logger.log("Program finished.")
Exemple #4
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')
Exemple #5
0
 def __reboot_core(cls):
     DataProvider.reboot()
     ModuleManager.reboot()
Exemple #6
0
 def __import_module(module):
     ModuleManager.load_module(module)
Exemple #7
0
 def __validate_import(module):
     if not module in ModuleManager.get_registered_modules():
         raise Validator.ValidationError(
             f"Module doesn't exist: `{module}`")
     pass
Exemple #8
0
class TextStyler:
    comments = []

    __just_replace = {'\n': '<br/>', ' ': '&nbsp;'}

    __word_type = {
        'command':
        sorted(ModuleManager.get_all_commands_names(),
               key=lambda x: len(x),
               reverse=True),
        'operator':
        Semantic.get_operators()
    }

    __type_color = {
        'command': 'blue',
        'operator': 'red',
        'comment': 'grey',
        'regular_word': 'black'
    }

    @classmethod
    def plain_2_rich(cls, text):
        result = copy.deepcopy(text)

        result = cls.__cut_out_comments(result)

        for old in cls.__just_replace.keys():
            new = cls.__just_replace[old]
            result = result.replace(old, new)

        for type in cls.__word_type:
            for word in cls.__word_type[type]:
                result = result.replace(word, cls.__get_colored_word(word))

        return cls.__insert_comments(result)

    @classmethod
    def rich_2_plain(cls, text):
        result = copy.deepcopy(text)

        result = result.replace('<br />', '<br/>')
        for new in cls.__just_replace.keys():
            old = cls.__just_replace[new]
            result = result.replace(old, new)

        x = cls.__clear_code(result)
        y = x[36:] + '\n'
        return y

    @classmethod
    def __cut_out_comments(cls, text):
        lines = text.split('\n')
        for idx, line in enumerate(lines):
            try:
                comment = line[line.index(Semantic.get_symbol('comment')):]
                lines[idx] = line[:line.index(Semantic.get_symbol('comment'))]
                if comment:
                    cls.comments.append((idx, comment))
            except ValueError as ignored:
                pass

        return '\n'.join(lines)

    @classmethod
    def __insert_comments(cls, text):
        lines = text.split('<br/>')
        for idx, comment in cls.comments:
            lines[idx] += (cls.__get_colored_word(comment,
                                                  cls.__type_color['comment']))

        cls.comments = []
        return '<br/>'.join(lines)

    @classmethod
    def __get_colored_word(cls, word, color=None):
        if color:
            return f'<font color=\"{color}\">{word}</font>'

        word_type = "regular_word"
        for x in cls.__word_type.keys():
            if word in cls.__word_type[x]:
                word_type = x

        color = cls.__type_color[word_type]
        return f'<font color=\"{color}\">{word}</font>'

    @classmethod
    def __clear_code(cls, rich):
        text = copy.deepcopy(rich)
        while True:
            try:
                text = text[:text.index('<')] + text[text.index('>') + 1:]
            except:
                break
        return text