Logo Search packages:      
Sourcecode: antlr version File versions

String antlr::CppCodeGenerator::mapTreeId ( String  idParam,
ActionTransInfo  transInfo 
) [inline, virtual]

Map an identifier to it's corresponding tree-node variable. This is context-sensitive, depending on the rule and alternative being generated

Parameters:
idParam The identifier name to map
Returns:
The mapped id (which may be the same as the input), or null if the mapping is invalid due to duplicates

Implements antlr::CodeGenerator.

Definition at line 4147 of file CppCodeGenerator.java.

References antlr::RuleBlock::getRuleName(), antlr::RuleBlock::labeledElements, and antlr::ActionTransInfo::refRuleRoot.

                                                                         {
            // if not in an action of a rule, nothing to map.
            if ( currentRule==null ) return idParam;
//          System.out.print("mapTreeId: "+idParam+" "+currentRule.getRuleName()+" ");

            boolean in_var = false;
            String id = idParam;
            if (grammar instanceof TreeWalkerGrammar)
            {
//                RK: hmmm this seems odd. If buildAST is false it translates
//                #rulename_in to 'rulename_in' else to 'rulename_AST_in' which indeed
//                exists. disabling for now.. and hope it doesn't blow up somewhere.
                  if ( !grammar.buildAST )
                  {
                        in_var = true;
//                      System.out.println("in_var1");
                  }
                  // If the id ends with "_in", then map it to the input variable
//                else
                  if (id.length() > 3 && id.lastIndexOf("_in") == id.length()-3)
                  {
                        // Strip off the "_in"
                        id = id.substring(0, id.length()-3);
                        in_var = true;
//                      System.out.println("in_var2");
                  }
            }
//          System.out.print(in_var+"\t");

            // Check the rule labels.  If id is a label, then the output
            // variable is label_AST, and the input variable is plain label.
            for (int i = 0; i < currentRule.labeledElements.size(); i++)
            {
                  AlternativeElement elt = (AlternativeElement)currentRule.labeledElements.elementAt(i);
                  if (elt.getLabel().equals(id))
                  {
//                      if( in_var )
//                            System.out.println("returning (vec) "+(in_var ? id : id + "_AST"));
                        return in_var ? id : id + "_AST";
                  }
            }

            // Failing that, check the id-to-variable map for the alternative.
            // If the id is in the map, then output variable is the name in the
            // map, and input variable is name_in
            String s = (String)treeVariableMap.get(id);
            if (s != null)
            {
                  if (s == NONUNIQUE)
                  {
//                      if( in_var )
//                            System.out.println("returning null (nonunique)");
                        // There is more than one element with this id
                        antlrTool.error("Ambiguous reference to AST element "+id+
                                                " in rule "+currentRule.getRuleName());
                        return null;
                  }
                  else if (s.equals(currentRule.getRuleName()))
                  {
                        // a recursive call to the enclosing rule is
                        // ambiguous with the rule itself.
//                      if( in_var )
//                            System.out.println("returning null (rulename)");
                        antlrTool.error("Ambiguous reference to AST element "+id+
                                                " in rule "+currentRule.getRuleName());
                        return null;
                  }
                  else
                  {
//                      if( in_var )
//                      System.out.println("returning "+(in_var?s+"_in":s));
                        return in_var ? s + "_in" : s;
                  }
            }

//          System.out.println("Last check: "+id+" == "+currentRule.getRuleName());
            // Failing that, check the rule name itself.  Output variable
            // is rule_AST; input variable is rule_AST_in (treeparsers).
            if( id.equals(currentRule.getRuleName()) )
            {
                  String r = in_var ? id + "_AST_in" : id + "_AST";
                  if ( transInfo!=null ) {
                        if ( !in_var ) {
                              transInfo.refRuleRoot = r;
                        }
                  }
//                if( in_var )
//                      System.out.println("returning (r) "+r);
                  return r;
            }
            else
            {
//                if( in_var )
//                System.out.println("returning (last) "+id);
                  // id does not map to anything -- return itself.
                  return id;
            }
      }


Generated by  Doxygen 1.6.0   Back to index