PHPackages                             sanmai/hoa-compiler - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sanmai/hoa-compiler

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

sanmai/hoa-compiler
===================

The Hoa\\Compiler library.

0.1.3(5y ago)1731[2 PRs](https://github.com/sanmai/hoa-compiler/pulls)BSD-3-ClausePHPPHP &gt;=7.0

Since Sep 18Pushed 5y ago2 watchersCompare

[ Source](https://github.com/sanmai/hoa-compiler)[ Packagist](https://packagist.org/packages/sanmai/hoa-compiler)[ GitHub Sponsors](https://github.com/sanmai)[ RSS](/packages/sanmai-hoa-compiler/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (4)Dependencies (8)Versions (9)Used By (0)

[![Build Status](https://camo.githubusercontent.com/f2ff2182c894a15a5531162e74821a145fc80593ae6da0feba87688f8063e046/68747470733a2f2f7472617669732d63692e636f6d2f73616e6d61692f686f612d636f6d70696c65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/sanmai/hoa-compiler)[![Coverage Status](https://camo.githubusercontent.com/fd60df51469c09b4fcefddc016042e7091ade07585b37e890e70c31dc41379ef/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f73616e6d61692f686f612d636f6d70696c65722f62616467652e737667)](https://coveralls.io/github/sanmai/hoa-compiler)[![Latest Stable Version](https://camo.githubusercontent.com/fc87346b9cb3c012911dfea1ec2bc2bf9a14d4c5624899223d202ac8206614a4/68747470733a2f2f706f7365722e707567782e6f72672f73616e6d61692f686f612d636f6d70696c65722f762f737461626c65)](https://packagist.org/packages/sanmai/hoa-compiler)

Install with:

```
composer require sanmai/hoa-compiler

```

This version of the library should be input-output-wise backward-compatible with [the original package](https://github.com/hoaproject/Compiler). For example, you can use this version together with `jms/serializer` to avoid [known problems](https://github.com/schmittjoh/serializer/issues/1182) some of the dependencies of the original package have with PHP 7.4.

BC breaking changes include:

- `Hoa\Exception\Exception` sub-classes are no longer thrown, please switch to `Hoa\Compiler\Exception`
- The package itself does not depend on development time only dependencies. If you need to call `getAST()`, you need to manually install `hoa/regex` and `hoa/file`.

Hoa\\Compiler
=============

[](#hoacompiler)

This library allows to manipulate LL(1) and LL(k) compiler compilers. A dedicated grammar description language is provided for the last one: the PP language.

[Learn more](https://central.hoa-project.net/Documentation/Library/Compiler).

Testing
-------

[](#testing)

To run all the test suites:

```
$ vendor/bin/hoa test:run
```

For more information, please read the [contributor guide](https://hoa-project.net/Literature/Contributor/Guide.html).

Quick usage
-----------

[](#quick-usage)

As a quick overview, we will look at the PP language and the LL(k) compiler compiler.

### The PP language

[](#the-pp-language)

A grammar is constituted by tokens (the units of a word) and rules (please, see the documentation for an introduction to the language theory). The PP language declares tokens with the following construction:

```
%token [source_namespace:]name value [-> destination_namespace]

```

The default namespace is `default`. The value of a token is represented by a [PCRE](http://pcre.org/). We can skip tokens with the `%skip` construction.

As an example, we will take the *simplified* grammar of the [JSON language](http://json.org/). The complete grammar is in the `hoa://Library/Json/Grammar.pp` file. Thus:

```
%skip   space          \s
// Scalars.
%token  true           true
%token  false          false
%token  null           null
// Strings.
%token  quote_         "        -> string
%token  string:string  [^"]+
%token  string:_quote  "        -> default
// Objects.
%token  brace_         {
%token _brace          }
// Arrays.
%token  bracket_       \[
%token _bracket        \]
// Rest.
%token  colon          :
%token  comma          ,
%token  number         \d+

value:
     |  |  | string() | object() | array() | number()

string:
    ::quote_::  ::_quote::

number:

#object:
    ::brace_:: pair() ( ::comma:: pair() )* ::_brace::

#pair:
    string() ::colon:: value()

#array:
    ::bracket_:: value() ( ::comma:: value() )* ::_bracket::

```

We can see the PP constructions:

- `rule()` to call a rule;
- `` and `::token::` to declare a token;
- `|` for a disjunction;
- `(…)` to group multiple declarations;
- `e?` to say that `e` is optional;
- `e+` to say that `e` can appear at least 1 time;
- `e*` to say that `e` can appear 0 or many times;
- `e{x,y}` to say that `e` can appear between `x` and `y` times;
- `#node` to create a node the AST (resulting tree);
- `token[i]` to unify tokens value between them.

Unification is very useful. For example, if we have a token that expresses a quote (simple or double), we could have:

```
%token  quote   "|'
%token  handle  \w+

string:
    ::quote::  ::quote::

```

So, the data `"foo"` and `'foo'` will be valid, but also `"foo'` and `'foo"`! To avoid this, we can add a new constraint on token value by unifying them, thus:

```
string:
    ::quote[0]::  ::quote[0]::

```

All `quote[0]` for the rule instance must have the same value. Another example is the unification of XML tags name.

### LL(k) compiler compiler

[](#llk-compiler-compiler)

The `Hoa\Compiler\Llk\Llk` class provide helpers to manipulate (load or save) a compiler. The following code will use the previous grammar to create a compiler, and we will parse a JSON string. If the parsing succeed, it will produce an AST (stands for Abstract Syntax Tree) we can visit, for example to dump the AST:

```
// 1. Load grammar.
$compiler = Hoa\Compiler\Llk\Llk::load(new Hoa\File\Read('Json.pp'));

// 2. Parse a data.
$ast = $compiler->parse('{"foo": true, "bar": [null, 42]}');

// 3. Dump the AST.
$dump = new Hoa\Compiler\Visitor\Dump();
echo $dump->visit($ast);

/**
 * Will output:
 *     >  #object
 *     >  >  #pair
 *     >  >  >  token(string, foo)
 *     >  >  >  token(true, true)
 *     >  >  #pair
 *     >  >  >  token(string, bar)
 *     >  >  >  #array
 *     >  >  >  >  token(null, null)
 *     >  >  >  >  token(number, 42)
 */
```

Pretty simple.

### Compiler in CLI

[](#compiler-in-cli)

This library proposes a script to parse and apply a visitor on a data with a specific grammar. Very useful. Moreover, we can use pipe (because `Hoa\File\Read` —please, see the [`Hoa\File`library](http://central.hoa-project.net/Resource/Library/File/)— supports `0` as `stdin`), thus:

```
$ echo '[1, [1, [2, 3], 5], 8]' | hoa compiler:pp Json.pp 0 --visitor dump
>  #array
>  >  token(number, 1)
>  >  #array
>  >  >  token(number, 1)
>  >  >  #array
>  >  >  >  token(number, 2)
>  >  >  >  token(number, 3)
>  >  >  token(number, 5)
>  >  token(number, 8)
```

You can apply any visitor classes.

### Errors

[](#errors)

Errors are well-presented:

```
$ echo '{"foo" true}' | hoa compiler:pp Json.pp 0 --visitor dump
Uncaught exception (Hoa\Compiler\Exception\UnexpectedToken):
Hoa\Compiler\Llk\Parser::parse(): (0) Unexpected token "true" (true) at line 1
and column 8:
{"foo" true}
       ↑
in hoa://Library/Compiler/Llk/Parser.php at line 1
```

### Samplers

[](#samplers)

Some algorithms are available to generate data based on a grammar. We will give only one example with the coverage-based generation algorithm that will activate all branches and tokens in the grammar:

```
$sampler = new Hoa\Compiler\Llk\Sampler\Coverage(
    // Grammar.
    Hoa\Compiler\Llk\Llk::load(new Hoa\File\Read('Json.pp')),
    // Token sampler.
    new Hoa\Regex\Visitor\Isotropic(new Hoa\Math\Sampler\Random())
);

foreach ($sampler as $i => $data) {
    echo $i, ' => ', $data, "\n";
}

/**
 * Will output:
 *     0 => true
 *     1 => {" )o?bz " : null , " %3W) " : [false, 130    , " 6"   ]  }
 *     2 => [{" ny  " : true } ]
 *     3 => {" Ne;[3 " :[ true , true ] , " th: " : true," C[8} " :   true }
 */
```

Research papers
---------------

[](#research-papers)

- *Grammar-Based Testing using Realistic Domains in PHP*, presented at [A-MOST 2012](https://sites.google.com/site/amost2012/) (Montréal, Canada) ([article](https://hoa-project.net/En/Literature/Research/Amost12.pdf), [presentation](http://keynote.hoa-project.net/Amost12/EDGB12.pdf), [details](https://hoa-project.net/En/Event/Amost12.html)).

License
-------

[](#license)

Hoa is under the New BSD License (BSD-3-Clause). Please, see [`LICENSE`](https://hoa-project.net/LICENSE) for details.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community19

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 70% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~113 days

Total

4

Last Release

1953d ago

PHP version history (2 changes)0.1PHP ^7.0

0.1.3PHP &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/edcb8dde95c71b1c97c3c91e57d3548795fa2014c657744fb878e2be3b5949fc?d=identicon)[sanmai](/maintainers/sanmai)

---

Top Contributors

[![Hywan](https://avatars.githubusercontent.com/u/946104?v=4)](https://github.com/Hywan "Hywan (366 commits)")[![sanmai](https://avatars.githubusercontent.com/u/139488?v=4)](https://github.com/sanmai "sanmai (126 commits)")[![SerafimArts](https://avatars.githubusercontent.com/u/2461257?v=4)](https://github.com/SerafimArts "SerafimArts (7 commits)")[![ohader](https://avatars.githubusercontent.com/u/402145?v=4)](https://github.com/ohader "ohader (6 commits)")[![vonglasow](https://avatars.githubusercontent.com/u/1275202?v=4)](https://github.com/vonglasow "vonglasow (3 commits)")[![Metalaka](https://avatars.githubusercontent.com/u/5406767?v=4)](https://github.com/Metalaka "Metalaka (2 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (2 commits)")[![jubianchi](https://avatars.githubusercontent.com/u/327237?v=4)](https://github.com/jubianchi "jubianchi (2 commits)")[![unkind](https://avatars.githubusercontent.com/u/1517363?v=4)](https://github.com/unkind "unkind (2 commits)")[![Majkl578](https://avatars.githubusercontent.com/u/144181?v=4)](https://github.com/Majkl578 "Majkl578 (1 commits)")[![lyrixx](https://avatars.githubusercontent.com/u/408368?v=4)](https://github.com/lyrixx "lyrixx (1 commits)")[![lovenunu](https://avatars.githubusercontent.com/u/6106094?v=4)](https://github.com/lovenunu "lovenunu (1 commits)")[![shulard](https://avatars.githubusercontent.com/u/482993?v=4)](https://github.com/shulard "shulard (1 commits)")[![stephpy](https://avatars.githubusercontent.com/u/232744?v=4)](https://github.com/stephpy "stephpy (1 commits)")[![jwage](https://avatars.githubusercontent.com/u/97422?v=4)](https://github.com/jwage "jwage (1 commits)")[![blmage](https://avatars.githubusercontent.com/u/25432517?v=4)](https://github.com/blmage "blmage (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/sanmai-hoa-compiler/health.svg)

```
[![Health](https://phpackages.com/badges/sanmai-hoa-compiler/health.svg)](https://phpackages.com/packages/sanmai-hoa-compiler)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
