注意:下面是我认为足以解决我的问题的部分代码。但我还将整个脚本的 zip 存档放在本文末尾。
需要做什么:我需要将行 "argc ; number "
替换为 string map [ list//$ 返回的值function_parameter_comment_sign ] "\"argc//number\""
和行 "argv ;arguments "
以及 string map [ list//$function_parameter_comment_sign ] 返回的值"\"argv//参数\""
我尝试了不同的方法来做到这一点:将它们包含到 [] 或 {}、为变量赋值并将其放置在该行中以及许多其他方法,但没有成功。我该怎么做?
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
下载整个代码的代码链接: https://mega.co.nz/#!QFhlnSIS!8lxgCFbXAweqrj72Gj8KRbc6o9GVlX-V9T1Fw9jwhN0
最佳答案
我不完全确定你在寻找什么,但如果我做对了,你可以尝试使用这样的东西:
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]
应该会产生相同的结果,并且可以让函数和替换发挥作用。
关于Tcl。如何用字符串映射返回的值替换代码行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23260664/