Logo Search packages:      
Sourcecode: antlr version File versions

void antlr::CppCodeGenerator::genElementAST ( AlternativeElement  el  )  [inline, private]

Generate code to link an element reference into the AST

Definition at line 2064 of file CppCodeGenerator.java.

References genASTDeclaration(), getASTCreateString(), antlr::GrammarAtom::getASTNodeType(), mapTreeVariable(), and println().

Referenced by gen(), and genCommonBlock().

                                                        {

            // handle case where you're not building trees, but are in tree walker.
            // Just need to get labels set up.
            if ( grammar instanceof TreeWalkerGrammar && !grammar.buildAST )
            {
                  String elementRef;
                  String astName;

                  // Generate names and declarations of the AST variable(s)
                  if (el.getLabel() == null)
                  {
                        elementRef = lt1Value;
                        // Generate AST variables for unlabeled stuff
                        astName = "tmp" + astVarNumber + "_AST";
                        astVarNumber++;
                        // Map the generated AST variable in the alternate
                        mapTreeVariable(el, astName);
                        // Generate an "input" AST variable also
                        println(labeledElementASTType+" "+astName+"_in = "+elementRef+";");
                  }
                  return;
            }

            if (grammar.buildAST && syntacticPredLevel == 0)
            {
                  boolean needASTDecl =
                        ( genAST && (el.getLabel() != null ||
                          el.getAutoGenType() != GrammarElement.AUTO_GEN_BANG ));

                  // RK: if we have a grammar element always generate the decl
                  // since some guy can access it from an action and we can't
                  // peek ahead (well not without making a mess).
                  // I'd prefer taking this out.
                  if( el.getAutoGenType() != GrammarElement.AUTO_GEN_BANG &&
                         (el instanceof TokenRefElement) )
                        needASTDecl = true;

                  boolean doNoGuessTest =
                        ( grammar.hasSyntacticPredicate && needASTDecl );

                  String elementRef;
                  String astNameBase;

                  // Generate names and declarations of the AST variable(s)
                  if (el.getLabel() != null)
                  {
                        // if the element is labeled use that name...
                        elementRef = el.getLabel();
                        astNameBase = el.getLabel();
                  }
                  else
                  {
                        // else generate a temporary name...
                        elementRef = lt1Value;
                        // Generate AST variables for unlabeled stuff
                        astNameBase = "tmp" + astVarNumber;
                        astVarNumber++;
                  }

                  // Generate the declaration if required.
                  if ( needASTDecl )
                  {
                        if ( el instanceof GrammarAtom )
                        {
                              GrammarAtom ga = (GrammarAtom)el;
                              if ( ga.getASTNodeType()!=null )
                              {
                                    genASTDeclaration( el, astNameBase, "Ref"+ga.getASTNodeType() );
//                                  println("Ref"+ga.getASTNodeType()+" " + astName + ";");
                              }
                              else
                              {
                                    genASTDeclaration( el, astNameBase, labeledElementASTType );
//                                  println(labeledElementASTType+" " + astName + " = "+labeledElementASTInit+";");
                              }
                        }
                        else
                        {
                              genASTDeclaration( el, astNameBase, labeledElementASTType );
//                            println(labeledElementASTType+" " + astName + " = "+labeledElementASTInit+";");
                        }
                  }

                  // for convenience..
                  String astName = astNameBase + "_AST";

                  // Map the generated AST variable in the alternate
                  mapTreeVariable(el, astName);
                  if (grammar instanceof TreeWalkerGrammar)
                  {
                        // Generate an "input" AST variable also
                        println(labeledElementASTType+" " + astName + "_in = "+labeledElementASTInit+";");
                  }

                  // Enclose actions with !guessing
                  if (doNoGuessTest) {
                        println("if ( inputState->guessing == 0 ) {");
                        tabs++;
                  }

                  // if something has a label assume it will be used
                  // so we must initialize the RefAST
                  if (el.getLabel() != null)
                  {
                        if ( el instanceof GrammarAtom )
                        {
                              println(astName + " = "+
                                            getASTCreateString((GrammarAtom)el,elementRef) + ";");
                        }
                        else
                        {
                              println(astName + " = "+
                                            getASTCreateString(elementRef) + ";");
                        }
                  }

                  // if it has no label but a declaration exists initialize it.
                  if( el.getLabel() == null && needASTDecl )
                  {
                        elementRef = lt1Value;
                        if ( el instanceof GrammarAtom )
                        {
                              println(astName + " = "+
                                            getASTCreateString((GrammarAtom)el,elementRef) + ";");
                        }
                        else
                        {
                              println(astName + " = "+
                                            getASTCreateString(elementRef) + ";");
                        }
                        // Map the generated AST variable in the alternate
                        if (grammar instanceof TreeWalkerGrammar)
                        {
                              // set "input" AST variable also
                              println(astName + "_in = " + elementRef + ";");
                        }
                  }

                  if (genAST)
                  {
                        switch (el.getAutoGenType())
                        {
                        case GrammarElement.AUTO_GEN_NONE:
                              if( usingCustomAST ||
                                     (el instanceof GrammarAtom &&
                                      ((GrammarAtom)el).getASTNodeType() != null) )
                                    println("astFactory->addASTChild(currentAST, static_cast<"+namespaceAntlr+"RefAST>("+ astName + "));");
                              else
                                    println("astFactory->addASTChild(currentAST, "+ astName + ");");
                              //                                  println("astFactory.addASTChild(currentAST, "+namespaceAntlr+"RefAST(" + astName + "));");
                              break;
                        case GrammarElement.AUTO_GEN_CARET:
                              if( usingCustomAST ||
                                     (el instanceof GrammarAtom &&
                                     ((GrammarAtom)el).getASTNodeType() != null) )
                                    println("astFactory->makeASTRoot(currentAST, static_cast<"+namespaceAntlr+"RefAST>(" + astName + "));");
                              else
                                    println("astFactory->makeASTRoot(currentAST, " + astName + ");");
                              break;
                        default:
                              break;
                        }
                  }
                  if (doNoGuessTest)
                  {
                        tabs--;
                        println("}");
                  }
            }
      }


Generated by  Doxygen 1.6.0   Back to index