Sha256: 7ee3f845f5d271991b6845b9064d32d6b7a52e981507ffafaa596982ad2afa6c
Contents?: true
Size: 1.35 KB
Versions: 5
Compression:
Stored size: 1.35 KB
Contents
module DR class SimpleKeywordsParser attr_accessor :opts, :keywords def initialize(hash, **opts) @opts=opts @keywords=hash end def keyword(name, &b) @keywords[name]=b end def parse(msg, **opts) opts=@opts.merge(opts) sep=opts[:sep] || ',' # Warning: the delims must take only one char delims= opts[:delims] || '()' bdelim= delims[0] edelim= delims[1] || bdelim keywords=@keywords.keys keywords_r="(?:"+keywords.map {|k| "(?:"+k+")"}.join("|")+")" reg = %r{(?<kw>#{keywords_r})(?<re>#{'\\'+bdelim}(?:(?>[^#{'\\'+bdelim}#{edelim == bdelim ? '' : '\\'+edelim}]+)|\g<re>)*#{'\\'+edelim})} if (m=reg.match(msg)) arg=m[:re][1...m[:re].length-1] arg=parse(arg, **opts) args=arg.split(sep) args=args.map {|a| a.strip} unless opts[:space] key=keywords.find {|k| /#{k}/ =~ m[:kw]} r=@keywords[key].call(*args).to_s msg=m.pre_match+r+parse(m.post_match,**opts) msg=keywords(msg,@keywords,**opts) if opts[:recursive] end return msg end # re = %r{ # (?<re> # \( # (?: # (?> [^()]+ ) # | # \g<re> # )* # \) # ) # }x #(?<re> name regexp/match #\g<re> reuse regexp #\k<re> reuse match #(?: grouping without capturing #(?> atomic grouping #x whitespace does not count # -> match balanced groups of parentheses end end
Version data entries
5 entries across 5 versions & 1 rubygems