diff options
author | Ivan Enderlin <ivan.enderlin@hoa-project.net> | 2015-05-28 12:10:30 +0200 |
---|---|---|
committer | Ivan Enderlin <ivan.enderlin@hoa-project.net> | 2015-05-28 12:10:30 +0200 |
commit | 0fce3078006928700d7e65660d49f693dc4293d2 (patch) | |
tree | 03636cd45b0422fc7558ef9d8c380c30e13b9069 /Visitor | |
parent | f7801604c0631304547eee00b68ce3174d40ccdc (diff) | |
download | Praspel-0fce3078006928700d7e65660d49f693dc4293d2.zip Praspel-0fce3078006928700d7e65660d49f693dc4293d2.tar.gz Praspel-0fce3078006928700d7e65660d49f693dc4293d2.tar.bz2 |
Move to PSR-1-2 & drop PHP5.3 & `from`/`import`.
Diffstat (limited to 'Visitor')
-rw-r--r-- | Visitor/Compiler.php | 350 | ||||
-rw-r--r-- | Visitor/Interpreter.php | 350 | ||||
-rw-r--r-- | Visitor/Praspel.php | 246 |
3 files changed, 467 insertions, 479 deletions
diff --git a/Visitor/Compiler.php b/Visitor/Compiler.php index eb033ce..ebf4192 100644 --- a/Visitor/Compiler.php +++ b/Visitor/Compiler.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -34,246 +34,240 @@ * POSSIBILITY OF SUCH DAMAGE. */ -namespace { +namespace Hoa\Praspel\Visitor; -from('Hoa') - -/** - * \Hoa\Praspel\Exception\Compiler - */ --> import('Praspel.Exception.Compiler') - -/** - * \Hoa\Visitor\Visit - */ --> import('Visitor.Visit'); - -} - -namespace Hoa\Praspel\Visitor { +use Hoa\Praspel; +use Hoa\Realdom; +use Hoa\Visitor; /** * Class \Hoa\Praspel\Visitor\Compiler. * * Compile the model to PHP code. * - * @author Ivan Enderlin <ivan.enderlin@hoa-project.net> - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class Compiler implements \Hoa\Visitor\Visit { - +class Compiler implements Visitor\Visit +{ /** * Visit an element. * - * @access public * @param \Hoa\Visitor\Element $element Element to visit. * @param mixed &$handle Handle (reference). * @param mixed $eldnah Handle (not reference). * @return mixed */ - public function visit ( \Hoa\Visitor\Element $element, - &$handle = null, $eldnah = null ) { - + public function visit( + Visitor\Element $element, + &$handle = null, + $eldnah = null + ) { $out = null; // Hoa\Praspel. - if($element instanceof \Hoa\Praspel\Model\Specification) { - + if ($element instanceof Praspel\Model\Specification) { $variable = '$' . $element->getId(); $out = $variable . ' = new \Hoa\Praspel\Model\Specification();' . "\n"; - foreach($element::getAllowedClauses() as $clause) - if(true === $element->clauseExists($clause)) + foreach ($element::getAllowedClauses() as $clause) { + if (true === $element->clauseExists($clause)) { $out .= $element->getClause($clause)->accept( $this, $handle, $eldnah ); - } - elseif($element instanceof \Hoa\Praspel\Model\Is) { - + } + } + } elseif ($element instanceof Praspel\Model\Is) { $variable = '$' . $element->getParent()->getId(); - $out = "\n" . - $variable . '->getClause(\'is\')->setProperty(' . - $element->getProperty() . - ');' . "\n"; - } - elseif($element instanceof \Hoa\Praspel\Model\Declaration) { - + $out = + "\n" . + $variable . '->getClause(\'is\')->setProperty(' . + $element->getProperty() . + ');' . "\n"; + } elseif ($element instanceof Praspel\Model\Declaration) { $variable = '$' . ($eldnah ?: $element->getId()); - $out = "\n" . - $variable . ' = $' . $element->getParent()->getId() . - '->getClause(\'' . $element->getName() . '\');' . "\n"; + $out = + "\n" . + $variable . ' = $' . $element->getParent()->getId() . + '->getClause(\'' . $element->getName() . '\');' . "\n"; - foreach($element->getLocalVariables() as $var) + foreach ($element->getLocalVariables() as $var) { $out .= $var->accept($this, $handle, $eldnah); + } - foreach($element->getPredicates() as $predicate) + foreach ($element->getPredicates() as $predicate) { $out .= $variable . '->predicate(\'' . $predicate . '\');' . "\n"; - } - elseif($element instanceof \Hoa\Praspel\Model\Variable) { - + } + } elseif ($element instanceof Praspel\Model\Variable) { $variable = '$' . ($eldnah ?: $element->getClause()->getId()); $name = $element->getName(); $start = $variable . '[\'' . $name . '\']'; - if(true === $element->isLocal()) + if (true === $element->isLocal()) { $out .= $variable . '->let[\'' . $name . '\']'; - else + } else { $out .= $start; + } - if(null !== $alias = $element->getAlias()) + if (null !== $alias = $element->getAlias()) { $out .= '->domainof(\'' . $alias . '\');' . "\n"; - else - $out .= '->in = ' . - $element->getDomains()->accept($this, $handle, $eldnah) . - ';' . "\n"; + } else { + $out .= + '->in = ' . + $element->getDomains()->accept($this, $handle, $eldnah) . + ';' . "\n"; + } $constraints = $element->getConstraints(); - if(isset($constraints['is'])) - $out .= $start . '->is(\'' . - implode('\', \'', $constraints['is']) . '\');' . - "\n"; + if (isset($constraints['is'])) { + $out .= + $start . '->is(\'' . + implode('\', \'', $constraints['is']) . '\');' . + "\n"; + } - if(isset($constraints['contains'])) - foreach($constraints['contains'] as $contains) + if (isset($constraints['contains'])) { + foreach ($constraints['contains'] as $contains) { $out .= $start . '->contains(' . $contains . ');' . "\n"; + } + } - if(isset($constraints['key'])) - foreach($constraints['key'] as $pairs) - $out .= $start . '->key(' . $pairs[0] . ')->in = ' . - $pairs[1] . ';' . "\n"; - } - elseif($element instanceof \Hoa\Praspel\Model\Throwable) { - + if (isset($constraints['key'])) { + foreach ($constraints['key'] as $pairs) { + $out .= + $start . '->key(' . $pairs[0] . ')->in = ' . + $pairs[1] . ';' . "\n"; + } + } + } elseif ($element instanceof Praspel\Model\Throwable) { $parent = '$' . $element->getParent()->getId(); $_variable = $element->getId(); $variable = '$' . $_variable; - $out = "\n" . - $variable . ' = ' . $parent . - '->getClause(\'throwable\');' . "\n"; - - foreach($element as $identifier) { + $out = + "\n" . + $variable . ' = ' . $parent . + '->getClause(\'throwable\');' . "\n"; + foreach ($element as $identifier) { $exception = $element[$identifier]; $start = $variable . '[\'' . $identifier . '\']'; - $out .= $start . ' = \'' . $exception->getInstanceName() . - '\';' . "\n"; - - if(false === $element->isDisjointed()) { - - if(null !== $with = $element->getWith()) { + $out .= $start . ' = \'' . $exception->getInstanceName() . '\';' . "\n"; + if (false === $element->isDisjointed()) { + if (null !== $with = $element->getWith()) { $temp = $_variable . '_' . $identifier . '_with'; - $out .= '$' . $temp . ' = ' . - $variable . '->newWith();' . "\n"; + $out .= + '$' . $temp . ' = ' . + $variable . '->newWith();' . "\n"; - foreach($with->getLocalVariables() as $var) + foreach ($with->getLocalVariables() as $var) { $out .= $var->accept($this, $handle, $temp); + } - foreach($with->getPredicates() as $predicate) - $out .= '$' . $temp . '->predicate(\'' . $predicate . - '\');' . "\n"; + foreach ($with->getPredicates() as $predicate) { + $out .= + '$' . $temp . '->predicate(\'' . $predicate . + '\');' . "\n"; + } $out .= $start . '->setWith($' . $temp . ');' . "\n"; } + } else { + $out .= + $start . '->disjunctionWith(\'' . + $exception->getDisjunction() . '\');' . "\n"; } - else - $out .= $start . '->disjunctionWith(\'' . - $exception->getDisjunction() . '\');' . "\n"; } - } - elseif($element instanceof \Hoa\Praspel\Model\DefaultBehavior) { - - $out = "\n" . - '$' . $element->getId() . ' = $' . - $element->getParent()->getId() . - '->getClause(\'default\')' . "\n"; - - foreach($element::getAllowedClauses() as $clause) - if(true === $element->clauseExists($clause)) + } elseif ($element instanceof Praspel\Model\DefaultBehavior) { + $out = + "\n" . + '$' . $element->getId() . ' = $' . + $element->getParent()->getId() . + '->getClause(\'default\')' . "\n"; + + foreach ($element::getAllowedClauses() as $clause) { + if (true === $element->clauseExists($clause)) { $out .= $element->getClause($clause)->accept( $this, $handle, $eldnah ); - } - elseif($element instanceof \Hoa\Praspel\Model\Behavior) { - - $out = "\n" . - '$' . $element->getId() . ' = $' . - $element->getParent()->getId() . - '->getClause(\'behavior\')' . - '->get(\'' . $element->getIdentifier() . '\');' . "\n"; - - foreach($element::getAllowedClauses() as $clause) - if(true === $element->clauseExists($clause)) + } + } + } elseif ($element instanceof Praspel\Model\Behavior) { + $out = + "\n" . + '$' . $element->getId() . ' = $' . + $element->getParent()->getId() . + '->getClause(\'behavior\')' . + '->get(\'' . $element->getIdentifier() . '\');' . "\n"; + + foreach ($element::getAllowedClauses() as $clause) { + if (true === $element->clauseExists($clause)) { $out .= $element->getClause($clause)->accept( $this, $handle, $eldnah ); - } - elseif($element instanceof \Hoa\Praspel\Model\Description) { - + } + } + } elseif ($element instanceof Praspel\Model\Description) { $parent = '$' . $element->getParent()->getId(); $variable = '$' . $element->getId(); - $out = "\n" . - $variable . ' = ' . $parent . - '->getClause(\'description\');' . "\n"; - - foreach($element as $example) - $out .= $variable . '[] = \'' . - preg_replace('#(?<!\\\)\'#', '\\\'', $example) . - '\';' . "\n"; - } - elseif($element instanceof \Hoa\Praspel\Model\Collection) - foreach($element as $el) + $out = + "\n" . + $variable . ' = ' . $parent . + '->getClause(\'description\');' . "\n"; + + foreach ($element as $example) { + $out .= + $variable . '[] = \'' . + preg_replace('#(?<!\\\)\'#', '\\\'', $example) . + '\';' . "\n"; + } + } elseif ($element instanceof Praspel\Model\Collection) { + foreach ($element as $el) { $out .= $el->accept($this, $handle, $eldnah); + } + } // Hoa\Realdom. - elseif($element instanceof \Hoa\Realdom\Disjunction) { - + elseif ($element instanceof Realdom\Disjunction) { $realdoms = $element->getUnflattenedRealdoms(); - if(!empty($realdoms)) { - - $oout = array(); + if (!empty($realdoms)) { + $oout = []; - foreach($realdoms as $realdom) { - - if($realdom instanceof \Hoa\Realdom\IRealdom\Constant) - $oout[] = 'const(' . - $realdom->accept($this, $handle, $eldnah) . - ')'; - else + foreach ($realdoms as $realdom) { + if ($realdom instanceof Realdom\IRealdom\Constant) { + $oout[] = + 'const(' . + $realdom->accept($this, $handle, $eldnah) . + ')'; + } else { $oout[] = $realdom->accept($this, $handle, $eldnah); + } } $out .= 'realdom()->' . implode('->or->', $oout); } - } - elseif($element instanceof \Hoa\Realdom) { - - if($element instanceof \Hoa\Realdom\IRealdom\Constant) { - - if($element instanceof \Hoa\Realdom\_Array) { - - $oout = array(); - - foreach($element['pairs'] as $pair) { + } elseif ($element instanceof Realdom) { + if ($element instanceof Realdom\IRealdom\Constant) { + if ($element instanceof Realdom\_Array) { + $oout = []; + foreach ($element['pairs'] as $pair) { $_oout = null; - foreach($pair as $_pair) { - - if(null !== $_oout) + foreach ($pair as $_pair) { + if (null !== $_oout) { $_oout .= ', '; + } $_oout .= $_pair->accept($this, $handle, $eldnah); } @@ -282,47 +276,47 @@ class Compiler implements \Hoa\Visitor\Visit { } $out .= 'array(' . implode(', ', $oout) . ')'; - } - else + } else { $out .= $element->getConstantRepresentation(); - } - else { - - $oout = array(); + } + } else { + $oout = []; - foreach($element->getArguments() as $argument) + foreach ($element->getArguments() as $argument) { $oout[] = $argument->accept($this, $handle, $eldnah); + } - $out .= $element->getName() . - '(' . implode(', ', $oout) . ')'; + $out .= + $element->getName() . + '(' . implode(', ', $oout) . ')'; } - } - elseif($element instanceof \Hoa\Realdom\Crate\Constant) { - + } elseif ($element instanceof Realdom\Crate\Constant) { $holder = $element->getHolder(); $praspel = $element->getPraspelRepresentation(); - $out .= '$' . $element->getDeclaration()->getId() . - '[\'' . $praspel() . '\']'; - } - elseif($element instanceof \Hoa\Realdom\Crate\Variable) { - + $out .= + '$' . $element->getDeclaration()->getId() . + '[\'' . $praspel() . '\']'; + } elseif ($element instanceof Realdom\Crate\Variable) { $holder = $element->getVariable(); - if($holder instanceof \Hoa\Praspel\Model\Variable\Implicit) - $out .= 'variable($' . $holder->getClause()->getId() . - '->getImplicitVariable(\'' . $holder->getName() . - '\'))'; - else - $out .= 'variable($' . $holder->getClause()->getId() . - '->getVariable(\'' . $holder->getName() . '\', true))'; + if ($holder instanceof Praspel\Model\Variable\Implicit) { + $out .= + 'variable($' . $holder->getClause()->getId() . + '->getImplicitVariable(\'' . $holder->getName() . + '\'))'; + } else { + $out .= + 'variable($' . $holder->getClause()->getId() . + '->getVariable(\'' . $holder->getName() . '\', true))'; + } + } else { + throw new Praspel\Exception\Compiler( + '%s is not yet implemented.', + 0, + get_class($element) + ); } - else - throw new \Hoa\Praspel\Exception\Compiler( - '%s is not yet implemented.', 0, get_class($element)); - return $out; } } - -} diff --git a/Visitor/Interpreter.php b/Visitor/Interpreter.php index 3c00144..e12b20c 100644 --- a/Visitor/Interpreter.php +++ b/Visitor/Interpreter.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -34,71 +34,55 @@ * POSSIBILITY OF SUCH DAMAGE. */ -namespace { +namespace Hoa\Praspel\Visitor; -from('Hoa') - -/** - * \Hoa\Praspel\Exception\Interpreter - */ --> import('Praspel.Exception.Interpreter') - -/** - * \Hoa\Praspel\Model\Specification - */ --> import('Praspel.Model.Specification') +use Hoa\Praspel; +use Hoa\Realdom; +use Hoa\String; +use Hoa\Visitor; -/** - * \Hoa\Visitor\Visit - */ --> import('Visitor.Visit') +from('Hoa') /** - * \Hoa\String + * \Hoa\Realdom\Disjunction */ --> import('String.~'); - -} - -namespace Hoa\Praspel\Visitor { +-> import('Realdom.Disjunction', true); /** * Class \Hoa\Praspel\Visitor\Interpreter. * * Compile Praspel to model. * - * @author Ivan Enderlin <ivan.enderlin@hoa-project.net> - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class Interpreter implements \Hoa\Visitor\Visit { - +class Interpreter implements Visitor\Visit +{ /** * Root. * - * @var \Hoa\Praspel\Model\Specification object + * @var \Hoa\Praspel\Model\Specification */ protected $_root = null; /** * Current clause. * - * @var \Hoa\Praspel\Model\Clause object + * @var \Hoa\Praspel\Model\Clause */ protected $_clause = null; /** * Current object. * - * @var \Hoa\Praspel\Model object + * @var \Hoa\Praspel\Model */ protected $_current = null; /** * Classname to bind to the specification. * - * @var \Hoa\Praspel\Visitor\Interpreter string + * @var string */ protected $_classnameToBind = null; @@ -107,53 +91,59 @@ class Interpreter implements \Hoa\Visitor\Visit { /** * Visit an element. * - * @access public * @param \Hoa\Visitor\Element $element Element to visit. * @param mixed &$handle Handle (reference). * @param mixed $eldnah Handle (not reference). * @return mixed - * @throw \Hoa\Praspel\Exception\Interpreter + * @throws \Hoa\Praspel\Exception\Interpreter */ - public function visit ( \Hoa\Visitor\Element $element, - &$handle = null, $eldnah = null ) { - + public function visit( + Visitor\Element $element, + &$handle = null, + $eldnah = null + ) { $id = $element->getId(); - switch($id) { - + switch ($id) { case '#specification': - $this->_clause = $this->_current - = $this->_root - = new \Hoa\Praspel\Model\Specification(); + $this->_clause = + $this->_current = + $this->_root = + new Praspel\Model\Specification(); - if(null !== $classname = $this->getBindedClass()) + if (null !== $classname = $this->getBindedClass()) { $this->_root->bindToClass($classname); + } - foreach($element->getChildren() as $child) + foreach ($element->getChildren() as $child) { $child->accept($this, $handle, $eldnah); + } return $this->_root; - break; case '#is': $this->_clause = $this->_root->getClause('is'); - foreach($element->getChildren() as $child) + foreach ($element->getChildren() as $child) { $this->_clause->addProperty( $this->_clause->getPropertyValue( $child->accept($this, $handle, $eldnah) ) ); - break; + } + + break; case '#requires': case '#ensures': case '#invariant': $this->_clause = $this->_current->getClause(substr($id, 1)); - foreach($element->getChildren() as $child) + foreach ($element->getChildren() as $child) { $child->accept($this, $handle, $eldnah); - break; + } + + break; case '#behavior': $children = $element->getChildren(); @@ -161,38 +151,44 @@ class Interpreter implements \Hoa\Visitor\Visit { $identifier = $child0->accept($this, $handle, false); $previous = $this->_current; - $this->_clause = $this->_current - = $this->_current - ->getClause('behavior') - ->get($identifier); + $this->_clause = + $this->_current = + $this + ->_current + ->getClause('behavior') + ->get($identifier); - foreach($children as $child) + foreach ($children as $child) { $child->accept($this, $handle, $eldnah); + } $this->_current = $previous; - break; + + break; case '#default': $children = $element->getChildren(); $previous = $this->_current; - $this->_clause = $this->_current - = $this->_current - ->getClause('default'); + $this->_clause = + $this->_current = + $this + ->_current + ->getClause('default'); - foreach($children as $child) + foreach ($children as $child) { $child->accept($this, $handle, $eldnah); + } $this->_current = $previous; - break; + + break; case '#throwable': $this->_clause = $this->_current->getClause('throwable'); $identifier = null; - foreach($element->getChildren() as $child) { - - switch($child->getId()) { - + foreach ($element->getChildren() as $child) { + switch ($child->getId()) { case '#exception_identifier': $_identifier = $child->getChild(1)->accept( $this, @@ -207,27 +203,32 @@ class Interpreter implements \Hoa\Visitor\Visit { $this->_clause[$_identifier] = $_instanceof; - if(null === $identifier) + if (null === $identifier) { $identifier = $_identifier; - else + } else { $this->_clause[$_identifier]->disjunctionWith( $identifier ); - break; + } + + break; case '#exception_with': $old = $this->_clause; $this->_clause = $old->newWith(); - foreach($child->getChildren() as $_child) + foreach ($child->getChildren() as $_child) { $_child->accept($this, $handle, $eldnah); + } $old[$identifier]->setWith($this->_clause); $this->_clause = $old; - break; + + break; } } - break; + + break; case '#description': $this->_clause = $this->_root->getClause('description'); @@ -236,7 +237,8 @@ class Interpreter implements \Hoa\Visitor\Visit { $handle, $eldnah ); - break; + + break; case '#declaration': $left = $element->getChild(0)->accept($this, $handle, false); @@ -244,11 +246,13 @@ class Interpreter implements \Hoa\Visitor\Visit { $variable = $left; - if($right instanceof \Hoa\Praspel\Model\Variable) + if ($right instanceof Praspel\Model\Variable) { $right = realdom()->variable($right); + } $this->_clause[$variable]->in = $right; - break; + + break; case '#local_declaration': $left = $element->getChild(0)->accept($this, $handle, false); @@ -256,11 +260,13 @@ class Interpreter implements \Hoa\Visitor\Visit { $variable = $left; - if($right instanceof \Hoa\Praspel\Model\Variable) + if ($right instanceof Praspel\Model\Variable) { $right = realdom()->variable($right); + } $this->_clause->let[$variable]->in = $right; - break; + + break; case '#qualification': $children = $element->getChildren(); @@ -270,60 +276,62 @@ class Interpreter implements \Hoa\Visitor\Visit { false )]; - foreach($children as $child) + foreach ($children as $child) { $variable->is($child->accept($this, $handle, false)); - break; + } + + break; case '#contains': $variable = $element->getChild(0)->accept($this, $handle, false); $value = $element->getChild(1)->accept($this, $handle, false); $this->_clause[$variable]->contains($value); - break; + + break; case '#domainof': $left = $element->getChild(0)->accept($this, $handle, false); $right = $element->getChild(1)->accept($this, $handle, false); $this->_clause[$left]->domainof($right); - break; + + break; case '#predicate': $this->_clause->predicate( $element->getChild(0)->accept($this, $handle, $eldnah) ); - break; + + break; case '#disjunction': $disjunction = realdom(); - foreach($element->getChildren() as $child) { - + foreach ($element->getChildren() as $child) { $value = $child->accept($this, $handle, $eldnah); - if($value instanceof \Hoa\Realdom\Disjunction) + if ($value instanceof Realdom\Disjunction) { $disjunction[] = $value; - elseif($value instanceof \Hoa\Praspel\Model\Variable) + } elseif ($value instanceof Praspel\Model\Variable) { $disjunction->variable($value); - else + } else { $disjunction->const($value); + } } return $disjunction; - break; case '#realdom': $children = $element->getChildren(); $child0 = array_shift($children); $name = $child0->accept($this, $handle, false); - $arguments = array(); - - foreach($children as $child) { + $arguments = []; + foreach ($children as $child) { $argument = $child->accept($this, $handle, $eldnah); - if($argument instanceof \Hoa\Realdom\Disjunction) { - + if ($argument instanceof Realdom\Disjunction) { $realdoms = $argument->getRealdoms(); $argument = $realdoms[0]; } @@ -332,71 +340,65 @@ class Interpreter implements \Hoa\Visitor\Visit { } return realdom()->_call($name, $arguments); - break; case '#concatenation': $string = null; - foreach($element->getChildren() as $child) + foreach ($element->getChildren() as $child) { $string .= $child->accept($this, $handle, $eldnah); + } return $string; - break; case '#array': - $array = array(); - - foreach($element->getChildren() as $child) { - - if('#pair' === $child->getId()) { + $array = []; - $key = $child->getChild(0) - ->accept($this, $handle, $eldnah); - $value = $child->getChild(1) - ->accept($this, $handle, $eldnah); - $array[] = array($key, $value); + foreach ($element->getChildren() as $child) { + if ('#pair' === $child->getId()) { + $key = $child->getChild(0)->accept($this, $handle, $eldnah); + $value = $child->getChild(1)->accept($this, $handle, $eldnah); + $array[] = [$key, $value]; continue; } $key = realdom()->natural(0, 1); $value = $child->accept($this, $handle, $eldnah); - $array[] = array($key, $value); + $array[] = [$key, $value]; } return $array; - break; case '#range': $left = $element->getChild(0)->accept($this, $handle, $eldnah); $right = $element->getChild(1)->accept($this, $handle, $eldnah); - if(is_float($left) || is_float($right)) + if (is_float($left) || is_float($right)) { return realdom()->boundfloat( floatval($left), floatval($right) ); + } return realdom()->boundinteger(intval($left), intval($right)); - break; case '#left_range': $left = $element->getChild(0)->accept($this, $handle, $eldnah); - if(is_float($left)) + if (is_float($left)) { return realdom()->boundfloat($left); + } return realdom()->boundinteger($left); - break; case '#right_range': $right = $element->getChild(0)->accept($this, $handle, $eldnah); - if(is_float($right)) + if (is_float($right)) { return realdom()->boundfloat(null, $right); + } return realdom()->boundinteger(null, $right); - break; case '#arrayaccessbykey': $variable = $element->getChild(0)->accept($this, $handle, $eldnah); @@ -405,95 +407,93 @@ class Interpreter implements \Hoa\Visitor\Visit { $this->_clause[$variable]->key($key); return $variable; - break; case '#dynamic_resolution': $value = null; - foreach($element->getChildren() as $child) { - - if(null !== $value) + foreach ($element->getChildren() as $child) { + if (null !== $value) { $value .= '->'; + } $value .= $child->accept($this, $handle, false); } - if(false !== $eldnah) + if (false !== $eldnah) { return $this->_clause->getVariable($value, true); + } return $value; - break; case '#self_identifier': case '#static_identifier': case '#parent_identifier': $identifier = substr($id, 1, strpos($id, '_', 1) - 1); - foreach($element->getChildren() as $child) + foreach ($element->getChildren() as $child) { $identifier .= '::' . $child->accept($this, $handle, $eldnah); + } return $identifier; - break; case '#old': - $value = '\old(' . - $element->getChild(0)->accept($this, $handle, false) . - ')'; + $value = + '\old(' . + $element->getChild(0)->accept($this, $handle, false) . + ')'; - if(false !== $eldnah) + if (false !== $eldnah) { return $this->_clause->getVariable($value); + } return $value; - break; case '#result': return '\result'; - break; case '#classname': - $classname = array(); + $classname = []; - foreach($element->getChildren() as $child) + foreach ($element->getChildren() as $child) { $classname[] = $child->accept($this, $handle, $eldnah); + } return implode('\\', $classname); - break; case '#nowdoc': case '#heredoc': return $element->getChild(1)->accept($this, $handle, $eldnah); - break; case '#regex': - $regex = $element->getChild(0)->accept($this, $handle, $eldnah); + $regex = $element->getChild(0)->accept($this, $handle, $eldnah); - if(true === $element->childExists(1)) + if (true === $element->childExists(1)) { $length = $element->getChild(1)->accept($this, $handle, $eldnah); + } return realdom()->regex($regex); - break; case '#class': - $classname = $element->getChild(0)->accept($this, $handle, false); + $classname = $element->getChild(0)->accept($this, $handle, false); return realdom()->class($classname); - break; case 'token': $tId = $element->getValueToken(); $value = $element->getValueValue(); - switch($tId) { - + switch ($tId) { case 'identifier': - if(false !== $eldnah) + if (false !== $eldnah) { return $this->getIdentifier($value); + } return $value; case 'this': - if(false !== $eldnah) + if (false !== $eldnah) { return $this->_root->getImplicitVariable($value); + } return $value; @@ -517,16 +517,18 @@ class Interpreter implements \Hoa\Visitor\Visit { case 'binary': $int = intval(substr($value, strpos($value, 'b') + 1), 2); - if('-' === $value[0]) + if ('-' === $value[0]) { return -$int; + } return $int; case 'octal': $int = intval(substr($value, strpos($value, '0') + 1), 8); - if('-' === $value[0]) + if ('-' === $value[0]) { return -$int; + } return $int; @@ -534,20 +536,21 @@ class Interpreter implements \Hoa\Visitor\Visit { $value = strtolower($value); $int = intval(substr($value, strpos($value, 'x') + 1), 16); - if('-' === $value[0]) + if ('-' === $value[0]) { return -$int; + } return $int; case 'decimal': - if(true === ctype_digit(ltrim($value, '+-'))) + if (true === ctype_digit(ltrim($value, '+-'))) { return intval($value); + } return floatval($value); case 'escaped': - switch($value[1]) { - + switch ($value[1]) { case 'n': return "\n"; @@ -570,13 +573,13 @@ class Interpreter implements \Hoa\Visitor\Visit { return "\033[D"; case 'x': - return \Hoa\String::fromCode(hexdec($value)); + return String::fromCode(hexdec($value)); case '\\': return $value[1]; default: - return \Hoa\String::fromCode(octdec($value)); + return String::fromCode(octdec($value)); } case 'accepted': @@ -585,30 +588,39 @@ class Interpreter implements \Hoa\Visitor\Visit { return $value; default: - throw new \Hoa\Praspel\Exception\Interpreter( - 'Token %s is not yet implemented.', 1, $tId); + throw new Praspel\Exception\Interpreter( + 'Token %s is not yet implemented.', + 1, + $tId + ); } - break; + + break; default: - throw new \Hoa\Praspel\Exception\Interpreter( - 'Element %s is unknown.', 2, $id); + throw new Praspel\Exception\Interpreter( + 'Element %s is unknown.', + 2, + $id + ); } } /** * Get identifier object. * - * @access public * @param string $identifier Identifier. * @return \Hoa\Praspel\Model\Variable */ - public function getIdentifier ( $identifier ) { - - if(!isset($this->_clause[$identifier])) - throw new \Hoa\Praspel\Exception\Interpreter( + public function getIdentifier($identifier) + { + if (!isset($this->_clause[$identifier])) { + throw new Praspel\Exception\Interpreter( 'The identifier %s does not exist on clause %s.', - 3, array($identifier, $this->_clause->getName())); + 3, + [$identifier, $this->_clause->getName()] + ); + } return $this->_clause[$identifier]; } @@ -616,34 +628,31 @@ class Interpreter implements \Hoa\Visitor\Visit { /** * Get root. * - * @access public * @return \Hoa\Praspel\Model\Specification */ - public function getRoot ( ) { - + public function getRoot() + { return $this->_root; } /** * Get current clause. * - * @access public * @return \Hoa\Praspel\Model\Clause */ - public function getClause ( ) { - + public function getClause() + { return $this->_clause; } /** * Set classname to bind. * - * @access public * @param string $classname Classname. * @return string */ - public function bindToClass ( $classname ) { - + public function bindToClass($classname) + { $old = $this->_classnameToBind; $this->_classnameToBind = $classname; @@ -653,13 +662,10 @@ class Interpreter implements \Hoa\Visitor\Visit { /** * Get classname to bind. * - * @access public * @return string */ - public function getBindedClass ( ) { - + public function getBindedClass() + { return $this->_classnameToBind; } } - -} diff --git a/Visitor/Praspel.php b/Visitor/Praspel.php index 350c7b0..6242422 100644 --- a/Visitor/Praspel.php +++ b/Visitor/Praspel.php @@ -8,7 +8,7 @@ * * New BSD License * - * Copyright © 2007-2015, Ivan Enderlin. All rights reserved. + * Copyright © 2007-2015, Hoa community. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -34,147 +34,142 @@ * POSSIBILITY OF SUCH DAMAGE. */ -namespace { +namespace Hoa\Praspel\Visitor; -from('Hoa') - -/** - * \Hoa\Praspel\Exception\Compiler - */ --> import('Praspel.Exception.Compiler') - -/** - * \Hoa\Visitor\Visit - */ --> import('Visitor.Visit'); - -} - -namespace Hoa\Praspel\Visitor { +use Hoa\Praspel as HoaPraspel; +use Hoa\Realdom; +use Hoa\Visitor; /** * Class \Hoa\Praspel\Visitor\Praspel. * * Compile the model to Praspel code. * - * @author Ivan Enderlin <ivan.enderlin@hoa-project.net> - * @copyright Copyright © 2007-2015 Ivan Enderlin. + * @copyright Copyright © 2007-2015 Hoa community * @license New BSD License */ - -class Praspel implements \Hoa\Visitor\Visit { - +class Praspel implements Visitor\Visit +{ /** * Visit an element. * - * @access public * @param \Hoa\Visitor\Element $element Element to visit. * @param mixed &$handle Handle (reference). * @param mixed $eldnah Handle (not reference). * @return mixed */ - public function visit ( \Hoa\Visitor\Element $element, - &$handle = null, $eldnah = null ) { - + public function visit( + Visitor\Element $element, + &$handle = null, + $eldnah = null + ) { $out = null; // Hoa\Praspel. - if($element instanceof \Hoa\Praspel\Model\Specification) { - - $oout = array(); + if ($element instanceof HoaPraspel\Model\Specification) { + $oout = []; - foreach($element::getAllowedClauses() as $clause) - if(true === $element->clauseExists($clause)) + foreach ($element::getAllowedClauses() as $clause) { + if (true === $element->clauseExists($clause)) { $oout[] = $element->getClause($clause)->accept( $this, $handle, $eldnah ); + } + } $out = implode("\n", $oout); - } - elseif($element instanceof \Hoa\Praspel\Model\Is) { - + } elseif ($element instanceof HoaPraspel\Model\Is) { $out = '@is ' . $element->getPropertyName() . ';'; - } - elseif($element instanceof \Hoa\Praspel\Model\Declaration) { - + } elseif ($element instanceof HoaPraspel\Model\Declaration) { $clause = $element->getName(); $out = '@' . $clause; - $oout = array(); + $oout = []; - foreach($element->getLocalVariables() as $name => $var) + foreach ($element->getLocalVariables() as $name => $var) { $oout[] = ' ' . $var->accept($this, $handle, $eldnah); + } - foreach($element->getPredicates() as $predicate) + foreach ($element->getPredicates() as $predicate) { $oout[] = ' \pred(\'' . $predicate . '\')'; + } $out .= implode(' and', $oout) . ';'; - } - elseif($element instanceof \Hoa\Praspel\Model\Variable) { - + } elseif ($element instanceof HoaPraspel\Model\Variable) { $name = $element->getName(); - if(true === $element->isLocal()) + if (true === $element->isLocal()) { $out = 'let '; + } $out .= $name; - if(null === $alias = $element->getAlias()) - $out .= ': ' . - $element->getDomains()->accept($this, $handle, $eldnah); - else + if (null === $alias = $element->getAlias()) { + $out .= + ': ' . + $element->getDomains()->accept($this, $handle, $eldnah); + } else { $out .= ' domainof ' . $alias; + } $constraints = $element->getConstraints(); - if(isset($constraints['is'])) - $out .= ' and ' . $name . ' is ' . - implode(', ', $constraints['is']); - - if(isset($constraints['contains'])) - foreach($constraints['contains'] as $contains) - $out .= ' and ' . $name . ' contains ' . - $contains->accept($this, $handle, $eldnah); - - if(isset($constraints['key'])) - foreach($constraints['key'] as $pairs) - $out .= ' and ' . $name . '[' . - $pairs[0]->accept($this, $handle, $eldnah) . - ']: ' . $pairs[1]->accept($this, $handle, $eldnah); - } - elseif($element instanceof \Hoa\Praspel\Model\Throwable) { + if (isset($constraints['is'])) { + $out .= + ' and ' . $name . ' is ' . + implode(', ', $constraints['is']); + } - $oout = array(); + if (isset($constraints['contains'])) { + foreach ($constraints['contains'] as $contains) { + $out .= + ' and ' . $name . ' contains ' . + $contains->accept($this, $handle, $eldnah); + } + } - foreach($element as $identifier) { + if (isset($constraints['key'])) { + foreach ($constraints['key'] as $pairs) { + $out .= + ' and ' . $name . '[' . + $pairs[0]->accept($this, $handle, $eldnah) . + ']: ' . $pairs[1]->accept($this, $handle, $eldnah); + } + } + } elseif ($element instanceof HoaPraspel\Model\Throwable) { + $oout = []; + foreach ($element as $identifier) { $exception = $element[$identifier]; - if(true === $exception->isDisjointed()) + if (true === $exception->isDisjointed()) { continue; + } - $line = ' ' . $exception->getInstanceName() . ' ' . - $identifier; - - foreach((array) $exception->getDisjunction() as $_identifier) { + $line = + ' ' . $exception->getInstanceName() . ' ' . + $identifier; + foreach ((array) $exception->getDisjunction() as $_identifier) { $_exception = $element[$_identifier]; - $line .= ' or ' . $_exception->getInstanceName() . ' ' . - $_identifier; + $line .= + ' or ' . $_exception->getInstanceName() . ' ' . + $_identifier; } - if(null !== $with = $exception->getWith()) { - + if (null !== $with = $exception->getWith()) { $line .= ' with '; - $liine = array(); + $liine = []; - foreach($with as $var) + foreach ($with as $var) { $liine[] = $var->accept($this, $handle, $eldnah); + } - foreach($with->getPredicates() as $predicate) + foreach ($with->getPredicates() as $predicate) { $liine[] = '\pred(\'' . $predicate . '\')'; + } $line .= implode(' and ', $liine); } @@ -183,14 +178,12 @@ class Praspel implements \Hoa\Visitor\Visit { } $out = '@throwable' . implode(' or', $oout) . ';'; - } - elseif($element instanceof \Hoa\Praspel\Model\DefaultBehavior) { - + } elseif ($element instanceof HoaPraspel\Model\DefaultBehavior) { $out = '@default {' . "\n"; - $oout = array(); + $oout = []; - foreach($element::getAllowedClauses() as $clause) - if(true === $element->clauseExists($clause)) + foreach ($element::getAllowedClauses() as $clause) { + if (true === $element->clauseExists($clause)) { $oout[] = ' ' . str_replace( "\n", "\n" . ' ', @@ -200,16 +193,16 @@ class Praspel implements \Hoa\Visitor\Visit { $eldnah ) ); + } + } $out .= implode("\n", $oout) . "\n" . '}'; - } - elseif($element instanceof \Hoa\Praspel\Model\Behavior) { - + } elseif ($element instanceof HoaPraspel\Model\Behavior) { $out = '@behavior ' . $element->getIdentifier() . ' {' . "\n"; - $oout = array(); + $oout = []; - foreach($element::getAllowedClauses() as $clause) - if(true === $element->clauseExists($clause)) + foreach ($element::getAllowedClauses() as $clause) { + if (true === $element->clauseExists($clause)) { $oout[] = ' ' . str_replace( "\n", "\n" . ' ', @@ -219,71 +212,66 @@ class Praspel implements \Hoa\Visitor\Visit { $eldnah ) ); + } + } $out .= implode("\n", $oout) . "\n" . '}'; - - } - elseif($element instanceof \Hoa\Praspel\Model\Description) { - - $oout = array(); - - foreach($element as $example) - $oout[] = '@description \'' . - preg_replace('#(?<!\\\)\'#', '\\\'', $example) . - '\';'; + } elseif ($element instanceof HoaPraspel\Model\Description) { + $oout = []; + + foreach ($element as $example) { + $oout[] = + '@description \'' . + preg_replace('#(?<!\\\)\'#', '\\\'', $example) . + '\';'; + } $out = implode("\n", $oout); - } - elseif($element instanceof \Hoa\Praspel\Model\Collection) - foreach($element as $el) + } elseif ($element instanceof HoaPraspel\Model\Collection) { + foreach ($element as $el) { $out .= $el->accept($this, $handle, $eldnah); + } + } // Hoa\Realdom. - elseif($element instanceof \Hoa\Realdom\Disjunction) { - + elseif ($element instanceof Realdom\Disjunction) { $realdoms = $element->getUnflattenedRealdoms(); - if(!empty($realdoms)) { - - $oout = array(); + if (!empty($realdoms)) { + $oout = []; - foreach($realdoms as $realdom) + foreach ($realdoms as $realdom) { $oout[] = $realdom->accept($this, $handle, $eldnah); + } $out .= implode(' or ', $oout); } - } - elseif($element instanceof \Hoa\Realdom) { - - if($element instanceof \Hoa\Realdom\IRealdom\Constant) + } elseif ($element instanceof Realdom) { + if ($element instanceof Realdom\IRealdom\Constant) { $out .= $element->getConstantRepresentation(); - else { - - $oout = array(); + } else { + $oout = []; - foreach($element->getArguments() as $argument) + foreach ($element->getArguments() as $argument) { $oout[] = $argument->accept($this, $handle, $eldnah); + } $out .= $element->getName() . '(' . implode(', ', $oout) . ')'; } - } - elseif($element instanceof \Hoa\Realdom\Crate\Constant) { - + } elseif ($element instanceof Realdom\Crate\Constant) { $praspel = $element->getPraspelRepresentation(); $out .= $praspel(); - } - elseif($element instanceof \Hoa\Realdom\Crate\Variable) { - + } elseif ($element instanceof Realdom\Crate\Variable) { $out .= $element->getVariable()->getName(); + } else { + throw new HoaPraspel\Exception\Compiler( + '%s is not yet implemented.', + 0, + get_class($element) + ); } - else - throw new \Hoa\Praspel\Exception\Compiler( - '%s is not yet implemented.', 0, get_class($element)); - return $out; } } - -} |