From: James Edward Gray II Date: 2005-08-17T07:24:51+09:00 Subject: Re: Generic Parsing Library On Aug 16, 2005, at 4:46 PM, Adam Sanderson wrote: > I was wondering if anyone would be interested in, or knows of a > generic > parsing library. I've just recently been throwing together my own tool for this. I just got done using it in a real-world (paid) project. It's small and really just a chainsaw tool for data mining, but it seems to be a good start. I haven't documented it yet, but here are a couple of examples from my unit tests: def test_complex path = File.join(File.dirname(__FILE__), "ross_report.txt") test = self input(path) do @state = :skip start_skipping_at("\f") stop_skipping_at(/\A-[- ]+-\Z/) skip(/\A\s*\Z/) skip(/--\Z/) find_in_skipped(/((?:Period|Week)\s+\d.+?)\s*\Z/) do | period| test.assert_equal("Period 02/2002", period) end stop_at("*** Selection Criteria ***") read do |line| test.assert_match(/\A\s+(?:Sales|Cust|SA)|\A[-\w]+\s +/, line) end end path = File.join(File.dirname(__FILE__), "car_ads.txt") data = input(path, "") do @state = :skip stop_skipping_at("Save Ad") skip(/\A\s*\Z/) pre { @price = @miles = nil } read(/\$([\d,]+\d)/) { |price| @price = price.delete (",").to_i } read(/([\d,]*\d)\s*m/) { |miles| @miles = miles.delete (",").to_i } read do |ad| if @price and @price < 20_000 and @miles and @miles < 40_000 (@ads ||= Array.new) << ad.strip end end end assert_equal([<