解析引号和转义时(参见为什么 Parslet(在 Ruby 中)在解析空字符串文字时返回一个空数组?)我在 Parslet 中遇到了一个奇怪的问题:(escape_char.absent? >> str('"')).absent? >> any
似乎 Parslet 实际上解决了双重否定并期望转义字符在那里。
require 'parslet'
require 'rspec'
require 'parslet/rig/rspec'
require 'parslet/convenience'
class Parser < Parslet::Parser
root(:quote)
rule :quote do
quote >> text >> quote
end
rule :text do
(quote.absent? >> any).repeat
end
rule :quote do
escape_char.absent? >> str('"')
end
rule :escape_char do
str('\\')
end
end
describe Parser do
it 'should parse text in quotes' do
is_expected.to parse('"hello"')
end
it 'should parse text in quotes with escaped quote' do
is_expected.to parse('"foo\"bar"')
end
it 'should parse text in quotes with trailing escaped quote' do
is_expected.to parse('"text\""')
end
end
我对如何解决这个问题不太感兴趣,正如上面链接的帖子中已经描述的那样,我只是想了解这种行为。起初这似乎违反直觉,但我相信这背后有充分的理由。