Beispiel #1
0
 /**
  * @param SyntaxNode $astNode
  * @param DotFile $dotFile
  *
  * @return bool
  */
 public function write(SyntaxNode $astNode, DotFile $dotFile)
 {
     $visitorProvider = new Writers($dotFile, new VisitContext());
     $dispatchingProvider = new DispatchingProvider($visitorProvider);
     $walkStrategy = PreOrderVisitStrategy::newDefaultInstance($dispatchingProvider);
     $walker = new StackBasedAstWalker($walkStrategy);
     $walkResult = $walker->walk($astNode);
     return $walkResult;
 }
Beispiel #2
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;
 }