Skip to content

Commit

Permalink
Changing recce->alternative() interface: t+
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey Kegler authored and Jeffrey Kegler committed May 23, 2012
1 parent a19509d commit 69f6ee4
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 35 deletions.
21 changes: 15 additions & 6 deletions r2/lib/Marpa/R2/Recognizer.pm
Expand Up @@ -539,14 +539,14 @@ sub Marpa::R2::Recognizer::show_progress {
} ## end sub Marpa::R2::Recognizer::show_progress

sub Marpa::R2::Recognizer::read {
my $recce = shift;
return if not $recce->alternative(@_);
my ($recce, $symbol_name, $value) = @_;
return if not $recce->alternative($symbol_name, \$value);
return $recce->earleme_complete();
}

sub Marpa::R2::Recognizer::alternative {

my ( $recce, $symbol_name, $value, $length ) = @_;
my ( $recce, $symbol_name, $value_ref, $length ) = @_;

Marpa::R2::exception(
'No recognizer object for Marpa::R2::Recognizer::tokens')
Expand All @@ -570,10 +570,19 @@ sub Marpa::R2::Recognizer::alternative {
}

my $value_ix = -1;
if ( defined $value ) {
SET_VALUE_IX: {
last SET_VALUE_IX if not defined $value_ref;
my $ref_type = ref $value_ref;
if ( $ref_type ne 'SCALAR'
and $ref_type ne 'REF'
and $ref_type ne 'VSTRING' )
{
Marpa::R2::exception(
qq{alternative(): value must be undef or ref});
} ## end if ( $ref_type ne 'SCALAR' and $ref_type ne 'REF' and...)
$value_ix = scalar @{$token_values};
push @{$token_values}, $value;
}
push @{$token_values}, ${$value_ref};
} ## end SET_VALUE_IX:
$length //= 1;

my $result = $recce_c->alternative( $symbol_id, $value_ix, $length );
Expand Down
10 changes: 5 additions & 5 deletions r2/pperl/Marpa/R2/Perl.pm
Expand Up @@ -1073,7 +1073,7 @@ sub Marpa::R2::Perl::read {
if ( $perl_type eq 'WORD' ) {
my $token_found = 0;
TYPE: for my $type (qw(WORD FUNC METHOD FUNCMETH)) {
defined $recce->alternative( $type, $content, 1 )
defined $recce->alternative( $type, \$content, 1 )
and $token_found++;
}
$token_found
Expand Down Expand Up @@ -1119,7 +1119,7 @@ sub Marpa::R2::Perl::read {
TYPE: for my $type (@potential_types) {
next TYPE if not $type ~~ $expected_tokens;
$token_found = 1;
defined $recce->alternative( $type, $content, 1 )
defined $recce->alternative( $type, \$content, 1 )
or token_not_accepted( $token, $type, $content, 1 );
} ## end for my $type (@potential_types)
defined $token_found or unknown_ppi_token($token);
Expand All @@ -1138,7 +1138,7 @@ sub Marpa::R2::Perl::read {
TYPE: for my $type (@potential_types) {
next TYPE if not $type ~~ $expected_tokens;
$token_found = 1;
defined $recce->alternative( $type, $content, 1 )
defined $recce->alternative( $type, \$content, 1 )
or token_not_accepted( $token, $type, $content, 1 );
} ## end for my $type (@potential_types)
defined $token_found or unknown_ppi_token($token);
Expand Down Expand Up @@ -1185,7 +1185,7 @@ sub Marpa::R2::Perl::read {
TYPE: for my $type (@potential_types) {
next TYPE if not $type ~~ $expected_tokens;
$token_found = 1;
defined $recce->alternative( $type, $content, 1 )
defined $recce->alternative( $type, \$content, 1 )
or token_not_accepted( $token, $type, $content, 1 );
} ## end for my $type (@potential_types)
defined $token_found or unknown_ppi_token($token);
Expand All @@ -1204,7 +1204,7 @@ sub Marpa::R2::Perl::read {
my $content = $token->{content};
my $token_found = 0;
TYPE: for my $type (qw(THING VERSION)) {
defined $recce->alternative( $type, $content, 1 )
defined $recce->alternative( $type, \$content, 1 )
and $token_found++;
}
$token_found or token_not_accepted( $token, 'THING', $content );
Expand Down
18 changes: 9 additions & 9 deletions r2/t/infinite.t
Expand Up @@ -124,7 +124,7 @@ package main;
my $cycle1_test = [
'cycle1',
$Test_Grammar::MARPA_OPTIONS_1,
[ [ [ 's', '1' ] ] ],
[ [ [ 's', \'1' ] ] ],
'1',
<<'EOS'
Cycle found involving rule: 0: s -> s
Expand All @@ -134,23 +134,23 @@ EOS
my $cycle2_test = [
'cycle2',
$Test_Grammar::MARPA_OPTIONS_2,
[ [ [ 'a', '1' ] ] ],
[ [ [ 'a', \'1' ] ] ],
'1',
<<'EOS'
Cycle found involving rule: 0: s -> a
Cycle found involving rule: 1: a -> s
EOS
];

my @cycle8_tokens = ( [ [ 'e', '1' ], [ 'v', '1' ], [ 'w', '1' ] ] );
my @cycle8_tokens = ( [ [ 'e', \'1' ], [ 'v', \'1' ], [ 'w', \'1' ] ] );

push @cycle8_tokens, map {
( [ [ 'e', $_ ],
[ 't', $_ ],
[ 'u', $_ ],
[ 'v', $_ ],
[ 'w', $_ ],
[ 'x', $_ ]
( [ [ 'e', \$_ ],
[ 't', \$_ ],
[ 'u', \$_ ],
[ 'v', \$_ ],
[ 'w', \$_ ],
[ 'x', \$_ ]
],
)
} qw( 2 3 4 5 6 );
Expand Down
5 changes: 3 additions & 2 deletions r2/t/limits.t
Expand Up @@ -61,7 +61,8 @@ sub test_grammar {
for my $token ( @{$tokens} ) {
my $earleme_result;
$eval_ok = eval {
$earleme_result = $recce->read( @{$token} );
$recce->alternative( @{$token} );
$earleme_result = $recce->earleme_complete();
1;
};
die "Exception while recognizing earleme:\n$EVAL_ERROR"
Expand Down Expand Up @@ -98,7 +99,7 @@ my $placebo = {

sub gen_tokens {
my ($earleme_length) = @_;
return [ [ 'a', 'a', 1 ], [ 'a', 'a', $earleme_length ] ];
return [ [ 'a', \'a', 1 ], [ 'a', \'a', $earleme_length ] ];
}

my $value;
Expand Down
4 changes: 2 additions & 2 deletions r2/t/minus.t
Expand Up @@ -177,8 +177,8 @@ my %expected = map { ( $_ => 1 ) } (

$recce->read( 'Number', '6' );
for ( 1 .. 4 ) {
$recce->alternative( 'MinusMinus', q{--}, 2 );
$recce->alternative( 'Minus', q{-} );
$recce->alternative( 'MinusMinus', \q{--}, 2 );
$recce->alternative( 'Minus', \q{-} );
$recce->earleme_complete();
}
$recce->read( 'Minus', q{-}, );
Expand Down
18 changes: 9 additions & 9 deletions r2/t/rabend.t
Expand Up @@ -98,11 +98,11 @@ sub duplicate_terminal_1 {
# Marpa::R2::Display
# name: Recognizer alternative Synopsis

$recce->alternative( 'a', 42, 1 ) or return 'First alternative failed';
$recce->alternative( 'a', \42, 1 ) or return 'First alternative failed';

# Marpa::R2::Display::End

return 1 if $recce->alternative( 'a', 711, 1 );
return 1 if $recce->alternative( 'a', \711, 1 );
return;
} ## end sub duplicate_terminal_1

Expand Down Expand Up @@ -140,9 +140,9 @@ $recce = Marpa::R2::Recognizer->new(
sub duplicate_terminal_2 {

# Should be OK, because different symbols
$recce->alternative( 'a', 11, 1 )
$recce->alternative( 'a', \11, 1 )
or return 'alternative a at 0 failed';
$recce->alternative( 'b', 12, 1 )
$recce->alternative( 'b', \12, 1 )
or return 'alternative b at 0 failed';

# Marpa::R2::Display
Expand All @@ -153,18 +153,18 @@ sub duplicate_terminal_2 {
# Marpa::R2::Display::End

# Should be OK, because different lengths
$recce->alternative( 'a', 21, 3 )
$recce->alternative( 'a', \21, 3 )
or return 'alternative a at 1 failed';
$recce->alternative( 'a', 22, 1 )
$recce->alternative( 'a', \22, 1 )
or return 'alternative b at 1 failed';
$recce->earleme_complete();
$recce->alternative( 'd', 42, 2 )
$recce->alternative( 'd', \42, 2 )
or return 'first alternative d at 2 failed';
$recce->alternative( 'b', 22, 1 )
$recce->alternative( 'b', \22, 1 )
or return 'alternative b at 1 failed';

# this should cause an abend -- a 2nd d, with the same length
return 1 if $recce->alternative( 'd', 711, 2 );
return 1 if $recce->alternative( 'd', \711, 2 );
return;
} ## end sub duplicate_terminal_2

Expand Down
2 changes: 1 addition & 1 deletion r2/t/randal.t
Expand Up @@ -203,7 +203,7 @@ TEST: for my $test_data (@test_data) {
if not $test_input =~ m{ \G \s* (?<match>$regex) }xgms;

## no critic (Variables::ProhibitPunctuationVars)
$recce->alternative( $token, $+{match},
$recce->alternative( $token, \$+{match},
( ( pos $test_input ) - $pos ) );

} ## end while ( my ( $token, $regex ) = each %regexes )
Expand Down
2 changes: 1 addition & 1 deletion r2/t/timeflies.t
Expand Up @@ -117,7 +117,7 @@ for my $data ( 'time flies like an arrow', 'fruit flies like a banana' ) {
# Marpa::R2::Display::End

for my $type ( @{ $vocabulary{$word} } ) {
$recce->alternative( $type, $word, 1 )
$recce->alternative( $type, \$word, 1 )
or die 'Recognition failed';
}
$recce->earleme_complete();
Expand Down

0 comments on commit 69f6ee4

Please sign in to comment.