Пример #1
0
    def analyzer_function_get(self, RequiredLocalVariablesDB):
        routed_address_set = set([])
        function_body = []

        # (*) Core State Machine
        #     All pattern detectors combined in single forward analyzer
        dsm = StateMachineDecorator(self.sm,
                                    self.state_machine_name,
                                    self.post_contexted_sm_id_list,
                                    BackwardLexingF=False,
                                    BackwardInputPositionDetectionF=False)

        variable_db.init(RequiredLocalVariablesDB)
        init_address_handling(dsm.get_direct_transition_to_terminal_db())

        # (*) Pre Context State Machine
        #     All pre-context combined in single backward analyzer.
        if self.pre_context_sm_list != []:
            code = self.__backward_analyzer()
            function_body.extend(code)

        # -- now, consider core state machine
        code = self.__forward_analyzer(dsm)
        function_body.extend(code)

        # At this point in time, the function body is completely defined
        routed_address_set = get_address_set_subject_to_routing()

        if len(routed_address_set) != 0 or is_label_referenced(
                "$state-router"):
            routed_state_info_list = state_router.get_info(
                routed_address_set, dsm)
            function_body.append(state_router.do(routed_state_info_list))
            variable_db.require("target_state_index",
                                Condition_ComputedGoto=False)

        if is_label_referenced("$reload-FORWARD") or is_label_referenced(
                "$reload-BACKWARD"):
            variable_db.require("target_state_else_index")
            variable_db.require("target_state_index")

        # Following function refers to the global 'variable_db'
        variable_definitions = self.language_db["$variable-definitions"](
            self.post_contexted_sm_id_list, self.pre_context_sm_id_list,
            self.language_db)

        # (*) Pack Pre-Context and Core State Machine into a single function
        analyzer_function = self.language_db["$analyzer-func"](
            self.state_machine_name,
            self.analyzer_state_class_name,
            self.stand_alone_analyzer_f,
            variable_definitions,
            function_body,
            self.mode_name_list,
            LanguageDB=self.language_db)

        return get_plain_strings(analyzer_function)
Пример #2
0
    def backward_detector_function_get(self, sm):
        assert sm.get_orphaned_state_index_list() == []

        dsm = StateMachineDecorator(sm, 
                                    "BACKWARD_DETECTOR_" + repr(sm.get_id()),
                                    PostContextSM_ID_List           = [], 
                                    BackwardLexingF                 = True, 
                                    BackwardInputPositionDetectionF = True)

        variable_db.init()
        init_address_handling(dsm.get_direct_transition_to_terminal_db())

        function_body = state_machine_coder.do(dsm)

        comment = []
        if Setup.comment_state_machine_transitions_f: 
            comment = Setup.language_db["$ml-comment"]("BEGIN: BACKWARD DETECTOR STATE MACHINE\n" + \
                                                       sm.get_string(NormalizeF=False)            + \
                                                       "\nEND: BACKWARD DETECTOR STATE MACHINE")
            comment.append("\n")


        # -- input position detectors simply the next 'catch' and return
        terminal = []
        terminal.append("\n")
        terminal.append("    __quex_assert_no_passage();\n")
        terminal.append(get_label("$terminal-general-bw") + ":\n")
        terminal.append("    " + self.language_db["$input/seek_position"]("end_of_core_pattern_position") + "\n")
        terminal.append("    " + self.language_db["$input/increment"] + "\n")
        terminal.append("    return;\n")

        routed_address_set = get_address_set_subject_to_routing()

        state_router_txt = ""
        if len(routed_address_set) != 0:
            routed_state_info_list = state_router.get_info(routed_address_set, dsm)
            state_router_txt       = state_router.do(routed_state_info_list)
            variable_db.require("target_state_index", Condition_ComputedGoto=False)

        variable_db.require("input")
        variable_db.require("end_of_core_pattern_position")

        local_variable_definition = self.language_db["$local-variable-defs"](variable_db.get())

        # Put all things together
        txt = []
        txt.append(bwd_prolog.replace("$$ID$$", repr(sm.get_id()).replace("L", "")))
        txt.extend(local_variable_definition)
        txt.extend(comment)
        txt.extend(function_body)
        txt.extend(terminal)
        txt.append(state_router_txt)
        txt.append(bwd_epilog.replace("$$INIT_STATE_ID$$", get_label_of_address(sm.init_state_index)))

        return get_plain_strings(txt)
Пример #3
0
    def analyzer_function_get(self, RequiredLocalVariablesDB):
        routed_address_set = set([])
        function_body      = []

        # (*) Core State Machine
        #     All pattern detectors combined in single forward analyzer
        dsm = StateMachineDecorator(self.sm, 
                                    self.state_machine_name, 
                                    self.post_contexted_sm_id_list, 
                                    BackwardLexingF=False, 
                                    BackwardInputPositionDetectionF=False)

        variable_db.init(RequiredLocalVariablesDB)
        init_address_handling(dsm.get_direct_transition_to_terminal_db())

        # (*) Pre Context State Machine
        #     All pre-context combined in single backward analyzer.
        if self.pre_context_sm_list != []:
            code = self.__backward_analyzer()
            function_body.extend(code)
            
        # -- now, consider core state machine
        code = self.__forward_analyzer(dsm)
        function_body.extend(code)

        # At this point in time, the function body is completely defined
        routed_address_set = get_address_set_subject_to_routing()

        if len(routed_address_set) != 0 or is_label_referenced("$state-router"):
            routed_state_info_list = state_router.get_info(routed_address_set, dsm)
            function_body.append(state_router.do(routed_state_info_list))
            variable_db.require("target_state_index", Condition_ComputedGoto=False) 

        if is_label_referenced("$reload-FORWARD") or is_label_referenced("$reload-BACKWARD"):
            variable_db.require("target_state_else_index")
            variable_db.require("target_state_index")

        # Following function refers to the global 'variable_db'
        variable_definitions = self.language_db["$variable-definitions"](self.post_contexted_sm_id_list,
                                                                         self.pre_context_sm_id_list, 
                                                                         self.language_db)

        # (*) Pack Pre-Context and Core State Machine into a single function
        analyzer_function = self.language_db["$analyzer-func"](self.state_machine_name, 
                                                               self.analyzer_state_class_name, 
                                                               self.stand_alone_analyzer_f,
                                                               variable_definitions, 
                                                               function_body, 
                                                               self.mode_name_list, 
                                                               LanguageDB=self.language_db)

        return get_plain_strings(analyzer_function)
Пример #4
0
def do(sm, LanguageDB, PrintStateMachineF):

    decorated_state_machine = StateMachineDecorator(sm, 
                                                    "BACKWARD_DETECTOR_" + repr(sm.get_id()),
                                                    PostContextSM_ID_List = [], 
                                                    BackwardLexingF=True, 
                                                    BackwardInputPositionDetectionF=True)

    function_body = state_machine_coder.do(decorated_state_machine)

    sm_str = "    " + LanguageDB["$comment"]("state machine") + "\n"
    if PrintStateMachineF: 
        sm_str += LanguageDB["$ml-comment"](sm.get_string(NormalizeF=False)) + "\n"

    # -- input position detectors simply the next 'catch' and return
    function_body += LanguageDB["$label-def"]("$terminal-general", True) + "\n"
    function_body += LanguageDB["$input/seek_position"]("end_of_core_pattern_position") + "\n"
    function_body += LanguageDB["$input/increment"] + "\n"

    variables_txt = LanguageDB["$local-variable-defs"](
            [["QUEX_CHARACTER_TYPE",          "input",                        "(QUEX_CHARACTER_TYPE)(0x0)"],
             ["QUEX_CHARACTER_POSITION_TYPE", "end_of_core_pattern_position", "(QUEX_CHARACTER_TYPE*)(0x0)"]])

    return blue_print(function_str, 
                      [["$$ID$$",              repr(sm.get_id()).replace("L", "")],
                       ["$$FUNCTION_BODY$$",   function_body],
                       ["$$LOCAL_VARIABLES$$", variables_txt],
                       ["$$STATE_MACHINE$$",   sm_str],
                      ])
Пример #5
0
    def __get_combined_pre_context_state_machine(self):
        LanguageDB = self.language_db

        assert self.pre_context_sm.get_orphaned_state_index_list() == []

        txt = "    " + LanguageDB["$comment"](
            "state machine for pre-condition test:") + "\n"
        if self.print_state_machine_f:
            txt += LanguageDB["$ml-comment"](
                self.pre_context_sm.get_string(NormalizeF=False)) + "\n"

        decorated_state_machine = StateMachineDecorator(
            self.pre_context_sm,
            self.state_machine_name,
            PostContextSM_ID_List=[],
            BackwardLexingF=True,
            BackwardInputPositionDetectionF=False)

        msg = state_machine_coder.do(decorated_state_machine)

        txt += msg

        txt += LanguageDB["$label-def"]("$terminal-general", True) + "\n"
        # -- set the input stream back to the real current position.
        #    during backward lexing the analyser went backwards, so it needs to be reset.
        txt += "    QuexBuffer_seek_lexeme_start(&me->buffer);\n"

        return txt
Пример #6
0
    def __get_core_state_machine(self):
        LanguageDB = self.language_db

        assert self.sm.get_orphaned_state_index_list() == []

        #  -- comment all state machine transitions
        txt = "    " + LanguageDB["$comment"]("state machine") + "\n"
        if self.print_state_machine_f:
            txt += LanguageDB["$ml-comment"](
                self.sm.get_string(NormalizeF=False)) + "\n"

        decorated_state_machine = StateMachineDecorator(
            self.sm,
            self.state_machine_name,
            self.post_contexted_sm_id_list,
            BackwardLexingF=False,
            BackwardInputPositionDetectionF=False)

        msg = state_machine_coder.do(decorated_state_machine)
        txt += msg

        #  -- terminal states: execution of pattern actions
        txt += LanguageDB["$terminal-code"](
            decorated_state_machine, self.action_db, self.default_action,
            self.end_of_stream_action, self.begin_of_line_condition_f,
            self.pre_context_sm_id_list, self.language_db)

        return txt
Пример #7
0
    def __backward_analyzer(self):
        LanguageDB = self.language_db

        assert self.pre_context_sm.get_orphaned_state_index_list() == []

        dsm = StateMachineDecorator(self.pre_context_sm,
                                    self.state_machine_name,
                                    PostContextSM_ID_List=[],
                                    BackwardLexingF=True,
                                    BackwardInputPositionDetectionF=False)

        txt = []
        if Setup.comment_state_machine_transitions_f:
            comment = LanguageDB["$ml-comment"]("BEGIN: PRE-CONTEXT STATE MACHINE\n"             + \
                                                self.pre_context_sm.get_string(NormalizeF=False) + \
                                                "END: PRE-CONTEXT STATE MACHINE")
            txt.append(comment)
            txt.append(
                "\n"
            )  # For safety: New content may have to start in a newline, e.g. "#ifdef ..."

        msg = state_machine_coder.do(dsm)
        txt.extend(msg)

        txt.append(get_label("$terminal-general-bw") + ":\n")
        # -- set the input stream back to the real current position.
        #    during backward lexing the analyzer went backwards, so it needs to be reset.
        txt.append("    QUEX_NAME(Buffer_seek_lexeme_start)(&me->buffer);\n")

        return txt
Пример #8
0
    def backward_detector_function_get(self, sm):
        assert sm.get_orphaned_state_index_list() == []

        dsm = StateMachineDecorator(sm,
                                    "BACKWARD_DETECTOR_" + repr(sm.get_id()),
                                    PostContextSM_ID_List=[],
                                    BackwardLexingF=True,
                                    BackwardInputPositionDetectionF=True)

        variable_db.init()
        init_address_handling(dsm.get_direct_transition_to_terminal_db())

        function_body = state_machine_coder.do(dsm)

        comment = []
        if Setup.comment_state_machine_transitions_f:
            comment = Setup.language_db["$ml-comment"]("BEGIN: BACKWARD DETECTOR STATE MACHINE\n" + \
                                                       sm.get_string(NormalizeF=False)            + \
                                                       "\nEND: BACKWARD DETECTOR STATE MACHINE")
            comment.append("\n")

        # -- input position detectors simply the next 'catch' and return
        terminal = []
        terminal.append("\n")
        terminal.append("    __quex_assert_no_passage();\n")
        terminal.append(get_label("$terminal-general-bw") + ":\n")
        terminal.append("    " + self.language_db["$input/seek_position"]
                        ("end_of_core_pattern_position") + "\n")
        terminal.append("    " + self.language_db["$input/increment"] + "\n")
        terminal.append("    return;\n")

        routed_address_set = get_address_set_subject_to_routing()

        state_router_txt = ""
        if len(routed_address_set) != 0:
            routed_state_info_list = state_router.get_info(
                routed_address_set, dsm)
            state_router_txt = state_router.do(routed_state_info_list)
            variable_db.require("target_state_index",
                                Condition_ComputedGoto=False)

        variable_db.require("input")
        variable_db.require("end_of_core_pattern_position")

        local_variable_definition = self.language_db["$local-variable-defs"](
            variable_db.get())

        # Put all things together
        txt = []
        txt.append(
            bwd_prolog.replace("$$ID$$",
                               repr(sm.get_id()).replace("L", "")))
        txt.extend(local_variable_definition)
        txt.extend(comment)
        txt.extend(function_body)
        txt.extend(terminal)
        txt.append(state_router_txt)
        txt.append(
            bwd_epilog.replace("$$INIT_STATE_ID$$",
                               get_label_of_address(sm.init_state_index)))

        return get_plain_strings(txt)