Skip to content
Browse files

Changing recce->alternative() interface: t+

  • Loading branch information...
1 parent a19509d commit 69f6ee492c810087c590e5644dd9e995d341f34b Jeffrey Kegler committed May 22, 2012
Showing with 45 additions and 35 deletions.
  1. +15 −6 r2/lib/Marpa/R2/Recognizer.pm
  2. +5 −5 r2/pperl/Marpa/R2/Perl.pm
  3. +9 −9 r2/t/infinite.t
  4. +3 −2 r2/t/limits.t
  5. +2 −2 r2/t/minus.t
  6. +9 −9 r2/t/rabend.t
  7. +1 −1 r2/t/randal.t
  8. +1 −1 r2/t/timeflies.t
View
21 r2/lib/Marpa/R2/Recognizer.pm
@@ -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')
@@ -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 );
View
10 r2/pperl/Marpa/R2/Perl.pm
@@ -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
@@ -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);
@@ -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);
@@ -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);
@@ -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 );
View
18 r2/t/infinite.t
@@ -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
@@ -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 );
View
5 r2/t/limits.t
@@ -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"
@@ -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;
View
4 r2/t/minus.t
@@ -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{-}, );
View
18 r2/t/rabend.t
@@ -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
@@ -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
@@ -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
View
2 r2/t/randal.t
@@ -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 )
View
2 r2/t/timeflies.t
@@ -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();

0 comments on commit 69f6ee4

Please sign in to comment.
Something went wrong with that request. Please try again.