Closed darshan-patel closed 7 years ago
@darshan-patel does it work when you try to match the longer string first? e.g.
rule(:data) { str('name_id') | str('name') }
require 'parslet'
require 'parslet/convenience'
class Lines < Parslet::Parser
rule(:open_tag) {str('[')}
rule(:close_tag) {str(']')}
rule(:data) { str('name_id') | str('name') }
rule(:text) { open_tag >> data >> close_tag }
root :text
end
begin
p Lines.new.parse("[name_id]") # <---- It throws error
rescue Parslet::ParseFailed => failure
Lines.new.parse_with_debug("[name_id]")
end
When using top-down parsers (and parslet can be thought of as one, in simplification) you will always have to match the longer alternative first, failing that you'll match the shorter one. The problem is the common substring of your 'data' token.
@kschiess
Is there any alternative for this problem?