sanmai / hoa-compiler
Hoa\Compiler 库。
Requires
- php: >=7.0
- hoa/iterator: ^2.17
- hoa/visitor: ^2.17
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- hoa/file: *
- hoa/regex: ^1
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: >=6.5.14
- sanmai/linter: ^0.1 || ^0.2
Suggests
- hoa/file: For getAST to work
- hoa/regex: For getAST to work
- sanmai/hoa-protocol: If you have trouble with a conflicting resolve()
Replaces
- hoa/compiler: 3.17.08.08
This package is auto-updated.
Last update: 2022-10-01 12:27:34 UTC
README
安装方式
composer require sanmai/hoa-compiler
本库版本在输入输出方面与原始包向后兼容。例如,您可以使用这个版本与jms/serializer
一起使用,以避免原始包的一些依赖项与PHP 7.4存在的问题已知问题。
BC 破坏性更改包括
Hoa\Exception\Exception
子类不再抛出,请切换到Hoa\Compiler\Exception
- 此包本身不依赖于开发时间依赖项。如果您需要调用
getAST()
,则需要手动安装hoa/regex
和hoa/file
。
Hoa\Compiler
这个库允许操作 LL(1) 和 LL(k) 编译器的编译器。为后者提供了一个专门的语法描述语言:PP 语言。
了解更多.
测试
运行所有测试套件
$ 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) */
很简单。
CLI中的编译器
此库提供了一种脚本来解析并应用特定语法的数据上的访问者。非常有用。此外,我们可以使用管道(因为 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 } */
研究论文
- 使用实际域的语法测试(Grammar-Based Testing using Realistic Domains in PHP),在 A-MOST 2012 (加拿大蒙特利尔)上提出(文章,演讲,详细信息)。
许可
Hoa 采用新BSD许可(BSD-3-Clause)。请参阅LICENSE
以获取详细信息。