Skip to content
generated from ghostwriter/wip

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

License

Notifications You must be signed in to change notification settings

ghostwriter/plex

Repository files navigation

Plex

Compliance Supported PHP Version GitHub Sponsors Code Coverage Type Coverage Psalm Level Latest Version on Packagist 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.

About

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

Resources

License

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages