/** * @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; }
/** * @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; }
/** * @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; }
/** * @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'); }
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; }
/** * @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); }
/** * @param GroupNode $astNode * * @return bool */ public function postVisitGroupNode(GroupNode $astNode) { $child = $this->visitContext->popOneExpression($this); $expression = new Group($child); $this->visitContext->pushExpression($expression); }
/** * @param LiteralNode $astNode * * @return bool */ public function visitLiteralNode(LiteralNode $astNode) { $expression = new ExpressionSymbol(Symbol::TYPE_TERMINAL, $astNode->getLiteral()); $this->visitContext->pushExpression($expression); return true; }