定制 ghostwriter/plex 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

ghostwriter/plex

Composer 安装命令:

composer require ghostwriter/plex

包简介

Provides the fastest lexer for PHP, tokenizing text with named regex patterns for efficient processing

README 文档

README

GitHub Sponsors Automation Supported PHP Version Downloads

Provides the fastest lexer for PHP, tokenizing text with named regex patterns for efficient processing.

Installation

You can install the package via composer:

composer require ghostwriter/plex

Star ⭐️ this repo if you find it useful

You can also star (🌟) this repo to find it easier later.

Usage

<?php

declare(strict_types=1);

use Ghostwriter\Plex\Grammar;
use Ghostwriter\Plex\Token;
use Ghostwriter\Plex\Lexer;


$grammar = Grammar::new([
    'T_NUMBER' => '\d+',
    'T_DOT' => '\.'
]);

$lexer = Lexer::new($grammar);

$expected = [
    Token::new('T_NUMBER', '1', 1, 1, []),
    Token::new('T_DOT', '.', 1, 2, []),
    Token::new('T_NUMBER', '2', 1, 3, []),
    Token::new('T_DOT', '.', 1, 4, []),
    Token::new('T_NUMBER', '3', 1, 5, []),
];

$content = '1.2.3';

assert($expected == iterator_to_array($lexer->lex($content)));

Named Patterns

When defining a grammar, you can reference other grammar rules by name using the (?&NAME) syntax, where NAME is the name of the grammar rule.

This allows you to create complex patterns that are easier to read and maintain.

<?php

declare(strict_types=1);

use Ghostwriter\Plex\Grammar;
use Ghostwriter\Plex\Token;
use Ghostwriter\Plex\Lexer;


$grammar = Grammar::new([
    // References both 'T_NUM' and 'ref-str' (which references T_STR)
    'T_ID' => '(?:(?&T_NUM)|(?&ref-str))*',
    
    // Matches numbers
    'T_NUM' => '\d+',
    
    // Matches word characters
    'T_STR' => '\w+',
    
    // References T_STR
    'ref-str' => '(?&T_STR)',
]);

$lexer = Lexer::new($grammar);

$expected = [
    Token::new('T_ID', '456def', 1, 6, [])
];

$content = '456def';

assert($expected == iterator_to_array($lexer->lex($content)));

Credits

Changelog

Please see CHANGELOG.md for more information on what has changed recently.

License

Please see LICENSE for more information on the license that applies to this project.

Security

Please see SECURITY.md for more information on security disclosure process.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: 未知许可证
  • 更新时间: 2024-08-30