krak/lex
Composer 安装命令:
composer require krak/lex
包简介
Functional Lexing library
README 文档
README
Lex is a library for lexical analysis in PHP. Currently, only simple regular expression lexers are available; but considering that you shouldn't be lexing anything complex in php, this should be fine :).
Installation
composer require krak/lex
Usage
<?php require_once __DIR__ . '/vendor/autoload.php'; use function Krak\Lex\lexer, Krak\Lex\skipLexer; const TOK_INT = 'int'; const TOK_PLUS = 'plus'; const TOK_MINUS = 'minus'; const TOK_WS = 'whitespace'; // creates a lexer that will use these RE's to match input // the A (anchor flag) is required $lex = lexer([ '/\d+/A' => TOK_INT, '/\+/A' => TOK_PLUS, '/\-/A' => TOK_MINUS, '/\s+/A' => TOK_WS ]); // decorator for skipping tokens, in this case, just throw away the whitespace tokens $lex = skipLexer($lex, [TOK_WS]); // lex the input and return an iterator of tokens $toks = $lex('1 + 2 - 3'); foreach ($toks as $matched_tok) { printf( "Matched token '%s' with input '%s' at offset %d\n", $matched_tok->token, $matched_tok->match, $matched_tok->offset ); }
The following program would output
Matched token 'int' with input '1' at offset 0
Matched token 'plus' with input '+' at offset 2
Matched token 'int' with input '2' at offset 4
Matched token 'minus' with input '-' at offset 6
Matched token 'int' with input '3' at offset 8
TokenStream
A token stream is a simple interface for consuming one token at a time. This is very useful for Recursive Decent Parsers
<?php use function Krak\Lex\lexer, Krak\Lex\tokenStreamLexer; $lex = lexer(['/a/A' => 'a', '/b/A' => 'b']); $lex = tokenStreamLexer($lex); $stream = $lex('aba'); assert($stream->peek() == 'a'); assert($stream->getToken() == 'a'); assert($stream->getToken() == 'b'); assert($stream->getToken() == 'a'); assert($stream->isEmpty());
API
Lexers
Each lexer will accept a string input and then return an iterable of MatchedToken
lexer($token_map, $throw = true)
Main lexer which lexes the strings based off of the $token_map. $throw determines whether or not the lexer should throw an exception on unrecognized input.
skipLexer($lex, $tokens)
Lexer decorator which will skip any Matched Tokens in the set of the $tokens passed in.
tokenStreamLexer($lex)
Lexer decorator that will convert the output of the $lex into a TokenStream
mockLexer($tokens)
Returns $tokens as is.
class MatchedToken
$match
Returns the text that was matched.
$token
Returns the token name that was matched.
$offset
Returns the string offset at which the match started.
interface TokenStream extends \IteratorAggregate
getToken()
Returns the current token and advances the internal pointer up by one.
peek()
Returns the current token but does not advance the internal pointer
isEmpty()
returns true if the token stream is empty, false if not.
krak/lex 适用场景与选型建议
krak/lex 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.46k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 04 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 krak/lex 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 krak/lex 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7.46k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 4
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-04-25