0

我正在编写一个递归过程,称为“subliner3”。简单来说,它取代了:

  1. [object method]Object->method()
  2. [object method:attr1 attr2 ...]object->method(attr1,attr2,...)

在 (2) 中替换 (1) 和 (2) 是递归的。任何属性都可能类似于 (1) 或 (2)。

因此,此代码会导致问题:

while {[regsub -all {\[([^\[\]:]+)[:]([^\[\]]+)\]} $subline "[subliner3 "\\1" "\\2"]" subline]} {}

这应该在 subline 中准确找到 (2)(subline 是一个属性列表)并再次为它调用函数。问题是,当使用 regsub 的 \1 和 \2 subliner3 调用 subline 时,subliner3 真的得到“\1”和“\2”,所以看起来它们是在 subliner3 调用之后被解释的。如何使用解释的 \1 和 \2 调用 [subliner3 "\1" "\2"]?

样本输入:

[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:5], [CCCallFunc actionWithTarget:self selector:@selector(resetMessage)], nil]]; 

输出:

self->runAction(CCSequence::actions(CCDelayTime::actionWithDuration(5), CCCallFunc::actionWithTarget(self, @selector(resetMessage)), nil);
4

2 回答 2

0

一种可能的解决方案是: expr \"[regsub -all {(1)} "number 1" "[getSp \1]"]\" 因此,首先,regsub 设法将 (1) 放在 \1 位置。然后 expr 调用 getSp。它不会用 \1 调用,而是用 (1) 调用。但是要使用此解决方案,我需要确保 regsub 返回的字符串 [ ] 仅针对过程指定,但并非总是如此。例如 regsub 调用后的字符串可能是这样的: [self runAction:[CCSequence actions:[subliner3 "CCDelayTime actionWithDuration" "5"], [subliner3 "CCCallFunc actionWithTarget" "self selector:@selector(resetMessage)"], nil] ]; 其中只有 subliner3 - 是一个过程。

于 2012-08-25T09:08:30.540 回答
0

You can do it (under some assumptions, such as no use of arrays) but you really need to work inside out and to put your substitution code in a loop.

# Your sample as input
set string {[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:5], [CCCallFunc actionWithTarget:self selector:@selector(resetMessage)], nil]];}

# Do most of the replacements, recursively.
#
# Note that some parts are changed to \001stripExtra stuff\002, because we need
# to do further processing on the arguments which can't quite be done in this
# looped [regsub].
while {[regsub -all {\[(\w+) (\w+):([^]]+)\]} $string \
        "\\1::\\2(\u0001stripExtra \\3\u0002)" string]} {}

# The further processing to do on arguments (removing selectors)
proc stripExtra args {
    foreach s $args {
        # The lack of a fourth argument means [regsub] returns the string
        lappend t [regsub {^\w+:(?!:)} [string trimright $s ","] {}]
    }
    return [join $t ","]
}
# Apply the further processing by converting to a substitutable string
set string [subst [string map {\u0001 "\[" \u0002 "\]"} $string]]

# Now transformed...
puts $string

The code above is rather brittle as it doesn't actually understand Objective-C, so you should check that its output is reasonable on your real input data…</p>

于 2012-08-25T15:26:01.463 回答