Right now, the parser that we have built is not really easy to use. In order to use the parser correctly, a user (in this context, read "user" as "another developer that uses your parser") has to call the match_Expr()
method (which is just one of many public match_*
functions offered by the parser that are not actually supposed to be called by external users), extract the node
property from the returned array, and then call the evaluate function on the root node contained in this property. Also, the parser also matches partial strings (remember the example (1 + 2)) * 3
, which was recognized as partially correct by our parser), which might really surprise some users.
This reason is enough to extend our project by a new class that encapsulates these quirks and to offer a cleaner interface to our parser. Let's create a new class, Packt\Chp8\DSL\ExpressionBuilder
:
namespace Packt\Chp8\DSL\ExpressionBuilder; use Packt\Chp8\DSL\AST\Expression; use Packt\Chp8\DSL\Exception...