| 1 | package Math::SymbolicX::Calculator::Interface; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Math::SymbolicX::Calculator; | ||
| 8 | use Params::Util qw/_INSTANCE/; | ||
| 9 | |||
| 10 | our $VERSION = '0.02'; | ||
| 11 | |||
| 12 | =encoding utf8 | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | Math::SymbolicX::Calculator::Interface - Miscallaneous routines for interfaces | ||
| 17 | |||
| 18 | =head1 SYNOPSIS | ||
| 19 | |||
| 20 | Do not use this in your scripts. Use one of the C<::Interface::Foo> classes. | ||
| 21 | |||
| 22 | =head1 DESCRIPTION | ||
| 23 | |||
| 24 | I<This is very much an internal class.> | ||
| 25 | |||
| 26 | This module is a base class for interfaces to L<Math::SymbolicX::Calculator>. | ||
| 27 | It is not an interface by itself. It doesn't even define stubs for all | ||
| 28 | methods of an interface and as such isn't an interface definition for | ||
| 29 | Calculator::Interfaces. Instead, it contains various miscellaneous methods | ||
| 30 | which may be of use to classes which actually implement interfaces. | ||
| 31 | |||
| 32 | =head1 METHODS RELATED TO PARSING | ||
| 33 | |||
| 34 | These methods parse commands of a somewhat generic syntax. | ||
| 35 | |||
| 36 | When defining the form of the accepted input strings below, | ||
| 37 | I'll be using certain variables: | ||
| 38 | |||
| 39 | $SYMBOL => An identifier matching /[A-Za-z][A-Za-z0-9_]*/ | ||
| 40 | $FORMULA => String that can be parsed as a Math::Symbolic tree | ||
| 41 | $PATTERN => String that can be parsed as a | ||
| 42 | Math::Symbolic::Custom::Transformation. (*) | ||
| 43 | $REPLACEMENT => String that can be parsed as the replacement part | ||
| 44 | of a Math::Symbolic::Custom::Transformation (*). | ||
| 45 | $TRANSFORMATION => String that can be parsed as a transformation in | ||
| 46 | the calculator context. (*) This means it has the | ||
| 47 | following form: $PATTERN -> $REPLACEMENT | ||
| 48 | $GROUP => A group of transformations. Grouping is done in | ||
| 49 | []-braces and the group elements are chained with | ||
| 50 | one of &, |, or ",". Form: [ $x & $x & .... ] | ||
| 51 | (Same for | or , but not mixed!) | ||
| 52 | $x is either another $GROUP, a $TRANSFORMATION | ||
| 53 | or a $SYMBOL in case of the latter, the symbol | ||
| 54 | table of the calculator is accessed to map the | ||
| 55 | $SYMBOL to a $TRANSFORMATION. | ||
| 56 | The ultimate result of parsing such a thing is a | ||
| 57 | Math::Symbolic::Custom::Transformation::Group | ||
| 58 | object (**). | ||
| 59 | |||
| 60 | (*) C<$PATTERN>s, C<$REPLACEMENT>s and C<$TRANSFORMATION>s | ||
| 61 | may contain some special syntax. | ||
| 62 | Instead of the C<TREE_foo>, C<VAR_foo>, and C<CONST_foo> special variables: | ||
| 63 | You can write C<?foo>, C<$foo> and C<!foo> respectively. | ||
| 64 | |||
| 65 | (**) L<Math::Symbolic::Custom::Transformation::Group> objects are also | ||
| 66 | L<Math::Symbolic::Custom::Transformation>s. | ||
| 67 | |||
| 68 | If errors occurr, these methods call the C<error()> method with | ||
| 69 | a description and then return the empty list. | ||
| 70 | |||
| 71 | If these methods require access to certain attributes of the interface | ||
| 72 | objects, this is mentioned in the docs. | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | |||
| 77 | # Matches identifiers | ||
| 78 | my $Ident = $Math::SymbolicX::Calculator::Identifier_Regex; | ||
| 79 | |||
| 80 | |||
| 81 | =head2 _parse_assignment | ||
| 82 | |||
| 83 | Parses an expression of one of the forms | ||
| 84 | |||
| 85 | $SYMBOL = $FORMULA | ||
| 86 | $SYMBOL = $TRANSFORMATION | ||
| 87 | $SYMBOL = $GROUP | ||
| 88 | |||
| 89 | Returns an instance of L<Math::SymbolicX::Calculator::Command::Assignment> | ||
| 90 | which assigns either a formula or a transformation(-group) when executed. | ||
| 91 | |||
| 92 | Uses C<$self->calc()> and expects it to return the Calculator object. | ||
| 93 | Uses the C<_parse_trafo_group> method. | ||
| 94 | |||
| 95 | =cut | ||
| 96 | |||
| 97 | sub _parse_assignment { | ||
| 98 | my $self = shift; | ||
| 99 | |||
| 100 | my $expr = shift; | ||
| 101 | my ($sym, $func) = split /\s*=\s*/, $expr, 2; | ||
| 102 | |||
| 103 | # get symbol name | ||
| 104 | $sym =~ s/^\s*//; | ||
| 105 | $sym =~ s/\s*$//; | ||
| 106 | $sym =~ /^$Ident$/ or $self->error("Invalid symbol name"), return(); | ||
| 107 | |||
| 108 | my $cmd; | ||
| 109 | if ($func =~ /->/ or $func =~ /\[/) { | ||
| 110 | # Transformation | ||
| 111 | |||
| 112 | # If this fails, it should call ->error(...) itself. | ||
| 113 | my $trafo = $self->_parse_trafo_group($func); | ||
| 114 | return() if not defined $trafo; | ||
| 115 | |||
| 116 | # Assigns the transformation to the symbol in the stash | ||
| 117 | $cmd = $self->calc->new_command( | ||
| 118 | type => 'Assignment', symbol => $sym, object => $trafo | ||
| 119 | ); | ||
| 120 | } | ||
| 121 | else { | ||
| 122 | # Function | ||
| 123 | eval { $func = Math::Symbolic->parse_from_string($func); }; | ||
| 124 | if ($@ or not defined $func) { | ||
| 125 | $self->error("Could not parse function." . ($@?" Error: $@":"")); | ||
| 126 | return(); | ||
| 127 | } | ||
| 128 | |||
| 129 | $cmd = $self->calc->new_command( | ||
| 130 | type => 'Assignment', symbol => $sym, object => $func | ||
| 131 | ); | ||
| 132 | } | ||
| 133 | |||
| 134 | return $cmd; | ||
| 135 | } | ||
| 136 | |||
| 137 | =head2 _parse_transformation | ||
| 138 | |||
| 139 | This method parses expressions of the form | ||
| 140 | |||
| 141 | $SYMBOL =~ $SYMBOL2 | ||
| 142 | $SYMBOL =~ $TRANSFORMATION | ||
| 143 | $SYMBOL =~ $GROUP | ||
| 144 | |||
| 145 | or | ||
| 146 | |||
| 147 | $SYMBOL =~~ $SYMBOL2 (*) | ||
| 148 | ... and so on ... | ||
| 149 | |||
| 150 | (*) The C<=~> operator stands for recursive application of the transformation | ||
| 151 | whereas the C<=~~> operator stands for shallow application. | ||
| 152 | |||
| 153 | These expressions generally stand for the application of the | ||
| 154 | transformation given on the right to the function C<$SYMBOL> | ||
| 155 | stands for. This method returns a | ||
| 156 | C<Math::SymbolicX::Calculator::Command::Transformation> object on success | ||
| 157 | or calls the C<error()> method and returns an empty list on failure. | ||
| 158 | |||
| 159 | This methods expects C<$self->calc()> to return the Calculator object. | ||
| 160 | It also uses the C<_parse_trafo_group> method. | ||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | sub _parse_transformation { | ||
| 165 | my $self = shift; | ||
| 166 | my $expr = shift; | ||
| 167 | |||
| 168 | my $shallow = 0; | ||
| 169 | $shallow = 1 if $expr =~ /=~~/; | ||
| 170 | |||
| 171 | my ($sym, $right) = split /\s*=~~?\s*/, $expr, 2; | ||
| 172 | |||
| 173 | # get symbol name | ||
| 174 | $sym =~ s/^\s*//; | ||
| 175 | $sym =~ s/\s*$//; | ||
| 176 | if (not $sym =~ /^$Ident$/) { | ||
| 177 | $self->error("Invalid symbol name"); | ||
| 178 | return(); | ||
| 179 | } | ||
| 180 | |||
| 181 | my $trafo = $self->_parse_trafo_group($right); | ||
| 182 | if (not defined $trafo) { | ||
| 183 | $self->error( | ||
| 184 | "Invalid transformation application: " | ||
| 185 | ."'$right' is not a transformation." | ||
| 186 | ); | ||
| 187 | return(); | ||
| 188 | } | ||
| 189 | |||
| 190 | my $cmd = $self->calc->new_command( | ||
| 191 | type => 'Transformation', symbol => $sym, object => $trafo, | ||
| 192 | shallow => $shallow, | ||
| 193 | ); | ||
| 194 | return $cmd; | ||
| 195 | } | ||
| 196 | |||
| 197 | |||
| 198 | =head2 _parse_trafo_group | ||
| 199 | |||
| 200 | Parses a string of the form: | ||
| 201 | |||
| 202 | $SYMBOL | ||
| 203 | $GROUP | ||
| 204 | $TRANSFORMATION | ||
| 205 | |||
| 206 | In case of C<$SYMBOL>, it accesses the Calculator symbol table to fetch | ||
| 207 | the referenced Transformation. (And throws an error if it's not one.) | ||
| 208 | |||
| 209 | Returns a L<Math::Symbolic::Custom::Transformation> object or calls | ||
| 210 | the C<error()> method and returns the empty list on failure. | ||
| 211 | |||
| 212 | This method uses L<Parse::RecDescent>. | ||
| 213 | Expects the C<calc()> method to return the Calculator object. | ||
| 214 | Uses the method C<_parse_simple_transformation>. | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 218 | { # {{{ block around _parse_trafo_group | ||
| 219 | my $group_grammar = <<'GROUP_GRAMMAR'; | ||
| 220 | parse: group /^\Z/ | ||
| 221 | { | ||
| 222 | $return = $item[1] | ||
| 223 | } | ||
| 224 | | // {undef} | ||
| 225 | |||
| 226 | group: /[^,\[\]&|]+/ | ||
| 227 | { $return = $item[1] } | ||
| 228 | | '[' /[^,\[\]&|]+/ ']' | ||
| 229 | { $return = $item[2] } | ||
| 230 | | '[' <leftop:group '&' group> ']' | ||
| 231 | { $return = ['&', $item[2]] } | ||
| 232 | | '[' <leftop:group '|' group> ']' | ||
| 233 | { $return = ['|', $item[2]] } | ||
| 234 | | '[' <leftop:group ',' group> ']' | ||
| 235 | { $return = [',', $item[2]] } | ||
| 236 | GROUP_GRAMMAR | ||
| 237 | |||
| 238 | my $group_parser; | ||
| 239 | |||
| 240 | sub _parse_trafo_group { | ||
| 241 | my $self = shift; | ||
| 242 | my $string = shift; | ||
| 243 | |||
| 244 | # Initialize parser | ||
| 245 | if (not defined $group_parser) { | ||
| 246 | require Parse::RecDescent; | ||
| 247 | $group_parser = Parse::RecDescent->new($group_grammar); | ||
| 248 | } | ||
| 249 | |||
| 250 | # If it's a symbol, access the stash | ||
| 251 | if ($string =~ /^\s*($Ident)\s*$/) { | ||
| 252 | my $trafo_sym = $1; | ||
| 253 | |||
| 254 | my $trafo = $self->calc->get_transformation($trafo_sym); | ||
| 255 | if (not ref($trafo)) { | ||
| 256 | $self->error($trafo); | ||
| 257 | return(); | ||
| 258 | } | ||
| 259 | else { | ||
| 260 | return $trafo; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | # It's a group | ||
| 264 | elsif ($string =~ /^\s*\[.*\]\s*$/) { | ||
| 265 | $string =~ s/\s+//g; | ||
| 266 | my $struct = $group_parser->parse($string); | ||
| 267 | if (not defined $struct) { | ||
| 268 | $self->error("There was an error in your previous line of input. Please check your syntax."); | ||
| 269 | return(); | ||
| 270 | } | ||
| 271 | |||
| 272 | if (not ref $struct) { | ||
| 273 | return $self->_parse_simple_transformation($struct); | ||
| 274 | } | ||
| 275 | |||
| 276 | my $group = $self->_struct_to_group($struct); | ||
| 277 | |||
| 278 | return($group); | ||
| 279 | } | ||
| 280 | # It must be an ordinary transformation | ||
| 281 | else { | ||
| 282 | return $self->_parse_simple_transformation($string); | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | no warnings 'recursion'; | ||
| 287 | |||
| 288 | # Helper method for _parse_trafo_group (recursive) | ||
| 289 | sub _struct_to_group { | ||
| 290 | my $self = shift; | ||
| 291 | my $struct = shift; | ||
| 292 | |||
| 293 | my $op = $struct->[0]; | ||
| 294 | my $inside = $struct->[1]; | ||
| 295 | |||
| 296 | my @expr; | ||
| 297 | foreach my $expr (@$inside) { | ||
| 298 | if (ref($expr)) { | ||
| 299 | # nested groups | ||
| 300 | push @expr, $self->_struct_to_group($expr); | ||
| 301 | } | ||
| 302 | else { | ||
| 303 | my $trafo = $self->_parse_trafo_group($expr); | ||
| 304 | if (not defined $trafo) { | ||
| 305 | $self->error( | ||
| 306 | "Error creating transformation from expression '$expr'." | ||
| 307 | ); | ||
| 308 | return(); | ||
| 309 | } | ||
| 310 | push @expr, $trafo; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | return Math::Symbolic::Custom::Transformation::Group->new( | ||
| 314 | $op, @expr | ||
| 315 | ); | ||
| 316 | } | ||
| 317 | |||
| 318 | } # }}} block around _parse_trafo_group | ||
| 319 | |||
| 320 | |||
| 321 | |||
| 322 | =head2 _parse_simple_transformation | ||
| 323 | |||
| 324 | Parses a string of the form "pattern -> replacement", i.e. a | ||
| 325 | simple C<$TRANSFORMATION>. | ||
| 326 | |||
| 327 | Returns a C<Math::Symbolic::Custom::Transformation> object or the | ||
| 328 | empty list on failure. | ||
| 329 | |||
| 330 | =cut | ||
| 331 | |||
| 332 | sub _parse_simple_transformation { | ||
| 333 | my $self = shift; | ||
| 334 | my $string = shift; | ||
| 335 | my ($pattern, $replacement) = split /\s*->\s*/, $string, 2; | ||
| 336 | |||
| 337 | # Special syntax for our transformations | ||
| 338 | for ($pattern, $replacement) { | ||
| 339 | s/(!|\$|\?)($Ident)/ | ||
| 340 | if ($1 eq '!') { "CONST_$2" } | ||
| 341 | elsif ($1 eq '$') { "VAR_$2" } | ||
| 342 | else { "TREE_$2" } | ||
| 343 | /ge; | ||
| 344 | } | ||
| 345 | |||
| 346 | my $trafo; | ||
| 347 | eval { | ||
| 348 | $trafo = Math::Symbolic::Custom::Transformation->new( | ||
| 349 | $pattern, $replacement | ||
| 350 | ); | ||
| 351 | }; | ||
| 352 | if ($@ or not defined $trafo) { | ||
| 353 | $self->error( | ||
| 354 | "Could not parse transformation '$pattern -> $replacement'." | ||
| 355 | . ($@?" Error: $@":"") | ||
| 356 | ); | ||
| 357 | return(); | ||
| 358 | } | ||
| 359 | |||
| 360 | return $trafo; | ||
| 361 | } | ||
| 362 | |||
| 363 | |||
| 364 | |||
| 365 | |||
| 366 | |||
| 367 | 1; | ||
| 368 | __END__ |