mossadal/math-parser

Parser.php tries to call unknown function

Closed this issue · 2 comments

Hi again! I found this other bug when I wanted to try to trigger various exceptions by writing bad equations.

$ php -v
PHP 7.0.15 (cli) (built: Feb 10 2017 16:55:27) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
        $equation = '1(2';

        $parser = new \MathParser\StdMathParser();

        // Generate an abstract syntax tree.
        $AST = $parser->parse( $equation );

        // Evaluate the expression in the AST.
        $evaluator = new \MathParser\Interpreting\Evaluator();
        $value = $AST->accept( $evaluator );
PHP Fatal error:  Uncaught Error: Call to undefined method MathParser\Parsing\Nodes\SubExpressionNode::setLeft() in ./vendor/mossadal/math-parser/src/MathParser/Parsing/Parser.php:278

Thank you. This is fixed in 1.3.9. Some malformed subexpressions managed to sneak their way through a point they shouldn't have. There may very well be other similar edge cases left to take care of. Feel free to report them if you find more.

@mossadal Thank you so much for the quick fixes to the two bugs!

So far I haven't encountered any other edge cases. And looking at your commit, it seems like the parser only warned about the "start of a mismatched subexpression" if that parenthesis node belonged to a function, but not to a regular number. Ie "sin(2" would have warned, but not "1(2".

Since we've found this one, and the only other kind of parenthesis is for functions, I think all should be good now. 👍

Thank you so much for writing this incredible library. Beautifully written, AST, shunting yard, great test coverage, and very reliable. I'm having fun with it! :)