Skip to content

Commit

Permalink
Rewrite of parse_name in NQP
Browse files Browse the repository at this point in the history
With many style fixes from moritz
  • Loading branch information
martifaj authored and moritz committed Mar 8, 2010
1 parent 49749c0 commit e4e1a0d
Showing 1 changed file with 36 additions and 44 deletions.
80 changes: 36 additions & 44 deletions src/Perl6/Grammar.pm
Expand Up @@ -1283,51 +1283,43 @@ grammar Perl6::Regex is Regex::P6Regex::Grammar {
}
}

our %is_sigil;
INIT {
our %is_sigil;
%is_sigil{'$'} := 1;
%is_sigil{'@'} := 1;
%is_sigil{'%'} := 1;
%is_sigil{'&'} := 1;
}


sub parse_name($name) {
# XXX Some enterprising soul could re-write this in NQP. ;-)
Q:PIR {
.local string name
$P0 = find_lex '$name'
name = $P0
## remove any type parameterization for now
.local string type_param
type_param = ''
$I0 = index name, '['
if $I0 == -1 goto type_param_done
type_param = substr name, $I0
name = substr name, 0, $I0
type_param_done:
## divide name based on ::
.local pmc list
list = split '::', name
## move any leading sigil to the last item
.local string sigil
$S0 = list[0]
sigil = substr $S0, 0, 1
$I0 = index '$@%&', sigil
if $I0 < 0 goto sigil_done
substr $S0, 0, 1, ''
list[0] = $S0
$S0 = list[-1]
$S0 = concat sigil, $S0
list[-1] = $S0
sigil_done:
## remove any empty items from the list
$P0 = iter list
list = new 'ResizablePMCArray'
iter_loop:
unless $P0 goto iter_done
$S0 = shift $P0
unless $S0 goto iter_loop
push list, $S0
goto iter_loop
iter_done:
if type_param == '' goto no_add_type_param
$S0 = pop list
concat $S0, type_param
push list, $S0
no_add_type_param:
.return (list)
my $type_param := '';
my $sep := pir::index__ISS($name,'[');
if ($sep > -1) {
$type_param := pir::substr__SSII($name, $sep);
$name := pir::substr__SSII($name, 0, $sep);
}

my @parts := pir::split__PSS('::', $name);
my $sigil := pir::substr__SSII(@parts[0], 0, 1);
if %is_sigil{$sigil} {
@parts[0] := pir::substr__SSII(@parts[0], 1);
my $last_part := @parts.pop();
$last_part := $sigil ~ $last_part;
@parts.push($last_part);
}

my @result;
for @parts {
@result.push($_) if $_;
}

if $type_param {
my $last_part := @result.pop();
$last_part := $last_part ~ $type_param;
@result.push($last_part);
}

@result;
}

0 comments on commit e4e1a0d

Please sign in to comment.