Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tacoberu/bnf-php
Framework for building BNF like parsers.
https://github.com/tacoberu/bnf-php
bnf bnf-parser parser parser-combinators
Last synced: about 2 months ago
JSON representation
Framework for building BNF like parsers.
- Host: GitHub
- URL: https://github.com/tacoberu/bnf-php
- Owner: tacoberu
- License: mit
- Created: 2020-02-25T23:40:08.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-05-02T21:44:43.000Z (over 1 year ago)
- Last Synced: 2024-10-06T07:37:23.563Z (3 months ago)
- Topics: bnf, bnf-parser, parser, parser-combinators
- Language: PHP
- Homepage:
- Size: 52.7 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
A PHP BNF like parser
=====================[travisimage]: https://travis-ci.org/tacoberu/bnf-php.svg?branch=master
[travislink]: https://travis-ci.org/tacoberu/bnf-php
[![Latest Stable Version](http://poser.pugx.org/tacoberu/bnf/v)](https://packagist.org/packages/tacoberu/bnf)
[![Latest Unstable Version](http://poser.pugx.org/tacoberu/bnf/v/unstable)](https://packagist.org/packages/tacoberu/bnf)
[![PHP Version Require](http://poser.pugx.org/tacoberu/bnf/require/php)](https://packagist.org/packages/tacoberu/bnf)
[![License](http://poser.pugx.org/tacoberu/bnf/license)](https://packagist.org/packages/tacoberu/bnf)This is a PHP implementation of BNF like parser.
Installation
------------The recommended way to install is via Composer:
composer require tacoberu/bnf
Usage
-----```php
require __dir__ . '/vendor/autoload.php';
use Taco\BNF\Parser;
use Taco\BNF\Combinators\Pattern;
use Taco\BNF\Combinators\Whitechars;$parser = new Parser([
new Whitechars(Null, False),
new Pattern('element', ['~[^\n]+~']),
]);
$tree = $parser->parse('
-brand-name = Foo 3000
welcome = Welcome, {$name}, to {-brand-name}!
');print_r($tree); /*
array (
[0] => Taco\BNF\Token (
[type] => Taco\BNF\Combinators\Pattern (...)
[content] => "-brand-name = Foo 3000"
[start] => 1
[end] => 23
)
[0] => Taco\BNF\Token (
[type] => Taco\BNF\Combinators\Pattern (...)
[content] => "welcome = Welcome, {$name}, to {-brand-name}!"
[start] => 24
[end] => 69
)
)*/
```or more complex:
```php
require __dir__ . '/vendor/autoload.php';
use Taco\BNF\Parser;
use Taco\BNF\Combinators\Pattern;
use Taco\BNF\Combinators\Whitechars;$parser = new Parser([
new Whitechars(Null, False),
new Sequence('element', [
new Pattern('id', ['~[a-z\-]+~']),
new Whitechars(Null, False),
new Match(Null, ['='], False),
new Whitechars(Null, False),
new Pattern('element', ['~[^\n]+~']),
]),
]);
$tree = $parser->parse('
-brand-name = Foo 3000
welcome = Welcome, {$name}, to {-brand-name}!
');print_r($tree); /*
array (
[0] => Taco\BNF\Token (
[type] => Taco\BNF\Combinators\Sequence (...)
[content] => array(
[0] => Taco\BNF\Token (
[type] => Taco\BNF\Combinators\Pattern (...)
[content] => "-brand-name"
[start] => 1
[end] => 12
)
[1] => Taco\BNF\Token (
[type] => Taco\BNF\Combinators\Pattern (...)
[content] => "Foo 3000"
[start] => 15
[end] => 23
)
)
[start] => 1
[end] => 23
)
[0] => Taco\BNF\Token (
[type] => Taco\BNF\Combinators\Pattern (...)
[content] => array(
[0] => Taco\BNF\Token (
[type] => Taco\BNF\Combinators\Pattern (...)
[content] => "welcome"
[start] => 24
[end] => 31
)
[1] => Taco\BNF\Token (
[type] => Taco\BNF\Combinators\Pattern (...)
[content] => "Welcome, {$name}, to {-brand-name}!"
[start] => 34
[end] => 69
)
)
[start] => 24
[end] => 69
)
)*/
```See more examples in 'tests/ExhibitionTest.php'.