diff --git a/Alice.pp b/Alice.pp new file mode 100644 index 000000000..4b0e0fb35 --- /dev/null +++ b/Alice.pp @@ -0,0 +1,62 @@ +// +// LEXEMES +// +%token true true +%token false false +%token null null +%token escape_token \\ +%token string .+ + + +// +// RULES +// +value: + string() + +string: + ::escape_token:: | | + +// +// +// +// +// +// +//%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: +// | | | string() | object() | array() | number() +// +//string: +// ::quote_:: ::_quote:: +// +//number: +// +// +//#object: +// ::brace_:: pair() ( ::comma:: pair() )* ::_brace:: +// +//#pair: +// string() ::colon:: value() +// +//#array: +// ::bracket_:: value() ( ::comma:: value() )* ::_bracket:: \ No newline at end of file diff --git a/composer.json b/composer.json index fbe6eda22..fbbe8a322 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "require": { "php": "^7.0", "fzaninotto/faker": "^1.6", + "hoa/compiler": "3.17.01.10", "myclabs/deep-copy": "^1.5.2", "symfony/property-access": "^2.7.11 || ^3.0 || ^4.0", "symfony/yaml": "^2.7 || ^3.0 || ^4.0" diff --git a/src/FixtureBuilder/ExpressionLanguage/Lexer/HoaLexer.php b/src/FixtureBuilder/ExpressionLanguage/Lexer/HoaLexer.php new file mode 100644 index 000000000..244fec1ab --- /dev/null +++ b/src/FixtureBuilder/ExpressionLanguage/Lexer/HoaLexer.php @@ -0,0 +1,29 @@ +parser = $parser; + } + + /** + * @inheritdoc + */ + public function lex(string $value) + { + return $this->parser->parse($value); + } +} \ No newline at end of file diff --git a/src/FixtureBuilder/ExpressionLanguage/LexerInterface.php b/src/FixtureBuilder/ExpressionLanguage/LexerInterface.php index 9c55cd2d5..17b652f1d 100644 --- a/src/FixtureBuilder/ExpressionLanguage/LexerInterface.php +++ b/src/FixtureBuilder/ExpressionLanguage/LexerInterface.php @@ -29,5 +29,5 @@ interface LexerInterface * * @return Token[] */ - public function lex(string $value): array; + public function lex(string $value); } diff --git a/src/Loader/NativeLoader.php b/src/Loader/NativeLoader.php index eb2382295..2d06c2999 100644 --- a/src/Loader/NativeLoader.php +++ b/src/Loader/NativeLoader.php @@ -16,6 +16,8 @@ use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\Chainable\SimpleDenormalizer as NelmioSimpleDenormalizer; use Faker\Factory as FakerGeneratorFactory; use Faker\Generator as FakerGenerator; +use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Lexer\HoaLexer; +use Hoa\File\Read; use Nelmio\Alice\DataLoaderInterface; use Nelmio\Alice\Faker\Provider\AliceProvider; use Nelmio\Alice\FileLoaderInterface; @@ -209,6 +211,13 @@ class NativeLoader implements FilesLoaderInterface, FileLoaderInterface, DataLoa /** @protected */ const LOCALE = 'en_US'; + /** + * @var string Path to Alice grammar defined in the PP language. + * + * @see https://hoa-project.net/En/Literature/Hack/Compiler.html#PP_language + */ + protected $ppFilePath = __DIR__.'/../../Alice.pp'; + private $previous = ''; /** @@ -441,19 +450,9 @@ protected function createExpressionLanguageParser(): ExpressionLanguageParserInt protected function createLexer(): LexerInterface { - return new EmptyValueLexer( - new ReferenceEscaperLexer( - new GlobalPatternsLexer( - new FunctionLexer( - new StringThenReferenceLexer( - new SubPatternsLexer( - new ReferenceLexer() - ) - ) - ) - ) - ) - ); + $parser = \Hoa\Compiler\Llk\Llk::load(new Read($this->ppFilePath)); + + return new HoaLexer($parser); } protected function createExpressionLanguageTokenParser(): TokenParserInterface diff --git a/tests/FixtureBuilder/ExpressionLanguage/Lexer/LexerIntegrationTest.php b/tests/FixtureBuilder/ExpressionLanguage/Lexer/LexerIntegrationTest.php index 5c0b87b6c..684c1ba8d 100644 --- a/tests/FixtureBuilder/ExpressionLanguage/Lexer/LexerIntegrationTest.php +++ b/tests/FixtureBuilder/ExpressionLanguage/Lexer/LexerIntegrationTest.php @@ -13,6 +13,7 @@ namespace Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Lexer; +use Hoa\Compiler\Llk\TreeNode; use InvalidArgumentException; use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\LexerInterface; use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Token;