samples/parse-bind
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'd-parse'
include DParse::DSL # rubocop:disable Style/MixinUsage
digit = chars(*('0'..'9')).capture
letter = chars(*('a'..'z')).capture
init = alt(char('d'), char('l')).capture
parser_map = {
'd' => digit,
'l' => letter,
}
pair =
init.bind do |data|
new_parser = parser_map[data]
new_parser.map { |d| [data, d] }
end
parser = repeat(pair)
p parser.apply('lad1d2d3lblc')