/** * Construct a realistic domain. * * @return void */ protected function construct() { if (null === self::$_compiler) { self::$_compiler = Compiler\Llk::load(new File\Read('hoa://Library/Regex/Grammar.pp')); } if (!isset($this['regex'])) { $this['regex'] = new Conststring(''); } $this->_ast = self::$_compiler->parse(mb_substr($regex = $this['regex']->getConstantValue(), 1, mb_strrpos($regex, mb_substr($regex, 0, 1), 1) - 1)); return; }
/** * Predicate whether the sampled value belongs to the realistic domains. * * @param mixed $q Sampled value. * @return boolean */ protected function _predicate($q) { // How to handle size (because the unit of size is token, not // character)? try { $this->_compiler->parse($q, null, false); } catch (\Exception $e) { return false; } return true; }
/** * Get AST of the token representation. * * @return \Hoa\Compiler\Llk\TreeNode */ public function getAST() { if (null === static::$_regexCompiler) { $stream = new File\Read('hoa://Library/Regex/Grammar.pp'); $stream->rewind(); static::$_regexCompiler = Compiler\Llk::load($stream); } if (null === $this->_ast) { $this->_ast = static::$_regexCompiler->parse($this->getRepresentation()); } return $this->_ast; }
public function case_unification_palindrome() { $_grammar = <<<GRAMMAR %token t \\w root: ::t[0]:: root()? ::t[0]:: GRAMMAR; $this->given($grammar = new File\ReadWrite('hoa://Test/Vfs/Palindrome.pp?type=file'), $grammar->writeAll($_grammar), $compiler = LUT\Llk::load($grammar))->when($result = $compiler->parse('aa', null, false))->then->boolean($result)->isTrue()->when($result = $compiler->parse('abba', null, false))->then->boolean($result)->isTrue()->when($result = $compiler->parse('abccba', null, false))->then->boolean($result)->isTrue()->when($result = $compiler->parse('abcddcba', null, false))->then->boolean($result)->isTrue()->exception(function () use(&$compiler) { $compiler->parse('abcdcba', null, false); })->isInstanceOf('Hoa\\Compiler\\Exception\\UnexpectedToken'); }
protected function compute($expression) { // Load the compiler $compiler = \Hoa\Compiler\Llk::load(new \Hoa\File\Read('hoa://Library/Math/Arithmetic.pp')); // Load the visitor, aka the "evaluator" $visitor = new \Hoa\Math\Visitor\Arithmetic(); // Parse the expression $ast = $compiler->parse($expression); // Evaluate $result = $visitor->visit($ast); return $result; }
/** * Short interpreter. * * @param string $praspel Praspel. * @param string $bindToClass Classname to bind. * @return \Hoa\Praspel\Model\Clause */ public static function interpret($praspel, $bindToClass = null) { static $_compiler = null; static $_interpreter = null; if (null === $_compiler) { $_compiler = Compiler\Llk::load(new File\Read('hoa://Library/Praspel/Grammar.pp')); } if (null === $_interpreter) { $_interpreter = new Visitor\Interpreter(); } $ast = $_compiler->parse($praspel); if (null !== $bindToClass) { $_interpreter->bindToClass($bindToClass); } return $_interpreter->visit($ast); }
/** * Get compiler. * * @return \Hoa\Compiler\Llk\Parser */ public static function getCompiler() { if (null === static::$_compiler) { static::$_compiler = Compiler\Llk::load(new File\Read('hoa://Library/Ruler/Grammar.pp')); } return static::$_compiler; }
/** * The entry method. * * @return int */ public function main() { while (false !== ($c = $this->getOption($v))) { switch ($c) { case 'h': case '?': return $this->usage(); case '__ambiguous': $this->resolveOptionAmbiguity($v); break; } } $this->parser->listInputs($expression); $compiler = Compiler\Llk::load(new File\Read('hoa://Library/Math/Arithmetic.pp')); $visitor = new Math\Visitor\Arithmetic(); $dump = new Compiler\Visitor\Dump(); if (null !== $expression) { $ast = $compiler->parse($expression); echo $expression . ' = ' . $visitor->visit($ast), "\n"; return; } $readline = new Console\Readline(); $readline->setAutocompleter(new Console\Readline\Autocompleter\Word(array_merge(array_keys($visitor->getConstants()->getArrayCopy()), array_keys($visitor->getFunctions()->getArrayCopy())))); $handle = null; $expression = 'h'; do { switch ($expression) { case 'h': case 'help': echo 'Usage:', "\n", ' h[elp] to print this help;', "\n", ' c[onstants] to print available constants;', "\n", ' f[unctions] to print available functions;', "\n", ' e[xpression] to print the current expression;', "\n", ' d[ump] to dump the tree of the expression;', "\n", ' q[uit] to quit.', "\n"; break; case 'c': case 'constants': echo implode(', ', array_keys($visitor->getConstants()->getArrayCopy())), "\n"; break; case 'f': case 'functions': echo implode(', ', array_keys($visitor->getFunctions()->getArrayCopy())), "\n"; break; case 'e': case 'expression': echo $handle, "\n"; break; case 'd': case 'dump': if (null === $handle) { echo 'Type a valid expression before (“> 39 + 3”).', "\n"; } else { echo $dump->visit($compiler->parse($handle)), "\n"; } break; case 'q': case 'quit': break 2; default: if (null === $expression) { break; } try { echo $visitor->visit($compiler->parse($expression)), "\n"; } catch (Compiler\Exception $e) { echo $e->getMessage(), "\n"; break; } $handle = $expression; break; } } while (false !== ($expression = $readline->readLine('> '))); return; }
public function case_whole_process() { $_grammar = <<<GRAMMAR %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:: GRAMMAR; $_result = <<<GRAMMAR > #object > > #pair > > > token(string:string, foo) > > > token(true, true) > > #pair > > > token(string:string, bar) > > > #array > > > > token(null, null) > > > > token(number, 42) GRAMMAR; $this->given($grammar = new File\ReadWrite('hoa://Test/Vfs/Json.pp?type=file'), $grammar->writeAll($_grammar), $compiler = LUT\Llk::load($grammar))->when($ast = $compiler->parse('{"foo": true, "bar": [null, 42]}'))->then->object($ast)->given($dump = new LUT\Visitor\Dump())->when($result = $dump->visit($ast))->then->string($result)->isEqualTo($_result); }
protected function getJSONCompiler() { return LUT\Llk::load(new File\Read('hoa://Library/Json/Grammar.pp')); }
public function __construct() { $this->compiler = Compiler\Llk::load(new File\Read(__DIR__ . '/../Grammar.pp')); }
/** * The entry method. * * @return int */ public function main() { $visitor = null; $tokenSequence = false; $trace = false; while (false !== ($c = $this->getOption($v))) { switch ($c) { case 'v': switch (strtolower($v)) { case 'dump': $visitor = 'Hoa\\Compiler\\Visitor\\Dump'; break; default: return $this->usage(); } break; case 'c': $visitor = str_replace('.', '\\', $v); break; case 's': $tokenSequence = true; break; case 't': $trace = true; break; case '__ambiguous': $this->resolveOptionAmbiguity($v); break; case 'h': case '?': default: return $this->usage(); } } $this->parser->listInputs($grammar, $language); if (empty($grammar) || empty($language) && '0' !== $language) { return $this->usage(); } $compiler = Compiler\Llk::load(new File\Read($grammar)); $data = new File\Read($language); try { $ast = $compiler->parse($data->readAll()); } catch (Compiler\Exception $e) { if (true === $tokenSequence) { $this->printTokenSequence($compiler); echo "\n\n"; } throw $e; return 1; } if (true === $tokenSequence) { $this->printTokenSequence($compiler); echo "\n\n"; } if (true === $trace) { $this->printTrace($compiler); echo "\n\n"; } if (null !== $visitor) { $visitor = dnew($visitor); echo $visitor->visit($ast); } return; }
/** * The entry method. * * @return int */ public function main() { while (false !== ($c = $this->getOption($v))) { switch ($c) { case 'h': case '?': return $this->usage(); case '__ambiguous': $this->resolveOptionAmbiguity($v); break; } } Realdom::setDefaultSampler(new Math\Sampler\Random()); $compiler = Compiler\Llk::load(new File\Read('hoa://Library/Praspel/Grammar.pp')); $interpreter = new Praspel\Visitor\Interpreter(); $dump = new Praspel\Visitor\Compiler(); $interpreter->visit($compiler->parse('@requires;')); $words = []; from('Hoathis or Hoa')->foreachImport('Realdom.*', function ($classname) use(&$words) { $class = new \ReflectionClass($classname); if ($class->isSubclassOf('\\Hoa\\Realdom')) { $words[] = $classname::NAME; } return; }); $readline = new Console\Readline(); $readline->setAutocompleter(new Console\Readline\Autocompleter\Word($words)); $expression = '.h'; do { try { if ('.' === $expression[0]) { @(list($expression, $tail) = explode(' ', $expression)); } switch ($expression) { case '.h': case '.help': echo 'Usage:', "\n", ' .h[elp] to print this help;', "\n", ' .c[lear] to clear the screen;', "\n", ' .v[ariables] to print all variables;', "\n", ' .s[ample] to sample a value of a variable;', "\n", ' .u[nset] to unset a variable;', "\n", ' .d[ump] to dump the tree of the expression;', "\n", ' .q[uit] to quit.', "\n"; break; case '.c': case '.clear': Console\Cursor::clear('↕'); break; case '.v': case '.variables': foreach ($interpreter->getClause() as $variable) { echo $variable->getName(), ': ', $variable->getHeld()->toPraspel(), "\n"; } break; case '.s': case '.sample': if (null === $tail) { echo 'You must precise a variable name.', "\n"; break; } $_clause = $interpreter->getClause(); if (!isset($_clause[$tail])) { echo 'Variable ', $tail, ' does not exist.', "\n"; break; } $_variable = $_clause[$tail]; var_export($_variable->sample()); echo "\n"; $_variable->reset(); break; case '.u': case '.unset': if (null === $tail) { echo 'You must precise a variable name.', "\n"; break; } $_clause = $interpreter->getClause(); unset($_clause[$tail]); break; case '.d': case '.dump': echo $dump->visit($interpreter->getRoot()); break; case '.q': case '.quit': break 2; default: if (null === $expression) { break; } $interpreter->visit($compiler->parse($expression, 'expression')); break; } } catch (\Exception $e) { echo $e->getMessage(), "\n"; } echo "\n"; } while (false !== ($expression = $readline->readLine('> '))); return; }