Logo Search packages:      
Sourcecode: antlr version File versions

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

Generate the lexer Java file

Implements antlr::CodeGenerator.

Definition at line 261 of file JavaCodeGenerator.java.

References antlr::CodeGenerator::_println(), antlr::CodeGenerator::behavior, antlr::CodeGenerator::bitsetsUsed, antlr::Grammar::classMemberAction, antlr::Grammar::comment, antlr::CodeGenerator::currentOutput, currentRule, antlr::Grammar::debuggingOutput, genBitsets(), genHeader(), genNextToken(), genRule(), genSemPredMap(), antlr::Grammar::getClassName(), antlr::DefineGrammarSymbols::getHeaderAction(), antlr::Token::getLine(), antlr::TokenManager::getName(), antlr::Grammar::getSuperClass(), antlr::Token::getText(), antlr::TokenManager::getTokenSymbol(), antlr::TokenManager::getTokenSymbolKeys(), antlr::CodeGenerator::grammar, 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, create a new sempred vector for this grammar
        if (g.debuggingOutput)
            semPreds = new Vector();

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

        // SAS: moved output creation to method so a subclass can change
        //      how the output is generated (for VAJ interface)
        setupOutput(grammar.getClassName());

        genAST = false; // no way to gen trees.
        saveText = true;      // save consumed characters.

        tabs = 0;

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

        // Generate header specific to lexer Java file
        // println("import java.io.FileInputStream;");
        println("import java.io.InputStream;");
        println("import antlr.TokenStreamException;");
        println("import antlr.TokenStreamIOException;");
        println("import antlr.TokenStreamRecognitionException;");
        println("import antlr.CharStreamException;");
        println("import antlr.CharStreamIOException;");
        println("import antlr.ANTLRException;");
        println("import java.io.Reader;");
        println("import java.util.Hashtable;");
        println("import antlr." + grammar.getSuperClass() + ";");
        println("import antlr.InputBuffer;");
        println("import antlr.ByteBuffer;");
        println("import antlr.CharBuffer;");
        println("import antlr.Token;");
        println("import antlr.CommonToken;");
        println("import antlr.RecognitionException;");
        println("import antlr.NoViableAltForCharException;");
        println("import antlr.MismatchedCharException;");
        println("import antlr.TokenStream;");
        println("import antlr.ANTLRHashString;");
        println("import antlr.LexerSharedInputState;");
        println("import antlr.collections.impl.BitSet;");
        println("import antlr.SemanticException;");

        // Generate user-defined lexer file preamble
        println(grammar.preambleAction.getText());

        // Generate lexer 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 + ", TokenStream");
            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 lexer class members
        print(
            processActionForSpecialSymbols(grammar.classMemberAction.getText(), grammar.classMemberAction.getLine(), currentRule, null)
        );

        //
        // Generate the constructor from InputStream, which in turn
        // calls the ByteBuffer constructor
        //
        println("public " + grammar.getClassName() + "(InputStream in) {");
        tabs++;
        println("this(new ByteBuffer(in));");
        tabs--;
        println("}");

        //
        // Generate the constructor from Reader, which in turn
        // calls the CharBuffer constructor
        //
        println("public " + grammar.getClassName() + "(Reader in) {");
        tabs++;
        println("this(new CharBuffer(in));");
        tabs--;
        println("}");

        println("public " + grammar.getClassName() + "(InputBuffer ib) {");
        tabs++;
        // if debugging, wrap the input buffer in a debugger
        if (grammar.debuggingOutput)
            println("this(new LexerSharedInputState(new antlr.debug.DebuggingInputBuffer(ib)));");
        else
            println("this(new LexerSharedInputState(ib));");
        tabs--;
        println("}");

        //
        // Generate the constructor from InputBuffer (char or byte)
        //
        println("public " + grammar.getClassName() + "(LexerSharedInputState state) {");
        tabs++;

        println("super(state);");
        // if debugging, set up array variables and call user-overridable
        //   debugging setup method
        if (grammar.debuggingOutput) {
            println("  ruleNames  = _ruleNames;");
            println("  semPredNames = _semPredNames;");
            println("  setupDebugging();");
        }

        // Generate the setting of various generated options.
        // These need to be before the literals since ANTLRHashString depends on
        // the casesensitive stuff.
        println("caseSensitiveLiterals = " + g.caseSensitiveLiterals + ";");
        println("setCaseSensitive(" + g.caseSensitive + ");");

        // Generate the initialization of a hashtable
        // containing the string literals used in the lexer
        // The literals variable itself is in CharScanner
        println("literals = new Hashtable();");
        Enumeration keys = grammar.tokenManager.getTokenSymbolKeys();
        while (keys.hasMoreElements()) {
            String key = (String)keys.nextElement();
            if (key.charAt(0) != '"') {
                continue;
            }
            TokenSymbol sym = grammar.tokenManager.getTokenSymbol(key);
            if (sym instanceof StringLiteralSymbol) {
                StringLiteralSymbol s = (StringLiteralSymbol)sym;
                println("literals.put(new ANTLRHashString(" + s.getId() + ", this), new Integer(" + s.getTokenType() + "));");
            }
        }
        tabs--;

        Enumeration ids;
        println("}");

        // generate the rule name array for debugging
        if (grammar.debuggingOutput) {
            println("private static final String _ruleNames[] = {");

            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 nextToken() rule.
        // nextToken() is a synthetic lexer rule that is the implicit OR of all
        // user-defined lexer rules.
        genNextToken();

        // Generate code for each rule in the lexer
        ids = grammar.rules.elements();
        int ruleNum = 0;
        while (ids.hasMoreElements()) {
            RuleSymbol sym = (RuleSymbol)ids.nextElement();
            // Don't generate the synthetic rules
            if (!sym.getId().equals("mnextToken")) {
                genRule(sym, false, ruleNum++);
            }
            exitIfError();
        }

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

        // Generate the bitsets used throughout the lexer
        genBitsets(bitsetsUsed, ((LexerGrammar)grammar).charVocabulary.size());

        println("");
        println("}");

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


Generated by  Doxygen 1.6.0   Back to index