Created
April 19, 2012 15:31
-
-
Save VienosNotes/2421749 to your computer and use it in GitHub Desktop.
Minimal XML Parser for Perl6 with Grammars
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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