phplrt/phplrt

PHP Language Recognition Tool

3.1.0 2020-07-18 00:44 UTC

README

Phplrt

Travis CI 68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f39306565363865663935396637326665376266362f746573745f636f766572616765 68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f39306565363865663935396637326665376266362f6d61696e7461696e6162696c697479

PHP 7.1+ Latest Stable Version Latest Stable Version Total Downloads License MIT

68747470733a2f2f6f70656e636f6c6c6563746976652e636f6d2f7068706c72742f646f6e6174652f627574746f6e4032782e706e67

Thanks To

Antlr Hoa Project nikic/PHP-Parser JetBrains

Introduction

The phplrt is a set of tools for programming languages recognition. The library provides lexer, parser, grammar compiler, library for working with errors, text analysis and so on.

Installation

Phplrt is available as composer repository and can be installed using the following command in a root of your project:

$ composer require phplrt/phplrt

More detailed installation instructions are here.

Quick Start

<?php

use Phplrt\Compiler\Compiler;

$compiler = new Compiler();
$compiler->load(<<<EBNF
   
    %token T_DIGIT          \d
    %token T_PLUS           \+
    %token T_MINUS          \-
    %token T_POW            \*
    %token T_DIV            /
    %skip  T_WHITESPACE     \s+
    
    #Expression
      : <T_DIGIT> (Operator() <T_DIGIT>)* 
      ;

    #Operator
      : <T_PLUS>
      | <T_MINUS>
      | <T_POW>
      | <T_DIV>
      ;
EBNF);

Execution

echo $compiler->parse('2 + 2');

//
// Output:
//
// <Expression offset="0">
//     <T_DIGIT offset="0">2</T_DIGIT>
//     <Operator offset="2">
//         <T_PLUS offset="2">+</T_PLUS>
//     </Operator>
//     <T_DIGIT offset="4">2</T_DIGIT>
// </Expression>
//

Compilation

\file_put_contents(__DIR__ . '/grammar.php', (string)$compiler->build());

Documentation