Exemplo n.º 1
0
 /**
  * @param RepetitionNode $astNode
  *
  * @return bool
  */
 public function postVisitRepetitionNode(RepetitionNode $astNode)
 {
     $child = $this->visitContext->popOneExpression($this);
     $expression = new Repetition($child);
     $this->visitContext->pushExpression($expression);
     return true;
 }
Exemplo n.º 2
0
 /**
  * @param OptionalNode $astNode
  *
  * @return bool
  */
 public function postVisitOptionalNode(OptionalNode $astNode)
 {
     $child = $this->visitContext->popOneExpression($this);
     $expression = new Optional($child);
     $this->visitContext->pushExpression($expression);
     return true;
 }
Exemplo n.º 3
0
 /**
  * @param RuleNode $astNode
  * @return bool
  */
 public function postVisitRuleNode(RuleNode $astNode)
 {
     $expressions = $this->visitContext->popExpressions($this);
     $symbol = $this->visitContext->popLeftHandSymbol($this);
     //remove the lhs we just popped from the visit context
     array_shift($expressions);
     $production = new StandardProduction($symbol, array_shift($expressions));
     $this->visitContext->collectProduction($production);
     return true;
 }
Exemplo n.º 4
0
 /**
  * @param IdentifierNode $astNode
  * @return bool
  * @throws \DomainException
  */
 public function visitIdentifierNode(IdentifierNode $astNode)
 {
     $name = $astNode->getIdentifierName();
     $uppercase = strtoupper($name);
     if ($uppercase == $name) {
         $expression = new ExpressionSymbol(Symbol::TYPE_TERMINAL, $name);
         $this->visitContext->pushExpression($expression);
         return true;
     }
     $lowercase = strtolower($name);
     if ($lowercase == $name) {
         $expression = new ExpressionSymbol(Symbol::TYPE_NON_TERMINAL, $name);
         $this->visitContext->pushExpression($expression);
         return true;
     }
     throw new \DomainException('unknown type of identifier name');
 }
Exemplo n.º 5
0
 public function translate(SyntaxNode $node)
 {
     $visitContext = new AstTranslatorContext();
     $visitorProvider = new VisitorProvider($visitContext);
     $dispatchingProvider = new DispatchingProvider($visitorProvider);
     $walkStrategy = PreOrderVisitStrategy::newDefaultInstance($dispatchingProvider);
     $walker = new StackBasedAstWalker($walkStrategy);
     foreach ($node->getRuleNodes() as $rule) {
         $walker->walk($rule);
     }
     $productions = $visitContext->getProductions();
     if (empty($productions)) {
         throw new \Exception('no productions found');
     }
     $grammarTitle = 'untitled';
     $grammarTitleNode = $node->getGrammarTitleNode();
     if ($grammarTitleNode instanceof LiteralNode) {
         $grammarTitle = $grammarTitleNode->getLiteral();
     }
     $grammar = new StandardGrammar($grammarTitle, $productions);
     return $grammar;
 }
Exemplo n.º 6
0
 /**
  * @param ChoiceNode $astNode
  *
  * @return bool
  */
 public function postVisitChoiceNode(ChoiceNode $astNode)
 {
     $children = $this->visitContext->popExpressions($this);
     $expression = new Choice(array_shift($children), $children);
     $this->visitContext->pushExpression($expression);
 }
Exemplo n.º 7
0
 /**
  * @param GroupNode $astNode
  *
  * @return bool
  */
 public function postVisitGroupNode(GroupNode $astNode)
 {
     $child = $this->visitContext->popOneExpression($this);
     $expression = new Group($child);
     $this->visitContext->pushExpression($expression);
 }
Exemplo n.º 8
0
 /**
  * @param LiteralNode $astNode
  *
  * @return bool
  */
 public function visitLiteralNode(LiteralNode $astNode)
 {
     $expression = new ExpressionSymbol(Symbol::TYPE_TERMINAL, $astNode->getLiteral());
     $this->visitContext->pushExpression($expression);
     return true;
 }