我想创建一个规则,要求非数字、非字母字符连续重复三次。规则看起来像这样:
# Note, this code does not do what I want!
grammar ThreeCharacters
rule threeConsecutiveCharacters
(![a-zA-Z0-9] .) 3..3
end
end
有没有办法要求它检测到的第一个字符重复三次?
之前有一个关于检测缩进数量的类似问题:PEG for Python style indentation
解决方案是首先初始化缩进堆栈:
&{|s| @indents = [-1] }
然后保存当前行的缩进:
&{|s|
level = s[0].indentation.text_value.length
@indents << level
true
}
每当新行开始时,它都会像这样查看缩进:
!{|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
}
如果缩进更大,它会将新的缩进级别添加到堆栈中。
我可以为我的问题创建类似的东西,但这似乎是一种非常乏味的解决方法。还有其他方法可以创建我的规则吗?