jojo1981 / json-ast-builder
Json AST builder
1.0.0
2019-10-18 09:07 UTC
Requires
- php: ^7.1
- ext-ctype: *
Requires (Dev)
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2024-12-05 06:30:18 UTC
README
Author: Joost Nijhuis <jnijhuis81@gmail.com>
A PHP Implementation for building an AST (Abstract Syntax Tree) from a JSON
string.
The AST is visitable and can be visited by implementing a visitor (Behavioral Design Pattern Visitor).
Purposes of this library are:
- Validate JSON syntax and give the end user a precise error message about syntax errors
- Transform an AST to a JSON string and control the format
- Travers AST by implementing your own visitor
More information about the lightweight data-interchange format JSON (JavaScript Object Notation) can be found here.
Installation
Library
git clone https://github.com/jojo1981/json-ast-builder.git
Composer
composer require jojo1981/json-ast-builder
Usage
<?php require 'vendor/autoload.php'; use Jojo1981\JsonAstBuilder\Generator; use Jojo1981\JsonAstBuilder\Lexer\Lexer; use Jojo1981\JsonAstBuilder\Parser; // setup lexer and parser $parser = new Parser(new Lexer()); $parser->setInput(\file_get_contents('data.json')); // build AST $ast = $parser->parse(); // You can use the generator to generate multiple things $generator = new Generator(); // default generate json string options $generateJsonStringOptions = [ 'useTabs' => false, 'pretty' => true, 'indentSize' => 2, 'spacesBeforeColon' => 0, 'spacesAfterColon' => 1, 'lineSeparator' => PHP_EOL ]; // options can be omitted $jsonString = $generator->generateJsonString($ast, $generateJsonStringOptions); // default generate json string options $generateDataOptions = [ 'assoc' => false ]; // options can be omitted $data = $generator->generateData($ast, $generateDataOptions); $plantUmlString = $generator->generatePlantUmlData($ast); \file_put_contents('test-output.puml', $plantUmlString); $statistics = $generator->getStatistics($ast);