示例#1
0
def analyzer_functions_get(ModeDB):
    code = []

    # (*) Get list of modes that are actually implemented
    #     (abstract modes only serve as common base)
    mode_name_list = ModeDB.keys()

    for name, mode_descr in ModeDB.iteritems():
        dial_db.clear()

        # -- Generate 'Mode' from 'ModeDescriptions'
        mode = Mode(mode_descr)
        blackboard.mode_db[name] = mode

        if not mode.is_implemented(): continue

        txt_analyzer = cpp_generator.do(mode, mode_name_list)
        txt_counter = cpp_generator.do_default_counter(mode)

        code.extend(txt_counter)
        code.extend(txt_analyzer)

    code.append(
        do_comment_pattern_action_pairs(blackboard.mode_db.itervalues()))

    if not Setup.token_class_only_f:
        determine_start_mode(blackboard.mode_db)

    # (*) perform consistency check on newly generated mode_db
    consistency_check.do(blackboard.mode_db)

    # generate frame for analyser code
    return cpp_generator.frame_this("".join(code)), blackboard.mode_db
示例#2
0
文件: core.py 项目: dkopecek/amplify
def analyzer_functions_get(ModeDB):
    code = []

    # (*) Get list of modes that are actually implemented
    #     (abstract modes only serve as common base)
    mode_name_list = ModeDB.keys()  

    for name, mode_descr in ModeDB.iteritems():        
        dial_db.clear()

        # -- Generate 'Mode' from 'ModeDescriptions'
        mode = Mode(mode_descr)
        blackboard.mode_db[name] = mode

        if not mode.is_implemented(): continue

        txt_analyzer = cpp_generator.do(mode, mode_name_list)
        txt_counter  = cpp_generator.do_default_counter(mode)

        code.extend(txt_counter)
        code.extend(txt_analyzer)

    code.append(do_comment_pattern_action_pairs(blackboard.mode_db.itervalues()))

    if not Setup.token_class_only_f:
        determine_start_mode(blackboard.mode_db)

    # (*) perform consistency check on newly generated mode_db
    consistency_check.do(blackboard.mode_db)

    # generate frame for analyser code
    return cpp_generator.frame_this("".join(code)), blackboard.mode_db
示例#3
0
def test_init(DidCl_Iterable):
    dial_db.clear()
    stdb = SharedTailDB(4711L, DidCl_Iterable)

    print "_" * 80
    print
    print "Setup:"
    for door_id, command_list in DidCl_Iterable:
        print "    %s: [%s]" % (str(door_id),
                                ("".join("%s " % alias_db[cmd]
                                         for cmd in command_list)).strip())
    print
    print "SharedTailDB:"
    print
    print "    " + stdb.get_string(alias_db).replace("\n", "\n    ")
示例#4
0
def do_default_counter(Mode):
    if not Mode.default_character_counter_required_f:
        return []

    dial_db.clear()
    ccfactory = CountCmdFactory.from_ParserDataLineColumn(
        Mode.counter_db, Setup.buffer_codec.source_set, Lng.INPUT_P())

    variable_db.init()

    # May be, the default counter is the same as for another mode. In that
    # case call the default counter of the other mode with the same one and
    # only macro.
    default_character_counter_function_name,   \
    default_character_counter_function_code  = counter.get(ccfactory, Mode.name)

    txt = [Lng.DEFAULT_COUNTER_PROLOG(default_character_counter_function_name)]

    if default_character_counter_function_code is not None:
        txt.append(default_character_counter_function_code)

    return txt
示例#5
0
def test_pop(DidCl_Iterable):
    dial_db.clear()
    stdb = SharedTailDB(4711L, DidCl_Iterable)

    print "_" * 80
    print
    print "Setup:"
    for door_id, command_list in DidCl_Iterable:
        print "    %s: [%s]" % (str(door_id),
                                ("".join("%s " % alias_db[cmd]
                                         for cmd in command_list)).strip())

    print
    print "___ Initial ___"
    print
    print "".join(stdb.get_tree_text(alias_db))

    i = 0
    while stdb.pop_best():
        i += 1
        print "___ Step %i ____" % i
        print
        print "".join(stdb.get_tree_text(alias_db))
示例#6
0
文件: core.py 项目: xxyzzzq/quex
def do_default_counter(Mode):
    if not Mode.default_character_counter_required_f:
        return []

    dial_db.clear()
    ci_map = CountInfoMap.from_LineColumnCount(Mode.counter_db, 
                                               Setup.buffer_codec.source_set, 
                                               Lng.INPUT_P())

    variable_db.init()

    # May be, the default counter is the same as for another mode. In that
    # case call the default counter of the other mode with the same one and
    # only macro.
    default_character_counter_function_name,   \
    default_character_counter_function_code  = counter.get(ci_map, Mode.name)

    txt = [ Lng.DEFAULT_COUNTER_PROLOG(default_character_counter_function_name) ]

    if default_character_counter_function_code is not None:
        txt.append(default_character_counter_function_code)

    return txt
import quex.engine.analyzer.core as analyzer_generator
from quex.engine.analyzer.door_id_address_label import dial_db
from quex.engine.analyzer.transition_map import TransitionMap
from quex.engine.codec_db.core import CodecDynamicInfo, CodecInfo
from   quex.blackboard                                    import setup as Setup, \
                                                                 E_MapImplementationType, \
                                                                 E_IncidenceIDs, \
                                                                 Lng
from collections import defaultdict

Setup.language_db = languages.db["C++"]
Setup.buffer_element_type = "uint32_t"
Setup.buffer_element_specification_prepare()
Setup.buffer_codec_prepare("unicode", None)

dial_db.clear()

if "--hwut-info" in sys.argv:
    print "Single State: Transition Code Generation;"
    print "CHOICES: A, B, C, A-UTF8, B-UTF8, C-UTF8;"
    sys.exit(0)

choice, codec = {
    "A": ("A", ""),
    "B": ("B", ""),
    "C": ("C", ""),
    "A-UTF8": ("A", "UTF8"),
    "B-UTF8": ("B", "UTF8"),
    "C-UTF8": ("C", "UTF8"),
}[sys.argv[1]]
import quex.engine.analyzer.core                          as     analyzer_generator
from   quex.engine.analyzer.door_id_address_label         import dial_db
from   quex.engine.analyzer.transition_map                import TransitionMap  
from   quex.engine.codec_db.core                          import CodecDynamicInfo, CodecInfo
from   quex.blackboard                                    import setup as Setup, \
                                                                 E_MapImplementationType, \
                                                                 E_IncidenceIDs, \
                                                                 Lng
from   collections import defaultdict

Setup.language_db = languages.db["C++"]
Setup.buffer_element_type = "uint32_t"
Setup.buffer_element_specification_prepare()
Setup.buffer_codec_prepare("unicode", None)

dial_db.clear()

if "--hwut-info" in sys.argv:
    print "Single State: Transition Code Generation;"
    print "CHOICES: A, B, C, A-UTF8, B-UTF8, C-UTF8;"
    sys.exit(0)

choice, codec = {
        "A": ("A", ""),
        "B": ("B", ""),
        "C": ("C", ""),
        "A-UTF8": ("A", "UTF8"),
        "B-UTF8": ("B", "UTF8"),
        "C-UTF8": ("C", "UTF8"),
}[sys.argv[1]]
示例#9
0
def create_state_machine_function(PatternActionPairList, PatternDictionary, 
                                  BufferLimitCode, SecondModeF=False):

    # (*) Initialize address handling
    dial_db.clear()     # BEFORE constructor of generator; 
    variable_db.variable_db.init()  # because constructor creates some addresses.
    blackboard.required_support_begin_of_line_set()

    def action(ThePattern, PatternName): 
        txt = []
        if ThePattern.bipd_sm is not None:
            TerminalFactory.do_bipd_entry_and_return(txt, pattern)

        txt.append("%s\n" % Lng.STORE_LAST_CHARACTER(blackboard.required_support_begin_of_line()))
        txt.append("%s\n" % Lng.LEXEME_TERMINATING_ZERO_SET(True))
        txt.append('printf("%19s  \'%%s\'\\n", Lexeme); fflush(stdout);\n' % PatternName)

        if   "->1" in PatternName: txt.append("me->current_analyzer_function = QUEX_NAME(Mr_analyzer_function);\n")
        elif "->2" in PatternName: txt.append("me->current_analyzer_function = QUEX_NAME(Mrs_analyzer_function);\n")

        if "CONTINUE" in PatternName: txt.append("")
        elif "STOP" in PatternName:   txt.append("return false;\n")
        else:                         txt.append("return true;\n")


        txt.append("%s\n" % Lng.GOTO(DoorID.continue_with_on_after_match()))
        ## print "#", txt
        return CodeTerminal(txt)
    
    # -- Display Setup: Patterns and the related Actions
    print "(*) Lexical Analyser Patterns:"
    for pair in PatternActionPairList:
        print "%20s --> %s" % (pair[0], pair[1])

    if not SecondModeF:  sm_name = "Mr"
    else:                sm_name = "Mrs"

    Setup.analyzer_class_name = sm_name
    
    pattern_action_list = [
        (regex.do(pattern_str, PatternDictionary), action_str)
        for pattern_str, action_str in PatternActionPairList
    ]
    
    support_begin_of_line_f = False
    for pattern, action_str in pattern_action_list:
        support_begin_of_line_f |= pattern.pre_context_trivial_begin_of_line_f

    for pattern, action_str in pattern_action_list:
        pattern.prepare_count_info(LineColumnCount_Default(), CodecTrafoInfo=None)
        pattern.mount_post_context_sm()
        pattern.mount_pre_context_sm()
        pattern.cut_character_list(signal_character_list(Setup))

    # -- PatternList/TerminalDb
    #    (Terminals can only be generated after the 'mount procedure', because, 
    #     the bipd_sm is generated through mounting.)
    on_failure              = CodeTerminal(["return false;\n"])
    support_begin_of_line_f = False
    terminal_db             = {
        E_IncidenceIDs.MATCH_FAILURE: Terminal(on_failure, "FAILURE", 
                                               E_IncidenceIDs.MATCH_FAILURE),
        E_IncidenceIDs.END_OF_STREAM: Terminal(on_failure, "END_OF_STREAM", 
                                               E_IncidenceIDs.END_OF_STREAM),
        E_IncidenceIDs.BAD_LEXATOM:   Terminal(on_failure, "BAD_LEXATOM", 
                                               E_IncidenceIDs.BAD_LEXATOM),
        E_IncidenceIDs.OVERFLOW:      Terminal(on_failure, "NO_SPACE_TO_LOAD", 
                                               E_IncidenceIDs.OVERFLOW),
        E_IncidenceIDs.LOAD_FAILURE:  Terminal(on_failure, "LOAD_FAILURE", 
                                               E_IncidenceIDs.LOAD_FAILURE),
    }
    for pattern, action_str in pattern_action_list:
        name     = safe_string(pattern.pattern_string())
        terminal = Terminal(action(pattern, action_str), name)
        terminal.set_incidence_id(pattern.incidence_id())
        terminal_db[pattern.incidence_id()] = terminal

    # -- create default action that prints the name and the content of the token
    #    store_last_character_str = ""
    #    if support_begin_of_line_f:
    #        store_last_character_str  = "    %s = %s;\n" % \
    #                                    ("me->buffer._lexatom_before_lexeme_start", 
    #                                     "*(me->buffer._read_p - 1)")
    #    set_terminating_zero_str  = "    QUEX_LEXEME_TERMINATING_ZERO_SET(&me->buffer);\n"
    #    prefix = store_last_character_str + set_terminating_zero_str

    print "## (1) code generation"    

    pattern_list = [ pattern for pattern, action_str in pattern_action_list ]
    function_body, variable_definitions = cpp_generator.do_core(pattern_list, terminal_db)
    function_body += "if(0) { __QUEX_COUNT_VOID((QUEX_TYPE_ANALYZER*)0, (QUEX_TYPE_LEXATOM*)0, (QUEX_TYPE_LEXATOM*)0); }\n"
    function_txt                        = cpp_generator.wrap_up(sm_name, function_body, 
                                                                variable_definitions, 
                                                                ModeNameList=[])

    assert all_isinstance(function_txt, str)

    return   "#define  __QUEX_OPTION_UNIT_TEST\n" \
           + nonsense_default_counter(not SecondModeF) \
           + "".join(function_txt)
示例#10
0
def create_state_machine_function(PatternActionPairList,
                                  PatternDictionary,
                                  BufferLimitCode,
                                  SecondModeF=False):

    # (*) Initialize address handling
    dial_db.clear()  # BEFORE constructor of generator;
    variable_db.variable_db.init(
    )  # because constructor creates some addresses.
    blackboard.required_support_begin_of_line_set()

    def action(ThePattern, PatternName):
        txt = []
        if ThePattern.bipd_sm is not None:
            TerminalFactory.do_bipd_entry_and_return(txt, pattern)

        txt.append("%s\n" % Lng.STORE_LAST_CHARACTER(
            blackboard.required_support_begin_of_line()))
        txt.append("%s\n" % Lng.LEXEME_TERMINATING_ZERO_SET(True))
        txt.append('printf("%19s  \'%%s\'\\n", Lexeme); fflush(stdout);\n' %
                   PatternName)

        if "->1" in PatternName:
            txt.append(
                "me->current_analyzer_function = QUEX_NAME(Mr_analyzer_function);\n"
            )
        elif "->2" in PatternName:
            txt.append(
                "me->current_analyzer_function = QUEX_NAME(Mrs_analyzer_function);\n"
            )

        if "CONTINUE" in PatternName: txt.append("")
        elif "STOP" in PatternName: txt.append("return false;\n")
        else: txt.append("return true;\n")

        txt.append("%s\n" % Lng.GOTO(DoorID.continue_with_on_after_match()))
        ## print "#", txt
        return CodeTerminal(txt)

    # -- Display Setup: Patterns and the related Actions
    print "(*) Lexical Analyser Patterns:"
    for pair in PatternActionPairList:
        print "%20s --> %s" % (pair[0], pair[1])

    if not SecondModeF: sm_name = "Mr"
    else: sm_name = "Mrs"

    Setup.analyzer_class_name = sm_name

    pattern_action_list = [
        (regex.do(pattern_str, PatternDictionary), action_str)
        for pattern_str, action_str in PatternActionPairList
    ]

    support_begin_of_line_f = False
    for pattern, action_str in pattern_action_list:
        support_begin_of_line_f |= pattern.pre_context_trivial_begin_of_line_f

    for pattern, action_str in pattern_action_list:
        pattern.prepare_count_info(LineColumnCount_Default(),
                                   CodecTrafoInfo=None)
        pattern.mount_post_context_sm()
        pattern.mount_pre_context_sm()
        pattern.cut_character_list(signal_character_list(Setup))

    # -- PatternList/TerminalDb
    #    (Terminals can only be generated after the 'mount procedure', because,
    #     the bipd_sm is generated through mounting.)
    on_failure = CodeTerminal(["return false;\n"])
    support_begin_of_line_f = False
    terminal_db = {
        E_IncidenceIDs.MATCH_FAILURE:
        Terminal(on_failure, "FAILURE", E_IncidenceIDs.MATCH_FAILURE),
        E_IncidenceIDs.END_OF_STREAM:
        Terminal(on_failure, "END_OF_STREAM", E_IncidenceIDs.END_OF_STREAM),
        E_IncidenceIDs.BAD_LEXATOM:
        Terminal(on_failure, "BAD_LEXATOM", E_IncidenceIDs.BAD_LEXATOM),
        E_IncidenceIDs.OVERFLOW:
        Terminal(on_failure, "NO_SPACE_TO_LOAD", E_IncidenceIDs.OVERFLOW),
        E_IncidenceIDs.LOAD_FAILURE:
        Terminal(on_failure, "LOAD_FAILURE", E_IncidenceIDs.LOAD_FAILURE),
    }
    for pattern, action_str in pattern_action_list:
        name = safe_string(pattern.pattern_string())
        terminal = Terminal(action(pattern, action_str), name)
        terminal.set_incidence_id(pattern.incidence_id())
        terminal_db[pattern.incidence_id()] = terminal

    # -- create default action that prints the name and the content of the token
    #    store_last_character_str = ""
    #    if support_begin_of_line_f:
    #        store_last_character_str  = "    %s = %s;\n" % \
    #                                    ("me->buffer._lexatom_before_lexeme_start",
    #                                     "*(me->buffer._read_p - 1)")
    #    set_terminating_zero_str  = "    QUEX_LEXEME_TERMINATING_ZERO_SET(&me->buffer);\n"
    #    prefix = store_last_character_str + set_terminating_zero_str

    print "## (1) code generation"

    pattern_list = [pattern for pattern, action_str in pattern_action_list]
    function_body, variable_definitions = cpp_generator.do_core(
        pattern_list, terminal_db)
    function_body += "if(0) { __QUEX_COUNT_VOID((QUEX_TYPE_ANALYZER*)0, (QUEX_TYPE_LEXATOM*)0, (QUEX_TYPE_LEXATOM*)0); }\n"
    function_txt = cpp_generator.wrap_up(sm_name,
                                         function_body,
                                         variable_definitions,
                                         ModeNameList=[])

    assert all_isinstance(function_txt, str)

    return   "#define  __QUEX_OPTION_UNIT_TEST\n" \
           + nonsense_default_counter(not SecondModeF) \
           + "".join(function_txt)