Skip to content

Commit

Permalink
- Minor performance improvement inside the parser for parsing delimit…
Browse files Browse the repository at this point in the history
…ers around variable declaractions.

Example:
	var(a, b, c);  # the parentheses are now parsed much faster
  • Loading branch information
trizen committed Aug 20, 2015
1 parent 8d5b9c6 commit e689281
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -527,21 +527,37 @@ package Sidef::Parser {
return ({self => $obj}, 0, '');
}

sub get_init_vars {
sub _parse_delim {
my ($self, %opt) = @_;

local *_ = $opt{code};

my @delims = ('|', keys(%{$self->{delim_pairs}}));
if (exists $opt{ignore_delim}) {
@delims = grep { not exists $opt{ignore_delim}{$_} } @delims;
}

my $regex = do {
local $" = "";
qr/\G([@delims])\h*/;
};

my $end_delim;
foreach my $key ('|', (keys %{$self->{delim_pairs}})) {
next if exists $opt{ignore_delim} and exists $opt{ignore_delim}{$key};
if (/\G\Q$key\E\h*/gc) {
$end_delim = $self->{delim_pairs}{$key} // '|';
$self->parse_whitespace(code => $opt{code});
last;
}
if (/$regex/gc) {
$end_delim = $self->{delim_pairs}{$1} // $1;
$self->parse_whitespace(code => $opt{code});
}

return $end_delim;
}

sub get_init_vars {
my ($self, %opt) = @_;

local *_ = $opt{code};

my $end_delim = $self->_parse_delim(%opt);

my @vars;
while (/\G([*:]?$self->{var_name_re})/goc) {
push @vars, $1;
Expand Down Expand Up @@ -576,15 +592,7 @@ package Sidef::Parser {

local *_ = $opt{code};

my $end_delim;
foreach my $key ('|', (keys %{$self->{delim_pairs}})) {
next if exists $opt{ignore_delim} and exists $opt{ignore_delim}{$key};
if (/\G\Q$key\E\h*/gc) {
$end_delim = $self->{delim_pairs}{$key} // '|';
$self->parse_whitespace(code => $opt{code});
last;
}
}
my $end_delim = $self->_parse_delim(%opt);

my @var_objs;
while (/\G([*:]?)($self->{var_name_re})/goc) {
Expand Down

0 comments on commit e689281

Please sign in to comment.