PHPackages                             edin/lexicon - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. edin/lexicon

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

edin/lexicon
============

Attribute-driven PHP lexer and parser toolkit with enum tokens, grammar recipes, diagnostics, and AST/BNF debugging.

v0.4.1(1mo ago)428↓87.5%1MITPHPPHP ^8.2CI passing

Since Jun 7Pushed 1mo agoCompare

[ Source](https://github.com/edin/Lexicon)[ Packagist](https://packagist.org/packages/edin/lexicon)[ RSS](/packages/edin-lexicon/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependencies (4)Versions (6)Used By (1)

Lexicon
=======

[](#lexicon)

Attribute-driven PHP lexer and parser toolkit.

Lexicon lets you define tokens with PHP enums, parse into typed AST nodes, and inspect tokens, AST shape, and generated BNF-like grammar.

```
$tokens = Lexer::from(MyToken::class)->scan($source);
$node = Parser::fromTokens($tokens)->parse(MyNode::class);
```

Features
--------

[](#features)

- enum-based token definitions with PHP attributes
- matcher-backed literals, fixed tokens, keywords, symbols, trivia, unknown tokens, and EOF
- trivia preservation with `Token::fullText()`
- user-defined lexer modes with enter/push/pop transitions
- diagnostics for lexer and parser errors
- recursive descent parser primitives
- attribute grammar recipes: `Terminal`, `OneOf`, `Between`, `Optional`, `Many`, `SeparatedBy`, `SeparatedByRequired`, `ListBetween`, `Sequence`, `Fold`
- reusable parser extension points with parslets and `#[Parse(...)]`
- custom parser escape hatch with `ParseableNodeInterface`
- token table, AST printer, and BNF grammar printer

Install
-------

[](#install)

```
composer require edin/lexicon
```

For local development:

```
composer install
```

Quick Taste
-----------

[](#quick-taste)

```
#[OneOf([
    GroupedExpressionNode::class,
    NumberNode::class,
])]
interface PrimaryExpressionNode extends ExpressionNode
{
}

#[Terminal(MathToken::Number)]
final readonly class NumberNode implements PrimaryExpressionNode
{
    public function __construct(public Token $token)
    {
    }
}

#[Fold(
    operators: [MathToken::Plus, MathToken::Minus],
    operand: TermNode::class
)]
final readonly class ExpressionNode
{
    public function __construct(
        public Token $operator,
        public Node $left,
        public Node $right
    ) {
    }
}
```

Generated grammar:

```
Start ::= AdditiveExpressionNode

AdditiveExpressionNode ::= MultiplicativeExpressionNode ((Plus | Minus) MultiplicativeExpressionNode)*
MultiplicativeExpressionNode ::= PrimaryExpressionNode ((Star | Slash) PrimaryExpressionNode)*
PrimaryExpressionNode ::= GroupedExpressionNode | NumberNode
GroupedExpressionNode ::= OpenParen AdditiveExpressionNode CloseParen
NumberNode ::= Number
```

Documentation
-------------

[](#documentation)

- [Quick Start](docs/quick-start.md)
- [Token Attributes](docs/token-attributes.md)
- [Matchers](docs/matchers.md)
- [Modes](docs/modes.md)
- [Parser Primitives And Recipes](docs/parser.md)
- [Debugging Tokens](docs/debugging.md)
- [Examples](docs/examples.md)

Run Examples
------------

[](#run-examples)

```
php examples/basic-scanner.php
php examples/json-scanner.php
php examples/xml-scanner.php
php examples/math.php
```

Test
----

[](#test)

```
composer test
composer analyse
```

License
-------

[](#license)

MIT

###  Health Score

39

—

LowBetter than 84% of packages

Maintenance91

Actively maintained with recent releases

Popularity11

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~1 days

Total

5

Last Release

43d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/82bb42da001bbd10ee7a34aa349e7cf749cb509408f40170fa9cfdb80984ca87?d=identicon)[EdinOmeragic](/maintainers/EdinOmeragic)

---

Top Contributors

[![edin](https://avatars.githubusercontent.com/u/572400?v=4)](https://github.com/edin "edin (8 commits)")

---

Tags

attributeslexerscannerjsonxmlenumparserlexertokenizerattributesastgrammarscannerbnf

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/edin-lexicon/health.svg)

```
[![Health](https://phpackages.com/badges/edin-lexicon/health.svg)](https://phpackages.com/packages/edin-lexicon)
```

###  Alternatives

[cerbero/json-parser

Zero-dependencies pull parser to read large JSON from any source in a memory-efficient way.

807588.3k5](/packages/cerbero-json-parser)

PHPackages © 2026

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