All Downloads are FREE. Search and download functionalities are using the official Maven repository.

msv.tahiti.src.com.sun.tahiti.compiler.ll.RuleSerializer Maven / Gradle / Ivy

The newest version!
/*
 * @(#)$Id: RuleSerializer.java 1256 2001-10-23 18:27:03Z Bear $
 *
 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * This software is the proprietary information of Sun Microsystems, Inc.  
 * Use is subject to license terms.
 * 
 */
package com.sun.tahiti.compiler.ll;

import com.sun.msv.datatype.xsd.XSDatatype;
import com.sun.msv.grammar.*;
import com.sun.msv.grammar.util.ExpressionWalker;
import com.sun.tahiti.compiler.XMLWriter;
import com.sun.tahiti.compiler.Symbolizer;
import com.sun.tahiti.grammar.*;
import com.sun.tahiti.reader.TypeUtil;
import com.sun.tahiti.reader.NameUtil;
import org.relaxng.datatype.Datatype;
import org.xml.sax.DocumentHandler;
import org.xml.sax.SAXException;
import java.io.PrintStream;
import java.util.Map;
import java.util.Set;
import java.util.Iterator;
import java.text.MessageFormat;

/**
 * serializes a LL grammar into the XML representation.
 * 
 * @author
 *	Kohsuke KAWAGUCHI
 */
public class RuleSerializer implements Symbolizer {
	
	private RuleSerializer() {}
	
	/**
	 * 
	 * @param grammar
	 *		Grammar object to be generated as a Java source code.
	 * @param rules
	 *		production rules of the grammar. Actual type must be
	 *		non-terminal -> Rule[].
	 * @param grammarClassName
	 *		the fully-qualified name of the class that is going to be generated.
	 * @param outHandler
	 *		Generated source code will be sent to this handler.
	 * 
	 * @return
	 *		return a symbolizer that is necessary to serialize class definitions.
	 */
	public static Symbolizer serialize( AnnotatedGrammar grammar, Rules rules, DocumentHandler outHandler ) throws SAXException {
		RuleSerializer gen = new RuleSerializer();
		gen._serialize( grammar, rules, outHandler );
		return gen;
	}
	
	
	/**
	 * this map serves as a dictionary to lookup the name of the given symbol.
	 */
	final Map allNames = new java.util.HashMap();
	public String getId( Object symbol ) {
		if(symbol==null)	return "null";
		String s = (String)allNames.get(symbol);
		
		if(s==null) {
			System.out.println(symbol);
			assert(false);
		}
		return s;
	}
	
	private void _serialize( AnnotatedGrammar grammar, Rules rules, DocumentHandler outHandler ) throws SAXException {
		
		// add pre-defined special symbols.
		allNames.put( Expression.epsilon, "epsilon" );
		
		try {
			final XMLWriter out = new XMLWriter(outHandler);
			outHandler.setDocumentLocator(new org.xml.sax.helpers.LocatorImpl());
			outHandler.startDocument();
			outHandler.processingInstruction("xml-stylesheet","type='text/xsl' href='../grammarDebug.xslt'");
			out.start("grammar");
			
			{
				int idx = grammar.grammarName.lastIndexOf('.');
				if(idx<0) {
					out.element("name", grammar.grammarName);
				} else {
					out.element("package", grammar.grammarName.substring(0,idx));
					out.element("name", grammar.grammarName.substring(idx+1));
				}
			}
			
		// collect various primitives (a map to its name)
		//====================================================================
			final Map elements = new java.util.HashMap();	// ElementExps
			final Map attributes = new java.util.HashMap();	// AttributeExps
			final Map datatypes = new java.util.HashMap();	// Datatypes
			final Map classes = new java.util.HashMap();	// ClassItems
			final Map fields = new java.util.HashMap();		// FieldItems
			final Map primitives = new java.util.HashMap();	// PrimitiveItems
			final Map ignores = new java.util.HashMap();	// IgnoreItems
			
			grammar.getTopLevel().visit( new ExpressionWalker(){
				public void onElement( ElementExp exp ) {
					if(!elements.containsKey(exp)) {
						elements.put(exp,computeName(exp.getNameClass(),elements));
						super.onElement(exp);
					}
				}
				public void onAttribute( AttributeExp exp ) {
					if(!attributes.containsKey(exp)) {
						attributes.put(exp,computeName(exp.nameClass,attributes));
						super.onAttribute(exp);
					}
				}
				public void onData( DataExp exp ) {
					if(!datatypes.containsKey(exp)) {
						datatypes.put(exp,computeName(exp.dt,datatypes));
						super.onData(exp);
					}
				}
				public void onValue( ValueExp exp ) {
					if(!datatypes.containsKey(exp)) {
						datatypes.put(exp,computeName(exp.dt,datatypes));
						super.onValue(exp);
					}
				}
				public void onOther( OtherExp exp ) {
					if(exp instanceof ClassItem) {
						if(!classes.containsKey(exp)) {
							classes.put(exp,computeName((ClassItem)exp,classes));
							super.onOther(exp);
						}
						return;
					}
					if(exp instanceof PrimitiveItem) {
						if(!primitives.containsKey(exp)) {
							primitives.put(exp,computeName((PrimitiveItem)exp,primitives));
							super.onOther(exp);
						}
						return;
					}
					if(exp instanceof FieldItem) {
						if(!fields.containsKey(exp)) {
							fields.put(exp,computeName((FieldItem)exp,fields));
							super.onOther(exp);
						}
						return;
					}
					if(exp instanceof IgnoreItem) {
						if(!ignores.containsKey(exp)) {
							ignores.put(exp,computeName((IgnoreItem)exp,ignores));
							super.onOther(exp);
						}
						return;
					}
					
					super.onOther(exp);
				}
			});
			
		// assign names to intermediate non-terminals.
		//====================================================================
			
			
			copyAll( elements, "E", allNames );
			copyAll( attributes, "A", allNames );
			copyAll( datatypes, "D", allNames );
			copyAll( classes, "C", allNames );
			copyAll( fields, "N", allNames );
			copyAll( primitives, "P", allNames );
			copyAll( ignores, "Ignore", allNames );
			
			
			final ElementExp[] elms = (ElementExp[])elements.keySet().toArray(new ElementExp[0]);
			final AttributeExp[] atts = (AttributeExp[])attributes.keySet().toArray(new AttributeExp[0]);
			final DataOrValueExp[] dts = (DataOrValueExp[])datatypes.keySet().toArray(new DataOrValueExp[0]);
			final ClassItem[] cis = (ClassItem[])classes.keySet().toArray(new ClassItem[0]);
			final FieldItem[] fis = (FieldItem[])fields.keySet().toArray(new FieldItem[0]);
			final PrimitiveItem[] pis = (PrimitiveItem[])primitives.keySet().toArray(new PrimitiveItem[0]);
			final IgnoreItem[] iis = (IgnoreItem[])ignores.keySet().toArray(new IgnoreItem[0]);
			
			for( int i=0; i rule relationship
				out.start("rulesList");
				Iterator itr = rules.iterateKeys();
				while(itr.hasNext()) {
					Expression symbol = (Expression)itr.next();
					out.start("nonTerminal",
						new String[]{"id",getId(symbol)});
					Rule[] rs = rules.getAll(symbol);
					for( int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy