Hoa central
Regex.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 as HoaRegex;
43 
52 class Regex extends Realdom
53 {
59  const NAME = 'regex';
60 
66  protected $_arguments = [
67  'Conststring regex'
68  ];
69 
75  protected static $_compiler = null;
76 
82  protected static $_visitor = null;
83 
89  protected $_ast = null;
90 
91 
92 
98  protected function construct()
99  {
100  if (null === self::$_compiler) {
101  self::$_compiler = Compiler\Llk::load(
102  new File\Read('hoa://Library/Regex/Grammar.pp')
103  );
104  }
105 
106  if (!isset($this['regex'])) {
107  $this['regex'] = new Conststring('');
108  }
109 
110  $this->_ast = self::$_compiler->parse(
111  mb_substr(
112  $regex = $this['regex']->getConstantValue(),
113  1,
114  mb_strrpos($regex, mb_substr($regex, 0, 1), 1) - 1
115  )
116  );
117 
118  return;
119  }
120 
127  protected function _predicate($q)
128  {
129  return 0 !== preg_match($this['regex']->getConstantValue(), $q);
130  }
131 
138  protected function _sample(Math\Sampler $sampler)
139  {
140  if (null === self::$_visitor) {
141  self::$_visitor = new HoaRegex\Visitor\Isotropic($sampler);
142  }
143 
144  return self::$_visitor->visit($this->_ast);
145  }
146 }
static $_compiler
Definition: Regex.php:75
static $_visitor
Definition: Regex.php:82
_sample(Math\Sampler $sampler)
Definition: Regex.php:138