Hoa central
Sampler.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Compiler\Llk\Sampler;
38 
39 use Hoa\Compiler;
40 use Hoa\Core;
41 use Hoa\Visitor;
42 
51 abstract class Sampler
52 {
58  protected $_compiler = null;
59 
65  protected $_tokens = null;
66 
72  protected $_rules = null;
73 
79  protected $_tokenSampler = null;
80 
86  protected $_rootRuleName = null;
87 
93  protected $_currentNamespace = 'default';
94 
95 
96 
104  public function __construct(
105  Compiler\Llk\Parser $compiler,
106  Visitor\Visit $tokenSampler
107  ) {
108  $this->_compiler = $compiler;
109  $this->_tokens = $compiler->getTokens();
110  $this->_rules = $compiler->getRules();
111  $this->_tokenSampler = $tokenSampler;
112  $this->_rootRuleName = $compiler->getRootRule();
113 
114  return;
115  }
116 
122  public function getCompiler()
123  {
124  return $this->_compiler;
125  }
126 
134  protected function completeToken(Compiler\Llk\Rule\Token $token)
135  {
136  if (null !== $token->getRepresentation()) {
138  }
139 
140  $name = $token->getTokenName();
141  $token->setNamespace($this->_currentNamespace);
142  $toNamespace = $this->_currentNamespace;
143 
144  if (isset($this->_tokens[$this->_currentNamespace][$name])) {
145  $token->setRepresentation(
146  $this->_tokens[$this->_currentNamespace][$name]
147  );
148  } else {
149  foreach ($this->_tokens[$this->_currentNamespace] as $_name => $regex) {
150  if (false === strpos($_name, ':')) {
151  continue;
152  }
153 
154  list($_name, $toNamespace) = explode(':', $_name, 2);
155 
156  if ($_name === $name) {
157  break;
158  }
159  }
160 
161  $token->setRepresentation($regex);
162  }
163 
164  return $toNamespace;
165  }
166 
173  protected function setCurrentNamespace($namespace)
174  {
176  $this->_currentNamespace = $namespace;
177 
178  return $old;
179  }
180 
188  protected function generateToken(Compiler\Llk\Rule\Token $token)
189  {
190  $toNamespace = $this->completeToken($token);
191  $this->setCurrentNamespace($toNamespace);
192 
193  return $this->_tokenSampler->visit(
194  $token->getAST()
195  ) . ' '; // use skip token @TODO
196  }
197 }
198 
202 Core\Consistency::flexEntity('Hoa\Compiler\Llk\Sampler\Sampler');
completeToken(Compiler\Llk\Rule\Token $token)
Definition: Sampler.php:134
generateToken(Compiler\Llk\Rule\Token $token)
Definition: Sampler.php:188
__construct(Compiler\Llk\Parser $compiler, Visitor\Visit $tokenSampler)
Definition: Sampler.php:104