Hoa central
Public Member Functions | Protected Attributes | List of all members
Hoa\Praspel\Visitor\Interpreter Class Reference
Inheritance diagram for Hoa\Praspel\Visitor\Interpreter:

Public Member Functions

 visit (Visitor\Element $element, &$handle=null, $eldnah=null)
 
 getIdentifier ($identifier)
 
 getRoot ()
 
 getClause ()
 
 bindToClass ($classname)
 
 getBindedClass ()
 
- Public Member Functions inherited from Hoa\Visitor\Visit
 visit (Element $element, &$handle=null, $eldnah=null)
 

Protected Attributes

 $_root = null
 
 $_clause = null
 
 $_current = null
 
 $_classnameToBind = null
 

Detailed Description

Class .

Compile Praspel to model.

Definition at line 59 of file Interpreter.php.

Member Function Documentation

Hoa\Praspel\Visitor\Interpreter::bindToClass (   $classname)

Set classname to bind.

Parameters
string$classnameClassname.
Returns
string

Definition at line 654 of file Interpreter.php.

655  {
657  $this->_classnameToBind = $classname;
658 
659  return $old;
660  }
Hoa\Praspel\Visitor\Interpreter::getBindedClass ( )

Get classname to bind.

Returns
string

Definition at line 667 of file Interpreter.php.

668  {
670  }

Here is the caller graph for this function:

Hoa\Praspel\Visitor\Interpreter::getClause ( )

Get current clause.

Returns

Definition at line 643 of file Interpreter.php.

644  {
645  return $this->_clause;
646  }
Hoa\Praspel\Visitor\Interpreter::getIdentifier (   $identifier)

Get identifier object.

Parameters
string$identifierIdentifier.
Returns

Definition at line 615 of file Interpreter.php.

616  {
617  if (!isset($this->_clause[$identifier])) {
618  throw new Praspel\Exception\Interpreter(
619  'The identifier %s does not exist on clause %s.',
620  3,
621  [$identifier, $this->_clause->getName()]
622  );
623  }
624 
625  return $this->_clause[$identifier];
626  }

Here is the caller graph for this function:

Hoa\Praspel\Visitor\Interpreter::getRoot ( )

Get root.

Returns

Definition at line 633 of file Interpreter.php.

634  {
635  return $this->_root;
636  }
Hoa\Praspel\Visitor\Interpreter::visit ( Visitor\Element  $element,
$handle = null,
  $eldnah = null 
)

Visit an element.

Parameters
\Hoa\Visitor\Element$elementElement to visit.
mixed&$handleHandle (reference).
mixed$eldnahHandle (not reference).
Returns
mixed
Exceptions

Definition at line 100 of file Interpreter.php.

104  {
105  $id = $element->getId();
106 
107  switch ($id) {
108  case '#specification':
109  $this->_clause =
110  $this->_current =
111  $this->_root =
112  new Praspel\Model\Specification();
113 
114  if (null !== $classname = $this->getBindedClass()) {
115  $this->_root->bindToClass($classname);
116  }
117 
118  foreach ($element->getChildren() as $child) {
119  $child->accept($this, $handle, $eldnah);
120  }
121 
122  return $this->_root;
123 
124  case '#is':
125  $this->_clause = $this->_root->getClause('is');
126 
127  foreach ($element->getChildren() as $child) {
128  $this->_clause->addProperty(
129  $this->_clause->getPropertyValue(
130  $child->accept($this, $handle, $eldnah)
131  )
132  );
133  }
134 
135  break;
136 
137  case '#requires':
138  case '#ensures':
139  case '#invariant':
140  $this->_clause = $this->_current->getClause(substr($id, 1));
141 
142  foreach ($element->getChildren() as $child) {
143  $child->accept($this, $handle, $eldnah);
144  }
145 
146  break;
147 
148  case '#behavior':
149  $children = $element->getChildren();
150  $child0 = array_shift($children);
151  $identifier = $child0->accept($this, $handle, false);
152  $previous = $this->_current;
153 
154  $this->_clause =
155  $this->_current =
156  $this
157  ->_current
158  ->getClause('behavior')
159  ->get($identifier);
160 
161  foreach ($children as $child) {
162  $child->accept($this, $handle, $eldnah);
163  }
164 
165  $this->_current = $previous;
166 
167  break;
168 
169  case '#default':
170  $children = $element->getChildren();
171  $previous = $this->_current;
172  $this->_clause =
173  $this->_current =
174  $this
175  ->_current
176  ->getClause('default');
177 
178  foreach ($children as $child) {
179  $child->accept($this, $handle, $eldnah);
180  }
181 
182  $this->_current = $previous;
183 
184  break;
185 
186  case '#throwable':
187  $this->_clause = $this->_current->getClause('throwable');
188  $identifier = null;
189 
190  foreach ($element->getChildren() as $child) {
191  switch ($child->getId()) {
192  case '#exception_identifier':
193  $_identifier = $child->getChild(1)->accept(
194  $this,
195  $handle,
196  false
197  );
198  $_instanceof = $child->getChild(0)->accept(
199  $this,
200  $handle,
201  false
202  );
203 
204  $this->_clause[$_identifier] = $_instanceof;
205 
206  if (null === $identifier) {
207  $identifier = $_identifier;
208  } else {
209  $this->_clause[$_identifier]->disjunctionWith(
210  $identifier
211  );
212  }
213 
214  break;
215 
216  case '#exception_with':
217  $old = $this->_clause;
218  $this->_clause = $old->newWith();
219 
220  foreach ($child->getChildren() as $_child) {
221  $_child->accept($this, $handle, $eldnah);
222  }
223 
224  $old[$identifier]->setWith($this->_clause);
225  $this->_clause = $old;
226 
227  break;
228  }
229  }
230 
231  break;
232 
233  case '#description':
234  $this->_clause = $this->_root->getClause('description');
235  $this->_clause[] = $element->getChild(0)->accept(
236  $this,
237  $handle,
238  $eldnah
239  );
240 
241  break;
242 
243  case '#declaration':
244  $left = $element->getChild(0)->accept($this, $handle, false);
245  $right = $element->getChild(1)->accept($this, $handle, $eldnah);
246 
247  $variable = $left;
248 
249  if ($right instanceof Praspel\Model\Variable) {
250  $right = realdom()->variable($right);
251  }
252 
253  $this->_clause[$variable]->in = $right;
254 
255  break;
256 
257  case '#local_declaration':
258  $left = $element->getChild(0)->accept($this, $handle, false);
259  $right = $element->getChild(1)->accept($this, $handle, $eldnah);
260 
261  $variable = $left;
262 
263  if ($right instanceof Praspel\Model\Variable) {
264  $right = realdom()->variable($right);
265  }
266 
267  $this->_clause->let[$variable]->in = $right;
268 
269  break;
270 
271  case '#qualification':
272  $children = $element->getChildren();
273  $variable = $this->_clause[array_shift($children)->accept(
274  $this,
275  $handle,
276  false
277  )];
278 
279  foreach ($children as $child) {
280  $variable->is($child->accept($this, $handle, false));
281  }
282 
283  break;
284 
285  case '#contains':
286  $variable = $element->getChild(0)->accept($this, $handle, false);
287  $value = $element->getChild(1)->accept($this, $handle, false);
288 
289  $this->_clause[$variable]->contains($value);
290 
291  break;
292 
293  case '#domainof':
294  $left = $element->getChild(0)->accept($this, $handle, false);
295  $right = $element->getChild(1)->accept($this, $handle, false);
296 
297  $this->_clause[$left]->domainof($right);
298 
299  break;
300 
301  case '#predicate':
302  $this->_clause->predicate(
303  $element->getChild(0)->accept($this, $handle, $eldnah)
304  );
305 
306  break;
307 
308  case '#disjunction':
309  $disjunction = realdom();
310 
311  foreach ($element->getChildren() as $child) {
312  $value = $child->accept($this, $handle, $eldnah);
313 
314  if ($value instanceof Realdom\Disjunction) {
315  $disjunction[] = $value;
316  } elseif ($value instanceof Praspel\Model\Variable) {
317  $disjunction->variable($value);
318  } else {
319  $disjunction->const($value);
320  }
321  }
322 
323  return $disjunction;
324 
325  case '#realdom':
326  $children = $element->getChildren();
327  $child0 = array_shift($children);
328  $name = $child0->accept($this, $handle, false);
329  $arguments = [];
330 
331  foreach ($children as $child) {
332  $argument = $child->accept($this, $handle, $eldnah);
333 
334  if ($argument instanceof Realdom\Disjunction) {
335  $realdoms = $argument->getRealdoms();
336  $argument = $realdoms[0];
337  }
338 
339  $arguments[] = $argument;
340  }
341 
342  return realdom()->_call($name, $arguments);
343 
344  case '#concatenation':
345  $string = null;
346 
347  foreach ($element->getChildren() as $child) {
348  $string .= $child->accept($this, $handle, $eldnah);
349  }
350 
351  return $string;
352 
353  case '#array':
354  $array = [];
355 
356  foreach ($element->getChildren() as $child) {
357  if ('#pair' === $child->getId()) {
358  $key = $child->getChild(0)->accept($this, $handle, $eldnah);
359  $value = $child->getChild(1)->accept($this, $handle, $eldnah);
360  $array[] = [$key, $value];
361 
362  continue;
363  }
364 
365  $key = realdom()->natural(0, 1);
366  $value = $child->accept($this, $handle, $eldnah);
367  $array[] = [$key, $value];
368  }
369 
370  return $array;
371 
372  case '#range':
373  $left = $element->getChild(0)->accept($this, $handle, $eldnah);
374  $right = $element->getChild(1)->accept($this, $handle, $eldnah);
375 
376  if (is_float($left) || is_float($right)) {
377  return realdom()->boundfloat(
378  floatval($left),
379  floatval($right)
380  );
381  }
382 
383  return realdom()->boundinteger(intval($left), intval($right));
384 
385  case '#left_range':
386  $left = $element->getChild(0)->accept($this, $handle, $eldnah);
387 
388  if (is_float($left)) {
389  return realdom()->boundfloat($left);
390  }
391 
392  return realdom()->boundinteger($left);
393 
394  case '#right_range':
395  $right = $element->getChild(0)->accept($this, $handle, $eldnah);
396 
397  if (is_float($right)) {
398  return realdom()->boundfloat(null, $right);
399  }
400 
401  return realdom()->boundinteger(null, $right);
402 
403  case '#arrayaccessbykey':
404  $variable = $element->getChild(0)->accept($this, $handle, $eldnah);
405  $key = $element->getChild(1)->accept($this, $handle, $eldnah);
406 
407  $this->_clause[$variable]->key($key);
408 
409  return $variable;
410 
411  case '#dynamic_resolution':
412  $value = null;
413 
414  foreach ($element->getChildren() as $child) {
415  if (null !== $value) {
416  $value .= '->';
417  }
418 
419  $value .= $child->accept($this, $handle, false);
420  }
421 
422  if (false !== $eldnah) {
423  return $this->_clause->getVariable($value, true);
424  }
425 
426  return $value;
427 
428  case '#self_identifier':
429  case '#static_identifier':
430  case '#parent_identifier':
431  $identifier = substr($id, 1, strpos($id, '_', 1) - 1);
432 
433  foreach ($element->getChildren() as $child) {
434  $identifier .= '::' . $child->accept($this, $handle, $eldnah);
435  }
436 
437  return $identifier;
438 
439  case '#old':
440  $value =
441  '\old(' .
442  $element->getChild(0)->accept($this, $handle, false) .
443  ')';
444 
445  if (false !== $eldnah) {
446  return $this->_clause->getVariable($value);
447  }
448 
449  return $value;
450 
451  case '#result':
452  return '\result';
453 
454  case '#classname':
455  $classname = [];
456 
457  foreach ($element->getChildren() as $child) {
458  $classname[] = $child->accept($this, $handle, $eldnah);
459  }
460 
461  return implode('\\', $classname);
462 
463  case '#nowdoc':
464  case '#heredoc':
465  return $element->getChild(1)->accept($this, $handle, $eldnah);
466 
467  case '#regex':
468  $regex = $element->getChild(0)->accept($this, $handle, $eldnah);
469 
470  if (true === $element->childExists(1)) {
471  $length = $element->getChild(1)->accept($this, $handle, $eldnah);
472  }
473 
474  return realdom()->regex($regex);
475 
476  case '#class':
477  $classname = $element->getChild(0)->accept($this, $handle, false);
478 
479  return realdom()->class($classname);
480 
481  case 'token':
482  $tId = $element->getValueToken();
483  $value = $element->getValueValue();
484 
485  switch ($tId) {
486  case 'identifier':
487  if (false !== $eldnah) {
488  return $this->getIdentifier($value);
489  }
490 
491  return $value;
492 
493  case 'this':
494  if (false !== $eldnah) {
495  return $this->_root->getImplicitVariable($value);
496  }
497 
498  return $value;
499 
500  case 'content':
501  case 'pure':
502  case 'result':
503  return $value;
504 
505  case 'default':
506  return …;
507 
508  case 'null':
509  return null;
510 
511  case 'true':
512  return true;
513 
514  case 'false':
515  return false;
516 
517  case 'binary':
518  $int = intval(substr($value, strpos($value, 'b') + 1), 2);
519 
520  if ('-' === $value[0]) {
521  return -$int;
522  }
523 
524  return $int;
525 
526  case 'octal':
527  $int = intval(substr($value, strpos($value, '0') + 1), 8);
528 
529  if ('-' === $value[0]) {
530  return -$int;
531  }
532 
533  return $int;
534 
535  case 'hexa':
536  $value = strtolower($value);
537  $int = intval(substr($value, strpos($value, 'x') + 1), 16);
538 
539  if ('-' === $value[0]) {
540  return -$int;
541  }
542 
543  return $int;
544 
545  case 'decimal':
546  if (true === ctype_digit(ltrim($value, '+-'))) {
547  return intval($value);
548  }
549 
550  return floatval($value);
551 
552  case 'escaped':
553  switch ($value[1]) {
554  case 'n':
555  return "\n";
556 
557  case 'r':
558  return "\r";
559 
560  case 't':
561  return "\t";
562 
563  case 'v':
564  return "\v";
565 
566  case 'e':
567  return "\033";
568 
569  case 'f':
570  return "\f";
571 
572  case 'b':
573  return "\033[D";
574 
575  case 'x':
576  return Ustring::fromCode(hexdec($value));
577 
578  case '\\':
579  return $value[1];
580 
581  default:
582  return Ustring::fromCode(octdec($value));
583  }
584 
585  case 'accepted':
586  case 'string':
587  case 'regex';
588  return $value;
589 
590  default:
591  throw new Praspel\Exception\Interpreter(
592  'Token %s is not yet implemented.',
593  1,
594  $tId
595  );
596  }
597 
598  break;
599 
600  default:
601  throw new Praspel\Exception\Interpreter(
602  'Element %s is unknown.',
603  2,
604  $id
605  );
606  }
607  }
static fromCode($code)
Definition: Ustring.php:928

Here is the call graph for this function:

Member Data Documentation

Hoa\Praspel\Visitor\Interpreter::$_classnameToBind = null
protected

Definition at line 87 of file Interpreter.php.

Hoa\Praspel\Visitor\Interpreter::$_clause = null
protected

Definition at line 73 of file Interpreter.php.

Hoa\Praspel\Visitor\Interpreter::$_current = null
protected

Definition at line 80 of file Interpreter.php.

Hoa\Praspel\Visitor\Interpreter::$_root = null
protected

Definition at line 66 of file Interpreter.php.


The documentation for this class was generated from the following file: