Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry committed Dec 17, 2017
1 parent 32b6f54 commit f39c152
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 14 deletions.
62 changes: 62 additions & 0 deletions Alice.pp
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
29 changes: 29 additions & 0 deletions src/FixtureBuilder/ExpressionLanguage/Lexer/HoaLexer.php
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ interface LexerInterface
*
* @return Token[]
*/
public function lex(string $value): array;
public function lex(string $value);
}
25 changes: 12 additions & 13 deletions src/Loader/NativeLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = '';

/**
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f39c152

Please sign in to comment.