Logo Search packages:      
Sourcecode: antlr version File versions

void antlr::JavaCodeGenerator::gen ( TreeWalkerGrammar  g  )  throws IOException [inline, virtual]

Generate the tree-parser Java file

Implements antlr::CodeGenerator.

Definition at line 997 of file JavaCodeGenerator.java.

References antlr::CodeGenerator::_println(), antlr::CodeGenerator::behavior, antlr::CodeGenerator::bitsetsUsed, antlr::Grammar::buildAST, antlr::Grammar::classMemberAction, antlr::Grammar::comment, antlr::CodeGenerator::currentOutput, currentRule, genBitsets(), genHeader(), genRule(), genTokenStrings(), antlr::Grammar::getClassName(), antlr::DefineGrammarSymbols::getHeaderAction(), antlr::Token::getLine(), antlr::TokenManager::getName(), antlr::Grammar::getSuperClass(), antlr::Token::getText(), antlr::CodeGenerator::grammar, antlr::TokenManager::maxTokenType(), antlr::Grammar::options, antlr::Grammar::preambleAction, antlr::CodeGenerator::print(), antlr::CodeGenerator::println(), processActionForSpecialSymbols(), antlr::Grammar::rules, antlr::CodeGenerator::setGrammar(), setupOutput(), antlr::Grammar::superClass, antlr::CodeGenerator::tabs, and antlr::Grammar::tokenManager.

                                                            {
        // SAS: debugging stuff removed for now...
        setGrammar(g);
        if (!(grammar instanceof TreeWalkerGrammar)) {
            antlrTool.panic("Internal error generating tree-walker");
        }
        // Open the output stream for the parser and set the currentOutput
        // SAS: move file open to method so subclass can override it
        //      (mainly for VAJ interface)
        setupOutput(grammar.getClassName());

        genAST = grammar.buildAST;
        tabs = 0;

        // Generate the header common to all output files.
        genHeader();
        // Do not use printAction because we assume tabs==0
        println(behavior.getHeaderAction(""));

        // Generate header for the parser
        println("import antlr." + grammar.getSuperClass() + ";");
        println("import antlr.Token;");
        println("import antlr.collections.AST;");
        println("import antlr.RecognitionException;");
        println("import antlr.ANTLRException;");
        println("import antlr.NoViableAltException;");
        println("import antlr.MismatchedTokenException;");
        println("import antlr.SemanticException;");
        println("import antlr.collections.impl.BitSet;");
        println("import antlr.ASTPair;");
        println("import antlr.collections.impl.ASTArray;");

        // Output the user-defined parser premamble
        println(grammar.preambleAction.getText());

        // Generate parser class definition
        String sup = null;
        if (grammar.superClass != null) {
            sup = grammar.superClass;
        }
        else {
            sup = "antlr." + grammar.getSuperClass();
        }
        println("");

        // print javadoc comment if any
        if (grammar.comment != null) {
            _println(grammar.comment);
        }

            // get prefix (replaces "public" and lets user specify)
            String prefix = "public";
            Token tprefix = (Token)grammar.options.get("classHeaderPrefix");
            if (tprefix != null) {
                  String p = StringUtils.stripFrontBack(tprefix.getText(), "\"", "\"");
                  if (p != null) {
                        prefix = p;
                  }
            }

            print(prefix+" ");
            print("class " + grammar.getClassName() + " extends " + sup);
        println("       implements " + grammar.tokenManager.getName() + TokenTypesFileSuffix);
        Token tsuffix = (Token)grammar.options.get("classHeaderSuffix");
        if (tsuffix != null) {
            String suffix = StringUtils.stripFrontBack(tsuffix.getText(), "\"", "\"");
            if (suffix != null) {
                print(", " + suffix);     // must be an interface name for Java
            }
        }
        println(" {");

        // Generate user-defined parser class members
        print(
            processActionForSpecialSymbols(grammar.classMemberAction.getText(), grammar.classMemberAction.getLine(), currentRule, null)
        );

        // Generate default parser class constructor
        println("public " + grammar.getClassName() + "() {");
        tabs++;
        println("tokenNames = _tokenNames;");
        tabs--;
        println("}");
        println("");

        // Generate code for each rule in the grammar
        Enumeration ids = grammar.rules.elements();
        int ruleNum = 0;
        String ruleNameInits = "";
        while (ids.hasMoreElements()) {
            GrammarSymbol sym = (GrammarSymbol)ids.nextElement();
            if (sym instanceof RuleSymbol) {
                RuleSymbol rs = (RuleSymbol)sym;
                genRule(rs, rs.references.size() == 0, ruleNum++);
            }
            exitIfError();
        }

        // Generate the token names
        genTokenStrings();

        // Generate the bitsets used throughout the grammar
        genBitsets(bitsetsUsed, grammar.tokenManager.maxTokenType());

        // Close class definition
        println("}");
        println("");

        // Close the parser output stream
        currentOutput.close();
        currentOutput = null;
    }


Generated by  Doxygen 1.6.0   Back to index