Logo Search packages:      
Sourcecode: antlr version File versions

void antlr::CSharpCodeGenerator::gen ( OneOrMoreBlock  blk  )  [inline, virtual]

Generate code for the given grammar element.

Parameters:
blk The (...)+ block to generate

Implements antlr::CodeGenerator.

Definition at line 317 of file CSharpCodeGenerator.java.

References antlr::CodeGenerator::_print(), antlr::CodeGenerator::DEBUG_CODE_GENERATOR, genBlockFinish(), genBlockInitAction(), genBlockPreamble(), genCommonBlock(), antlr::CodeGenerator::grammar, antlr::Grammar::maxk, antlr::CodeGenerator::println(), antlr::CodeGenerator::tabs, and antlr::Grammar::theLLkAnalyzer.

                                          {
            if ( DEBUG_CODE_GENERATOR ) System.out.println("gen+("+blk+")");
            String label;
            String cnt;
            println("{ // ( ... )+");
            genBlockPreamble(blk);
            if ( blk.getLabel() != null ) {
                  cnt = "_cnt_"+blk.getLabel();
            }
            else {
                  cnt = "_cnt" + blk.ID;
            }
            println("int "+cnt+"=0;");
            if ( blk.getLabel() != null ) {
                  label = blk.getLabel();
            }
            else {
                  label = "_loop" + blk.ID;
            }

            println("for (;;)");
            println("{");
            tabs++;
      // generate the init action for ()+ ()* inside the loop
      // this allows us to do usefull EOF checking...
      genBlockInitAction(blk);

            // Tell AST generation to build subrule result
            String saveCurrentASTResult = currentASTResult;
            if (blk.getLabel() != null) {
                  currentASTResult = blk.getLabel();
            }

            boolean ok = grammar.theLLkAnalyzer.deterministic(blk);

            // generate exit test if greedy set to false
            // and an alt is ambiguous with exit branch
            // or when lookahead derived purely from end-of-file
            // Lookahead analysis stops when end-of-file is hit,
            // returning set {epsilon}.  Since {epsilon} is not
            // ambig with any real tokens, no error is reported
            // by deterministic() routines and we have to check
            // for the case where the lookahead depth didn't get
            // set to NONDETERMINISTIC (this only happens when the
            // FOLLOW contains real atoms + epsilon).
            boolean generateNonGreedyExitPath = false;
            int nonGreedyExitDepth = grammar.maxk;

            if ( !blk.greedy &&
                  blk.exitLookaheadDepth<=grammar.maxk &&
                  blk.exitCache[blk.exitLookaheadDepth].containsEpsilon() )
            {
                  generateNonGreedyExitPath = true;
                  nonGreedyExitDepth = blk.exitLookaheadDepth;
            }
            else if ( !blk.greedy &&
                  blk.exitLookaheadDepth==LLkGrammarAnalyzer.NONDETERMINISTIC )
            {
                  generateNonGreedyExitPath = true;
            }

            // generate exit test if greedy set to false
            // and an alt is ambiguous with exit branch
            if ( generateNonGreedyExitPath ) {
                  if ( DEBUG_CODE_GENERATOR ) {
                        System.out.println("nongreedy (...)+ loop; exit depth is "+
                              blk.exitLookaheadDepth);
                  }
                  String predictExit =
                        getLookaheadTestExpression(blk.exitCache,
                        nonGreedyExitDepth);
                  println("// nongreedy exit test");
                  println("if (("+cnt+" >= 1) && "+predictExit+") goto "+label+"_breakloop;");
            }

            CSharpBlockFinishingInfo howToFinish = genCommonBlock(blk, false);
            genBlockFinish(
                  howToFinish,
                  "if ("+cnt+" >= 1) { goto "+label+"_breakloop; } else { " + throwNoViable + "; }"
                  );

            println(cnt+"++;");
            tabs--;
            println("}");
            _print(label + "_breakloop:");
            println(";");
            println("}    // ( ... )+");

            // Restore previous AST generation
            currentASTResult = saveCurrentASTResult;
      }


Generated by  Doxygen 1.6.0   Back to index