htmlcodegenerator.java

来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 781 行 · 第 1/2 页

JAVA
781
字号
package antlr_oaa;

/* ANTLR Translator Generator
 * Project led by Terence Parr at http://www.jGuru.com
 * Software rights: http://www.antlr.org/RIGHTS.html
 *
 * $Id: HTMLCodeGenerator.java,v 1.1 2002/11/08 17:38:00 agno Exp $
 */

import java.util.Enumeration;
import antlr_oaa.collections.impl.BitSet;
import antlr_oaa.collections.impl.Vector;
import java.io.PrintWriter; //SAS: changed for proper text file io
import java.io.IOException;
import java.io.FileWriter;

/**Generate P.html, a cross-linked representation of P with or without actions */
public class HTMLCodeGenerator extends CodeGenerator {
	/** non-zero if inside syntactic predicate generation */
	protected int syntacticPredLevel = 0;

	/** true during lexer generation, false during parser generation */
	protected boolean doingLexRules = false;

	protected boolean firstElementInAlt;

	protected AlternativeElement prevAltElem = null;	// what was generated last?

	/** Create a Diagnostic code-generator using the given Grammar
	 * The caller must still call setTool, setBehavior, and setAnalyzer
	 * before generating code.
	 */
	public HTMLCodeGenerator() {
		super();
		charFormatter = new JavaCharFormatter();
	}
	public void gen() {

		// Do the code generation
		try {
			// Loop over all grammars
			Enumeration grammarIter = behavior.grammars.elements();
			while (grammarIter.hasMoreElements()) {
				Grammar g = (Grammar)grammarIter.nextElement();

				// Connect all the components to each other
				/*
				g.setGrammarAnalyzer(analyzer);
				analyzer.setGrammar(g);
				*/
				g.setCodeGenerator(this);

				// To get right overloading behavior across hetrogeneous grammars
				g.generate();
	
				if (tool.hasError) {
					System.out.println("Exiting due to errors.");
					System.exit(1);
				}

			}

		}
		catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}
	/** Generate code for the given grammar element.
	 * @param blk The {...} action to generate
	 */
	public void gen(ActionElement action) {
		/*
		_print("{");
		_printAction(action.actionText);
		_print("} ");
		*/
	}
/** Generate code for the given grammar element.
 * @param blk The "x|y|z|..." block to generate
 */
public void gen(AlternativeBlock blk) {
	genGenericBlock(blk, "");
}
	/** Generate code for the given grammar element.
	 * @param blk The block-end element to generate.  Block-end
	 * elements are synthesized by the grammar parser to represent
	 * the end of a block.
	 */
	public void gen(BlockEndElement end) {
		// no-op
	}
	/** Generate code for the given grammar element.
	 * @param blk The character literal reference to generate
	 */
	public void gen(CharLiteralElement atom) {
		/*
		if (atom.label != null) {
			_print(atom.label+"=");
		}
		*/
		if (atom.not) {
			_print("~");
		}
		_print(atom.atomText+" ");
	}
	/** Generate code for the given grammar element.
	 * @param blk The character-range reference to generate
	 */
	public void gen(CharRangeElement r) {
		/*
		if ( r.label!=null ) {
			_print(r.label+"=");
		}
		*/
		print(r.beginText + ".." + r.endText+" ");
	}
	/** Generate the lexer TXT file */
	public void gen(LexerGrammar g) throws IOException {
		setGrammar(g);
		System.out.println("Generating " + grammar.getClassName() + TokenTypesFileExt);
		currentOutput = antlr_oaa.Tool.openOutputFile(grammar.getClassName() + TokenTypesFileExt);
		//SAS: changed for proper text file io
		
		tabs=0;
		doingLexRules = true;

		// Generate header common to all TXT output files
		genHeader();

		/*
		// Output the user-defined lexer premamble
		println(grammar.preambleAction.getText());
		*/
		
		// Generate lexer class definition
		println("");

		// print javadoc comment if any
		if ( grammar.comment!=null ) {
			_println(grammar.comment);
		}
		
		println("class " + grammar.getClassName() + " extends " + grammar.getSuperClass() + " {");

		// Generate user-defined parser class members
		// printAction(grammar.classMemberAction.getText());

		/*
		// Generate string literals
		println("");
		println("*** String literals used in the parser");
		println("The following string literals were used in the parser.");
		println("An actual code generator would arrange to place these literals");
		println("into a table in the generated lexer, so that actions in the");
		println("generated lexer could match token text against the literals.");
		println("String literals used in the lexer are not listed here, as they");
		println("are incorporated into the mainstream lexer processing.");
		tabs++;
		// Enumerate all of the symbols and look for string literal symbols
		Enumeration ids = grammar.getSymbols();
		while ( ids.hasMoreElements() ) {
			GrammarSymbol sym = (GrammarSymbol)ids.nextElement();
			// Only processing string literals -- reject other symbol entries
			if ( sym instanceof StringLiteralSymbol ) {
				StringLiteralSymbol s = (StringLiteralSymbol)sym;
				println(s.getId() + " = " + s.getTokenType());
			}
		}
		tabs--;
		println("*** End of string literals used by the parser");
		*/
		
		// 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
		
		Enumeration ids = grammar.rules.elements();
		while ( ids.hasMoreElements() ) {
			RuleSymbol rs = (RuleSymbol)ids.nextElement();
			if (!rs.id.equals("mnextToken")) {
				genRule(rs);
			}
		}

		// Close the lexer output file
		currentOutput.close();
		currentOutput = null;
		doingLexRules = false;
	}
/** Generate code for the given grammar element.
 * @param blk The (...)+ block to generate
 */
public void gen(OneOrMoreBlock blk) {
	genGenericBlock(blk, "+");
}
	/** Generate the parser TXT file */
	public void gen(ParserGrammar g) throws IOException {
		setGrammar(g);
		// Open the output stream for the parser and set the currentOutput
		System.out.println("Generating " + grammar.getClassName() + ".html");
		currentOutput = antlr_oaa.Tool.openOutputFile(grammar.getClassName()+".html");
		
		tabs = 0;

		// Generate the header common to all output files.
		genHeader();
		
		/*
		_print("{");
		printAction(grammar.preambleAction.getText());
		_print("}");
		*/

		// Generate parser class definition
		println("");
		
		// print javadoc comment if any
		if ( grammar.comment!=null ) {
			_println(grammar.comment);
		}
		
		println("class " + grammar.getClassName() + " extends " + grammar.getSuperClass());

		/*
		// Generate user-defined parser class members
		println("");
		_print("{");
		printAction(grammar.classMemberAction.getText());
		_print("}");
		*/

		// Enumerate the parser rules
		Enumeration rules = grammar.rules.elements();
		while ( rules.hasMoreElements() ) {
			println("");
			// Get the rules from the list and downcast it to proper type
			GrammarSymbol sym = (GrammarSymbol) rules.nextElement();
			// Only process parser rules
			if ( sym instanceof RuleSymbol) {
				genRule((RuleSymbol)sym);
			}
		}
		tabs--;
		println("");

		genTail();
		
		// Close the parser output stream
		currentOutput.close();
		currentOutput = null;
	}
	/** Generate code for the given grammar element.
	 * @param blk The rule-reference to generate
	 */
	public void gen(RuleRefElement rr) {
		RuleSymbol rs = (RuleSymbol)grammar.getSymbol(rr.targetRule);

		// Generate the actual rule description
		/*
		if ( rr.idAssign!=null ) {
			_print(rr.idAssign+"=");
		}
		*/
		_print("<a href="+grammar.getClassName() + ".html#"+rr.targetRule+">");
		_print(rr.targetRule);
		_print("</a>");
		if (rr.args != null) {
			_print("["+rr.args+"]");
		}
		_print(" ");
	}
	/** Generate code for the given grammar element.
	 * @param blk The string-literal reference to generate
	 */
	public void gen(StringLiteralElement atom) {
		/*
		if (atom.label != null) {
			_print(atom.label+"=");
		}
		*/
		if (atom.not) {
			_print("~");
		}
		_print(atom.atomText);
		_print(" ");
	}
	/** Generate code for the given grammar element.
	 * @param blk The token-range reference to generate
	 */
	public void gen(TokenRangeElement r) {
		/*
		if ( r.label!=null ) {
			_print(r.label+"=");
		}
		*/
		print(r.beginText + ".." + r.endText+" ");
	}
	/** Generate code for the given grammar element.
	 * @param blk The token-reference to generate
	 */
	public void gen(TokenRefElement atom) {
		/*
		if (atom.label != null) {
			_print(atom.label+"=");
		}
		*/
		if (atom.not) {
			_print("~");
		}
		_print(atom.atomText);
		_print(" ");
	}
	public void gen(TreeElement t) {
		print(t+" ");
	}
	/** Generate the tree-walker TXT file */
	public  void gen(TreeWalkerGrammar g) throws IOException {
		setGrammar(g);
		// Open the output stream for the parser and set the currentOutput
		System.out.println("Generating " + grammar.getClassName() + ".txt");
		currentOutput = antlr_oaa.Tool.openOutputFile(grammar.getClassName()+".txt");
		//SAS: changed for proper text file io
		
		tabs = 0;

		// Generate the header common to all output files.
		genHeader();
		
		// Output the user-defined parser premamble
		println("");
		println("*** Tree-walker Preamble Action.");
		println("This action will appear before the declaration of your tree-walker class:");
		tabs++;
		println(grammar.preambleAction.getText());
		tabs--;
		println("*** End of tree-walker Preamble Action");

		// Generate tree-walker class definition
		println("");
		
		// print javadoc comment if any
		if ( grammar.comment!=null ) {
			_println(grammar.comment);
		}
		
		println("class " + grammar.getClassName() + " extends " + grammar.getSuperClass() + "{");

		// Generate user-defined tree-walker class members
		println("");
		println("*** User-defined tree-walker class members:");
		println("These are the member declarations that you defined for your class:");
		tabs++;
		printAction(grammar.classMemberAction.getText());
		tabs--;
		println("*** End of user-defined tree-walker class members");

		// Generate code for each rule in the grammar
		println("");
		println("*** tree-walker rules:");
		tabs++;

		// Enumerate the tree-walker rules
		Enumeration rules = grammar.rules.elements();
		while ( rules.hasMoreElements() ) {
			println("");
			// Get the rules from the list and downcast it to proper type
			GrammarSymbol sym = (GrammarSymbol) rules.nextElement();
			// Only process tree-walker rules
			if ( sym instanceof RuleSymbol) {
				genRule((RuleSymbol)sym);
			}
		}
		tabs--;
		println("");
		println("*** End of tree-walker rules");

		println("");
		println("*** End of tree-walker");

		// Close the tree-walker output stream
		currentOutput.close();
		currentOutput = null;
	}
	/** Generate a wildcard element */
	public void gen(WildcardElement wc) {
		/*
		if ( wc.getLabel()!=null ) {
			_print(wc.getLabel()+"=");

⌨️ 快捷键说明

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