TCL。如何用字符串映射返回的值替换代码行?

时间:2014-04-24 05:41:21

标签: tcl

注意:下面有部分代码我认为对我的问题来说已经足够了。但我也把整个脚本的zip存档放在这篇文章的末尾。

需要做什么:我需要将行" argc ; number "替换为string map [ list // $function_parameter_comment_sign ] "\" argc // number \""返回的值和行" argv ; arguments "的值{由string map [ list // $function_parameter_comment_sign ] "\" argv // arguments \""

返回

我尝试了不同的方法:将它们包含在[]或{}中,为变量赋值并将其置于该行以及许多其他方式但未成功。我该怎么办?

if { [ llength $argv ] == 1 } {
  set test [ lindex $argv 0 ]
  testone $test
} else {
  testmain
}


proc testmain { } {
    global ut_all_tests ut_current_test

    foreach test $ut_all_tests {
        set ut_current_test $test
        puts $test
        $test
    }
}

proc testone { torun } {
    global ut_all_tests ut_current_test

    foreach test $ut_all_tests {
    if { $torun == $test } {
      set ut_current_test $test
      puts $test
      $test
    }
    }
}


proc tproc { name args body } {
    global ut_all_tests

    lappend ut_all_tests $name
    proc $name $args $body
}


tproc extract_tcl_signature_test { } {

proc load_generators {} {
        set script_path [ file dirname [ file normalize [ info script ] ] ]
        set drakon_editor_path [string trimright $script_path "unittest" ]
    set scripts [ glob -- "$drakon_editor_path/generators/*.tcl" ]
    foreach script $scripts {
      source $script
    }       
}

namespace eval gen {

array set generators {}

# This procedure is called by language generator files. In the beginning of every language generator there is calling code.
proc add_generator { language generator } {
    variable generators
    if { [ info exists generator($language) ] } {
        error "Generator for language $language already registered."
    }
    set gen::generators($language) $generator
}

}
load_generators
puts "=================================="
puts "Started: extract_tcl_signature_test"
foreach { language generator } [ array get gen::generators ] {
    puts "----------------------------------"
    puts $language
    puts $generator

    namespace eval current_file_generation_info {}
    set current_file_generation_info::language $language
    set current_file_generation_info::generator $generator

    set find [string first :: $generator]
    set generator_namespace [ string range $generator 0 $find-1 ]

    # These 3 lines is to check is current generator have commentator procedure.
    # If not commentator_status_var is set to "" .
    set commentator_for_namespace_text "::commentator"
    set commentator_call_text "$generator_namespace$commentator_for_namespace_text"
    set commentator_status_var [ namespace which $commentator_call_text ]

    # If current language does not have commentator procedure or current languages is in if conditions, then // sign for function parameter commenting will be used.
    # It is done so for compability with diagrams which are made with previous versions of DRAKON Editor.
    # If you are adding new language generator to DRAKON Editor and want to use line comment sign as
    # commenting sign for function parameters, just make commentator procedure in your language generator
    # as it is for example in AutoHotkey code generator.
    if { $commentator_status_var == "" ||
    $language == "C" ||
    $language == "C#" ||
    $language == "C++" ||
    $language == "D" ||
    $language == "Erlang" ||
    $language == "Java" ||
    $language == "Javascript" ||
    $language == "Lua" ||
    $language == "Processing.org" ||
    $language == "Python 2.x" ||
    $language == "Python 3.x" ||
    $language == "Tcl" ||
    $language == "Verilog" } {

            good_signature_tcl { " " " #comment " "" "what?" } comment "" {} ""

            good_signature_tcl { "" 
                    "" 
                    " argc // number "
                    " argv // arguments "
                    " "
                    } procedure public { "argc" "argv" } ""

            good_signature_tcl { "one"
                    "two" } procedure public { "one" "two" } ""

    } else {

            # Get current generator line comment simbol and calculate its length without space sign.
            set function_parameter_comment_sign [ $commentator_call_text "" ]
            set function_parameter_comment_sign [string trim $function_parameter_comment_sign " " ]
            if { $function_parameter_comment_sign == "#" } {

            #good_signature_tcl { " " " #comment " "" "what?" } comment "" {} ""

            good_signature_tcl { "" 
                    "" 
                    " argc # number "
                    " argv # arguments "
                    " "
                    } procedure public { "argc" "argv" } ""

            good_signature_tcl { "one"
                    "two" } procedure public { "one" "two" } ""

            } else {

            good_signature_tcl { " " " #comment " "" "what?" } comment "" {} ""

            good_signature_tcl { "" 
                    "" 
                    " argc ; number "
                    " argv ; arguments "
                    " "
                    } procedure public { "argc" "argv" } ""

            good_signature_tcl { "one"
                    "two" } procedure public { "one" "two" } ""

            }
    }

    #puts $function_parameter_comment_sign

}

puts "----------------------------------"
puts "Successfully ended: extract_tcl_signature_test"
puts "=================================="

}


proc good_signature_tcl { lines type access parameters returns } {
    set text [ join $lines "\n" ]
    unpack [ gen_tcl::extract_signature $text foo ] message signature
    equal $message ""

    unpack $signature atype aaccess aparameters areturns
    equal $atype $type
    equal $aaccess $access
    set par_part0 {}
    foreach par $aparameters {
        lappend par_part0 [ lindex $par 0 ]
    }
    list_equal $par_part0 $parameters
    equal [ lindex $areturns 0 ] $returns
}

以上代码部分来自文件:unittest.tclutest_utils.tclgen_test.tcl

下载整个代码的代码链接: https://mega.co.nz/#!QFhlnSIS!8lxgCFbXAweqrj72Gj8KRbc6o9GVlX-V9T1Fw9jwhN0

2 个答案:

答案 0 :(得分:1)

假设你总是想要一个不断的替换(即,你没有在每个站点改变$function_parameter_comment_sign!)那么它并不太难。只需构建地图即可在几个阶段中应用string map。 (我不是100%确定我有你想要替换的东西,但你应该很容易从这里修复它。)

# Shared value to keep line length down
set replacer [list "//" $function_parameter_comment_sign]

# Build the map (no need to do it in one monster [list]!)
set map {}
lappend map {" argc ; number "} [string map $replacer "\" argc // number \""]
lappend map {" argv ; arguments "} [string map $replacer "\" argv // arguments \""]

# Apply the map
set theMappedText [string map $map $theInputText]

如果你想做一些事情而不是用常量替换不需要的字符串的每个实例,那么你需要使用subst就会变得更加复杂(因此你还需要额外的清理工作)步)。幸运的是,我认为你不需要那个;你的情况似乎很简单。

答案 1 :(得分:1)

我不完全确定你在寻找什么,但如果我做对了,你可以尝试使用这样的东西:

good_signature_tcl [list "" \
    "" \
    [string map [list // $function_parameter_comment_sign] " argc // number "] \
    [string map [list // $function_parameter_comment_sign] " argv // arguments "] \
    " " \
] procedure public { "argc" "argv" } ""

由于您的{}被用于制作列表,因此使用[list]应该会产生相同的功能,并且可以使用功能和替换功能。