Regex 如何要求一个未确定的字符在Ruby Treetop中连续重复一定次数?
我想创建一个规则,要求非数字、非字母字符连续重复三次。这条规则看起来像这样:Regex 如何要求一个未确定的字符在Ruby Treetop中连续重复一定次数?,regex,ruby,treetop,Regex,Ruby,Treetop,我想创建一个规则,要求非数字、非字母字符连续重复三次。这条规则看起来像这样: # Note, this code does not do what I want! grammar ThreeCharacters rule threeConsecutiveCharacters (![a-zA-Z0-9] .) 3..3 end end !{|s| # Peek at the following indentation: save = index; i = _nt_
# Note, this code does not do what I want!
grammar ThreeCharacters
rule threeConsecutiveCharacters
(![a-zA-Z0-9] .) 3..3
end
end
!{|s|
# Peek at the following indentation:
save = index; i = _nt_indentation; index = save
# We're closing if the indentation is less or the same as our enclosing block's:
closing = i.text_value.length <= @indents.last
}
有没有办法要求检测到的第一个字符重复三次
之前关于检测压痕数量也有类似的问题:
解决方案是首先初始化缩进堆栈:
&{|s| @indents = [-1] }
然后保存当前行的缩进:
&{|s|
level = s[0].indentation.text_value.length
@indents << level
true
}
&{s|
level=s[0]。indentation.text\u value.length
@缩进是的,你可以在树梢上这样做。由于packrat解析的工作方式,这种事情通常不可能发生;它是贪婪的,但你需要使用解析早期的语义信息来限制它的贪婪。它只是在树梢上添加语义谓词(&{…}
}这是可能的。所以,它是乏味的。你可以考虑使用RaTror,因为它除了树梢中的那些之外,还有很多特征。我不能建议(作为树的维护者,但不是响尾蛇的用户),但是它的特征集给我留下了很深的印象,我想它会更好地处理这个情况。
如果您继续使用Treetop,请记住,每个语义谓词都应该返回一个布尔值,指示成功或失败。这在上面初始化@indents
时并不明确。非常感谢您的回答!我将看看Cratter。