使用 tcl API 进行字符串替换

发布于 2024-08-08 10:02:53 字数 606 浏览 3 评论 0原文

有没有办法(ab)使用 tcl C-API 来“解析”字符串,执行所有替换(包括方括号中的子命令),但在实际评估生成的命令行之前停止?

我想做的是创建一个命令(用 C 语言,但我会考虑做一个 tcl 包装器,如果有一种优雅的方法可以做到这一点),它接受一个块作为参数(即大括号引用) -细绳)。我想采用该块,将其分割并以与执行它相同的方式执行替换,但停在那里并解释结果行。

我考虑过创建一个命名空间,其中所有有效的第一个单词都被定义为命令,但是这个列表是如此庞大(而且几乎是动态的),所以它很快就会变得太麻烦。我也尝试过这种方法,但使用 unknown 命令来拦截不同的命令。但是,unknown 用于一堆东西,并且不能绑定到命名空间,因此每当我执行该块时我都必须定义它,并将其设置回之前的值我已经完成了,感觉很不稳定。最重要的是,我会冒与实际命令发生冲突的风险(风险相当低,但不是零),所以我非常不愿意使用 unknown 命令。

我能得到的最接近的是 Tcl_ParseCommand (以及该系列的其余部分),它生成一个解析树,我可以手动评估它。我想如果没有更好的解决方案,我会采取这种方式,但如果有“官方”方式,我当然更喜欢它..

我错过了什么吗?

Is there a way to (ab)use the tcl C-API to 'parse' a string, doing all the replacement (including sub commands in square brackets), but stopping before actually evaluating the resulting command line?

What I'm trying to do is create a command (in C, but I'll consider doing a tcl-wrapper, if there's an elegant way to do it there) which takes a block as a parameter (i.e. curly-braces-quoted-string). I'd like to take that block, split it up and perform substitutions in the same way as if it was to be executed, but stop there and interpret the resulting lines instead.

I've considered creating a namespace, where all valid first-words are defined as commands, however this list is so vast (and pretty much dynamic) so it quickly becomes too cumbersome. I also tried this approach but with the unknown command to intercept the different commands. However, unknown is used for a bunch of stuff, and cannot be bound to a namespace, so I'd have to define it whenever I execute the block, and set it back to whatever it was before when I'm done, which feels pretty shaky. On top of that I'd run the risk (fairly low risk, but not zero) of colliding with an actual command, so I'd very much prefer to not use the unknown command.

The closest I can get is Tcl_ParseCommand (and the rest of the family), which produces a parse tree, which I could manually evaluate. I guess I'll resort to doing it this way if there's no better solution, but I would of course prefer it, if there was an 'official' way..

Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半﹌身腐败 2024-08-15 10:02:54

看一下 Tcl_SubstObj。它是 [subst] 命令的 C 等效项,这似乎就是您正在寻找的内容。

正如您在评论中指出的那样, subst 并没有完全完成您想要做的事情。如果有帮助,以下 Tcl 代码可能就是您正在寻找的:

> set mydata {mylist item $listitem group item {$group item}}
> set listitem {1 2 3}
> subst $mydata      ;# error: can't read "group": no such variable
> proc groupsubst {data} {
    return [uplevel 1 list $data]
}
> groupsubst $mydata ;# mylist item {1 2 3} group item {$group item}

Take a look at Tcl_SubstObj. It's the C equivalent of the [subst] command, which appears to be what you're looking for.

As you indicated in your comment, subst doesn't quite do what you're looking to do. If it helps, the following Tcl code may be what you're looking for:

> set mydata {mylist item $listitem group item {$group item}}
> set listitem {1 2 3}
> subst $mydata      ;# error: can't read "group": no such variable
> proc groupsubst {data} {
    return [uplevel 1 list $data]
}
> groupsubst $mydata ;# mylist item {1 2 3} group item {$group item}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文