⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codegenerator.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @param atom The string-literal reference to generate
     */
    public abstract void gen(StringLiteralElement atom);

    /** Generate code for the given grammar element.
     * @param r The token-range reference to generate
     */
    public abstract void gen(TokenRangeElement r);

    /** Generate code for the given grammar element.
     * @param atom The token-reference to generate
     */
    public abstract void gen(TokenRefElement atom);

    /** Generate code for the given grammar element.
     * @param blk The tree to generate code for.
     */
    public abstract void gen(TreeElement t);

    /** Generate the code for a parser */
    public abstract void gen(TreeWalkerGrammar g) throws IOException;

    /** Generate code for the given grammar element.
     * @param wc The wildcard element to generate
     */
    public abstract void gen(WildcardElement wc);

    /** Generate code for the given grammar element.
     * @param blk The (...)* block to generate
     */
    public abstract void gen(ZeroOrMoreBlock blk);

    /** Generate the token types as a text file for persistence across shared lexer/parser */
    protected void genTokenInterchange(TokenManager tm) throws IOException {
	// Open the token output Java file and set the currentOutput stream
	String fName = tm.getName() + TokenTypesFileSuffix+TokenTypesFileExt;
	currentOutput = antlr_oaa.Tool.openOutputFile(fName);

	println("// $ANTLR "+Tool.version+": "+
		Tool.fileMinusPath(tool.grammarFile)+
		" -> "+
		fName+
		"$");

	tabs = 0;

	// Header
	println(tm.getName() + "    // output token vocab name");

	// Generate a definition for each token type
	Vector v = tm.getVocabulary();
	for (int i = Token.MIN_USER_TYPE; i < v.size(); i++) {
	    String s = (String)v.elementAt(i);
	    if ( DEBUG_CODE_GENERATOR ) {
		System.out.println("gen persistence file entry for: "+s);
	    }
	    if (s != null && !s.startsWith("<") ) {
				// if literal, find label
		if ( s.startsWith("\"") ) {
		    StringLiteralSymbol sl = (StringLiteralSymbol)tm.getTokenSymbol(s);
		    if ( sl!=null && sl.label != null ) {
			print(sl.label+"=");
		    }	
		    println(s + "=" + i);
		}
		else {
		    print(s);
		    // check for a paraphrase
		    TokenSymbol ts = (TokenSymbol)tm.getTokenSymbol(s);
		    if ( ts==null ) {
			tool.warning("undefined token symbol: "+s);
		    }
		    else {
			if ( ts.getParaphrase()!=null ) {
			    print("("+ts.getParaphrase()+")");
			}
		    }	
		    println("=" + i);
		}		
	    }
	}

	// Close the tokens output file
	currentOutput.close();
	currentOutput = null;
    }

	/** Process a string for an simple expression for use in xx/action.g
	 * it is used to cast simple tokens/references to the right type for
	 * the generated language.
	 * @param str A String.
	 */
	public String processStringForASTConstructor( String str )
	{
		return str;
	}
    /** Get a string for an expression to generate creation of an AST subtree.
     * @param v A Vector of String, where each element is an expression in the target language yielding an AST node.
     */
    public abstract String getASTCreateString(Vector v);

    /** Get a string for an expression to generate creating of an AST node
     * @param str The text of the arguments to the AST construction
     */
    public abstract String getASTCreateString(GrammarAtom atom, String str);

    /** Given the index of a bitset in the bitset list, generate a unique name.
     * Specific code-generators may want to override this
     * if the language does not allow '_' or numerals in identifiers.
     * @param index  The index of the bitset in the bitset list.
     */
    protected String getBitsetName(int index)
    {
	return "_tokenSet_" + index;
    }

    public static String lexerRuleName(String id) {
	return "m"+id;
    }

    /** Map an identifier to it's corresponding tree-node variable.
     * This is context-sensitive, depending on the rule and alternative
     * being generated
     * @param id The identifier name to map
     * @param forInput true if the input tree node variable is to be returned, otherwise the output variable is returned.
     * @return The mapped id (which may be the same as the input), or null if the mapping is invalid due to duplicates
     */
    public abstract String mapTreeId(String id, ActionTransInfo tInfo);

    /** Add a bitset to the list of bitsets to be generated.
     * if the bitset is already in the list, ignore the request.
     * Always adds the bitset to the end of the list, so the
     * caller can rely on the position of bitsets in the list.
     * The returned position can be used to format the bitset 
     * name, since it is invariant.
     * @param p Bit set to mark for code generation
     * @param forParser true if the bitset is used for the parser, false for the lexer
     * @return The position of the bitset in the list.
     */
    protected int markBitsetForGen(BitSet p) {
	// Is the bitset (or an identical one) already marked for gen?
	for (int i = 0; i < bitsetsUsed.size(); i++)
	    {
		BitSet set = (BitSet)bitsetsUsed.elementAt(i);
		if (p.equals(set))
		    {
				// Use the identical one already stored
			return i;
		    }
	    }

	// Add the new bitset
	bitsetsUsed.appendElement(p.clone());
	return bitsetsUsed.size()-1;
    }

    /** Output tab indent followed by a String, to the currentOutput stream.
     * Ignored if string is null.
     * @param s The string to output.  
     */
    protected void print(String s) {
	if (s != null) {
	    printTabs();
	    currentOutput.print(s);
	}
    }

    /** Print an action with leading tabs, attempting to
     * preserve the current indentation level for multi-line actions
     * Ignored if string is null.
     * @param s The action string to output
     */
    protected void printAction(String s) { 
	if (s != null) {
	    printTabs();
	    _printAction(s);
	}
    }

    /** Output tab indent followed by a String followed by newline,
     * to the currentOutput stream.  Ignored if string is null.
     * @param s The string to output
     */
    protected void println(String s) {
	if (s != null) {
	    printTabs();
	    currentOutput.println(s);
	}
    }

    /** Output the current tab indentation.  This outputs the number of tabs 
     * indicated by the "tabs" variable to the currentOutput stream.
     */
    protected void printTabs() {
	for (int i=1; i<=tabs; i++) {
	    currentOutput.print("\t");
	}
    }

    /** Lexically process tree-specifiers in the action.
     *  This will replace #id and #(...) with the appropriate
     *  function calls and/or variables.
     * 
     *  This is the default Java action translator, but I have made
     *  it work for C++ also.  
     */
    protected String processActionForTreeSpecifiers(String actionStr, int line, RuleBlock currentRule, ActionTransInfo tInfo) {
	if ( actionStr==null || actionStr.length()==0 ) return null;
	// The action trans info tells us (at the moment) whether an
	// assignment was done to the rule's tree root.
	if (grammar==null) return actionStr;
	if ( (grammar.buildAST && actionStr.indexOf('#') != -1) ||
	     grammar instanceof TreeWalkerGrammar ||
	     (grammar instanceof LexerGrammar && actionStr.indexOf('$') != -1) ) {
	    // Create a lexer to read an action and return the translated version
	    antlr_oaa.actions.java.ActionLexer lexer = new antlr_oaa.actions.java.ActionLexer(actionStr, currentRule, this, tInfo);
	    lexer.setLineOffset(line);
	    lexer.setTool(tool);
	    try {
		lexer.mACTION(true);
		actionStr = lexer.getTokenObject().getText();
				// System.out.println("action translated: "+actionStr);
				// System.out.println("trans info is "+tInfo);
	    }
	    catch (RecognitionException ex) {
		lexer.reportError(ex);
		return actionStr;
	    }
	    catch (TokenStreamException tex) {
		antlr_oaa.Tool.panic("Error reading action:"+actionStr);
		return actionStr;
	    }
	    catch (CharStreamException io) {
		antlr_oaa.Tool.panic("Error reading action:"+actionStr);
		return actionStr;
	    }
	}
	return actionStr;
    }

    /**
     * Remove the assignment portion of a declaration, if any.
     * @param d the declaration
     * @return the declaration without any assignment portion
     */
    protected String removeAssignmentFromDeclaration(String d) {
	// If d contains an equal sign, then it's a declaration
	// with an initialization.  Strip off the initialization part.
	if (d.indexOf('=') >= 0) d = d.substring(0, d.indexOf('=')).trim();
	return d;
    } 

    /** Set all fields back like one just created */
    private void reset() {
	tabs = 0;
	// Allocate list of bitsets tagged for code generation
	bitsetsUsed = new Vector();
	currentOutput = null;
	grammar = null;
	DEBUG_CODE_GENERATOR = false;
	makeSwitchThreshold = DEFAULT_MAKE_SWITCH_THRESHOLD;
	bitsetTestThreshold = DEFAULT_BITSET_TEST_THRESHOLD;
    }

    public static String reverseLexerRuleName(String id) {
	return id.substring(1,id.length());
    }

    public void setAnalyzer(LLkGrammarAnalyzer analyzer_) {
	analyzer = analyzer_;
    }

    public void setBehavior(DefineGrammarSymbols behavior_) { 
	behavior = behavior_;
    }

    /** Set a grammar for the code generator to use */
    protected void setGrammar(Grammar g) {
	reset();
	grammar = g;
	// Lookup make-switch threshold in the grammar generic options
	if (grammar.hasOption("codeGenMakeSwitchThreshold")) {
	    try {
		makeSwitchThreshold = grammar.getIntegerOption("codeGenMakeSwitchThreshold");
				//System.out.println("setting codeGenMakeSwitchThreshold to " + makeSwitchThreshold);
	    } catch (NumberFormatException e) {
		tool.error(
			   "option 'codeGenMakeSwitchThreshold' must be an integer",
			   grammar.getClassName(),
			   grammar.getOption("codeGenMakeSwitchThreshold").getLine()
			   );
	    }
	}
		
	// Lookup bitset-test threshold in the grammar generic options
	if (grammar.hasOption("codeGenBitsetTestThreshold")) {
	    try {
		bitsetTestThreshold = grammar.getIntegerOption("codeGenBitsetTestThreshold");
				//System.out.println("setting codeGenBitsetTestThreshold to " + bitsetTestThreshold);
	    } catch (NumberFormatException e) {
		tool.error(
			   "option 'codeGenBitsetTestThreshold' must be an integer",
			   grammar.getClassName(),
			   grammar.getOption("codeGenBitsetTestThreshold").getLine()
			   );
	    }
	}
		
	// Lookup debug code-gen in the grammar generic options
	if (grammar.hasOption("codeGenDebug")) {
	    Token t = grammar.getOption("codeGenDebug");
	    if (t.getText().equals("true")) {
				//System.out.println("setting code-generation debug ON");
		DEBUG_CODE_GENERATOR = true;
	    }
	    else if (t.getText().equals("false")) {
				//System.out.println("setting code-generation debug OFF");
		DEBUG_CODE_GENERATOR = false;
	    }
	    else {
		tool.error("option 'codeGenDebug' must be true or false", grammar.getClassName(), t.getLine());
	    }
	}
    }
    public void setTool(Tool tool_) {
	tool = tool_;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -