tmilos / gold-parser
Golder parser PHP runtime LALR engine and compiled grammar loader
Requires
- php: >=5.6.1
- symfony/event-dispatcher: ^2.0|^3.0
- tmilos/value: ^1.0
Requires (Dev)
- phpunit/phpunit: ~4.8|~5.6
- satooshi/php-coveralls: ~1.0
This package is not auto-updated.
Last update: 2024-11-09 21:01:38 UTC
README
Gold parser PHP runtime LALR engine and compiled grammar loader. For more information check Gold Parser website. Library is written based on the Calitha C# GOLD Parser Engine.
Installation
You can use Composer to install
$ composer require tmilos/gold-parser
Usage
Use Loader
class to load compiled grammar file, and it's createNewParser()
to get the LALR parser for that grammar.
<?php $parser = Loader::fromFile('grammar.cgt')->createNewParser(); $nonTerminal = $parser->parse($inputString); $parser->isAccepted(); // true
Events
The Parser
instance has a default event listener, which you could replace. During parsing it dispatches various events.
In the Events
class are enumerated all events that are dispatched.
Errors
By default the parser adds error listeners that will throw exceptions when error events are dispatched. You can add your own
listeners for the error events and disable those default listeners with Parser::setThrowExceptionsOnErrors(false)
Default error handlers will throw ParseException
on PARSE_ERROR
event, and TokenException
on TOKEN_ERROR
event.
Performance
On my modest laptop with PHP 7.0 it takes around 0.2 seconds to load grammar and create parser, and around 0.04 seconds to parse ~700 chars json. Feel free to contribute and improve performance. Think the loading is critical.