Logo Search packages:      
Sourcecode: antlr version File versions

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

Generate the parser Java file

Implements antlr::CodeGenerator.

Definition at line 564 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, antlr::Grammar::debuggingOutput, genBitsets(), genHeader(), genRule(), genSemPredMap(), genTokenASTNodeMap(), genTokenStrings(), antlr::Grammar::getClassName(), antlr::DefineGrammarSymbols::getHeaderAction(), antlr::Token::getLine(), antlr::TokenManager::getName(), antlr::Grammar::getSuperClass(), antlr::Token::getText(), antlr::CodeGenerator::grammar, antlr::Grammar::maxk, 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.

                                                        {

        // if debugging, set up a new vector to keep track of sempred
        //   strings for this grammar
        if (g.debuggingOutput)
            semPreds = new Vector();

        setGrammar(g);
        if (!(grammar instanceof ParserGrammar)) {
            antlrTool.panic("Internal error generating parser");
        }

        // Open the output stream for the parser and set the currentOutput
        // SAS: moved file setup so subclass could do it (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.TokenBuffer;");
        println("import antlr.TokenStreamException;");
        println("import antlr.TokenStreamIOException;");
        println("import antlr.ANTLRException;");
        println("import antlr." + grammar.getSuperClass() + ";");
        println("import antlr.Token;");
        println("import antlr.TokenStream;");
        println("import antlr.RecognitionException;");
        println("import antlr.NoViableAltException;");
        println("import antlr.MismatchedTokenException;");
        println("import antlr.SemanticException;");
        println("import antlr.ParserSharedInputState;");
        println("import antlr.collections.impl.BitSet;");
        if ( genAST ) {
                  println("import antlr.collections.AST;");
                  println("import java.util.Hashtable;");
                  println("import antlr.ASTFactory;");
            println("import antlr.ASTPair;");
            println("import antlr.collections.impl.ASTArray;");
        }

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

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

        // 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(" {");

        // set up an array of all the rule names so the debugger can
        // keep track of them only by number -- less to store in tree...
        if (grammar.debuggingOutput) {
            println("private static final String _ruleNames[] = {");

            Enumeration ids = grammar.rules.elements();
            int ruleNum = 0;
            while (ids.hasMoreElements()) {
                GrammarSymbol sym = (GrammarSymbol)ids.nextElement();
                if (sym instanceof RuleSymbol)
                    println("  \"" + ((RuleSymbol)sym).getId() + "\",");
            }
            println("};");
        }

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

        // Generate parser class constructor from TokenBuffer
        println("");
        println("protected " + grammar.getClassName() + "(TokenBuffer tokenBuf, int k) {");
        println("  super(tokenBuf,k);");
        println("  tokenNames = _tokenNames;");
        // if debugging, set up arrays and call the user-overridable
        //   debugging setup method
        if (grammar.debuggingOutput) {
            println("  ruleNames  = _ruleNames;");
            println("  semPredNames = _semPredNames;");
            println("  setupDebugging(tokenBuf);");
        }
            if ( grammar.buildAST ) {
                  println("  buildTokenTypeASTClassMap();");
                  println("  astFactory = new ASTFactory(getTokenTypeToASTClassMap());");
            }
        println("}");
        println("");

        println("public " + grammar.getClassName() + "(TokenBuffer tokenBuf) {");
        println("  this(tokenBuf," + grammar.maxk + ");");
        println("}");
        println("");

        // Generate parser class constructor from TokenStream
        println("protected " + grammar.getClassName() + "(TokenStream lexer, int k) {");
        println("  super(lexer,k);");
        println("  tokenNames = _tokenNames;");

        // if debugging, set up arrays and call the user-overridable
        //   debugging setup method
        if (grammar.debuggingOutput) {
            println("  ruleNames  = _ruleNames;");
            println("  semPredNames = _semPredNames;");
            println("  setupDebugging(lexer);");
        }
            if ( grammar.buildAST ) {
                  println("  buildTokenTypeASTClassMap();");
                  println("  astFactory = new ASTFactory(getTokenTypeToASTClassMap());");
            }
        println("}");
        println("");

        println("public " + grammar.getClassName() + "(TokenStream lexer) {");
        println("  this(lexer," + grammar.maxk + ");");
        println("}");
        println("");

        println("public " + grammar.getClassName() + "(ParserSharedInputState state) {");
        println("  super(state," + grammar.maxk + ");");
        println("  tokenNames = _tokenNames;");
            if ( grammar.buildAST ) {
                  println("  buildTokenTypeASTClassMap();");
                  println("  astFactory = new ASTFactory(getTokenTypeToASTClassMap());");
            }
        println("}");
        println("");

        // Generate code for each rule in the grammar
        Enumeration ids = grammar.rules.elements();
        int ruleNum = 0;
        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();

            if ( grammar.buildAST ) {
                  genTokenASTNodeMap();
            }

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

        // Generate the semantic predicate map for debugging
        if (grammar.debuggingOutput)
            genSemPredMap();

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

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


Generated by  Doxygen 1.6.0   Back to index