1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
# Grammar to parse the UCA collation data
grammar Collation-Gram {
token TOP {
<codepoints>
\s* ';' \s*
<coll-key>+
<comment>
.*
}
token codepoints {
<codepoint>+ % \s+
}
token codepoint {
<:AHex>+
#[$<cp>=(<:AHex>)\s+]
}
token comment { \s* '#' \s* <( .* $ }
token coll-key {
'[' ~ ']'
[
<dot-star> <primary> '.' <secondary> '.' <tertiary>
]
}
token dot-star { <[.*]> }
token primary { <:AHex>+ }
token secondary { <:AHex>+ }
token tertiary { <:AHex>+ }
}
class Collation-Gram::Action {
has @!array;
has $!comment;
has $!dot-star;
has @!codepoints;
method TOP ($/) {
@!codepoints = @!codepoints.chrs.ords;
make %(
array => @!array,
comment => ~$<comment>,
codepoints => @!codepoints.chrs.ords
)
}
method coll-key ($/) {
my $a = ($<primary>, $<secondary>, $<tertiary>).map(*.Str.parse-base(16)).Array;
$a.push: ($<dot-star> eq '.' ?? 0 !! $<dot-star> eq '*' ?? 1 !! do { die $<dot-star> });
@!array.push: $a;
}
method codepoints ($/) {
@!codepoints.append: $<codepoint>.map(*.Str.parse-base(16));
}
}
|