FileDocCategorySizeDatePackage
ParserEventSupport.javaAPI DocGlassfish v2 API13920Wed Feb 08 12:31:22 GMT 2006persistence.antlr.debug

ParserEventSupport

public class ParserEventSupport extends Object
A class to assist in firing parser events NOTE: I intentionally _did_not_ synchronize the event firing and add/remove listener methods. This is because the add/remove should _only_ be called by the parser at its start/end, and the _same_thread_ should be performing the parsing. This should help performance a tad...

Fields Summary
private Object
source
private Hashtable
doneListeners
private Vector
matchListeners
private Vector
messageListeners
private Vector
tokenListeners
private Vector
traceListeners
private Vector
semPredListeners
private Vector
synPredListeners
private Vector
newLineListeners
private ParserMatchEvent
matchEvent
private MessageEvent
messageEvent
private ParserTokenEvent
tokenEvent
private SemanticPredicateEvent
semPredEvent
private SyntacticPredicateEvent
synPredEvent
private TraceEvent
traceEvent
private NewLineEvent
newLineEvent
private ParserController
controller
protected static final int
CONSUME
protected static final int
ENTER_RULE
protected static final int
EXIT_RULE
protected static final int
LA
protected static final int
MATCH
protected static final int
MATCH_NOT
protected static final int
MISMATCH
protected static final int
MISMATCH_NOT
protected static final int
REPORT_ERROR
protected static final int
REPORT_WARNING
protected static final int
SEMPRED
protected static final int
SYNPRED_FAILED
protected static final int
SYNPRED_STARTED
protected static final int
SYNPRED_SUCCEEDED
protected static final int
NEW_LINE
protected static final int
DONE_PARSING
private int
ruleDepth
Constructors Summary
public ParserEventSupport(Object source)



	   
		matchEvent   = new ParserMatchEvent(source);
		messageEvent = new MessageEvent(source);
		tokenEvent   = new ParserTokenEvent(source);
		traceEvent   = new TraceEvent(source);
		semPredEvent = new SemanticPredicateEvent(source);
		synPredEvent = new SyntacticPredicateEvent(source);
		newLineEvent = new NewLineEvent(source);
		this.source = source;
	
Methods Summary
public voidaddDoneListener(persistence.antlr.debug.ListenerBase l)

		if (doneListeners == null) doneListeners = new Hashtable();
		Integer i = (Integer)doneListeners.get(l);
		int val;
		if (i != null)
			val = i.intValue() + 1;
		else
			val = 1;
		doneListeners.put(l, new Integer(val));
	
public voidaddMessageListener(persistence.antlr.debug.MessageListener l)

		if (messageListeners == null) messageListeners = new Vector();
		messageListeners.addElement(l);
		addDoneListener(l);
	
public voidaddNewLineListener(persistence.antlr.debug.NewLineListener l)

		if (newLineListeners == null) newLineListeners = new Vector();
		newLineListeners.addElement(l);
		addDoneListener(l);
	
public voidaddParserListener(persistence.antlr.debug.ParserListener l)

		if (l instanceof ParserController) {
			((ParserController)l).setParserEventSupport(this);
			controller = (ParserController)l;
		}	
		addParserMatchListener(l);
		addParserTokenListener(l);

		addMessageListener(l);
		addTraceListener(l);
		addSemanticPredicateListener(l);
		addSyntacticPredicateListener(l);
	
public voidaddParserMatchListener(persistence.antlr.debug.ParserMatchListener l)

		if (matchListeners == null) matchListeners = new Vector();
		matchListeners.addElement(l);
		addDoneListener(l);
	
public voidaddParserTokenListener(persistence.antlr.debug.ParserTokenListener l)

		if (tokenListeners == null) tokenListeners = new Vector();
		tokenListeners.addElement(l);
		addDoneListener(l);
	
public voidaddSemanticPredicateListener(persistence.antlr.debug.SemanticPredicateListener l)

		if (semPredListeners == null) semPredListeners = new Vector();
		semPredListeners.addElement(l);
		addDoneListener(l);
	
public voidaddSyntacticPredicateListener(persistence.antlr.debug.SyntacticPredicateListener l)

		if (synPredListeners == null) synPredListeners = new Vector();
		synPredListeners.addElement(l);
		addDoneListener(l);
	
public voidaddTraceListener(persistence.antlr.debug.TraceListener l)

		if (traceListeners == null) traceListeners = new Vector();
		traceListeners.addElement(l);
		addDoneListener(l);
	
public voidfireConsume(int value)

		tokenEvent.setValues(ParserTokenEvent.CONSUME, 1, value);
		fireEvents(CONSUME, tokenListeners);		
	
public voidfireDoneParsing()

		traceEvent.setValues(TraceEvent.DONE_PARSING, 0,0,0);

		Hashtable targets=null;
//		Hashtable targets=doneListeners;
		ListenerBase l=null;
		
		synchronized (this) {
			if (doneListeners == null) return;
			targets = (Hashtable)doneListeners.clone();
		}
		
		if (targets != null) {
			Enumeration e = targets.keys();
			while(e.hasMoreElements()) {
				l = (ListenerBase)e.nextElement();
				fireEvent(DONE_PARSING, l);
			}
		}	
		if (controller != null)
			controller.checkBreak();
	
public voidfireEnterRule(int ruleNum, int guessing, int data)

		ruleDepth++;
		traceEvent.setValues(TraceEvent.ENTER, ruleNum, guessing, data);
		fireEvents(ENTER_RULE, traceListeners);
	
public voidfireEvent(int type, persistence.antlr.debug.ListenerBase l)

		switch(type) {
			case CONSUME:    ((ParserTokenListener)l).parserConsume(tokenEvent); break;
			case LA:         ((ParserTokenListener)l).parserLA(tokenEvent);      break;

			case ENTER_RULE: ((TraceListener)l).enterRule(traceEvent);           break;
			case EXIT_RULE:  ((TraceListener)l).exitRule(traceEvent);            break;

			case MATCH:        ((ParserMatchListener)l).parserMatch(matchEvent);       break;
			case MATCH_NOT:    ((ParserMatchListener)l).parserMatchNot(matchEvent);    break;
			case MISMATCH:     ((ParserMatchListener)l).parserMismatch(matchEvent);    break;
			case MISMATCH_NOT: ((ParserMatchListener)l).parserMismatchNot(matchEvent); break;

			case SEMPRED:      ((SemanticPredicateListener)l).semanticPredicateEvaluated(semPredEvent); break;

			case SYNPRED_STARTED:   ((SyntacticPredicateListener)l).syntacticPredicateStarted(synPredEvent);   break;
			case SYNPRED_FAILED:    ((SyntacticPredicateListener)l).syntacticPredicateFailed(synPredEvent);    break;
			case SYNPRED_SUCCEEDED: ((SyntacticPredicateListener)l).syntacticPredicateSucceeded(synPredEvent); break;

			case REPORT_ERROR:   ((MessageListener)l).reportError(messageEvent);   break;
			case REPORT_WARNING: ((MessageListener)l).reportWarning(messageEvent); break;

			case DONE_PARSING: l.doneParsing(traceEvent); break;
			case NEW_LINE:     ((NewLineListener)l).hitNewLine(newLineEvent); break;
			
			default:
				throw new IllegalArgumentException("bad type "+type+" for fireEvent()");
		}	
	
public voidfireEvents(int type, java.util.Vector listeners)

		ListenerBase l=null;
		
		if (listeners != null)
			for (int i = 0; i < listeners.size(); i++) {
				l = (ListenerBase)listeners.elementAt(i);
				fireEvent(type, l);
			}
		if (controller != null)
			controller.checkBreak();
	
public voidfireExitRule(int ruleNum, int guessing, int data)

		traceEvent.setValues(TraceEvent.EXIT, ruleNum, guessing, data);
		fireEvents(EXIT_RULE, traceListeners);
		ruleDepth--;
		if (ruleDepth == 0)
			fireDoneParsing();
	
public voidfireLA(int k, int la)

		tokenEvent.setValues(ParserTokenEvent.LA, k, la);
		fireEvents(LA, tokenListeners);
	
public voidfireMatch(char c, int guessing)

		matchEvent.setValues(ParserMatchEvent.CHAR, c, new Character(c), null, guessing, false, true);
		fireEvents(MATCH, matchListeners);
	
public voidfireMatch(char value, persistence.antlr.collections.impl.BitSet b, int guessing)

		matchEvent.setValues(ParserMatchEvent.CHAR_BITSET, value, b, null, guessing, false, true);
		fireEvents(MATCH, matchListeners);
	
public voidfireMatch(char value, java.lang.String target, int guessing)

		matchEvent.setValues(ParserMatchEvent.CHAR_RANGE, value, target, null, guessing, false, true);
		fireEvents(MATCH, matchListeners);
	
public voidfireMatch(int value, persistence.antlr.collections.impl.BitSet b, java.lang.String text, int guessing)

		matchEvent.setValues(ParserMatchEvent.BITSET, value, b, text, guessing, false, true);
		fireEvents(MATCH, matchListeners);
	
public voidfireMatch(int n, java.lang.String text, int guessing)

		matchEvent.setValues(ParserMatchEvent.TOKEN, n, new Integer(n), text, guessing, false, true);
		fireEvents(MATCH, matchListeners);
	
public voidfireMatch(java.lang.String s, int guessing)

		matchEvent.setValues(ParserMatchEvent.STRING, 0, s, null, guessing, false, true);
		fireEvents(MATCH, matchListeners);
	
public voidfireMatchNot(char value, char n, int guessing)

		matchEvent.setValues(ParserMatchEvent.CHAR, value, new Character(n), null, guessing, true, true);
		fireEvents(MATCH_NOT, matchListeners);
	
public voidfireMatchNot(int value, int n, java.lang.String text, int guessing)

		matchEvent.setValues(ParserMatchEvent.TOKEN, value, new Integer(n), text, guessing, true, true);
		fireEvents(MATCH_NOT, matchListeners);
	
public voidfireMismatch(char value, char n, int guessing)

		matchEvent.setValues(ParserMatchEvent.CHAR, value, new Character(n), null, guessing, false, false);
		fireEvents(MISMATCH, matchListeners);
	
public voidfireMismatch(char value, persistence.antlr.collections.impl.BitSet b, int guessing)

		matchEvent.setValues(ParserMatchEvent.CHAR_BITSET, value, b, null, guessing, false, true);
		fireEvents(MISMATCH, matchListeners);
	
public voidfireMismatch(char value, java.lang.String target, int guessing)

		matchEvent.setValues(ParserMatchEvent.CHAR_RANGE, value, target, null, guessing, false, true);
		fireEvents(MISMATCH, matchListeners);
	
public voidfireMismatch(int value, int n, java.lang.String text, int guessing)

		matchEvent.setValues(ParserMatchEvent.TOKEN, value, new Integer(n), text, guessing, false, false);
		fireEvents(MISMATCH, matchListeners);
	
public voidfireMismatch(int value, persistence.antlr.collections.impl.BitSet b, java.lang.String text, int guessing)

		matchEvent.setValues(ParserMatchEvent.BITSET, value, b, text, guessing, false, true);
		fireEvents(MISMATCH, matchListeners);
	
public voidfireMismatch(java.lang.String value, java.lang.String text, int guessing)

		matchEvent.setValues(ParserMatchEvent.STRING, 0, text, value, guessing, false, true);
		fireEvents(MISMATCH, matchListeners);
	
public voidfireMismatchNot(char value, char c, int guessing)

		matchEvent.setValues(ParserMatchEvent.CHAR, value, new Character(c), null, guessing, true, true);
		fireEvents(MISMATCH_NOT, matchListeners);
	
public voidfireMismatchNot(int value, int n, java.lang.String text, int guessing)

		matchEvent.setValues(ParserMatchEvent.TOKEN, value, new Integer(n), text, guessing, true, true);
		fireEvents(MISMATCH_NOT, matchListeners);
	
public voidfireNewLine(int line)

		newLineEvent.setValues(line);
		fireEvents(NEW_LINE, newLineListeners);
	
public voidfireReportError(java.lang.Exception e)

		messageEvent.setValues(MessageEvent.ERROR, e.toString());
		fireEvents(REPORT_ERROR, messageListeners);
	
public voidfireReportError(java.lang.String s)

		messageEvent.setValues(MessageEvent.ERROR, s);
		fireEvents(REPORT_ERROR, messageListeners);
	
public voidfireReportWarning(java.lang.String s)

		messageEvent.setValues(MessageEvent.WARNING, s);
		fireEvents(REPORT_WARNING, messageListeners);
	
public booleanfireSemanticPredicateEvaluated(int type, int condition, boolean result, int guessing)

		semPredEvent.setValues(type, condition, result, guessing);
		fireEvents(SEMPRED, semPredListeners);
		return result;
	
public voidfireSyntacticPredicateFailed(int guessing)

		synPredEvent.setValues(0, guessing);
		fireEvents(SYNPRED_FAILED, synPredListeners);
	
public voidfireSyntacticPredicateStarted(int guessing)

		synPredEvent.setValues(0, guessing);
		fireEvents(SYNPRED_STARTED, synPredListeners);
	
public voidfireSyntacticPredicateSucceeded(int guessing)

		synPredEvent.setValues(0, guessing);
		fireEvents(SYNPRED_SUCCEEDED, synPredListeners);
	
protected voidrefresh(java.util.Vector listeners)

		Vector v;
		synchronized (listeners) {
			v = (Vector)listeners.clone();
		}
		if (v != null)
			for (int i = 0; i < v.size(); i++)
				((ListenerBase)v.elementAt(i)).refresh();
	
public voidrefreshListeners()

		refresh(matchListeners);
		refresh(messageListeners);
		refresh(tokenListeners);
		refresh(traceListeners);
		refresh(semPredListeners);
		refresh(synPredListeners);
	
public voidremoveDoneListener(persistence.antlr.debug.ListenerBase l)

		if (doneListeners == null) return;
		Integer i = (Integer)doneListeners.get(l);
		int val=0;
		if (i != null)
			val = i.intValue() - 1;

		if (val == 0) 
			doneListeners.remove(l);
		else
			doneListeners.put(l, new Integer(val));
	
public voidremoveMessageListener(persistence.antlr.debug.MessageListener l)

		if (messageListeners != null)
			messageListeners.removeElement(l);
		removeDoneListener(l);
	
public voidremoveNewLineListener(persistence.antlr.debug.NewLineListener l)

		if (newLineListeners != null)
			newLineListeners.removeElement(l);
		removeDoneListener(l);
	
public voidremoveParserListener(persistence.antlr.debug.ParserListener l)

		removeParserMatchListener(l);
		removeMessageListener(l);
		removeParserTokenListener(l);
		removeTraceListener(l);
		removeSemanticPredicateListener(l);
		removeSyntacticPredicateListener(l);
	
public voidremoveParserMatchListener(persistence.antlr.debug.ParserMatchListener l)

		if (matchListeners != null)
			matchListeners.removeElement(l);
		removeDoneListener(l);
	
public voidremoveParserTokenListener(persistence.antlr.debug.ParserTokenListener l)

		if (tokenListeners != null)
			tokenListeners.removeElement(l);
		removeDoneListener(l);
	
public voidremoveSemanticPredicateListener(persistence.antlr.debug.SemanticPredicateListener l)

		if (semPredListeners != null)
			semPredListeners.removeElement(l);
		removeDoneListener(l);
	
public voidremoveSyntacticPredicateListener(persistence.antlr.debug.SyntacticPredicateListener l)

		if (synPredListeners != null)
			synPredListeners.removeElement(l);
		removeDoneListener(l);
	
public voidremoveTraceListener(persistence.antlr.debug.TraceListener l)

		if (traceListeners != null)
			traceListeners.removeElement(l);
		removeDoneListener(l);