goaop/dissect
Composer 安装命令:
composer require goaop/dissect
包简介
A set of tools for lexical and syntactical analysis written in pure PHP
README 文档
README
A pure-PHP toolkit for building custom lexers and LALR(1) parsers — fast, type-safe, and dependency-free.
✨ What is Dissect?
Dissect is a pure-PHP library for lexical and syntactical analysis — the foundational building blocks for any language tooling: expression evaluators, template engines, DSL interpreters, query parsers, and more.
It powers the GoAOP framework, where it parses pointcut DSL expressions into an AST for aspect-oriented programming.
Data flow
Input String
│
▼
┌─────────┐ ┌──────────────┐ ┌──────────────────┐
│ Lexer │ ───▶ │ TokenStream │ ───▶ │ LALR(1) Parser │ ───▶ Result / AST
└─────────┘ └──────────────┘ └──────────────────┘
▲
Grammar (rules
+ callbacks)
🚀 Key Features
🔤 Flexible Lexers
| Lexer | Description |
|---|---|
SimpleLexer |
Fluent builder API — define tokens with strings or regex, mark skippable tokens |
StatefulLexer |
Context-aware tokenization with explicit state transitions (e.g. for string interpolation) |
RegexLexer |
Abstract base class adapted from Doctrine — ultra-fast single-pass regex lexing |
📐 LALR(1) Parser
- Full LALR(1) grammar support — handles the vast majority of real-world grammars
- Fluent grammar API — define productions and semantic actions with readable PHP closures
- Operator precedence & associativity — built-in
left(),right(),nonassoc()declarations - Conflict resolution — configurable strategies: shift-wins, longer-reduce, earlier-reduce
- Precomputed parse tables — analyze once, serialize to PHP file, load instantly in production
🌳 AST Construction
CommonNode— ready-to-use tree node with named children and arbitrary attributes- Countable & iterable — traverse subtrees with standard PHP constructs
🛠 Developer Experience
- Zero runtime dependencies — only Symfony Console as an optional CLI dep
- PHPStan level 10 — fully typed with generics, array shapes, and readonly properties
- CLI tool — dump parse tables and visualize automaton states as Graphviz graphs
📦 Installation
composer require goaop/dissect
⚡ Quick Example
use Dissect\Lexer\SimpleLexer; use Dissect\Parser\Grammar; use Dissect\Parser\LALR1\Parser; // 1. Define a lexer $lexer = new SimpleLexer(); $lexer->regex('INT', '/[0-9]+/') ->token('PLUS', '+') ->token('MINUS', '-') ->regex('WS', '/\s+/') ->skip('WS'); // 2. Define a grammar $grammar = new Grammar(); $grammar('Expr') ->is('Expr', 'PLUS', 'Expr') ->call(fn($l, $_, $r) => $l + $r) ->is('Expr', 'MINUS', 'Expr') ->call(fn($l, $_, $r) => $l - $r) ->is('INT') ->call(fn($t) => (int) $t->getValue()); $grammar->operators('PLUS', 'MINUS')->left()->prec(1); $grammar->start('Expr'); // 3. Parse! $parser = new Parser($grammar); $result = $parser->parse($lexer->lex('3 + 5 - 2')); // → 6
📖 Documentation
| Topic | Description |
|---|---|
| Lexical analysis | SimpleLexer, StatefulLexer, RegexLexer, performance tips |
| Writing a grammar | Productions, callbacks, operator precedence, conflict resolution |
| Building an AST | CommonNode, tree traversal |
| Common patterns | Lists, comma-separated sequences, expression grammars |
| CLI tool | Precomputing parse tables, exporting automaton graphs |
🧪 Testing & Quality
# Run tests composer test # Run tests with coverage composer test-coverage # Static analysis (PHPStan level 10) composer phpstan
🙏 Credits
Originally created by @jakubledl, extended by @WalterWoshid, maintained by the GoAOP team.
Give a ⭐ if Dissect saved you from writing a parser by hand!
goaop/dissect 适用场景与选型建议
goaop/dissect 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 112.9k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 02 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「parser」 「parsing」 「ast」 「lexing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 goaop/dissect 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 goaop/dissect 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 goaop/dissect 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A port of the PEAR Net_HL7 library to PHP5.3+
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
Editable JSON AST with visitor traversal and formatting-preserving printing.
Parse Handlebars templates to a spec-compliant AST with PHP.
An MT940 bank statement parser for PHP
Html5 stream tokenizer/reader (not using libxml)
统计信息
- 总下载量: 112.9k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 23
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-18