in reply to more bracket chaos

Your solution doesn't scale. Suppose you've decided to use the first solution

(case c="(qd)whichfile(/qd)") <file1><insertfile>.... <file2... (/case)
and then later you need to enclose this in yet another case statement. OK, you can go and change all those <> to square brackets and normal brackets to <>. And what do you do next time? If the two case statements end up in yet another or in an if?

I think you need to find a different way to do this. Even if it means that the parser has to know these few statements as <if>, <case> and <for>.

Or maybe it would be best to parse the file as an ordinary XML and then process it FROM THE ROOT up, always parsing and processing the values of all attributes the same way the whole file is and then if there is a plugin specified for the tag pass both the already processed attributes and not processed content to the plugin and let it decide whether to call the processing for the subtags and then do something with the results or do something with the subtags and then call the recursive processing for the subtags. If there is no plugin, then all the subtags will be processed in order.

I've actually tried to write such a beast ... and found out that I can't write <tag attr="xxx <othertag/> yyy"/> so I decided to change < to { and > to } within the attributes --> <tag attr="xxx {othertag/} yyy"/>. Here's the result:

use strict; use XML::Rules; # at least 0.20 ! -- I just uploaded that version! my $parser = XML::Rules->new( rules => [ _default => 'raw extended arr +ay']); my %plugins; %plugins = ( set => sub { die "The <set> plugin requires a name attribute!\n" unless exi +sts($_[0]->{name}); my $content = $_[0]->{_content}; $plugins{$_[0]->{name}} = sub {$content}; return; }, print => sub { print $parser->ToXML( '', process($_[0]->{_content})); return; }, select => sub { die "The <select> plugin requires a switch attribute!\n" unles +s exists($_[0]->{switch}); my $switch = $_[0]->{switch}; if (exists $_[0]->{':' . $switch}) { # exists a subtag with th +at name return process($_[0]->{':' . $switch}[0]{_content}) } elsif (exists $_[0]->{':case'}) { # maybe there is a <case i +d="that name"> foreach my $case (@{$_[0]->{':case'}}) { return process($case->{_content}) if $case->{id} eq $s +witch; } return; } else { return; # the case was not found } }, ); my $data = $parser->parse( \*DATA); $data = $data->{_content}; #use Data::Dumper; #print Dumper($data); #print Dumper(process($data)); my $result = $parser->ToXML(@{(process($data))->[0]}); print "\n-----------------------------\n\n$result"; sub process { my ($what) = @_; if (! ref $what) { return $what } elsif (ref($what) eq 'ARRAY') { return [ map { if (!ref $_) { $_ } elsif (ref($_) eq 'ARRAY' and @$_ == 2) { processAttr($_->[1]); if (exists $plugins{$_->[0]}) { $plugins{$_->[0]}->($_->[1]) } else { $_->[1]{_content} = process($_->[1]{_content}); [ $_->[0] => $_->[1]] } } } @$what] } else { die "process() only accepts strings and array refs.\n"; } } sub processAttr { my ($attrs) = @_; foreach my $attr (keys %$attrs) { next if $attr =~ /^:/; next unless $attrs->{$attr} =~ /\{/; #print "$attrs->{$attr} -> "; $attrs->{$attr} =~ tr/{}/<>/; my $data = $parser->parse( '<root>' . $attrs->{$attr} . '</roo +t>'); $data = $data->{_content}[0][1]{_content}; #print "(" . Dumper($data) . ")"; $attrs->{$attr} = $parser->ToXML('', process($data)); #print "$attrs->{$attr}\n"; } } __END__ <html> <set name="foo">blah blah</set> <set name="bar">two</set> <set name="x">3</set> <h1>I say <foo/>, you know.</h1> <print>This is printed always!</print> <p> <select switch="{bar/}"> <one>This is one.</one> <two>This is the other.</two> <three>And this is yet another.</three> <print>This is NOT printed!</print> </select> </p> <p> <select switch="{x/}0"> <case id="10">This is ten.</case> <case id="20">This is twenty.</case> <case id="30">This is thirty.</case> <case id="41">This is fourty one.<print>This is not printed neither +!</print></case> </select> </p> </html>

The plugins get as their only parameter a reference to a hash containing the attributes, subtags and content of the tag. There are three kinds of keys in the hash:

Not sure it will work for you, but it was an interesting exercise.

Replies are listed 'Best First'.
Re^2: more bracket chaos
by simonodell (Acolyte) on Aug 31, 2007 at 21:05 UTC
    thanks for your reply, it was interesting having a look at your solution.

    my processor works with the examples I gave and more complex code. within a recursive structure where all the brackets are of the same bracket type my system allows virtually unlimited recursion, however when mixtures of brackets are used, it does as you pointed out have a limitation.

    my solution to this, was to make the insertfile routine restart the parser, and thus through multiple files you can indefinitely continue to use the 3 bracket structure. of course with anything very complicated this would become cumbersome having lots of sub files to deal with

    as a result i tend to try and be as abstract as possible within the xml-esque control file, and have the plugins do the donkey work. this also leaves the top control file being very close to an english description of what the plugins are doing below, such as <trylogin> or <uploadfile>. Whilst the structure allows for a lot of effects, it wasnt intended to be a scripting language of its own :)