Tcl。如何用字符串映射返回的值替换代码行?
注意:下面是我认为对我的问题来说足够的部分代码。但我也把整个脚本的zip存档放在了这篇文章的末尾。 需要做什么:我需要用Tcl。如何用字符串映射返回的值替换代码行?,tcl,Tcl,注意:下面是我认为对我的问题来说足够的部分代码。但我也把整个脚本的zip存档放在了这篇文章的末尾。 需要做什么:我需要用字符串映射[list//$function\u parameter\u comment\u sign]“argc//number\”返回的值替换行“argc;number”,并用字符串映射返回的值替换行“argv;arguments”[list/$function\u parameter\u comment\u sign]“\”argv//arguments\” 我尝试了不同的
字符串映射[list//$function\u parameter\u comment\u sign]“argc//number\”
返回的值替换行“argc;number”
,并用字符串映射返回的值替换行“argv;arguments”
[list/$function\u parameter\u comment\u 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.tcl
、utest_-utils.tcl
和gen_-test.tcl
下载整个代码的代码链接:假设您总是希望不断地进行替换(即,您不会在每个站点上改变
$function\u parameter\u comment\u sign
),那么这并不难。只需构建映射,在几个阶段中应用字符串映射即可。(我不能100%确定我有你想要替换的东西,但从这里开始修复应该很容易。)
如果您不想用常量替换不需要的字符串的每个实例,那么事情将变得更加复杂,因为您需要使用subst
(因此还需要额外的清理步骤).幸运的是,我认为您不需要这样做;您的案例似乎很简单。我不完全确定您在寻找什么,但如果我做对了,您可以尝试使用以下方法:
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]
应该会产生同样的结果,因为函数和替换可以工作。对不起,我没有正确解释我的问题。这是上面代码的过程调用。我需要传递过程,而不是“argc;number”
表示“argc$var1号”
并代替“argv;参数”
表示“argv$var2参数”
。我该怎么做?$var1
和$var2
是保存字符串映射
操作结果的变量,这是我的问题。非常感谢!这正是我想要的。
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" } ""