Skip to content

Instantly share code, notes, and snippets.

@snarkyboojum
Created October 31, 2011 02:33
Show Gist options
  • Save snarkyboojum/1326784 to your computer and use it in GitHub Desktop.
Save snarkyboojum/1326784 to your computer and use it in GitHub Desktop.
vCard
use v6;
grammar Vcard::Grammar {
rule TOP {
<begin>
<version>
<property> **
<end>
}
rule version {
'VERSION' ':' <number>
}
rule property {
<key> ':' <value>
}
token number {
<[0..9]>+ '.' <[0..9]>+
}
token key {
<-[:]>+
}
token value {
<-[:]>+
}
token begin {
'BEGIN:VCARD'
}
token end {
'END:VCARD'
}
}
# test the parser
my $vcard = Q{
BEGIN:VCARD
VERSION:3.0
N:Adrian White
END:VCARD
};
my $match = Vcard::Grammar.parse($vcard);
if $match {
say 'Parsed vCard (ver ' ~ $match<version><number> ~ '):';
for $match<property> -> $p {
say $p<key>;
say $p<value>;
}
}
else {
say "Failed to parse vCard";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment