Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Apr 5, 2017
1 parent f4d75a8 commit a36d01c
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 20 deletions.
62 changes: 62 additions & 0 deletions 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:: <string> | <escape_token> | <string>

//
//
//
//
//
//
//%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::
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -22,6 +22,7 @@
"require": {
"php": "7.0 - 7.1",
"fzaninotto/faker": "^1.6",
"hoa/compiler": "3.17.01.10",
"myclabs/deep-copy": "^1.5.2",
"symfony/property-access": "^2.7.11||^3.0",
"symfony/yaml": "^2.0||^3.0"
Expand Down
29 changes: 29 additions & 0 deletions src/FixtureBuilder/ExpressionLanguage/Lexer/HoaLexer.php
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

namespace Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Lexer;

use Hoa\Compiler\Llk\Parser as HoaParser;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\LexerInterface;

final class HoaLexer implements LexerInterface
{
/**
* @var HoaParser
*/
private $parser;

public function __construct(HoaParser $parser)
{

$this->parser = $parser;
}

/**
* @inheritdoc
*/
public function lex(string $value)
{
return $this->parser->parse($value);
}
}
2 changes: 1 addition & 1 deletion src/FixtureBuilder/ExpressionLanguage/LexerInterface.php
Expand Up @@ -29,5 +29,5 @@ interface LexerInterface
*
* @return Token[]
*/
public function lex(string $value): array;
public function lex(string $value);
}
23 changes: 12 additions & 11 deletions src/Loader/NativeLoader.php
Expand Up @@ -15,6 +15,8 @@

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\FixtureBuilder\Denormalizer\Fixture\Chainable\CollectionDenormalizerWithTemporaryFixture;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\Chainable\NullListNameDenormalizer;
Expand Down Expand Up @@ -190,6 +192,13 @@ class NativeLoader implements FileLoaderInterface, DataLoaderInterface
{
use IsAServiceTrait;

/**
* @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 = '';

/**
Expand Down Expand Up @@ -389,17 +398,9 @@ protected function createExpressionLanguageParser(): ExpressionLanguageParserInt

protected function createLexer(): LexerInterface
{
return new EmptyValueLexer(
new ReferenceEscaperLexer(
new GlobalPatternsLexer(
new FunctionLexer(
new SubPatternsLexer(
new ReferenceLexer()
)
)
)
)
);
$parser = \Hoa\Compiler\Llk\Llk::load(new Read($this->ppFilePath));

return new HoaLexer($parser);
}

protected function createExpressionLanguageTokenParser(): TokenParserInterface
Expand Down
Expand Up @@ -13,6 +13,8 @@

namespace Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Lexer;

use Hoa\Compiler\Llk\TreeNode;
use InvalidArgumentException;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\LexerInterface;
use Nelmio\Alice\Throwable\Exception\FixtureBuilder\ExpressionLanguage\LexException;
use Nelmio\Alice\FixtureBuilder\ExpressionLanguage\Token;
Expand Down Expand Up @@ -53,7 +55,7 @@ public function testTestCanLexValues(string $value, $expected)
)
);
}
} catch (\InvalidArgumentException $exception) {
} catch (InvalidArgumentException $exception) {
if (null === $expected) {
return;
}
Expand All @@ -68,7 +70,6 @@ public function testTestCanLexValues(string $value, $expected)
}

$this->assertEquals($expected, $actual, var_export($actual, true));
$this->assertSameSize($expected, $actual);
}

/**
Expand All @@ -77,29 +78,53 @@ public function testTestCanLexValues(string $value, $expected)
public function provideValues()
{
// simple values
yield 'empty string' => [
'',
[
new Token('', new TokenType(TokenType::STRING_TYPE)),
],
];
// yield 'empty string' => [
// '',
// [
// new Token('', new TokenType(TokenType::STRING_TYPE)),
// ],
// ];

yield 'regular string value' => [
'dummy',
new TreeNode(
'token',
[
'token' => 'string',
'value' => 'dummy',
'namespace' => 'default',
]
),
[
new Token('dummy', new TokenType(TokenType::STRING_TYPE)),
],
];

yield 'string value with quotes' => [
'\'dummy\'',
new TreeNode(
'token',
[
'token' => 'string',
'value' => '\'dummy\'',
'namespace' => 'default',
]
),
[
new Token('\'dummy\'', new TokenType(TokenType::STRING_TYPE)),
],
];

yield 'string value with double quotes' => [
'"dummy"',
new TreeNode(
'token',
[
'token' => 'string',
'value' => '"dummy"',
'namespace' => 'default',
]
),
[
new Token('"dummy"', new TokenType(TokenType::STRING_TYPE)),
],
Expand All @@ -108,6 +133,14 @@ public function provideValues()
// Escaped character
yield '[Escape character] nominal (1)' => [
'\\',
new TreeNode(
'token',
[
'token' => 'string',
'value' => '\\',
'namespace' => 'default',
]
),
null,
];
yield '[Escape character] nominal (2)' => [
Expand Down

0 comments on commit a36d01c

Please sign in to comment.