Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Changed the grammar to make the match object easier to walk through

  • Loading branch information...
commit 1b974536856270aaf183f259908ecf47c2a81953 1 parent dcd8548
@krunen authored
Showing with 61 additions and 20 deletions.
  1. +42 −7 lib/XML.pm
  2. +14 −10 lib/XML/Grammar/Document.pm
  3. +5 −0 min.xml
  4. +0 −3  t.pl
View
49 lib/XML.pm
@@ -3,21 +3,54 @@ use XML::Grammar::Document;
sub parse (Str $str) {
if ($str ~~ /<XML::Grammar::Document::TOP>/) {
- return dumptree($/<XML::Grammar::Document::TOP>);
+ #return dumptree($/<XML::Grammar::Document::TOP>);
+ #say $/.perl;
+ dump_node($/<XML::Grammar::Document::TOP><root>);
} else {
return "no match";
}
}
-sub dumptree ($tree,$indent=' ') {
+sub dump ($docmatch) {
+}
+
+sub dump_node($node,$indent='') {
+ say $indent ~ $node<name> ~ " => \{";
+ if ($node<attribute>) {
+ for @($node<attribute>) -> $a {
+ say "$indent " ~ $a<name> ~ " => \"" ~ $a<value> ~ "\"";
+ }
+ }
+ if ($node<child>) {
+ for @($node<child>) -> $c {
+ if ($c<cdata>) {
+ say "$indent CDATA:" ~ $c<cdata>;
+ } elsif ($c<text>) {
+ my $txt = $c<text>;
+ $txt = $txt.subst(/\s+/, " ", :g);
+ chomp($txt);
+ say "$indent TEXT:" ~ $txt;
+ } elsif ($c<comment>) {
+ say "$indent COMMENT: " ~ $c<comment>;
+ } elsif ($c<pi>) {
+ say "$indent PI: " ~ $c<pi>;
+ } elsif ($c<element>) {
+ dump_node($c<element>,"$indent ");
+ }
+ }
+ }
+ say $indent ~ "\},";
+}
+
+sub dumptree_old ($tree,$indent=' ') {
if ($tree.hash) {
for $tree.hash.kv -> $k,$v {
- if ($v !~~ Str) {
- say "$indent$k => \{";
+ if ($v.list or $v.hash) {
+ say "$indent'$k' => \{";
dumptree($v, $indent~' ');
say "$indent\},";
} else {
- say "$indent$k => " ~ $v.substr(0,60).subst(/\n/,' ') ~ ",";
+ say "$indent'$k' => \"" ~ $v.substr(0,60).subst(/\n/,' ') ~ "\",";
}
}
}
@@ -25,14 +58,16 @@ sub dumptree ($tree,$indent=' ') {
my $i=0;
for $tree.list -> $v {
if ($v !~~ Str and ($v.list or $v.hash)) {
- say $indent~"$i => \{";
+ say $indent~"$i => \[";
dumptree($v, $indent~' ') if $v !== $tree;
- say "$indent\},";
+ say "$indent\],";
} else {
say $indent~"TEXT => " ~ ($v.chars >= 60 ?? ($v.substr(0,58).subst(/\n/,' ')~"..") !! $v.subst(/\n/,' ')) ~ ",";
}
$i++;
}
+ } else {
+ say "$indent\"$tree\"";
}
return '';
}
View
24 lib/XML/Grammar/Document.pm
@@ -8,18 +8,22 @@ rule TOP {
$
}
-token comment { '<!--' ~ '-->' $<content> = .*? }
-token pi { '<?' ~ '?>' $<content> = .*? }
+token comment { '<!--' ~ '-->' <content> }
+token pi { '<?' ~ '?>' <content> }
+token content { .*? }
rule xmldecl {
'<?xml'
- 'version' '=' '"' $<version> = <-[\"]>+ '"'
- 'encoding' '=' '"' $<encoding> = <-[\"]>+ '"'
+ 'version' '=' '"' ~ '"' <version>
+ 'encoding' '=' '"' ~ '"' <encoding>
'?>'
}
+token version { <-[\"]>+ }
+token encoding { <-[\"]>+ }
+token value { <-[\"]>+ }
rule doctypedecl {
- '<!DOCTYPE ' <name> $<therest> = <-[\>]>* '>'
+ '<!DOCTYPE ' <name> ~ '>' <content>
}
rule element {
@@ -30,22 +34,22 @@ rule element {
]
}
-rule attribute { <name> '=' '"' $<value> = <-["]>* '"' }
+rule attribute {
+ <name> '=' '"' ~ '"' <value>
+}
rule child {
- [
| <element>
| <cdata>
| <text=textnode>
| <comment>
| <pi>
- ]
}
token cdata {
- '<![CDATA[' ~ ']]>' $<text> = .*?
+ '<![CDATA[' ~ ']]>' <content>
}
token textnode { <-[<]>+ }
-token name { <alpha><ident>+ }
+token name { <.alpha><.ident>+[:<.ident>+]? }
View
5 min.xml
@@ -1,3 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE minimumxml SYSTEM "minimum.dtd">
<boksamling>
+ Dette er en tekst
+ <bok id="1">
+ <tittel>Boken</tittel>
+ </bok>
</boksamling>
View
3  t.pl
@@ -1,3 +0,0 @@
-use XML;
-
-my $xml = XML::parse(slurp(@*ARGS[0]));
Please sign in to comment.
Something went wrong with that request. Please try again.