When parsing quotes and escapes (cf. Why does Parslet (in Ruby) return an empty array when parsing an empty string literal?) I came across an oddity in Parslet: (escape_char.absent? >> str('"')).absent? >> any
It seems that Parslet actually resolves the double negation and expects the escape character to be there.
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
I am not so much interested in how to solve this, as it is already described in the Post linked above, but merely curious to understand this behaviour. It seems counterintuitive at first but I am sure there is good reason behind this.