Name |
Date |
Size |
#Lines |
LOC |
||
---|---|---|---|---|---|---|
.. | 28-Mar-2019 | - | ||||
Bin/ | H | 28-Mar-2019 | - | 269 | 155 | |
Documentation/ | H | 28-Mar-2019 | - | 2,528 | 2,298 | |
Exception/ | H | 28-Mar-2019 | - | 432 | 62 | |
Llk/ | H | 28-Mar-2019 | - | 5,159 | 2,571 | |
Test/ | H | 28-Mar-2019 | - | 3,793 | 2,584 | |
Visitor/ | H | 28-Mar-2019 | - | 125 | 45 | |
.Mime | H A D | 27-Mar-2019 | 25 | 2 | 1 | |
.State | H A D | 27-Mar-2019 | 10 | 2 | 1 | |
CHANGELOG.md | H A D | 27-Mar-2019 | 9.8 KiB | 166 | 133 | |
Ll1.php | H A D | 27-Mar-2019 | 28.4 KiB | 1,015 | 424 | |
README.md | H A D | 27-Mar-2019 | 8.8 KiB | 299 | 227 | |
composer.json | H A D | 27-Mar-2019 | 1.7 KiB | 54 | 53 |
README.md
1<p align="center"> 2 <img src="https://static.hoa-project.net/Image/Hoa.svg" alt="Hoa" width="250px" /> 3</p> 4 5--- 6 7<p align="center"> 8 <a href="https://travis-ci.org/hoaproject/compiler"><img src="https://img.shields.io/travis/hoaproject/compiler/master.svg" alt="Build status" /></a> 9 <a href="https://coveralls.io/github/hoaproject/compiler?branch=master"><img src="https://img.shields.io/coveralls/hoaproject/compiler/master.svg" alt="Code coverage" /></a> 10 <a href="https://packagist.org/packages/hoa/compiler"><img src="https://img.shields.io/packagist/dt/hoa/compiler.svg" alt="Packagist" /></a> 11 <a href="https://hoa-project.net/LICENSE"><img src="https://img.shields.io/packagist/l/hoa/compiler.svg" alt="License" /></a> 12</p> 13<p align="center"> 14 Hoa is a <strong>modular</strong>, <strong>extensible</strong> and 15 <strong>structured</strong> set of PHP libraries.<br /> 16 Moreover, Hoa aims at being a bridge between industrial and research worlds. 17</p> 18 19# Hoa\Compiler 20 21[](https://webchat.freenode.net/?channels=#hoaproject) 22[](https://gitter.im/hoaproject/central) 23[](https://central.hoa-project.net/Documentation/Library/Compiler) 24[](https://waffle.io/hoaproject/compiler) 25 26This library allows to manipulate LL(1) and LL(k) compiler compilers. A 27dedicated grammar description language is provided for the last one: the PP 28language. 29 30[Learn more](https://central.hoa-project.net/Documentation/Library/Compiler). 31 32## Installation 33 34With [Composer](https://getcomposer.org/), to include this library into 35your dependencies, you need to 36require [`hoa/compiler`](https://packagist.org/packages/hoa/compiler): 37 38```sh 39$ composer require hoa/compiler '~3.0' 40``` 41 42For more installation procedures, please read [the Source 43page](https://hoa-project.net/Source.html). 44 45## Testing 46 47Before running the test suites, the development dependencies must be installed: 48 49```sh 50$ composer install 51``` 52 53Then, to run all the test suites: 54 55```sh 56$ vendor/bin/hoa test:run 57``` 58 59For more information, please read the [contributor 60guide](https://hoa-project.net/Literature/Contributor/Guide.html). 61 62## Quick usage 63 64As a quick overview, we will look at the PP language and the LL(k) compiler 65compiler. 66 67### The PP language 68 69A grammar is constituted by tokens (the units of a word) and rules (please, see 70the documentation for an introduction to the language theory). The PP language 71declares tokens with the following construction: 72 73``` 74%token [source_namespace:]name value [-> destination_namespace] 75``` 76 77The default namespace is `default`. The value of a token is represented by a 78[PCRE](http://pcre.org/). We can skip tokens with the `%skip` construction. 79 80As an example, we will take the *simplified* grammar of the [JSON 81language](http://json.org/). The complete grammar is in the 82`hoa://Library/Json/Grammar.pp` file. Thus: 83 84``` 85%skip space \s 86// Scalars. 87%token true true 88%token false false 89%token null null 90// Strings. 91%token quote_ " -> string 92%token string:string [^"]+ 93%token string:_quote " -> default 94// Objects. 95%token brace_ { 96%token _brace } 97// Arrays. 98%token bracket_ \[ 99%token _bracket \] 100// Rest. 101%token colon : 102%token comma , 103%token number \d+ 104 105value: 106 <true> | <false> | <null> | string() | object() | array() | number() 107 108string: 109 ::quote_:: <string> ::_quote:: 110 111number: 112 <number> 113 114#object: 115 ::brace_:: pair() ( ::comma:: pair() )* ::_brace:: 116 117#pair: 118 string() ::colon:: value() 119 120#array: 121 ::bracket_:: value() ( ::comma:: value() )* ::_bracket:: 122``` 123 124We can see the PP constructions: 125 126 * `rule()` to call a rule; 127 * `<token>` and `::token::` to declare a token; 128 * `|` for a disjunction; 129 * `(…)` to group multiple declarations; 130 * `e?` to say that `e` is optional; 131 * `e+` to say that `e` can appear at least 1 time; 132 * `e*` to say that `e` can appear 0 or many times; 133 * `e{x,y}` to say that `e` can appear between `x` and `y` times; 134 * `#node` to create a node the AST (resulting tree); 135 * `token[i]` to unify tokens value between them. 136 137Unification is very useful. For example, if we have a token that expresses a 138quote (simple or double), we could have: 139 140``` 141%token quote "|' 142%token handle \w+ 143 144string: 145 ::quote:: <handle> ::quote:: 146``` 147 148So, the data `"foo"` and `'foo'` will be valid, but also `"foo'` and `'foo"`! To 149avoid this, we can add a new constraint on token value by unifying them, thus: 150 151``` 152string: 153 ::quote[0]:: <handle> ::quote[0]:: 154``` 155 156All `quote[0]` for the rule instance must have the same value. Another example 157is the unification of XML tags name. 158 159### LL(k) compiler compiler 160 161The `Hoa\Compiler\Llk\Llk` class provide helpers to manipulate (load or save) a 162compiler. The following code will use the previous grammar to create a compiler, 163and we will parse a JSON string. If the parsing succeed, it will produce an AST 164(stands for Abstract Syntax Tree) we can visit, for example to dump the AST: 165 166```php 167// 1. Load grammar. 168$compiler = Hoa\Compiler\Llk\Llk::load(new Hoa\File\Read('Json.pp')); 169 170// 2. Parse a data. 171$ast = $compiler->parse('{"foo": true, "bar": [null, 42]}'); 172 173// 3. Dump the AST. 174$dump = new Hoa\Compiler\Visitor\Dump(); 175echo $dump->visit($ast); 176 177/** 178 * Will output: 179 * > #object 180 * > > #pair 181 * > > > token(string, foo) 182 * > > > token(true, true) 183 * > > #pair 184 * > > > token(string, bar) 185 * > > > #array 186 * > > > > token(null, null) 187 * > > > > token(number, 42) 188 */ 189``` 190 191Pretty simple. 192 193### Compiler in CLI 194 195This library proposes a script to parse and apply a visitor on a data with a 196specific grammar. Very useful. Moreover, we can use pipe (because 197`Hoa\File\Read` —please, see the [`Hoa\File` 198library](http://central.hoa-project.net/Resource/Library/File/)— supports `0` as 199`stdin`), thus: 200 201```sh 202$ echo '[1, [1, [2, 3], 5], 8]' | hoa compiler:pp Json.pp 0 --visitor dump 203> #array 204> > token(number, 1) 205> > #array 206> > > token(number, 1) 207> > > #array 208> > > > token(number, 2) 209> > > > token(number, 3) 210> > > token(number, 5) 211> > token(number, 8) 212``` 213 214You can apply any visitor classes. 215 216### Errors 217 218Errors are well-presented: 219 220```sh 221$ echo '{"foo" true}' | hoa compiler:pp Json.pp 0 --visitor dump 222Uncaught exception (Hoa\Compiler\Exception\UnexpectedToken): 223Hoa\Compiler\Llk\Parser::parse(): (0) Unexpected token "true" (true) at line 1 224and column 8: 225{"foo" true} 226 ↑ 227in hoa://Library/Compiler/Llk/Parser.php at line 1 228``` 229 230### Samplers 231 232Some algorithms are available to generate data based on a grammar. We will give 233only one example with the coverage-based generation algorithm that will activate 234all branches and tokens in the grammar: 235 236```php 237$sampler = new Hoa\Compiler\Llk\Sampler\Coverage( 238 // Grammar. 239 Hoa\Compiler\Llk\Llk::load(new Hoa\File\Read('Json.pp')), 240 // Token sampler. 241 new Hoa\Regex\Visitor\Isotropic(new Hoa\Math\Sampler\Random()) 242); 243 244foreach ($sampler as $i => $data) { 245 echo $i, ' => ', $data, "\n"; 246} 247 248/** 249 * Will output: 250 * 0 => true 251 * 1 => {" )o?bz " : null , " %3W) " : [false, 130 , " 6" ] } 252 * 2 => [{" ny " : true } ] 253 * 3 => {" Ne;[3 " :[ true , true ] , " th: " : true," C[8} " : true } 254 */ 255``` 256 257## Research papers 258 259 * *Grammar-Based Testing using Realistic Domains in PHP*, 260 presented at [A-MOST 2012](https://sites.google.com/site/amost2012/) (Montréal, Canada) 261 ([article](https://hoa-project.net/En/Literature/Research/Amost12.pdf), 262 [presentation](http://keynote.hoa-project.net/Amost12/EDGB12.pdf), 263 [details](https://hoa-project.net/En/Event/Amost12.html)). 264 265## Documentation 266 267The 268[hack book of `Hoa\Compiler`](https://central.hoa-project.net/Documentation/Library/Compiler) contains 269detailed information about how to use this library and how it works. 270 271To generate the documentation locally, execute the following commands: 272 273```sh 274$ composer require --dev hoa/devtools 275$ vendor/bin/hoa devtools:documentation --open 276``` 277 278More documentation can be found on the project's website: 279[hoa-project.net](https://hoa-project.net/). 280 281## Getting help 282 283There are mainly two ways to get help: 284 285 * On the [`#hoaproject`](https://webchat.freenode.net/?channels=#hoaproject) 286 IRC channel, 287 * On the forum at [users.hoa-project.net](https://users.hoa-project.net). 288 289## Contribution 290 291Do you want to contribute? Thanks! A detailed [contributor 292guide](https://hoa-project.net/Literature/Contributor/Guide.html) explains 293everything you need to know. 294 295## License 296 297Hoa is under the New BSD License (BSD-3-Clause). Please, see 298[`LICENSE`](https://hoa-project.net/LICENSE) for details. 299