Exemple #1
0
def get_transition_to_reload(StateIdx, SMD, ReturnStateIndexStr=None):
    LanguageDB = Setup.language_db

    if SMD != None and SMD.backward_lexing_f(): direction = "BACKWARD"
    else: direction = "FORWARD"

    if SMD != None and (StateIdx == SMD.sm().init_state_index
                        and SMD.forward_lexing_f()):
        return "goto __RELOAD_INIT_STATE;"

    elif SMD == None or not SMD.backward_input_position_detection_f():
        if ReturnStateIndexStr != None:
            state_reference = ReturnStateIndexStr
        else:
            state_reference = "QUEX_LABEL(%i)" % get_address(
                "$entry", StateIdx, R=True)

        # Ensure that '__STATE_ROUTER' is marked as referenced
        get_label("$state-router", U=True)

        return "QUEX_GOTO_RELOAD(%s, %s, QUEX_LABEL(%i));" \
               % (get_label("$reload-%s" % direction, U=True),
                  state_reference,
                  get_address("$drop-out", StateIdx, U=True, R=True))

    else:
        return ""
Exemple #2
0
def get_transition_to_reload(StateIdx, SMD, ReturnStateIndexStr=None):
    LanguageDB = Setup.language_db

    if SMD != None and SMD.backward_lexing_f(): direction = "BACKWARD"
    else:                                       direction = "FORWARD"

    if SMD != None and (StateIdx == SMD.sm().init_state_index and SMD.forward_lexing_f()):
        return "goto __RELOAD_INIT_STATE;" 

    elif SMD == None or not SMD.backward_input_position_detection_f():
        if ReturnStateIndexStr != None: 
            state_reference = ReturnStateIndexStr
        else:                           
            state_reference = "QUEX_LABEL(%i)" % get_address("$entry", StateIdx, R=True)

        # Ensure that '__STATE_ROUTER' is marked as referenced
        get_label("$state-router", U=True)

        return "QUEX_GOTO_RELOAD(%s, %s, QUEX_LABEL(%i));" \
               % (get_label("$reload-%s" % direction, U=True),
                  state_reference,
                  get_address("$drop-out", StateIdx, U=True, R=True)) 

    else:
        return "" 
Exemple #3
0
 def __array_to_code(Array, ComputedGotoF=False):
     txt = ["{ "]
     for index in Array:
         if index != None: elm = "QUEX_LABEL(%i)" % get_address("$entry", index, U=True, R=True)
         else:             elm = "QUEX_LABEL(%i)" % get_address("$drop-out", template_index, U=True, R=True)
         txt.append(elm + ", ")
     txt.append("}")
     return "".join(txt)
Exemple #4
0
def get_address_of_terminal(Origin):
    assert Origin.is_acceptance()
    LanguageDB = Setup.language_db
    # The seek for the end of the core pattern is part of the 'normal' terminal
    # if the terminal 'is' a post conditioned pattern acceptance.
    if Origin.post_context_id() == -1:
        return get_address("$terminal", Origin.state_machine_id)
    else:
        return get_address("$terminal-direct", Origin.state_machine_id)
Exemple #5
0
def get_address_of_terminal(Origin):
    assert Origin.is_acceptance()
    LanguageDB = Setup.language_db
    # The seek for the end of the core pattern is part of the 'normal' terminal
    # if the terminal 'is' a post conditioned pattern acceptance.
    if Origin.post_context_id() == -1:
        return get_address("$terminal", Origin.state_machine_id)
    else:
        return get_address("$terminal-direct", Origin.state_machine_id)
Exemple #6
0
 def __array_to_code(Array, ComputedGotoF=False):
     txt = ["{ "]
     for index in Array:
         if index != None:
             elm = "QUEX_LABEL(%i)" % get_address(
                 "$entry", index, U=True, R=True)
         else:
             elm = "QUEX_LABEL(%i)" % get_address(
                 "$drop-out", template_index, U=True, R=True)
         txt.append(elm + ", ")
     txt.append("}")
     return "".join(txt)
Exemple #7
0
    def __get_state_list():
        result = []
        for path in PathList:
            Sequence = path.sequence()
            for state_index, dummy in Sequence[:-1]:
                result.append("QUEX_LABEL(%i), " % get_address("$entry", state_index, R=True, U=True))

            end_state_index = Sequence[-1][0]
            # If 'end_state_index' belongs to a non-implemented dead-end-state
            # => 'get_real_address()' returns address of immediately entered terminal
            end_state_label = "QUEX_LABEL(%i), " % get_address("$entry", end_state_index, R=True, U=True)
            result.append(end_state_label)
        return result
Exemple #8
0
    def get_direct_transition_to_terminal_db(self):
        """Returns a dictionary that maps:

                state-index  -->   directly entered terminal

           where the 'state-index' is the index of a dead-end-state, i.e.
           a state that does nothing else but transiting into the terminal.
           Thus, this intermediate state can be shortcut.
        """
        result = {}
        for key, item in self.__dead_end_state_db.iteritems():
            # item[0] == False => State is a dead-end and an acceptance state
            if item[0] != False: continue
            assert len(item[1]) == 1
            result[key] = address.get_address("$terminal",
                                              item[1][0].state_machine_id)
        return result
Exemple #9
0
def get_info(StateIndexList, DSM):
    LanguageDB = Setup.language_db

    # In some strange cases, a 'dummy' state router is required so that
    # 'goto __STATE_ROUTER;' does not reference a non-existing label. Then,
    # we return an empty text array.
    if len(StateIndexList) == 0: return []

    # Make sure, that for every state the 'drop-out' state is also mentioned
    result = [None] * len(StateIndexList)
    for i, index in enumerate(StateIndexList):
        assert type(index) != str
        if index >= 0:
            # Transition to state entry
            code = "goto %s; " % get_label_of_address(index)
            result[i] = (index, code)
        else:
            # Transition to a templates 'drop-out'
            code = "goto " + get_label("$drop-out", -index) + "; "
            result[i] = (get_address("$drop-out", -index), code)
    return result
Exemple #10
0
def get_info(StateIndexList, DSM):
    LanguageDB = Setup.language_db

    # In some strange cases, a 'dummy' state router is required so that 
    # 'goto __STATE_ROUTER;' does not reference a non-existing label. Then,
    # we return an empty text array.
    if len(StateIndexList) == 0: return []

    # Make sure, that for every state the 'drop-out' state is also mentioned
    result = [None] * len(StateIndexList)
    for i, index in enumerate(StateIndexList):
        assert type(index) != str
        if index >= 0:
            # Transition to state entry
            code = "goto %s; " % get_label_of_address(index)
            result[i] = (index, code)
        else:
            # Transition to a templates 'drop-out'
            code = "goto " + get_label("$drop-out", - index) + "; "
            result[i] = (get_address("$drop-out", - index), code)
    return result
Exemple #11
0
    def _on_detection_code(Origin):
        """Store the name of the winner pattern (last_acceptance) and the position
           where it has matched (use of $input/tell_position).
        """
        if type(Origin) == type(None):
            # Case if no un-conditional acceptance occured, then register nothing
            return ""

        assert Origin.is_acceptance()

        info = [ 
           "    last_acceptance                = QUEX_LABEL(%i);\n" \
           % get_address("$terminal-direct", Origin.state_machine_id, U=True, R=True) 
        ]

        # NOTE: When a post conditioned pattern ends it does not need to store the input 
        #       position. Rather, the acceptance position of the core pattern is retrieved
        #       in the terminal state.
        if Origin.post_context_id() == -1:
            info.append("    ")
            info.append(LanguageDB["$input/tell_position"]("last_acceptance_input_position") + "\n")

        return info
Exemple #12
0
    def _on_detection_code(Origin):
        """Store the name of the winner pattern (last_acceptance) and the position
           where it has matched (use of $input/tell_position).
        """
        if type(Origin) == type(None):
            # Case if no un-conditional acceptance occured, then register nothing
            return ""

        assert Origin.is_acceptance()

        info = [
           "    last_acceptance                = QUEX_LABEL(%i);\n" \
           % get_address("$terminal-direct", Origin.state_machine_id, U=True, R=True)
        ]

        # NOTE: When a post conditioned pattern ends it does not need to store the input
        #       position. Rather, the acceptance position of the core pattern is retrieved
        #       in the terminal state.
        if Origin.post_context_id() == -1:
            info.append("    ")
            info.append(LanguageDB["$input/tell_position"]
                        ("last_acceptance_input_position") + "\n")

        return info
Exemple #13
0
def __state_entries(txt, PathWalker, SMD):
    """Defines the entries of the path's states, so that the state key
       for the template is set, before the jump into the template. E.g.

            STATE_4711: 
               path_iterator = path_23 + 0; goto PATHWALKER_23;
            STATE_3123: 
               path_iterator = path_23 + 1; goto PATHWALKER_23;
            STATE_8912: 
               path_iterator = path_23 + 2; goto PATHWALKER_23;
    """
    sm = SMD.sm()

    PathN = len(PathWalker.path_list())
    require_path_end_state_variable_f = False
    txt.append("\n")
    for path in PathWalker.path_list():
        prev_state_index = None
        # Last state of sequence is not in the path, it is the first state after.
        for i, info in enumerate(path.sequence()[:-1]):
            state_index = info[0]
            # No need for state router if:
            #   (i) PathWalker is uniform, because then even after reload no dedicated
            #       state entry is required.
            #   (ii) The state is not entered from any other state except the predecessor
            #        on the path.
            # But:
            #   The first state always needs an entry.
            if prev_state_index != None:
                candidate = sm.get_only_entry_to_state(state_index)
                if PathWalker.uniform_state_entries_f() and prev_state_index == candidate:
                    prev_state_index = state_index
                    continue

            state = SMD.sm().states[state_index]

            entry_txt = []
            if PathWalker.uniform_state_entries_f():
                # If all state entries are uniform, the entry handling happens uniformly at
                # the entrance of the template, not each state.
                label_str = get_label("$entry", state_index) + ":\n"
                if state_index != SMD.sm().init_state_index:
                    label_str = "    __quex_assert_no_passage();\n" + label_str

                entry_txt.append(label_str)
                entry_txt.append("    ")
                entry_txt.append(LanguageDB["$debug-state"](state_index, SMD.forward_lexing_f()))
            else:
                entry_txt.extend(input_block.do(state_index, False, SMD))
                entry_txt.extend(acceptance_info.do(state, state_index, SMD, ForceSaveLastAcceptanceF=True))

            if PathWalker.uniform_state_entries_f() and PathN != 1:
                require_path_end_state_variable_f = True
                end_state_index = path.sequence()[-1][0]
                entry_txt.append("    path_end_state                 = QUEX_LABEL(%i);\n" \
                                 % get_address("$entry", end_state_index, U=True, R=True))
                
            entry_txt.append("    ")
            entry_txt.append(LanguageDB["$assignment"](
                                       "path_iterator                 ", 
                                       "path_%i + %i" % (path.index(), i)))
            entry_txt.append("goto %s;\n\n" % get_label_of_address(PathWalker.core().state_index, U=True))

            txt.append(Address("$entry", state_index, Code=entry_txt))
            prev_state_index = state_index

    return require_path_end_state_variable_f