emanuelecoppola/phpeg 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

emanuelecoppola/phpeg

最新稳定版本:v1.0.1

Composer 安装命令:

composer require emanuelecoppola/phpeg

包简介

A modern PHP PEG parsing library.

README 文档

README

Tests Latest Version Total Downloads License

PHPeg is a modern PEG parsing library for PHP.

It gives you:

  • a fluent PHP grammar builder
  • two grammar loaders: CleanPeg for compact grammars with built-in conveniences, Classic PEG for explicit traditional PEG syntax
  • AST querying, mutation, and source-preserving printing
  • configurable parsing trade-offs for speed, memory, and diagnostics
  • left-recursive grammars are detected and handled automatically

If you want to parse PHP-native grammars and still get serious AST tooling and source-preserving editing, this library is built for that.

Installation

composer require emanuelecoppola/phpeg

Quick Start

This example parses a tiny env-style line and then replaces its value in place.

<?php

declare(strict_types=1);

use EmanueleCoppola\PHPeg\Ast\AstNodeFactory;
use EmanueleCoppola\PHPeg\Builder\GrammarBuilder;

require __DIR__ . '/vendor/autoload.php';

$g = GrammarBuilder::create();

$grammar = $g->grammar('Start')
    ->rule('Word', $g->oneOrMore($g->charClass('[A-Z_]')))
    ->rule('Value', $g->oneOrMore($g->charClass('[a-z]')))
    ->rule('Assignment', $g->seq(
        $g->ref('Word'),
        $g->literal('='),
        $g->ref('Value'),
    ))
    ->rule('Start', $g->seq($g->ref('Assignment'), $g->eof()))
    ->build();

$input = 'APP_ENV=local';
$result = $grammar->parse($input);

if (!$result->isSuccess()) {
    echo $result->error()?->message() . PHP_EOL;
    exit(1);
}

echo $result->node()?->name() . PHP_EOL;
echo $result->matchedText() . PHP_EOL;

$document = $grammar->parseDocument($input);
$factory = new AstNodeFactory();

$document->query('Value')->first()?->replaceWith(
    $factory->token('Value', 'production')
);

echo $document->print();

The output is:

Start
APP_ENV=local
APP_ENV=production

Parser options

Parser behavior is configured through ParserOptions. The available options and recommended combinations are documented in docs/options.md.

Documentation

Examples

The repository includes end-to-end examples for:

  • calculator parsing with CleanPeg
  • JSON parsing
  • nginx config editing
  • dotenv config editing
  • tiny-markup parsing with named captures in CleanPeg
  • recursive language AST editing
  • Bixby-style language parsing
  • access-policy parsing

Useful entry points:

References

This repository implements lake symbols for island parsing, and its grammar tooling is also inspired by Arpeggio.

Okuda, K., Chiba, S.
"Lake Symbols for Island Parsing"
https://arxiv.org/abs/2010.16306

The paper is included in this repository as docs/papers/lake-symbols-for-island-parsing.pdf for reference.

Dejanović I., Milosavljević G., Vaderna R.
"Arpeggio: A flexible PEG parser for Python"
https://doi.org/10.1016/j.knosys.2015.12.004

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-29

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固