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

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

/*
 * @(#)$Id: Rule.java 1087 2001-08-18 01:37:50Z 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.grammar.Expression;
import com.sun.tahiti.compiler.XMLWriter;
import com.sun.tahiti.compiler.Symbolizer;
import java.util.Iterator;
import java.util.Set;

/**
 * a production rule of context-free grammar (CFG).
 * 
 * Rule object is unified.
 * 
 * @author
 *	Kohsuke KAWAGUCHI
 */
public final class Rule {
	
	/**
	 * right-hand side of the rules.
	 * 
	 * Expression.epsilon is used to denote A -> $epsilon.
	 * TypedStringExp is used instead of Datatype.
	 */
	public Expression[]		right;
	
	public boolean			isInterleave;
	
	public Rule( Expression[] right, boolean isInterleave ) {
		this.right = right;
		this.isInterleave = isInterleave;
		
		assert(right!=null);
		for( int i=0; i B), then we can change the interleaveness of the rule.
		if(replace.right.length==1)
			interleave = this.isInterleave;
		else
		if(this.right.length==1)
			interleave = replace.isInterleave;
		else
		if(this.isInterleave!=replace.isInterleave)
			// if one is an interleaving rule and the other is a normal rule,
			// then we can't rewrite rules.
			return false;
		else
			interleave = this.isInterleave;
		
		int rlen = replace.right.length;
		if(rlen==1 && replace.right[0]==Expression.epsilon)
			rlen=0;	// coerce epsilon
			
		Expression[] seq = new Expression[right.length+rlen-1];
			
		if(seq.length==0)
			seq = new Expression[]{Expression.epsilon};
		else {
			System.arraycopy( right, 0, seq, 0, idx );
			System.arraycopy( replace.right, 0, seq, idx, rlen );
			System.arraycopy( right, idx+1, seq, idx+rlen, right.length-(idx+1) );
		}
			
		this.right = seq;
		this.isInterleave = interleave;
		return true;
	}
	
	/**
	 * produces the string representation of this rule.
	 * Mainly for debugging.
	 */
	public String toString( Symbolizer symbolizr ) {
		StringBuffer buf = new StringBuffer();
		buf.append( " --> ");
		if(isInterleave)
			buf.append("(interleave) ");
		for( int i=0; i, we have to spit the filter definition.
					// But we don't need a filter for the first one symbol.
					writer.start("filter");
					Iterator itr = FilterCalculator.calc(right[i]).iterator();
					while(itr.hasNext())
						writer.element("item",new String[]{"symbolRef",symbolizer.getId(itr.next())});
					writer.end("filter");
				}
				writer.end("item");
			}
		
		writer.end("right");
		
		writer.end("rule");
	}
	
	private static void assert( boolean b ) {
		if(!b)	throw new Error();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy