Skip to content

Commit efdbc04

Browse files
committed
Perlito5 - parser - string interpolation fix
1 parent 09eb964 commit efdbc04

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

TODO-perlito5

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ TODO list for Perlito5
6161
--- check signature in sort()
6262
--- fix the prototype for 'stat(*)' (see t/test.pl in the perl test suite)
6363

64-
-- $v->{x} doesn't interpolate inside double quotes
6564
-- deref inside double quotes:
6665
$ node perlito5.js -Bjs -e ' my $x = "123"; my $y = \$x; print "[$$y]\n" '
6766
[$[object Object]]

html/perlito5.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9564,6 +9564,40 @@ var p5100 = p5pkg['main'];
95649564
var v_p;
95659565
(v_p = (v_pos));
95669566
var v_m_index;
9567+
if ( (p5pkg["Perlito5::Grammar::String"].substr([v_str, v_p, 3], 0) == '->[') ) {
9568+
(function () {
9569+
(v_p = ((p5num(v_p) + 3)));
9570+
(v_m_index = (p5call(p5pkg["Perlito5::Expression"], "list_parse", [v_str, v_p], 0)));
9571+
if ( p5bool(v_m_index) ) {
9572+
null;
9573+
}
9574+
else {
9575+
p5pkg["Perlito5::Grammar::String"].die([['syntax error']], null);
9576+
};
9577+
var v_exp;
9578+
(v_exp = ((v_m_index || (v_m_index = new p5HashRef({})))._hash_.p5hget('capture')));
9579+
(v_p = ((v_m_index || (v_m_index = new p5HashRef({})))._hash_.p5hget('to')));
9580+
if ( ((p5str(v_exp) == '*undef*') || (p5pkg["Perlito5::Grammar::String"].substr([v_str, v_p, 1], 0) != ']')) ) {
9581+
p5pkg["Perlito5::Grammar::String"].die([['syntax error']], null);
9582+
};
9583+
(v_p)++;
9584+
(v_m_index || (v_m_index = new p5HashRef({})))._hash_.p5hset('capture', (p5call(p5pkg["Perlito5::AST::Call"], "new", p5list_to_a('method', 'postcircumfix:<[ ]>', 'invocant', (v_m_var || (v_m_var = new p5HashRef({})))._hash_.p5hget('capture'), 'arguments', v_exp), 0)));
9585+
(v_m_index || (v_m_index = new p5HashRef({})))._hash_.p5hset('to', (v_p));
9586+
throw(p5call(v_self, "double_quoted_var_with_subscript", [v_m_index, v_interpolate], p5want));
9587+
})();
9588+
};
9589+
if ( (p5pkg["Perlito5::Grammar::String"].substr([v_str, v_p, 3], 0) == '->{') ) {
9590+
(v_pos = ((p5num(v_pos) + 2)));
9591+
(v_m_index = (p5call(p5pkg["Perlito5::Expression"], "term_curly", [v_str, v_pos], 0)));
9592+
if ( p5bool(v_m_index) ) {
9593+
null;
9594+
}
9595+
else {
9596+
p5pkg["Perlito5::Grammar::String"].die([['syntax error']], null);
9597+
};
9598+
(v_m_index || (v_m_index = new p5HashRef({})))._hash_.p5hset('capture', (p5call(p5pkg["Perlito5::AST::Call"], "new", p5list_to_a('method', 'postcircumfix:<{ }>', 'invocant', (v_m_var || (v_m_var = new p5HashRef({})))._hash_.p5hget('capture'), 'arguments', (p5pkg["Perlito5::Match"].flat([v_m_index], p5want) || (p5pkg["Perlito5::Match"].flat([v_m_index], p5want) = new p5ArrayRef([])))._array_.p5aget_array(2)._array_.p5aget(0)), 0)));
9599+
throw(p5call(v_self, "double_quoted_var_with_subscript", [v_m_index, v_interpolate], p5want));
9600+
};
95679601
if ( (p5pkg["Perlito5::Grammar::String"].substr([v_str, v_p, 1], 0) == '[') ) {
95689602
if ( (p5num(v_interpolate) == 2) ) {
95699603
(function () {

perlito5.pl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5086,6 +5086,37 @@ sub Perlito5::Grammar::String::double_quoted_var_with_subscript {
50865086
((my $pos) = $m_var->{'to'});
50875087
((my $p) = $pos);
50885088
(my $m_index);
5089+
if ((substr($str, $p, 3) eq '->[')) {
5090+
($p = ($p + 3));
5091+
($m_index = Perlito5::Expression->list_parse($str, $p));
5092+
if ($m_index) {
5093+
5094+
}
5095+
else {
5096+
die('syntax error')
5097+
};
5098+
((my $exp) = $m_index->{'capture'});
5099+
($p = $m_index->{'to'});
5100+
if ((($exp eq '*undef*') || (substr($str, $p, 1) ne ']'))) {
5101+
die('syntax error')
5102+
};
5103+
($p)++;
5104+
($m_index->{'capture'} = Perlito5::AST::Call->new('method', 'postcircumfix:<[ ]>', 'invocant', $m_var->{'capture'}, 'arguments', $exp));
5105+
($m_index->{'to'} = $p);
5106+
return ($self->double_quoted_var_with_subscript($m_index, $interpolate))
5107+
};
5108+
if ((substr($str, $p, 3) eq '->{')) {
5109+
($pos = ($pos + 2));
5110+
($m_index = Perlito5::Expression->term_curly($str, $pos));
5111+
if ($m_index) {
5112+
5113+
}
5114+
else {
5115+
die('syntax error')
5116+
};
5117+
($m_index->{'capture'} = Perlito5::AST::Call->new('method', 'postcircumfix:<{ }>', 'invocant', $m_var->{'capture'}, 'arguments', Perlito5::Match::flat($m_index)->[2]->[0]));
5118+
return ($self->double_quoted_var_with_subscript($m_index, $interpolate))
5119+
};
50895120
if ((substr($str, $p, 1) eq '[')) {
50905121
if (($interpolate == 2)) {
50915122
((my $m) = ((Perlito5::Expression->term_digit($str, ($p + 1)) || (((substr($str, ($p + 1), 1) eq '-') && Perlito5::Expression->term_digit($str, ($p + 2))))) || Perlito5::Grammar::Sigil->term_sigil($str, ($p + 1))));

src5/lib/Perlito5/Grammar/String.pm

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,33 @@ sub double_quoted_var_with_subscript {
715715
my $pos = $m_var->{to};
716716
my $p = $pos;
717717
my $m_index;
718+
if (substr($str, $p, 3) eq '->[') {
719+
$p += 3;
720+
$m_index = Perlito5::Expression->list_parse($str, $p);
721+
die "syntax error" unless $m_index;
722+
my $exp = $m_index->{capture};
723+
$p = $m_index->{to};
724+
die "syntax error" if $exp eq '*undef*' || substr($str, $p, 1) ne ']';
725+
$p++;
726+
$m_index->{capture} = Perlito5::AST::Call->new(
727+
method => 'postcircumfix:<[ ]>',
728+
invocant => $m_var->{capture},
729+
arguments => $exp,
730+
);
731+
$m_index->{to} = $p;
732+
return $self->double_quoted_var_with_subscript($m_index, $interpolate);
733+
}
734+
if (substr($str, $p, 3) eq '->{') {
735+
$pos += 2;
736+
$m_index = Perlito5::Expression->term_curly($str, $pos);
737+
die "syntax error" unless $m_index;
738+
$m_index->{capture} = Perlito5::AST::Call->new(
739+
method => 'postcircumfix:<{ }>',
740+
invocant => $m_var->{capture},
741+
arguments => Perlito5::Match::flat($m_index)->[2][0],
742+
);
743+
return $self->double_quoted_var_with_subscript($m_index, $interpolate);
744+
}
718745
if (substr($str, $p, 1) eq '[') {
719746

720747
if ($interpolate == 2) {

0 commit comments

Comments
 (0)