Created
October 31, 2011 02:33
-
-
Save snarkyboojum/1326784 to your computer and use it in GitHub Desktop.
vCard
This file contains 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 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