PHPackages                             yosymfony/parser-utils - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. yosymfony/parser-utils

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

yosymfony/parser-utils
======================

Parser utilities

v2.0.0(7y ago)201.5M↓10.5%32MITPHPPHP &gt;=7.1

Since Nov 18Pushed 7y ago1 watchersCompare

[ Source](https://github.com/yosymfony/parser-utils)[ Packagist](https://packagist.org/packages/yosymfony/parser-utils)[ Docs](http://github.com/yosymfony/toml)[ RSS](/packages/yosymfony-parser-utils/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (2)

A library for writing [recursive descent parsers](https://en.wikipedia.org/wiki/Recursive_descent_parser)in PHP.

[![Build Status](https://camo.githubusercontent.com/1c05cf718686015a5e0c67a902232893d079a6c4f5c58d94c78cdd6903a12312/68747470733a2f2f7472617669732d63692e6f72672f796f73796d666f6e792f7061727365722d7574696c732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/yosymfony/parser-utils)

requires
--------

[](#requires)

- PHP &gt;= 7.1

Installation
------------

[](#installation)

The preferred installation method is [composer](https://getcomposer.org):

```
composer require yosymfony/parser-utils
```

An example
----------

[](#an-example)

First, you need to create a lexer. This one will recognize tokens

```
use Yosymfony\ParserUtils\BasicLexer;

$lexer = new BasicLexer([
    '/^([0-9]+)/x' => 'T_NUMBER',
    '/^(\+)/x' => 'T_PLUS',
    '/^(-)/x' => 'T_MINUS',
    '/^\s+/' => 'T_SPACE',  // We do not surround it with parentheses because
                            // this is not meaningful for us in this case
]);
```

Second, you need a parser for consuming the tokens provided by the lexer. The `AbstractParser` class contains an abstract method called `parseImplementation`that receives a `TokenStream` as an argument.

```
use Yosymfony\ParserUtils\AbstractParser;

class Parser extends AbstractParser
{
    protected function parseImplementation(TokenStream $stream)
    {
        $result = $stream->matchNext('T_NUMBER');

        while ($stream->isNextAny(['T_PLUS', 'T_MINUS'])) {
            switch ($stream->moveNext()->getName()) {
                case 'T_PLUS':
                    $result += $stream->matchNext('T_NUMBER');
                    break;
                case 'T_MINUS':
                    $result -= $stream->matchNext('T_NUMBER');
                    break;
                default:
                    throw new SyntaxErrorException("Something went wrong");
                    break;
            }
        }

        return $result;
    }
}
```

Now, you can see the results:

```
$parser = new Parser($lexer);
$parser->parse('1 + 1');          // 2
```

### The BasicLexer class

[](#the-basiclexer-class)

The lexer has the responsibility of recognizing tokens. This one works line by line. If you want to generate an special `T_NEWLINE` token for each line of the input, call `$lexer->generateNewlineTokens()` before tokenizing. You can set the name of this special token using the method `setNewlineTokenName`.

```
$lexer = new BasicLexer([...]);
$lexer->generateNewlineTokens()
      ->setNewlineTokenName('T_NL');

$lexer->tokenize('...');
```

Additionally, there is another special token `T_EOS` that determines the end of the input string. To enable this feature call `$lexer->generateEosToken()` before tokenizing. You can set the name of this special token using the method `setEosTokenName`.

```
$lexer = new BasicLexer([...]);
$lexer->generateEosToken()
      ->setEosTokenName('T_MY_EOS');

$lexer->tokenize('...');
```

### The TokenStream class

[](#the-tokenstream-class)

This class let you treat with the list of tokens returned by the lexer.

- **moveNext**: Moves the pointer one token forward. Returns a `Token` object or `null` if there are not more tokens. e.g: `$ts->moveNext()`.
- **matchNext**: Matches the next token and returns its value. This method moves the pointer one token forward. It will throw an `SyntaxErrorException` exception if the next token does not match. e.g: `$number = $ts->matchNext('T_NUMBER')`.
- **isNext**: Checks if the next token matches with the token name passed as argument. e.g: `$ts->isNext('T_PLUS') // true or false`.
- **skipWhile**: Skips tokens while they match with the token name passed as argument. This method moves the pointer "n" tokens forward until the last one that match with the token name. e.g: `$ts->skipWhile('T_PLUS')`
- **skipWhileAny**: Skips tokens while they match with one of the token names passed as argument. This method moves the pointer "n" tokens forward until the last one that match with one of the token names e.g: `$ts->skipWhileAny(['T_PLUS', 'T_MINUS'])`
- **isNextSequence**: Checks if the following tokens in the stream match with the sequence of tokens. e.g: `$ts->isNextSequence(['T_NUMBER', 'T_PLUS', 'T_NUMBER']) // true or false`.
- **isNextAny**: Checks if one of the tokens passed as argument is the next token. e.g: `$fs->isNextAny(['T_PLUS', 'T_SUB']) // true or false`
- **hasPendingTokens**: Has pending tokens? e.g: `$fs->hasPendingTokens() // true or false`.
- **reset**: Resets the stream to the beginning.

### Tokens

[](#tokens)

Tokens are instances of `Token` class, a class than contains the following methods:

- **getName**: returns the name of the toke. e.g: `T_SUM`.
- **getValue**: returns the value of the token.
- **getLine**: returns the line in where the token is found.

Unit tests
----------

[](#unit-tests)

You can run the unit tests with the following command:

```
$ cd parser-utils
$ composer test
```

License
-------

[](#license)

This library is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity49

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~222 days

Total

2

Last Release

2880d ago

Major Versions

v1.0.0 → v2.0.02018-06-29

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3321099?v=4)[Yo! Symfony](/maintainers/yosymfony)[@yosymfony](https://github.com/yosymfony)

---

Top Contributors

[![yosymfony](https://avatars.githubusercontent.com/u/3321099?v=4)](https://github.com/yosymfony "yosymfony (21 commits)")

---

Tags

lexerparserphprecursive-descent-parserparserlexer

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/yosymfony-parser-utils/health.svg)

```
[![Health](https://phpackages.com/badges/yosymfony-parser-utils/health.svg)](https://phpackages.com/packages/yosymfony-parser-utils)
```

###  Alternatives

[doctrine/lexer

PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.

11.2k910.8M118](/packages/doctrine-lexer)[cerbero/json-parser

Zero-dependencies pull parser to read large JSON from any source in a memory-efficient way.

803474.6k5](/packages/cerbero-json-parser)[creof/geo-parser

Parser for geography coordinate strings

624.4M15](/packages/creof-geo-parser)[creof/wkt-parser

Parser for well-known text (WKT) object strings

554.8M16](/packages/creof-wkt-parser)[tmilos/lexer

Lexical analyzer with individual token definition with regular expressions

211.7M2](/packages/tmilos-lexer)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
