2017 © Pedro Peláez
 

library regex-parser

AST for PCRE Regex

image

robinbressan/regex-parser

AST for PCRE Regex

  • Tuesday, November 3, 2015
  • by RobinBressan
  • Repository
  • 3 Watchers
  • 16 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

RegexParser

RegexParser is a parser for PCRE regex. It produces an AST which represents your regex. It can help you generating some inputs which match your regex., (*1)

Installation

It is available with Composer:, (*2)

composer install robinbressan/regex-parser

Usage

To build an AST you need to create a parser:, (*3)

$parser = \RegexParser\Parser\Parser::create();

$ast = $parser->parse('YOUR_REGEX');

You can now use a formatter to convert the AST to several format (only XML is supported today):, (*4)

$formatter = new \RegexParser\Parser\Formatter\XMLFormatter();

$xml = $formatter->format($ast); // $xml is now an instance of DOMDocument

If you wish you can display it easily:, (*5)

$xml->formatOutput = true;
echo $xml->saveXML();

Because you can get a DOMDocument, you are able to use XPath engine to query your AST., (*6)

Example

The regex ^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$ will produce the following AST:, (*7)

<?xml version="1.0" encoding="utf-8"?>
<ast>
  <begin>
    <repetition min="1">
      <block sub-pattern="false">
        <token type="underscore">_</token>
        <character-class>
          <token type="char">a</token>
          <token type="char">z</token>
        </character-class>
        <character-class>
          <token type="integer">0</token>
          <token type="integer">9</token>
        </character-class>
        <token type="minus">-</token>
      </block>
    </repetition>
  </begin>
  <repetition min="0">
    <block sub-pattern="true">
      <token type="char">.</token>
      <repetition min="1">
        <block sub-pattern="false">
          <token type="underscore">_</token>
          <character-class>
            <token type="char">a</token>
            <token type="char">z</token>
          </character-class>
          <character-class>
            <token type="integer">0</token>
            <token type="integer">9</token>
          </character-class>
          <token type="minus">-</token>
        </block>
      </repetition>
    </block>
  </repetition>
  <token type="at">@</token>
  <repetition min="1">
    <block sub-pattern="false">
      <character-class>
        <token type="char">a</token>
        <token type="char">z</token>
      </character-class>
      <character-class>
        <token type="integer">0</token>
        <token type="integer">9</token>
      </character-class>
      <token type="minus">-</token>
    </block>
  </repetition>
  <repetition min="0">
    <block sub-pattern="true">
      <token type="char">.</token>
      <repetition min="1">
        <block sub-pattern="false">
          <character-class>
            <token type="char">a</token>
            <token type="char">z</token>
          </character-class>
          <character-class>
            <token type="integer">0</token>
            <token type="integer">9</token>
          </character-class>
          <token type="minus">-</token>
        </block>
      </repetition>
    </block>
  </repetition>
  <end>
    <block sub-pattern="true">
      <token type="char">.</token>
      <repetition min="2" max="4">
        <block sub-pattern="false">
          <character-class>
            <token type="char">a</token>
            <token type="char">z</token>
          </character-class>
        </block>
      </repetition>
    </block>
  </end>
</ast>

Generator

You can also create a generator based on your AST which will generate a string that match your regex:, (*8)

$generator = new \RegexParser\Generator\RandomGenerator($ast);
$generator->generate($seed = null);

If you wish you can also create directly the generator :, (*9)

$generator = new \RegexParser\Generator\RandomGenerator::create('YOUR_REGEX');
$generator->generate($seed = null);

Test

To run the test you must run phpunit command., (*10)

Contributing

All contributions are welcome. If you add a new feature, ensure all tests are green!, (*11)

License

This application is available under the MIT License., (*12)

The Versions

03/11 2015

dev-master

9999999-dev

AST for PCRE Regex

  Sources   Download

MIT

The Development Requires

by Robin Bressan