yosymfony/parser-utils
Composer 安装命令:
composer require yosymfony/parser-utils
包简介
Parser utilities
README 文档
README
A library for writing recursive descent parsers in PHP.
requires
- PHP >= 7.1
Installation
The preferred installation method is composer:
composer require yosymfony/parser-utils
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 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
This class let you treat with the list of tokens returned by the lexer.
-
moveNext: Moves the pointer one token forward. Returns a
Tokenobject ornullif 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
SyntaxErrorExceptionexception 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 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
You can run the unit tests with the following command:
$ cd parser-utils $ composer test
License
This library is open-sourced software licensed under the MIT license.
yosymfony/parser-utils 适用场景与选型建议
yosymfony/parser-utils 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.59M 次下载、GitHub Stars 达 21, 最近一次更新时间为 2017 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「parser」 「lexer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yosymfony/parser-utils 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yosymfony/parser-utils 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yosymfony/parser-utils 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
A simple library for make Lexer and Parsers to build a language
An MT940 bank statement parser for PHP
Laravel integration for the jsonapi.org parser
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
PHP Library to tokenize various inputs.
统计信息
- 总下载量: 1.59M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 16
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-08-09