in reply to Re: Re: Using Perl6 patterns/grammar definition for <I>output</I>?
in thread Using Perl6 patterns/grammar definition for 'output'?
Using a separate formating object has several advantages. Firstly, it avoids the parallel hierachies maintenance problem (you want formatting to be robust against modifications to the grammar). Even if you are only overriding the terminal rules of the grammar, you still have the issue that a change to the base grammar would be obscured in the derived grammar.
A more important issue is that it will be common to import terminal symbols from other grammars (e.g. CORE::*). How many grammars to you want to override, just to format some output?
I see a formatting object as a simple map of rule_name => format spec; with some mechanism for composition. This doesn't need to follow the hierarchy of a grammer:
The output method would traverse the grammar; each time it meets a rule that has a format, it applies that format to the corresponding entry in the match object ($0). This does not require a derived grammar: just an extra specification object. Sometimes composition is better than inheritance.my $format = new Format ( ws => { col >= 70 ? "\n" : " " }, currency => { sprintf("%.2f", $currency) }, date => {sprintf("%04d-%02d-%02d", @$date{qw/year month day/})}, ); $text =~ /<grammar.rule>/; $format.output($0; grammar=>"grammar.rule", fh=>$*STDOUT)
--Dave
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Using Perl6 patterns/grammar definition for <I>output</I>?
by John M. Dlugosz (Monsignor) on Sep 05, 2002 at 20:39 UTC | |
by dpuu (Chaplain) on Sep 05, 2002 at 21:28 UTC | |
by John M. Dlugosz (Monsignor) on Sep 06, 2002 at 15:49 UTC |