Hoa central
Grammar.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Realdom;
38 
39 use Hoa\Compiler;
40 use Hoa\File;
41 use Hoa\Math;
42 use Hoa\Regex;
43 
52 class Grammar extends RealdomString
53 {
59  const NAME = 'grammar';
60 
66  protected $_arguments = [
67  'Conststring grammar'
68  ];
69 
75  protected $_compiler = null;
76 
82  protected $_compilerSampler = null;
83 
84 
85 
91  protected function construct()
92  {
93  $this->_compiler = Compiler\Llk::load(
94  new File\Read($this['grammar']->getConstantValue())
95  );
96 
97  return;
98  }
99 
106  protected function _predicate($q)
107  {
108  // How to handle size (because the unit of size is token, not
109  // character)?
110 
111  try {
112  $this->_compiler->parse($q, null, false);
113  } catch (\Exception $e) {
114  return false;
115  }
116 
117  return true;
118  }
119 
126  protected function _sample(Math\Sampler $sampler)
127  {
128  static $_values = [];
129 
130  if (null === $this->_compilerSampler) {
131  $this->_compilerSampler = new Compiler\Llk\Sampler\Coverage(
132  $this->_compiler,
133  new Regex\Visitor\Isotropic($sampler)
134  );
135  }
136 
137  if (empty($_values)) {
138  $_values = iterator_to_array($this->_compilerSampler);
139  shuffle($_values);
140  }
141 
142  return array_shift($_values);
143  }
144 }
_sample(Math\Sampler $sampler)
Definition: Grammar.php:126