From: Caleb Clausen Date: 2009-08-30T05:55:19+09:00 Subject: [ruby-core:25186] Re: RegOOps: An Object-Oriented Approach to Pattern Matching Run Paint Run Run wrote: > Regexps in Ruby can feel like a jagged edge to the otherwise smooth > curves. For all the syntactical smarts we employ so as to avoid > Perl-like punctuation, regular expressions take us two steps back, [snip] > Imagine a String as an Array of Char objects. The Char class would > define predicate methods corresponding to Unicode and regexp > properties. For instance Char#lowercase? or Char#digit?. The > implementation is trivial because Regexps are used internally. > > Matching the String against a pattern is now a case of calling the > appropriate predicate methods on the underlying characters. For > example, match(/\w+/) could be expressed as match(:word). This would > succeed if at least one character in the string returns true for > Char#word?. Similarly, match(:hex, :blank, :digit) would: This all reminds me intensely of Eric Mahurin's Grammar library/DSL. Much of what you're talking about is in there. Any ruby-based parser system (eg treetop or racc) is likely to have at least some similarity to this too. http://rubyforge.org/projects/grammar/ > I've so far talked in terms of matching against Strings, but the > generality of this approach suggests that it could be associated with > any enumerator. (I've overloaded String#chars to return Char objects > for String). This could allow functional-style matching against data > structures. Ah, now you're really reminding me of my own project, Reg (Ruby Extended Grammar). It has pattern-matching constructs for arrays, hashes, objects. With backtracking in arrays, even. A whole DSL for matching patterns in ruby object trees. The github project is here: http://github.com/coatl/reg but, I see you're already following it. :) (There's also a gem, but its really old.) I've gotten some good use out of Reg, for instance, I wrote a ruby parser (RedParse) using it. I find this subject deeply fascinating. I think it would be cool if ruby had more pattern matching built in, so that for instance, you could specify a pattern in a block or method parameter list or the left side of an assignment. Using unary star and parens, there's already some capability in this direction, but it's fairly limited as a pattern matching language compared to what Reg can do.