Exemplo n.º 1
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;
 }