Diff for /loncom/homework/math_parser/Parser.pm between versions 1.1 and 1.2

version 1.1, 2015/06/29 15:42:13 version 1.2, 2015/06/30 17:42:14
Line 39  use aliased 'Apache::math_parser::Tokeni Line 39  use aliased 'Apache::math_parser::Tokeni
 # @optional {boolean} unit_mode - handle only numerical expressions with units (no variable)  # @optional {boolean} unit_mode - handle only numerical expressions with units (no variable)
 ##  ##
 sub new {  sub new {
     my $class = shift;      my ($class, $implicit_operators, $unit_mode) = @_;
       if (!defined $implicit_operators) {
           $implicit_operators = 0;
       }
       if (!defined $unit_mode) {
           $unit_mode = 0;
       }
     my $self = {      my $self = {
         _implicit_operators => shift // 0,          _implicit_operators => $implicit_operators,
         _unit_mode => shift // 0,          _unit_mode => $unit_mode,
         _defs => Definitions->new(),          _defs => Definitions->new(),
     };      };
     $self->{_defs}->define();      $self->{_defs}->define();
Line 200  sub addHiddenOperators { Line 206  sub addHiddenOperators {
                 ($token_type == Token->NAME && $next_token_type == Token->NAME) ||                  ($token_type == Token->NAME && $next_token_type == Token->NAME) ||
                 ($token_type == Token->NUMBER && $next_token_type == Token->NAME) ||                  ($token_type == Token->NUMBER && $next_token_type == Token->NAME) ||
                 ($token_type == Token->NUMBER && $next_token_type == Token->NUMBER) ||                  ($token_type == Token->NUMBER && $next_token_type == Token->NUMBER) ||
                 ($token_type == Token->NUMBER && $next_token_value ~~ ["(","[","{"]) ||                  ($token_type == Token->NUMBER && string_in_array(["(","[","{"], $next_token_value)) ||
                 # ($token_type == Token->NAME && $next_token_value eq "(") ||                  # ($token_type == Token->NAME && $next_token_value eq "(") ||
                 # name ( could be a function call                  # name ( could be a function call
                 ($token_value ~~ [")","]","}"] && $next_token_type == Token->NAME) ||                  (string_in_array([")","]","}"], $token_value) && $next_token_type == Token->NAME) ||
                 ($token_value ~~ [")","]","}"] && $next_token_type == Token->NUMBER) ||                  (string_in_array([")","]","}"], $token_value) && $next_token_type == Token->NUMBER) ||
                 ($token_value ~~ [")","]","}"] && $next_token_value eq "(")                  (string_in_array([")","]","}"], $token_value) && $next_token_value eq "(")
            ) {             ) {
             # support for things like "(1/2) (m/s)" is complex...              # support for things like "(1/2) (m/s)" is complex...
             my $units = ($self->unit_mode && !$in_units &&              my $units = ($self->unit_mode && !$in_units &&
                 ($token_type == Token->NUMBER || $token_value ~~ [")","]","}"]) &&                  ($token_type == Token->NUMBER || string_in_array([")","]","}"], $token_value)) &&
                 ($next_token_type == Token->NAME ||                  ($next_token_type == Token->NAME ||
                     ($next_token_value ~~ ["(","[","{"] && scalar(@{$self->tokens}) > $i + 2 &&                      (string_in_array(["(","[","{"], $next_token_value) && scalar(@{$self->tokens}) > $i + 2 &&
                     $self->tokens->[$i + 2]->type == Token->NAME)));                      $self->tokens->[$i + 2]->type == Token->NAME)));
             if ($units) {              if ($units) {
                 my( $test_token, $index_test);                  my( $test_token, $index_test);
Line 276  sub parse { Line 282  sub parse {
     return $root;      return $root;
 }  }
   
   ##
   # Tests if a string is in an array (using eq) (to avoid using $value ~~ @array)
   # @param {Array<string>} array - reference to the array of strings
   # @param {string} value - the string to look for
   # @returns 1 if found, 0 otherwise
   ##
   sub string_in_array {
     my ($array, $value) = @_;
     foreach my $v (@{$array}) {
       if ($v eq $value) {
         return 1;
       }
     }
     return 0;
   }
   
 1;  1;
 __END__  __END__

Removed from v.1.1  
changed lines
  Added in v.1.2


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>