public function __invoke($_size) { $lexer = new Lexer($this->expression); $gen = new SimpleRandom(rand()); $result = null; $parser = new Parser($lexer, new Scope(), new Scope()); $parser->parse()->getResult()->generate($result, $gen); return $result; }
public function __invoke($_size, $rand) { $lexer = new Lexer($this->expression); $gen = new SimpleRandom($rand()); $result = null; $parser = new Parser($lexer, new Scope(), new Scope()); $parser->parse()->getResult()->generate($result, $gen); return GeneratedValue::fromJustValue($result, 'regex'); }
public function validate() { parent::validate(); try { $lexer = new Lexer($this->getOption(self::FORMAT)); $parser = new Parser($lexer, new Scope(), new Scope()); $this->result = $parser->parse()->getResult(); } catch (ReverseRegexException $e) { throw new EngineException($e->getMessage()); } return true; }
<?php use ReverseRegex\Lexer; use ReverseRegex\Parser; use ReverseRegex\Generator\Scope; use ReverseRegex\Random\MersenneRandom; # require composer require '../vendor/autoload.php'; # parse the regex $lexer = new Lexer("[a-z]{10}"); $parser = new Parser($lexer, new Scope(), new Scope()); $generator = $parser->parse()->getResult(); # run the generator $random = new MersenneRandom(777); for ($i = 50; $i > 0; $i--) { $result = ''; $generator->generate($result, $random); echo $result; echo PHP_EOL; }
public function seed() { $data = json_decode($this->generateLexer()); $lexer = new Lexer($data->{'lexer'}); srand(); $gen = new SimpleRandom(rand()); $result = ''; $parser = new Parser($lexer, new Scope(), new Scope()); // echo "<table border=1>"; // for ($i=0; $i < 100; $i++) { // echo "<tr>"; // for ($j=0; $j < 100; $j++) { // $result = ""; // $parser->parse()->getResult()->generate($result,$gen); // echo "<td>" . $result . "</td>"; // } // echo "</tr>"; // } // echo "</table>"; $parser->parse()->getResult()->generate($result, $gen); $data = array('equation' => $result, 'time_started' => Carbon::now(), 'difficulty' => $data->{'difficulty'}); $model = Auth::user()->equations()->create($data); return json_encode($model); }
/** * Will parse the regex into generator * * @access public * @return */ public function parse($sub = false) { try { while ($this->lexer->moveNext()) { $result = null; $scope = null; $parser = null; switch (true) { case $this->lexer->isNextToken(Lexer::T_GROUP_OPEN): # is the group character the first token? is the regex wrapped in brackets. //if($this->lexer->token === null) { // continue; //} # note this is a new group create new parser instance. $parser = new Parser($this->lexer, new Scope(), new Scope()); $this->left = $parser->parse(true)->getResult(); $this->head->attach($this->left); break; case $this->lexer->isNextToken(Lexer::T_GROUP_CLOSE): # group is finished don't want to contine this loop break = 2 break 2; break; case $this->lexer->isNextTokenAny(array(Lexer::T_LITERAL_CHAR, Lexer::T_LITERAL_NUMERIC)): # test for literal characters (abcd) $this->left = new LiteralScope(); $this->left->addLiteral($this->lexer->lookahead['value']); $this->head->attach($this->left); break; case $this->lexer->isNextToken(Lexer::T_SET_OPEN): # character classes [a-z] $this->left = new LiteralScope(); self::createSubParser('character')->parse($this->left, $this->head, $this->lexer); $this->head->attach($this->left); break; case $this->lexer->isNextTokenAny(array(Lexer::T_DOT, Lexer::T_SHORT_D, Lexer::T_SHORT_NOT_D, Lexer::T_SHORT_W, Lexer::T_SHORT_NOT_W, Lexer::T_SHORT_S, Lexer::T_SHORT_NOT_S)): # match short (. \d \D \w \W \s \S) $this->left = new LiteralScope(); self::createSubParser('short')->parse($this->left, $this->head, $this->lexer); $this->head->attach($this->left); break; case $this->lexer->isNextTokenAny(array(Lexer::T_SHORT_P, Lexer::T_SHORT_UNICODE_X, Lexer::T_SHORT_X)): # match short (\p{L} \x \X ) $this->left = new LiteralScope(); self::createSubParser('unicode')->parse($this->left, $this->head, $this->lexer); $this->head->attach($this->left); break; case $this->lexer->isNextTokenAny(array(Lexer::T_QUANTIFIER_OPEN, Lexer::T_QUANTIFIER_PLUS, Lexer::T_QUANTIFIER_QUESTION, Lexer::T_QUANTIFIER_STAR, Lexer::T_QUANTIFIER_OPEN)): # match quantifiers self::createSubParser('quantifer')->parse($this->left, $this->head, $this->lexer); break; case $this->lexer->isNextToken(Lexer::T_CHOICE_BAR): # match alternations $this->left = $this->head; $this->head = new Scope(); $this->result->useAlternatingStrategy(); $this->result->attach($this->head); break; default: # ignore character } } } catch (ParserException $e) { $pos = $this->lexer->lookahead['position']; $compressed = $this->compress(); throw new ParserException(sprintf('Error found STARTING at position %s after `%s` with msg %s ', $pos, $compressed, $e->getMessage())); } return $this; }
/** * @expectedException \ReverseRegex\Exception * @expectedExceptionMessage Error found STARTING at position 16 after `\(0[23478]\)[9-4]` with msg Character class range 9 - 4 is out of order */ public function testParserLexerErrorB() { $lexer = new Lexer('\\(0[23478]\\)[9-4]{4}-[0-9]{4}'); $container = new Scope(); $head = new Scope(); $parser = new Parser($lexer, $container, $head); $generator = $parser->parse()->getResult(); $result = ''; $random = new MersenneRandom(100); $generator->generate($result, $random); }
public function generateEquation() { // $lexer = new Lexer('[2-9]{1}[x]=[1-9]{1}([\+\-]{1}[2-9]{1}[x]{0,1}){0,1}'); $data = json_decode($this->generateLexer()); $lexer = new Lexer($data->{'lexer'}); // $lexer = new Lexer('[2-9]{1}[x]=[1-9]{1,2}'); $rand = rand(1, 99999); $gen = new SimpleRandom($rand); $result = ''; $parser = new Parser($lexer, new Scope(), new Scope()); $parser->parse()->getResult()->generate($result, $gen); $eq = array('equation' => $result, 'difficulty' => $data->{'difficulty'}); return $eq; }
/** * @param string $pattern * * @return string * @throws \ReverseRegex\Exception */ private static function getRandomString($pattern) { $parser = new Parser(new Lexer($pattern), new Scope(), new Scope()); $result = ''; $parser->parse()->getResult()->generate($result, self::getGenerator()); return $result; }