| 1 | package Locale::Babelfish::Phrase::Parser; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Babelfish syntax parser. | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 7 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Locale::Babelfish::Phrase::Literal (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use Locale::Babelfish::Phrase::Variable (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 7 | ▶ | use Locale::Babelfish::Phrase::PluralForms (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 7 | ▶ | use Locale::Babelfish::Phrase::PluralFormsParser (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 7 | ▶ | use parent qw( Locale::Babelfish::Phrase::ParserBase ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 7 | our $VERSION = '2.13'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 7 | __PACKAGE__->mk_accessors( qw( locale mode pieces escape pf0 ) ); | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 7 | ▶ | use constant { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 7 | LITERAL_MODE => 'Locale::Babelfish::Phrase::Literal', | |||||||||||||||||||||||||||||||||||||||||
| 22 | VARIABLE_MODE => 'Locale::Babelfish::Phrase::Variable', | ||||||||||||||||||||||||||||||||||||||||||
| 23 | PLURALS_MODE => 'Locale::Babelfish::Phrase::PluralForms', | ||||||||||||||||||||||||||||||||||||||||||
| 24 | VARIABLE_RE => qr/^[a-zA-Z0-9_\.]+$/, | ||||||||||||||||||||||||||||||||||||||||||
| 25 | 7 | }; | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | 7 | my ( $class, $phrase, $locale ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 7 | my $self = $class->SUPER::new( $phrase ); | |||||||||||||||||||||||||||||||||||||||||
| 31 | 7 | ▶ | $self->locale( $locale ) if $locale; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 7 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | 291 | ▶ | sub init { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 291 | my ( $self, $phrase ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 291 | $self->SUPER::init( $phrase ); | |||||||||||||||||||||||||||||||||||||||||
| 39 | 291 | $self->mode( LITERAL_MODE ); | |||||||||||||||||||||||||||||||||||||||||
| 40 | 291 | $self->pieces( [] ); | |||||||||||||||||||||||||||||||||||||||||
| 41 | 291 | $self->pf0( undef ); # plural forms without name yet | |||||||||||||||||||||||||||||||||||||||||
| 42 | 291 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 289 | ▶ | sub finalize_mode { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
finalize_mode: called CC=7 SCAR=20.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 289 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 289 | ▶ | if ( $self->mode eq LITERAL_MODE ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 237 | push @{ $self->pieces }, LITERAL_MODE->new( text => $self->piece ) | |||||||||||||||||||||||||||||||||||||||||
| 50 | 282 | ▶ | if length($self->piece) || scalar(@{ $self->pieces }) == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $self->piece or scalar @{$self->pieces;} == 0
MC/DC100%2/2
length $self->piece or scalar @{$self->pieces;} == 0
length $self->piece
scalar @{$self->pieces;} == 0
Truth table100%3/3
A or B
A= length $self->pieceB= scalar @{$self->pieces;} == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | elsif ( $self->mode eq VARIABLE_MODE ) { | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | $self->throw( "Variable definition not ended with \"}\": ". $self->piece ); | |||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | elsif ( $self->mode eq PLURALS_MODE ) { | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 7 | ▶ | $self->throw( "Plural forms definition not ended with \"))\": ". $self->piece ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | unless defined $self->pf0; | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 7 | push @{ $self->pieces }, PLURALS_MODE->new( forms => $self->pf0, name => $self->piece, locale => $self->locale, ); | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | $self->throw( "Logic broken, unknown parser mode: ". $self->mode ); | |||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 292 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=26 SCAR=32.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 292 | my ( $self, $phrase, $locale ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 292 | $self->SUPER::parse( $phrase ); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 291 | ▶ | $self->locale( $locale ) if $locale; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 291 | my $plurals_parser = Locale::Babelfish::Phrase::PluralFormsParser->new(); | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 291 | while ( 1 ) { | |||||||||||||||||||||||||||||||||||||||||
| 75 | 3570 | my $char = $self->to_next_char; | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 3570 | ▶ | unless ( length $char ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 289 | $self->finalize_mode; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 289 | return $self->pieces; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 3281 | ▶ | if ( $self->mode eq LITERAL_MODE ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 2014 | ▶ | if ( $self->escape ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 10 | $self->add_to_piece( $char ); | |||||||||||||||||||||||||||||||||||||||||
| 85 | 10 | $self->escape(0); | |||||||||||||||||||||||||||||||||||||||||
| 86 | 10 | next; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 2004 | ▶ | if ( $char eq "\\" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 10 | $self->escape( 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 10 | next; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1994 | ▶ | if ( $char eq '#' && $self->next_char eq '{' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$char eq "#" and $self->next_char eq "{"
MC/DC50%1/2
$char eq "#" and $self->next_char eq "{"
$char eq "#"
$self->next_char eq "{"
Truth table66%2/3
A and B
A= $char eq "#"B= $self->next_char eq "{"
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 70 | ▶ | if ( length $self->piece ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 34 | push @{ $self->pieces }, LITERAL_MODE->new( text => $self->piece ); | |||||||||||||||||||||||||||||||||||||||||
| 97 | 34 | $self->piece(''); | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 70 | $self->to_next_char; # skip "{" | |||||||||||||||||||||||||||||||||||||||||
| 100 | 70 | $self->mode( VARIABLE_MODE ); | |||||||||||||||||||||||||||||||||||||||||
| 101 | 70 | next; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 1924 | ▶ | if ( $char eq '(' && $self->next_char eq '(' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$char eq "(" and $self->next_char eq "("
MC/DC100%2/2
$char eq "(" and $self->next_char eq "("
$char eq "("
$self->next_char eq "("
Truth table100%3/3
A and B
A= $char eq "("B= $self->next_char eq "("
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 29 | ▶ | if ( length $self->piece ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 29 | push @{ $self->pieces }, LITERAL_MODE->new( text => $self->piece ); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 29 | $self->piece(''); | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 29 | $self->to_next_char; # skip second "(" | |||||||||||||||||||||||||||||||||||||||||
| 110 | 29 | $self->mode( PLURALS_MODE ); | |||||||||||||||||||||||||||||||||||||||||
| 111 | 29 | next; | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 3162 | ▶ | if ( $self->mode eq VARIABLE_MODE ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 569 | ▶ | if ( $self->escape ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $self->add_to_piece( $char ); | |||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $self->escape(0); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 569 | ▶ | if ( $char eq "\\" ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $self->escape( 1 ); | |||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 569 | ▶ | if ( $char eq '}' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 70 | my $name = $self->trim( $self->piece ); | |||||||||||||||||||||||||||||||||||||||||
| 129 | 70 | ▶ | unless ( length $name ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | $self->throw( "No variable name given." ); | |||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 69 | ▶ | if ( $name !~ VARIABLE_RE ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 1 | $self->throw( "Variable name doesn't meet conditions: $name." ); | |||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 68 | push @{ $self->pieces }, VARIABLE_MODE->new( name => $name ); | |||||||||||||||||||||||||||||||||||||||||
| 136 | 68 | $self->piece(''); | |||||||||||||||||||||||||||||||||||||||||
| 137 | 68 | $self->mode( LITERAL_MODE ); | |||||||||||||||||||||||||||||||||||||||||
| 138 | 68 | next; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 3092 | ▶ | if ( $self->mode eq PLURALS_MODE ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 698 | ▶ | if ( defined $self->pf0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 120 | ▶ | if ( $char =~ VARIABLE_RE && ($char ne '.' || $self->next_char =~ VARIABLE_RE) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$char =~ /^[a-zA-Z0-9_\.]+$/ and $char ne "." || $self->next_char =~ /^[a-zA-Z0-9_\.]+$/
$char ne "." or $self->next_char =~ /^[a-zA-Z0-9_\.]+$/
MC/DC66%2/3
$char =~ /^[a-zA-Z0-9_\.]+$/ and $char ne "." || $self->next_char =~ /^[a-zA-Z0-9_\.]+$/
$char =~ /^[a-zA-Z0-9_\.]+$/
$char ne "."
$self->next_char =~ /^[a-zA-Z0-9_\.]+$/
Truth table75%3/4
A and B or C
A= $char =~ /^[a-zA-Z0-9_\.]+$/B= $char ne "."C= $self->next_char =~ /^[a-zA-Z0-9_\.]+$/
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 102 | $self->add_to_piece( $char ); | |||||||||||||||||||||||||||||||||||||||||
| 146 | 102 | next; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 149 | 18 | push @{ $self->pieces }, PLURALS_MODE->new( forms => $self->pf0, name => $self->piece, locale => $self->locale, ); | |||||||||||||||||||||||||||||||||||||||||
| 150 | 18 | $self->pf0( undef ); | |||||||||||||||||||||||||||||||||||||||||
| 151 | 18 | $self->mode( LITERAL_MODE ); | |||||||||||||||||||||||||||||||||||||||||
| 152 | 18 | $self->piece(''); | |||||||||||||||||||||||||||||||||||||||||
| 153 | 18 | $self->backward; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 18 | next; | |||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||
| 157 | 578 | ▶ | if ( $char eq ')' && $self->next_char eq ')' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$char eq ")" and $self->next_char eq ")"
MC/DC50%1/2
$char eq ")" and $self->next_char eq ")"
$char eq ")"
$self->next_char eq ")"
Truth table66%2/3
A and B
A= $char eq ")"B= $self->next_char eq ")"
| |||||||||||||||||||||||||||||||||||||||||||
| 158 | 29 | $self->pf0( $plurals_parser->parse( $self->piece ) ); | |||||||||||||||||||||||||||||||||||||||||
| 159 | 29 | $self->piece(''); | |||||||||||||||||||||||||||||||||||||||||
| 160 | 29 | $self->to_next_char; # skip second ")" | |||||||||||||||||||||||||||||||||||||||||
| 161 | 29 | ▶ | if ( $self->next_char eq ':' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 25 | $self->to_next_char; # skip ":" | |||||||||||||||||||||||||||||||||||||||||
| 163 | 25 | next; | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 4 | push @{ $self->pieces }, PLURALS_MODE->new( forms => $self->pf0, name => 'count', locale => $self->locale, ); | |||||||||||||||||||||||||||||||||||||||||
| 166 | 4 | $self->pf0( undef ); | |||||||||||||||||||||||||||||||||||||||||
| 167 | 4 | $self->mode( LITERAL_MODE ); | |||||||||||||||||||||||||||||||||||||||||
| 168 | 4 | next; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 2943 | $self->add_to_piece( $char ); | |||||||||||||||||||||||||||||||||||||||||
| 172 | } # while ( 1 ) | ||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 7 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | __END__ | ||||||||||||||||||||||||||||||||||||||||||