| 1 | # | ||||||||||||||||||||||||||||
| 2 | # This file is part of Soar-Production | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # This software is copyright (c) 2012 by Nathan Glenn. | ||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||
| 6 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 7 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 8 | # | ||||||||||||||||||||||||||||
| 9 | package Soar::Production::Parser; | ||||||||||||||||||||||||||||
| 10 | # ABSTRACT: Parse Soar productions | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 29 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 29 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | #needed for advanced regex expressions | ||||||||||||||||||||||||||||
| 15 | 29 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 29 | our $VERSION = '0.03'; # VERSION | |||||||||||||||||||||||||||
| 18 | 29 | ▶ | use Exporter::Easy ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 29 | OK => [qw(no_comment)] | |||||||||||||||||||||||||||
| 20 | 29 | ); | |||||||||||||||||||||||||||
| 21 | 29 | ▶ | use Soar::Production::Parser::PRDGrammar; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 29 | ▶ | use Parse::RecDescent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 29 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 29 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | #a regular expression to split text into productions | ||||||||||||||||||||||||||||
| 29 | 29 | my $splitter = qr/ | |||||||||||||||||||||||||||
| 30 | (sp\s+ #start with 'sp' | ||||||||||||||||||||||||||||
| 31 | \{ #opening brace | ||||||||||||||||||||||||||||
| 32 | ( #save to $2 | ||||||||||||||||||||||||||||
| 33 | (?: #either | ||||||||||||||||||||||||||||
| 34 | \{ (?-1) \} #more braces and recurse | ||||||||||||||||||||||||||||
| 35 | | #or | ||||||||||||||||||||||||||||
| 36 | (?: #group | ||||||||||||||||||||||||||||
| 37 | [^|{}]++ #not bar or braces | ||||||||||||||||||||||||||||
| 38 | | #or | ||||||||||||||||||||||||||||
| 39 | \| #a bar | ||||||||||||||||||||||||||||
| 40 | (?: #group | ||||||||||||||||||||||||||||
| 41 | [^\\|]++ #no slashes or bars, no backtracking | ||||||||||||||||||||||||||||
| 42 | | #or | ||||||||||||||||||||||||||||
| 43 | \\. #slash anything | ||||||||||||||||||||||||||||
| 44 | )*+ | ||||||||||||||||||||||||||||
| 45 | \| | ||||||||||||||||||||||||||||
| 46 | )++ #one or more, no backtracking | ||||||||||||||||||||||||||||
| 47 | )* #0 or more times | ||||||||||||||||||||||||||||
| 48 | ) #end $2 | ||||||||||||||||||||||||||||
| 49 | \} #ending brace | ||||||||||||||||||||||||||||
| 50 | ) #end $1 | ||||||||||||||||||||||||||||
| 51 | /x; | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 29 | ▶ | __PACKAGE__->new->_run(shift) unless caller; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub _run { | ||||||||||||||||||||||||||
|
Subroutine
_run: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 56 | 0 | my ($soarParser, $filePath) = @_; | |||||||||||||||||||||||||||
| 57 | 0 | print Dumper($soarParser->productions(file => $filePath, parse => 1) ); | |||||||||||||||||||||||||||
| 58 | 0 | return; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 29 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 29 | my ($class) = @_; | |||||||||||||||||||||||||||
| 64 | 29 | my $soarParser = bless {}, $class; | |||||||||||||||||||||||||||
| 65 | 29 | $soarParser->_init; | |||||||||||||||||||||||||||
| 66 | 29 | return $soarParser; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 29 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | 29 | my ($soarParser) = @_; | |||||||||||||||||||||||||||
| 71 | 29 | $soarParser->{parser} = Parse::RecDescent->new($Soar::Production::Parser::PRDGrammar::GRAMMAR); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | #if you wish to debug the grammar, try turning on traces by uncommenting the following lines: | ||||||||||||||||||||||||||||
| 74 | # $::RD_TRACE = 1; | ||||||||||||||||||||||||||||
| 75 | # $::RD_HINT = 1; | ||||||||||||||||||||||||||||
| 76 | 29 | return; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 14 | ▶ | sub productions {## no critic RequireArgUnpacking | ||||||||||||||||||||||||||
|
Subroutine
productions: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 14 | my ($soarParser) = shift; | |||||||||||||||||||||||||||
| 82 | 14 | my %args = ( | |||||||||||||||||||||||||||
| 83 | parse => 0, | ||||||||||||||||||||||||||||
| 84 | text => undef, | ||||||||||||||||||||||||||||
| 85 | file => undef, | ||||||||||||||||||||||||||||
| 86 | @_ | ||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||
| 88 | defined $args{text} or defined $args{file} | ||||||||||||||||||||||||||||
| 89 | 14 | ▶ | or croak 'Must specify parameter \'file\' or \'text\' to extract productions.'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $args{"text"} or defined $args{"file"}
MC/DC0%0/2
defined $args{"text"} or defined $args{"file"}
defined $args{"text"}
defined $args{"file"}
Truth table66%2/3
A or B
A= defined $args{"text"}B= defined $args{"file"}
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 14 | ▶ | if($args{text}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 6 | return $soarParser->_productions_from_text($args{text}, $args{parse}); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | 8 | ▶ | if($args{file}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | # print "$args{file}\n"; | ||||||||||||||||||||||||||||
| 96 | 8 | return $soarParser->_productions_from_file($args{file}, $args{parse}); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 6 | ▶ | sub _productions_from_text { | ||||||||||||||||||||||||||
|
Subroutine
_productions_from_text: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 101 | 6 | my ( $soarParser, $text, $parse) = @_; | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | #remove comments | ||||||||||||||||||||||||||||
| 104 | 6 | $text = no_comment($text); | |||||||||||||||||||||||||||
| 105 | 6 | my $productions = _split_text(\$text); | |||||||||||||||||||||||||||
| 106 | 6 | ▶ | return $productions | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | unless($parse); | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | return $soarParser->get_parses($productions); | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 8 | ▶ | sub _productions_from_file { | ||||||||||||||||||||||||||
|
Subroutine
_productions_from_file: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 113 | 8 | my ( $soarParser, $file, $parse) = @_; | |||||||||||||||||||||||||||
| 114 | 8 | my $text = _readFile($file); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 8 | my $productions = _split_text($text); | |||||||||||||||||||||||||||
| 117 | 8 | ▶ | return $productions | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | unless($parse); | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 1 | return $soarParser->get_parses($productions); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | #split text reference into production | ||||||||||||||||||||||||||||
| 124 | 14 | ▶ | sub _split_text { | ||||||||||||||||||||||||||
|
Subroutine
_split_text: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 125 | 14 | my ($text) = @_; | |||||||||||||||||||||||||||
| 126 | #split the text into productions by looking for 'sp { ... }' | ||||||||||||||||||||||||||||
| 127 | 14 | my @productions; | |||||||||||||||||||||||||||
| 128 | 14 | ▶ | while($$text =~ /$splitter/g){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | # print "found production: $1"; | ||||||||||||||||||||||||||||
| 130 | 2489 | push @productions, $1; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 14 | return \@productions; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 2604 | ▶ | sub parse_text { | ||||||||||||||||||||||||||
|
Subroutine
parse_text: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 2604 | my ( $soarParser, $text ) = @_; | |||||||||||||||||||||||||||
| 138 | 2604 | ▶ | croak 'no text to parse!' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | unless defined $text; | ||||||||||||||||||||||||||||
| 140 | # $soarParser->{input} = \$text; | ||||||||||||||||||||||||||||
| 141 | 2603 | return $soarParser->{parser}->parse($text); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 1 | ▶ | sub get_parses { | ||||||||||||||||||||||||||
|
Subroutine
get_parses: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 1 | my ($soarParser, $productions) = @_; | |||||||||||||||||||||||||||
| 148 | 1 | my @parses; | |||||||||||||||||||||||||||
| 149 | 1 | for(@$productions){ | |||||||||||||||||||||||||||
| 150 | # print STDERR $_; | ||||||||||||||||||||||||||||
| 151 | 3 | push @parses, $soarParser->{parser}->parse($_); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 1 | return \@parses; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | #argument should be an opened file handle | ||||||||||||||||||||||||||||
| 157 | #returns string pointer to text | ||||||||||||||||||||||||||||
| 158 | 8 | ▶ | sub _readFile { | ||||||||||||||||||||||||||
|
Subroutine
_readFile: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 159 | 8 | my ($file) = @_; | |||||||||||||||||||||||||||
| 160 | 8 | ▶ | open my $fh, '<', $file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | or croak "Couldn't open $file"; | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 8 | my $text = q(); | |||||||||||||||||||||||||||
| 164 | 8 | ▶ | $text .= $_ while (<$fh>); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 8 | close $fh; | |||||||||||||||||||||||||||
| 167 | 8 | $text = no_comment($text); | |||||||||||||||||||||||||||
| 168 | 8 | return \$text; | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 22 | ▶ | sub no_comment { | ||||||||||||||||||||||||||
|
Subroutine
no_comment: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | 22 | my ($text) = @_; | |||||||||||||||||||||||||||
| 174 | 22 | $text =~ s/ | |||||||||||||||||||||||||||
| 175 | ( #save in $1 | ||||||||||||||||||||||||||||
| 176 | \| #literal bar | ||||||||||||||||||||||||||||
| 177 | (?: #group | ||||||||||||||||||||||||||||
| 178 | \\[|] #an escaped bar | ||||||||||||||||||||||||||||
| 179 | | | ||||||||||||||||||||||||||||
| 180 | [^|] #or anything but a literal bar | ||||||||||||||||||||||||||||
| 181 | )* #zero or more of previous group | ||||||||||||||||||||||||||||
| 182 | \| #literal bar | ||||||||||||||||||||||||||||
| 183 | ) #end $1 | ||||||||||||||||||||||||||||
| 184 | | #or | ||||||||||||||||||||||||||||
| 185 | (?:;\s*)? # optional semicolon | ||||||||||||||||||||||||||||
| 186 | \# # pound character | ||||||||||||||||||||||||||||
| 187 | .* #followed by anything | ||||||||||||||||||||||||||||
| 188 | / #replace with | ||||||||||||||||||||||||||||
| 189 | 2290 | ▶ | $1||'' # $1 or nothing (the quote if there was one; | ||||||||||||||||||||||||||
|
Condition100%2/2
$1 || ''
MC/DC100%1/1
$1 || ''
$1
Truth table100%2/2
A
A= $1
| |||||||||||||||||||||||||||||
| 190 | # no quote will simply remove matching comment) | ||||||||||||||||||||||||||||
| 191 | /xeg; | ||||||||||||||||||||||||||||
| 192 | 22 | return $text; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | 29 | 1; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | __END__ | ||||||||||||||||||||||||||||