Sha256: 3911f2f75b48c6d1475f3b2667c373dfa8ed458060bcc8119222cb52c4086e70
Contents?: true
Size: 1.05 KB
Versions: 6
Compression:
Stored size: 1.05 KB
Contents
require 'strscan' module HtmlBeautifier class Parser def self.debug_block(&blk) @debug_block = blk end def self.debug(match, method) if defined? @debug_block @debug_block.call(match, method) end end def initialize(&blk) @maps = [] if block_given? self.instance_eval(&blk) end end def map(pattern, method) @maps << [pattern, method] end def scan(subject, receiver) scanner = StringScanner.new(subject) until scanner.eos? dispatch(scanner, receiver) end end def dispatch(scanner, receiver) @maps.each do |pattern, method| if scanner.scan(pattern) params = [] i = 1 while scanner[i] params << scanner[i] i += 1 end params = [scanner[0]] if params.empty? self.class.debug(scanner[0], method) receiver.__send__(method, *params) return end end raise "Unmatched sequence #{match.inspect}" end end end
Version data entries
6 entries across 6 versions & 2 rubygems