Installation
Phplrt is installed with composer.
Requirements
- PHP 8.4 or above
- PCRE Extension
- Mbstring Extension (recommended)
Everything At Once
The simplest way to start is to install the whole project:
composer require phplrt/phplrt
This gives you the lexer, the parser, the builders and the grammar compiler.
Only What You Need
Every component is also published on its own, so you can install just the parts you actually use. This matters in production: the compiler reads grammar files and generates code, which is something you normally do while developing, not while serving requests.
# Runtime: reads source code and parses it composer require phplrt/runtime # Development: turns a grammar file into a lexer and a parser composer require phplrt/compiler --dev
Here is the full list:
| Package | What it does |
|---|---|
phplrt/source |
Reads source code from files, strings and streams |
phplrt/lexer |
Splits source code into tokens |
phplrt/parser |
Recognizes tokens against a grammar and builds a result |
phplrt/exception |
Renders errors with a snippet of the code around them |
phplrt/lexer-builder |
Describes a lexer in PHP and compiles it |
phplrt/parser-builder |
Describes a grammar in PHP and compiles it |
phplrt/compiler |
Reads *.pp2 or *.pp3 grammar files and generates PHP code |
And the contracts, if you only want to type-hint against interfaces:
composer require phplrt/lexer-contracts phplrt/parser-contracts phplrt/source-contracts
Autoloading
As with any composer package, include the autoloader:
require __DIR__ . '/vendor/autoload.php';
Which Packages Do I Actually Ship?
It depends on how you use phplrt.
If you generate a parser (recommended), the generated file is plain PHP that only refers to the runtime. The compiler is a development dependency:
1{ 2 "require": { 3 "phplrt/runtime": "^4.0" 4 }, 5 "require-dev": { 6 "phplrt/compiler": "^4.0" 7 } 8}
Or name the pieces yourself, if you would rather not pull in a metapackage:
1{ 2 "require": { 3 "phplrt/lexer": "^4.0", 4 "phplrt/parser": "^4.0", 5 "phplrt/source": "^4.0" 6 } 7}
If you read the grammar file at runtime, you need phplrt/compiler in
production too. That is fine for a script or a one-off tool, but it is slower:
the grammar has to be read and compiled on every run.