Skip to content

Commit

Permalink
Encode and decode strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
colomon committed Dec 9, 2013
1 parent e73d236 commit f3b1060
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 24 deletions.
30 changes: 30 additions & 0 deletions lib/ISO_10303_21/Actions.pm
Expand Up @@ -29,6 +29,36 @@ class ISO_10303_21::Actions {
method extended4($/) {
make $<hex_four>.map({ :16(~$_).chr }).join;
}
method control_directive($/) {
if $<page> {
make $<page>.ast;
} elsif $<alphabet> {
make $<alphabet>.ast;
} elsif $<extended2> {
make $<extended2>.ast;
} elsif $<extended4> {
make $<extended4>.ast;
} else {
make $<arbitrary>.ast;
}
}
method non_q_char($/) {
make ~$/;
}
method string_char($/) {
if $<non_q_char> {
make $<non_q_char>.ast;
} elsif $<apostrophe> {
make "'";
} elsif $<reverse_solidus> {
make "\\";
} else {
make $<control_directive>.ast;
}
}
method string($/) {
make @($<string_char>).map(*.ast).join;
}

method parameter($/) {
if $<typed_parameter> {
Expand Down
49 changes: 25 additions & 24 deletions t/06-strings.t
Expand Up @@ -4,32 +4,33 @@ use ISO_10303_21::Grammar;
use ISO_10303_21::Actions;
use ISO_10303_21::Utils;

plan 13;
plan 21;

# {
# my $match = ISO_10303_21::Grammar.parse('\X\42', :rule<arbitrary>, :actions(ISO_10303_21::Actions.new));
# isa_ok $match, Match, "<arbitrary> matches \\X\\42 - 1";
# ok $match, "<arbitrary> matches \\X\\42 - 2";
# is $match.ast, "B", "ast is correct";
# }
#
# {
# my $match = ISO_10303_21::Grammar.parse('\X2\03BA1F7903C303BC03B5\X0\\', :rule<extended2>,
# :actions(ISO_10303_21::Actions.new));
# isa_ok $match, Match, "<extended2> matches \\X2\\ - 1";
# ok $match, "<extended2> matches \\X2\\ - 2";
# is $match.ast.ords.join(","), "954,8057,963,956,949", "ast is correct";
# }
#
# {
# my $match = ISO_10303_21::Grammar.parse('\X4\000003BA00001F79000003C3000003BC000003B5\X0\\', :rule<extended4>,
# :actions(ISO_10303_21::Actions.new));
# isa_ok $match, Match, "<extended4> matches \\X4\\ - 1";
# ok $match, "<extended4> matches \\X4\\ - 2";
# is $match.ast.ords.join(","), "954,8057,963,956,949", "ast is correct";
# }
{
my $match = ISO_10303_21::Grammar.parse('\X\42', :rule<arbitrary>, :actions(ISO_10303_21::Actions.new));
isa_ok $match, Match, "<arbitrary> matches \\X\\42 - 1";
ok $match, "<arbitrary> matches \\X\\42 - 2";
is $match.ast, "B", "ast is correct";
}

my @strings = "Ἰοὺ ἰού· τὰ πάντʼ ἂν ἐξήκοι σαφῆ.";
{
my $match = ISO_10303_21::Grammar.parse('\X2\03BA1F7903C303BC03B5\X0\\', :rule<extended2>,
:actions(ISO_10303_21::Actions.new));
isa_ok $match, Match, "<extended2> matches \\X2\\ - 1";
ok $match, "<extended2> matches \\X2\\ - 2";
is $match.ast.ords.join(","), "954,8057,963,956,949", "ast is correct";
}

{
my $match = ISO_10303_21::Grammar.parse('\X4\000003BA00001F79000003C3000003BC000003B5\X0\\', :rule<extended4>,
:actions(ISO_10303_21::Actions.new));
isa_ok $match, Match, "<extended4> matches \\X4\\ - 1";
ok $match, "<extended4> matches \\X4\\ - 2";
is $match.ast.ords.join(","), "954,8057,963,956,949", "ast is correct";
}

my @strings = "Ἰοὺ ἰού· τὰ πάντʼ ἂν ἐξήκοι σαφῆ.", "子曰:「學而時習之,不亦說乎?有朋自遠方來,不亦樂乎? 人不知而不慍,不亦君子乎?」",
(0x28010, 0x28011, 0x28012).chrs;

for @strings -> $string {
my $encoded = EncodeString($string);
Expand Down

0 comments on commit f3b1060

Please sign in to comment.