From: Charles Comstock Date: 2004-03-01T20:44:46+09:00 Subject: Re: perl6 grammar rules in ruby Mauricio Fern�ndez wrote: > On Mon, Mar 01, 2004 at 05:14:47PM +0900, Charles Comstock wrote: > >>Has anyone taken a look at the idea of having embedded grammars in ruby >>like perl6 is intending to add? For instance, the example they give: >> >> grammar Letter { >> rule text { } >> >> rule greet :w { [Hi|Hey|Yo] $to:=(\S+?) , $$} >> >> rule body { + } >> >> rule close :w { Later dude, $from:=(.+) } >> >> # etc. >> } > > > I believe this can be implemented easily in Ruby as it stands now, > without additional keywords. Many domain specific languages have been > defined this way. > Two problems that I see with directly implementing this in Ruby: 1) That example didn't show it but they are embedding grammars inside of native regex, which is something we can't currently do and still get the look-ahead, fail, fall-back and try again aspect of the ruby regex. 2) The section immediatly afterwords that was in my post, which you deleted, when they derived a sub grammar from this specification and added more specific rules for that grammar. This seems easier to overload in ruby, but I forsee problems here. grammar FormalLetter is Letter { rule greet :w { Dear $to:=(\S+?) , $$} rule close :w { Yours sincerely, $from:=(.+) } } If you had a grammar block like this in a more ruby-esque syntax I would think it would be more like this: class Letter < Grammar rule :text {greet; body; close} ... end class FormalLetter < Letter rule :greet,:w { /Dear (\S+?)/ ... Here we run into a bunch of problems with translation. I will start thinking of a nice way to embed this in the ruby syntax, as I don't really think that much of the Perl style syntax. While the inheritence portion MAY be possible to implement in the current ruby syntax, other parts would definitely need a custom regex engine, which probably sacrifices speed amoung other things. Charles Comstock