| 1 | # -*- Perl -*- | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # a parser for logic expressions | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Logic::Expr::Parser; | ||||||||||||||||||||||||||||
| 6 | 3 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Logic::Expr ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use base 'Parser::MGC'; # 0.21 or higher required | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | # LE_NOT is handled outside the scope of this pair of variables | ||||||||||||||||||||||||||||
| 11 | 3 | our %le_map = ( | |||||||||||||||||||||||||||
| 12 | '&' => LE_AND, | ||||||||||||||||||||||||||||
| 13 | '|' => LE_OR, | ||||||||||||||||||||||||||||
| 14 | 'v' => LE_OR, | ||||||||||||||||||||||||||||
| 15 | '->' => LE_COND, | ||||||||||||||||||||||||||||
| 16 | '==' => LE_BICOND, | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | 3 | our $le_regex = qr/->|==|[&|v]/; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 10 | ▶ | sub on_parse_end { Logic::Expr->new( expr => $_[1] ) } | ||||||||||||||||||||||||||
|
Subroutine
on_parse_end: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | sub parse | ||||||||||||||||||||||||||||
| 23 | 18 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 18 | my ($self) = @_; | |||||||||||||||||||||||||||
| 25 | 18 | my $first = $self->_parse_term; | |||||||||||||||||||||||||||
| 26 | 15 | my ( $operator, $second ); | |||||||||||||||||||||||||||
| 27 | $self->maybe( | ||||||||||||||||||||||||||||
| 28 | 15 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 15 | $operator = $le_map{ $self->expect($le_regex) }; | |||||||||||||||||||||||||||
| 30 | 11 | $second = $self->_parse_term; | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | 15 | ); | |||||||||||||||||||||||||||
| 33 | 15 | ▶ | defined $operator ? [ $operator, $first, $second ] : $first; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub _parse_term | ||||||||||||||||||||||||||||
| 37 | 29 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_parse_term: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 38 | 29 | my ($self) = @_; | |||||||||||||||||||||||||||
| 39 | 29 | ▶ | my $neg = $self->maybe( sub { $self->expect(qr/!+|~+/) } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | my $term = $self->any_of( | ||||||||||||||||||||||||||||
| 41 | 29 | ▶ | sub { $self->scope_of( "(", \&parse, ")" ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 26 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 43 | 26 | my $atom = $self->expect(qr/[A-Z]+/); | |||||||||||||||||||||||||||
| 44 | 23 | ▶ | unless ( exists $Logic::Expr::atoms{$atom} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 10 | push @Logic::Expr::bools, TRUE; | |||||||||||||||||||||||||||
| 46 | 10 | $Logic::Expr::atoms{$atom} = \$Logic::Expr::bools[-1]; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 23 | $Logic::Expr::atoms{$atom}; | |||||||||||||||||||||||||||
| 49 | }, | ||||||||||||||||||||||||||||
| 50 | 29 | ); | |||||||||||||||||||||||||||
| 51 | # simplify !!!X to !X and !!X to X | ||||||||||||||||||||||||||||
| 52 | 26 | ▶ | ( defined $neg and length($neg) & 1 ) ? [ LE_NOT, $term ] : $term; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $neg and length $neg & 1
MC/DC100%2/2
defined $neg and length $neg & 1
defined $neg
length $neg & 1
Truth table100%3/3
A and B
A= defined $negB= length $neg & 1
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 3 | 1; | |||||||||||||||||||||||||||
| 56 | __END__ | ||||||||||||||||||||||||||||