Logo Search packages:      
Sourcecode: antlr version File versions

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

Generate code to link an element reference into the AST

Definition at line 1834 of file JavaCodeGenerator.java.

References antlr::Grammar::buildAST, getASTCreateString(), antlr::GrammarAtom::getASTNodeType(), antlr::CodeGenerator::grammar, antlr::Grammar::hasSyntacticPredicate, mapTreeVariable(), and antlr::CodeGenerator::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) {
                elementRef = el.getLabel();
                astNameBase = el.getLabel();
            }
            else {
                elementRef = lt1Value;
                // Generate AST variables for unlabeled stuff
                astNameBase = "tmp" + astVarNumber;
                ;
                astVarNumber++;
            }

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

            // 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 = null;");
            }

            // 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:
                        println("astFactory.addASTChild(currentAST, " + astName + ");");
                        break;
                    case GrammarElement.AUTO_GEN_CARET:
                        println("astFactory.makeASTRoot(currentAST, " + astName + ");");
                        break;
                    default:
                        break;
                }
            }
            if (doNoGuessTest) {
                // tabs--;
                // println("}");
            }
        }
    }


Generated by  Doxygen 1.6.0   Back to index