hoa-math-community / compiler
Hoa\Compiler 库。
Requires
- php: ~8.0
- hoa-math-community/consistency: ~3.0
- hoa-math-community/exception: ~3.0
- hoa-math-community/file: ~2.0
- hoa-math-community/iterator: ~3.0
- hoa-math-community/math: ~2.0
- hoa-math-community/protocol: ~2.0
- hoa-math-community/regex: ~2.0
- hoa-math-community/visitor: ~3.0
This package is auto-updated.
Last update: 2024-09-17 15:35:15 UTC
README
Hoa 是一套 模块化、可扩展 和 结构化 的 PHP 库。
此外,Hoa 致力于成为工业界和学术界之间的桥梁。
Hoa\Compiler
此库允许操作 LL(1) 和 LL(k) 编译器编译器。为后者提供了一个专门的语法描述语言:PP 语言。
了解更多.
安装
使用 Composer,要将此库包含到依赖项中,您需要要求 hoa/compiler
$ composer require hoa/compiler '~3.0'
有关更多安装说明,请参阅 源代码页面。
测试
在运行测试套件之前,必须安装开发依赖项
$ composer install
然后,要运行所有测试套件
$ vendor/bin/hoa test:run
有关更多信息,请参阅 贡献者指南。
快速使用
作为一个快速概述,我们将查看 PP 语言和 LL(k) 编译器编译器。
PP 语言
语法由词元(单词的单位)和规则(请参阅文档了解语言理论简介)组成。PP 语言使用以下结构声明词元
%token [source_namespace:]name value [-> destination_namespace]
默认命名空间是 default
。词元的值由一个 PCRE 表示。我们可以使用 %skip
构造来跳过词元。
例如,我们将使用 JSON 语言 的 简化 语法。完整的语法在 hoa://Library/Json/Grammar.pp
文件中。因此
%skip space \s
// Scalars.
%token true true
%token false false
%token null null
// Strings.
%token quote_ " -> string
%token string:string [^"]+
%token string:_quote " -> default
// Objects.
%token brace_ {
%token _brace }
// Arrays.
%token bracket_ \[
%token _bracket \]
// Rest.
%token colon :
%token comma ,
%token number \d+
value:
<true> | <false> | <null> | string() | object() | array() | number()
string:
::quote_:: <string> ::_quote::
number:
<number>
#object:
::brace_:: pair() ( ::comma:: pair() )* ::_brace::
#pair:
string() ::colon:: value()
#array:
::bracket_:: value() ( ::comma:: value() )* ::_bracket::
我们可以看到 PP 构造
rule()
用于调用规则;<token>
和::token::
用于声明词元;|
用于析取;(…)
用于组合多个声明;e?
表示e
是可选的;e+
表示e
至少出现 1 次;e*
表示e
可以出现 0 或多次;e{x,y}
表示e
可以出现x
到y
次;#node
用于在 AST(结果树)中创建节点;token[i]
用于在词元之间统一值。
统一非常有用。例如,如果我们有一个表示引号(简单或双引号)的词元,我们可以有
%token quote "|'
%token handle \w+
string:
::quote:: <handle> ::quote::
因此,数据 "foo"
和 'foo'
将是有效的,但也可以是 "foo'
和 'foo"
!为了避免这种情况,我们可以通过统一它们来添加对词元值的新约束,因此
string:
::quote[0]:: <handle> ::quote[0]::
所有 quote[0]
的规则实例必须具有相同的值。另一个例子是 XML 标签名称的统一。
LL(k) 编译器编译器
Hoa\Compiler\Llk\Llk
类提供了操作(加载或保存)编译器的辅助函数。以下代码将使用前面的语法创建一个编译器,并将解析一个 JSON 字符串。如果解析成功,它将生成一个 AST(抽象语法树),我们可以遍历,例如,将其转储
// 1. Load grammar. $compiler = Hoa\Compiler\Llk\Llk::load(new Hoa\File\Read('Json.pp')); // 2. Parse a data. $ast = $compiler->parse('{"foo": true, "bar": [null, 42]}'); // 3. Dump the AST. $dump = new Hoa\Compiler\Visitor\Dump(); echo $dump->visit($ast); /** * Will output: * > #object * > > #pair * > > > token(string, foo) * > > > token(true, true) * > > #pair * > > > token(string, bar) * > > > #array * > > > > token(null, null) * > > > > token(number, 42) */
非常简单。
命令行界面中的编译器
这个库提出了一种脚本,用于解析和应用于具有特定语法的数据。非常有用。此外,我们可以使用管道(因为 Hoa\File\Read
—请参阅Hoa\File
库— 支持 0
作为 stdin
),因此
$ echo '[1, [1, [2, 3], 5], 8]' | hoa compiler:pp Json.pp 0 --visitor dump > #array > > token(number, 1) > > #array > > > token(number, 1) > > > #array > > > > token(number, 2) > > > > token(number, 3) > > > token(number, 5) > > token(number, 8)
您可以使用任何访问者类。
错误
错误得到了很好的展示
$ echo '{"foo" true}' | hoa compiler:pp Json.pp 0 --visitor dump Uncaught exception (Hoa\Compiler\Exception\UnexpectedToken): Hoa\Compiler\Llk\Parser::parse(): (0) Unexpected token "true" (true) at line 1 and column 8: {"foo" true} ↑ in hoa://Library/Compiler/Llk/Parser.php at line 1
采样器
一些算法可以根据语法生成数据。我们将仅提供一个示例,使用基于覆盖的生成算法,该算法将激活语法中的所有分支和标记
$sampler = new Hoa\Compiler\Llk\Sampler\Coverage( // Grammar. Hoa\Compiler\Llk\Llk::load(new Hoa\File\Read('Json.pp')), // Token sampler. new Hoa\Regex\Visitor\Isotropic(new Hoa\Math\Sampler\Random()) ); foreach ($sampler as $i => $data) { echo $i, ' => ', $data, "\n"; } /** * Will output: * 0 => true * 1 => {" )o?bz " : null , " %3W) " : [false, 130 , " 6" ] } * 2 => [{" ny " : true } ] * 3 => {" Ne;[3 " :[ true , true ] , " th: " : true," C[8} " : true } */
研究论文
- 在PHP中使用真实域进行基于语法的测试,在A-MOST 2012(加拿大蒙特利尔)提出(文章,演示,详细信息)。
文档
Hoa\Compiler
的黑客手册包含了有关如何使用此库及其工作原理的详细信息。
要本地生成文档,请执行以下命令
$ composer require --dev hoa/devtools $ vendor/bin/hoa devtools:documentation --open
更多文档可以在项目的网站上找到:hoa-project.net。
获取帮助
获取帮助主要有两种方式
- 在
#hoaproject
IRC频道上, - 在users.hoa-project.net 论坛上。
贡献
您想贡献吗?谢谢!详细的贡献指南解释了您需要知道的一切。
许可
Hoa 采用新的BSD许可(BSD-3-Clause)。请参阅LICENSE
以获取详细信息。