Skip to content

Instantly share code, notes, and snippets.

@VienosNotes
Created April 19, 2012 15:31
Show Gist options
  • Select an option

  • Save VienosNotes/2421749 to your computer and use it in GitHub Desktop.

Select an option

Save VienosNotes/2421749 to your computer and use it in GitHub Desktop.
Minimal XML Parser for Perl6 with Grammars
use v6;
grammar XML {
token word { \w+ }
token element { [ $<characters>=(<.word>+ % <.ws>) || <tag>] }
token attr { $<attr_name>=<word> '=' '"' $<attr_val>=<word> '"' }
token tag {
'<' $<tag_name>=<word> <.ws> [<attr>+ % <.ws>]? '>' <.ws>
[<element>* % <.ws>] <.ws>
'</' <{$<tag_name>}> '>'
}
token TOP { <tag>+ }
}
my $target = '<out>
<in1 attr1="value1" attr2="value2">str</in1>
<in2>hoge</in2>
foo bar baz
</out>';
my $match = XML.parse($target);
say $match;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment