Permalink
Browse files

Better handling of string constants. Added 'perl6_string_literal' fil…

…ter to TT.
  • Loading branch information...
1 parent bc2a436 commit 135cb55de0b3468b4bbbc2df3807fca03195a848 @timbunce committed Sep 5, 2010
Showing with 29 additions and 11 deletions.
  1. +19 −1 lib/Java/Javap/Generator/Perl6.pm
  2. +4 −4 t/03_class.t
  3. BIN testjavas/dupMethodTest.class
  4. +6 −6 testjavas/dupMethodTest.java
@@ -4,6 +4,7 @@ use strict;
use warnings;
use Template;
+use Template::Filters;
use Java::Javap;
use Java::Javap::TypeCast;
@@ -93,13 +94,29 @@ if (0) { # check that types actually are built-in to current perl6
}
}
+sub perl6_string_literal_filter {
+ my $value = shift;
+ $value =~ s/\\/\\\\/g;
+ $value =~ s/"/\\"/g;
+ $value =~ s/([^[:print:]])/ sprintf "\\u%04x", ord($1) /eg;
+ return qq{"$value"};
+}
+
+
sub new {
my $class = shift;
my $self = bless { @_ }, $class;
+ my $filters = Template::Filters->new({
+ FILTERS => {
+ perl6_string_literal => \&perl6_string_literal_filter,
+ },
+ });
+
my $tt_args = {
POST_CHOMP => 1,
+ LOAD_FILTERS => [ $filters ],
};
$self->tt_args_set( $tt_args );
@@ -342,7 +359,8 @@ method [% elem.name -%]
[% END %]
[% BLOCK constant_whole %]
method [% elem.name %] (--> [% elem.type.perl_type_name %]) is export {
- [%- IF elem.type.perl_type_name == 'Str' %] '[% elem.value | replace('\'','\\\'') %]'
+ [%- IF elem.type.perl_type_name == 'Str'
+ %] [% elem.value | perl6_string_literal %]
[%- ELSIF elem.type.perl_type_name == 'Int' or elem.type.perl_type_name == 'Num' %] [% elem.value %]
[%- ELSE %] ...
[% END %] }
View
@@ -249,10 +249,10 @@ use v6;
role dupMethodTest {
- method INT_CONSTANT (--> Int) is export { 4 }
- method FLOAT_CONSTANT (--> Num) is export { 4.2 }
- method A_STRING_CONSTANT (--> Str) is export { 'A string' }
- method B_STRING_CONSTANT (--> Str) is export { 'A \'str\'' }
+ method INT_CONST (--> Int) is export { 4 }
+ method FLOAT_CONST (--> Num) is export { 4.2 }
+ method A_STRING_CONST (--> Str) is export { "A string" }
+ method B_STRING_CONST (--> Str) is export { "A 'str' & \u0008ack" }
method new (
--> dupMethodTest # dupMethodTest
Binary file not shown.
@@ -1,9 +1,9 @@
public class dupMethodTest {
- public static final int INT_CONSTANT = 4;
- public static final double FLOAT_CONSTANT = 4.2;
- public static final java.lang.String A_STRING_CONSTANT = "A string";
- public static final java.lang.String B_STRING_CONSTANT = "A 'str'";
- // public static final java.lang.String C_STRING_CONSTANT = "A \\u2297='\u2297'"; not working
+ public static final int INT_CONST = 4;
+ public static final double FLOAT_CONST = 4.2;
+ public static final java.lang.String A_STRING_CONST = "A string";
+ public static final java.lang.String B_STRING_CONST = "A 'str' & \back";
+ // public static final java.lang.String C_STRING_CONST = "A \\u2297='\u2297'"; not working
public dupMethodTest() {}
// the following 2 methods end up with the same method signature
@@ -14,7 +14,7 @@ public dupMethodTest() {}
// this one should have a different signature
public java.lang.String dupMethod(java.lang.String[] num) {return "";}
- public int nonDupedMethod(char num) {return INT_CONSTANT;}
+ public int nonDupedMethod(char num) {return INT_CONST;}
}

0 comments on commit 135cb55

Please sign in to comment.